Skip to content

Commit

Permalink
Rewrites model __str__ methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPaulSharp committed Jul 11, 2024
1 parent ee3fc99 commit 1d45336
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 15 additions & 3 deletions RATpy/classlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections.abc import Iterable, Sequence
from typing import Any, Union

import numpy as np
import prettytable


Expand Down Expand Up @@ -56,15 +57,26 @@ def __str__(self):
try:
[model.__dict__ for model in self.data]
except AttributeError:
output = repr(self.data)
output = str(self.data)
else:
if any(model.__dict__ for model in self.data):
table = prettytable.PrettyTable()
table.field_names = ["index"] + [key.replace("_", " ") for key in self.data[0].__dict__]
table.add_rows([[index] + list(model.__dict__.values()) for index, model in enumerate(self.data)])
table.add_rows(
[
[index]
+ list(
f"{'Data array: ['+' x '.join(str(i) for i in v.shape) if v.size > 0 else '['}]"
if isinstance(v, np.ndarray)
else str(v)
for v in model.__dict__.values()
)
for index, model in enumerate(self.data)
]
)
output = table.get_string()
else:
output = repr(self.data)
output = str(self.data)
return output

def __setitem__(self, index: int, item: object) -> None:
Expand Down
13 changes: 9 additions & 4 deletions RATpy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
except ImportError:
from strenum import StrEnum

np.set_printoptions(threshold=100)


def int_sequence():
"""Iterate through integers for use as model counters."""
Expand Down Expand Up @@ -48,8 +46,8 @@ def __repr__(self):

def __str__(self):
table = prettytable.PrettyTable()
table.field_names = ["Field", "Value"]
table.add_rows([[k, v] for k, v in self.model_dump().items()])
table.field_names = [key.replace("_", " ") for key in self.__dict__]
table.add_row(list(self.__dict__.values()))
return table.get_string()


Expand Down Expand Up @@ -207,6 +205,13 @@ def __eq__(self, other: object) -> bool:
else:
return NotImplemented # delegate to the other item in the comparison

def __str__(self):
table = prettytable.PrettyTable()
table.field_names = [key.replace("_", " ") for key in self.__dict__]
array_entry = f"{'Data array: ['+' x '.join(str(i) for i in self.data.shape) if self.data.size > 0 else '['}]"
table.add_row([self.name, array_entry, self.data_range, self.simulation_range])
return table.get_string()


class DomainContrast(RATModel):
"""Groups together the layers required for each domain."""
Expand Down

0 comments on commit 1d45336

Please sign in to comment.