Skip to content

Commit

Permalink
Fix: Correct dict.get() usage in TreeGrid_to_json renderer and remove…
Browse files Browse the repository at this point in the history
… debug print

- Corrected the usage of dict.get() method by removing keyword arguments and using positional arguments instead.
- Ensured the render method returns a dictionary as expected.
- Updated the to_list method to properly call the render method and handle exceptions.
- Improved the docstrings to reflect the correct return types and behaviors of the methods.
- Removed a debug print statement introduced in a previous commit.

This fixes the TypeError and ensures the TreeGrid is properly rendered to JSON format.
  • Loading branch information
Br4guette authored and Br4guette committed Jul 23, 2024
1 parent 8d7b9d4 commit b9771ec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
1 change: 0 additions & 1 deletion pydfirram/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ def __init__(self, operating_system: OperatingSystem, dump_file: Path):
self.validate_dump_file(dump_file)
self.os = operating_system
self.plugins: list[PluginEntry] = self.get_all_plugins()
print(self.plugins)
self.dump_file = dump_file
self.context: Optional[Context] = None
self.temp_data = None
Expand Down
37 changes: 18 additions & 19 deletions pydfirram/core/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
# allow no PascalCase naming style and "lambda may not be necessary"
# pylint: disable=W0108,C0103
# (todo) : switch to PascalCase

class TreeGrid_to_json(V3CLIRenderer): # type: ignore
class TreeGrid_to_json(V3CLIRenderer): # type: ignore
""" simple TreeGrid to JSON
"""
_type_renderers: Any = {
Expand Down Expand Up @@ -72,8 +71,8 @@ def get_render_options(self) -> list[V3RenderOption]:
"""
return []

# (fixme) : this methods should return nothing as defined in V3CLIRenderer
def render(self, grid: V3TreeGrid) -> list[V3TreeNode]:
# (fixme): This method should return nothing as defined in V3CLIRenderer
def render(self, grid: V3TreeGrid) -> dict[str, Any]:
"""
Render the TreeGrid to JSON format.
Expand All @@ -84,15 +83,14 @@ def render(self, grid: V3TreeGrid) -> list[V3TreeNode]:
Dict: The JSON representation of the TreeGrid.
"""
final_output: tuple[
dict[str, list[V3TreeNode]],
list[V3TreeNode],
dict[str, dict[str, Any]],
list[dict[str, Any]],
] = ({}, [])


def visitor(
node: V3TreeNode,
accumulator: tuple[dict[str,Any], list[dict[str,Any]]],
) -> tuple[dict[str,Any], list[dict[str,Any]]]:
accumulator: tuple[dict[str, Any], list[dict[str, Any]]],
) -> tuple[dict[str, Any], list[dict[str, Any]]]:
"""
A visitor function to process each node in the TreeGrid.
Expand All @@ -102,7 +100,7 @@ def visitor(
The accumulator containing the accumulated results.
Returns:
Tuple[Dict[str,Any], List[Dict[str, Any]]]: The updated
Tuple[Dict[str, Any], List[Dict[str, Any]]]: The updated
accumulator.
"""
acc_map = accumulator[0]
Expand All @@ -111,8 +109,8 @@ def visitor(

for column_index, column in enumerate(grid.columns):
renderer = self._type_renderers.get(
key = column.type,
default = self._type_renderers["default"],
column.type,
self._type_renderers["default"]
)
data = renderer(
list(node.values)[column_index],
Expand All @@ -132,11 +130,11 @@ def visitor(
grid.populate(visitor, final_output)
else:
grid.visit(
node = None,
function = visitor,
initial_accumulator = final_output,
node=None,
function=visitor,
initial_accumulator=final_output,
)
return final_output[1]
return {"data": final_output[1]}


class Renderer():
Expand All @@ -159,7 +157,7 @@ def __init__(self, data: Any) -> None:
"""
self.data = data

def to_list(self) -> dict[str,Any]:
def to_list(self):
"""
Convert the data to a list format.
Expand All @@ -174,7 +172,8 @@ def to_list(self) -> dict[str,Any]:
"""
try:
# (fixme) : `render()` should return nothing
return TreeGrid_to_json().render(self.data)
parsed_data : dict[str, Any] = TreeGrid_to_json().render(self.data)
return parsed_data.get("data")
except Exception as e:
logger.error("Impossible to render data in dictionary form.")
raise e
Expand Down Expand Up @@ -237,7 +236,7 @@ def to_df(self,max_row: bool = False) -> pd.DataFrame:
if max_row:
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
return pd.DataFrame(data_as_dict)
return pd.DataFrame(data_as_dict.get("data"))
except Exception as e:
logger.error("Data cannot be rendered as a DataFrame.")
raise e
2 changes: 1 addition & 1 deletion tests/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pathlib import Path

DUMP_FILE = Path("./data/dump.raw")
DUMP_FILE = Path("/home/braguette/dataset_memory/ch2.dmp")
1 change: 0 additions & 1 deletion tests/test_volatility_windows_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def test_volatility_pslist(generic_instance: Generic) -> None:
output: Renderer = generic_instance.pslist()
assert isinstance(output, Renderer), "Output is not an instance of Renderer"
pslist_content: List[Any] = output.to_list()
print(type(pslist_content))
assert isinstance(pslist_content, list), "Output content is not a list"
assert len(pslist_content) > 0, "Output list is empty"
logger.success("TEST PASSED!")
Expand Down

0 comments on commit b9771ec

Please sign in to comment.