Skip to content

Commit

Permalink
used isinstance and typing for List and Dict
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-dash committed Dec 31, 2023
1 parent 1f5bc18 commit 1de3327
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
11 changes: 6 additions & 5 deletions rtab/base_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module definies a Base class to dictate loading and printing rich table data."""

from abc import ABC, abstractmethod
from typing import Dict, List

from rich import box
from rich.console import Console
Expand Down Expand Up @@ -69,7 +70,7 @@ def pre_run(self, stdin_data, skip_load: bool = False): # pylint: disable=missi
data = self.load(stdin_data)
if not data:
console.print(f"Can not load stdin into {type(self).__name__}")
return data, type(data)
return data

def console_print(self) -> None:
"""Get custom rich console object and print.
Expand All @@ -86,28 +87,28 @@ def run(self, stdin_data, skip_load: bool = False) -> int: # pylint: disable=mi
:param stdin_data: Can be either a string or file handler, used to load data from
:param skip_load: used to skip the call to load if data is pre-loaded into stdin_data
"""
data, data_type = self.pre_run(stdin_data, skip_load)
data = self.pre_run(stdin_data, skip_load)
if not data:
return 1

# Initialize table object
self.create_table()

if data_type == dict:
if isinstance(data, Dict):
# Show Logic
self.add_column("Key")
self.add_column("Value")
for k, v in data.items():
self.add_row([k, str(v)])
elif data_type == list:
elif isinstance(data, List):
# List Logic
columns = list(data[0].keys())
for c in columns:
self.add_column(c)
for r in data:
self.add_row([str(v) for v in r.values()])
else:
console.print(f"Unsupported type {data_type}")
console.print(f"Unsupported type {type(data)}")
return 1

# Print the table
Expand Down
7 changes: 4 additions & 3 deletions rtab/csv_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Load csv data from stdin or file handler and print a rich table."""

import csv
from typing import List

from rich.console import Console

Expand Down Expand Up @@ -42,20 +43,20 @@ def run(self, stdin_data, skip_load: bool = False) -> int: # pylint: disable=mi
:param stdin_data: Can be either a string or file handler, used to load data from
:param skip_load: used to skip the call to load if data is pre-loaded into stdin_data
"""
data, data_type = self.pre_run(stdin_data, skip_load)
data = self.pre_run(stdin_data, skip_load)
if not data:
return 1

# Initialize table object
self.create_table()

if data_type == list:
if isinstance(data, List):
for column in list(data[0]):
self.add_column(column)
for row in data[1:]:
self.add_row(row)
else:
console.print(f"Csv reader can't handle {data_type}")
console.print(f"Csv reader can't handle {type(data)}")
return 1

# Print the table
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def test_create_table():
def test_pre_run_success():
obj = BaseToRichTable()
test_data = [{"test": "data"}]
data, td = obj.pre_run(test_data, skip_load=True)
assert td == list
data = obj.pre_run(test_data, skip_load=True)
assert type(data) is list
assert data == test_data


@patch.multiple(BaseToRichTable, __abstractmethods__=set())
def test_pre_run_failure():
obj = BaseToRichTable()
test_data = None
data, _ = obj.pre_run(test_data, skip_load=True)
data = obj.pre_run(test_data, skip_load=True)
assert data == test_data


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ def test_file_types(mode):
],
)
def test_file_types_with_failure(mode, mock_ret_value):
obj = FileToRichTable(**{"separator": ","})
obj = FileToRichTable()
ret = obj.run(f"tests/resources/test.{mode}")
assert ret == mock_ret_value

0 comments on commit 1de3327

Please sign in to comment.