Skip to content

Commit

Permalink
utils/_object: added __repr__ & __str__
Browse files Browse the repository at this point in the history
  • Loading branch information
BitterB0NG0 committed Nov 21, 2024
1 parent 840e638 commit 8051e05
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
3 changes: 0 additions & 3 deletions src/pymcnp/files/inp/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ def to_mcnp(self) -> str:

return f'{self.keyword.to_mcnp()}{suffix_str}{designator_str}={value_str}'

def __repr__(self):
return f'<CellOption: {self.__class__.__name__} ({self.to_mcnp()})>'


_CellImpFactory = _factory.CellOptionFactory(
'imp',
Expand Down
28 changes: 14 additions & 14 deletions src/pymcnp/files/inp/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class Data(_card.Card):
parameters: Data card parameters.
"""

GEOMETRY_MNEMONICS: Final[set[DataMnemonic]] = {
GEOMETRY_MNEMONICS: Final[tuple[DataMnemonic]] = (
DataMnemonic.VOLUME,
DataMnemonic.AREA,
DataMnemonic.TR,
Expand All @@ -264,9 +264,9 @@ class Data(_card.Card):
DataMnemonic.EMBTM,
DataMnemonic.EMBDB,
DataMnemonic.EMBDF,
}
)

MATERIAL_MNEMONICS: Final[set[DataMnemonic]] = {
MATERIAL_MNEMONICS: Final[tuple[DataMnemonic]] = (
DataMnemonic.M,
DataMnemonic.MT,
DataMnemonic.MATERIAL_NUCLIDE_SUBSTITUTION,
Expand All @@ -278,9 +278,9 @@ class Data(_card.Card):
DataMnemonic.VOID,
DataMnemonic.MGOPT,
DataMnemonic.DRXS,
}
)

PHYSICS_MNEMONICS: Final[set[DataMnemonic]] = {
PHYSICS_MNEMONICS: Final[tuple[DataMnemonic]] = (
DataMnemonic.MODE,
DataMnemonic.PARTICLE_PHYSICS_OPTIONS,
DataMnemonic.ACT,
Expand All @@ -302,9 +302,9 @@ class Data(_card.Card):
DataMnemonic.BFLD,
DataMnemonic.BFLCL,
DataMnemonic.GRAVITATIONAL_FIELD,
}
)

SOURCE_MNEMONICS: Final[set[DataMnemonic]] = {
SOURCE_MNEMONICS: Final[tuple[DataMnemonic]] = (
DataMnemonic.SDEF,
DataMnemonic.SOURCE_INFORMATION,
DataMnemonic.SOURCE_PROBABILITY,
Expand All @@ -320,9 +320,9 @@ class Data(_card.Card):
DataMnemonic.DEPLETION_BURNUP,
DataMnemonic.SOURCE,
DataMnemonic.SRCDX,
}
)

TALLY_MNEMONICS: Final[set[DataMnemonic]] = {
TALLY_MNEMONICS: Final[tuple[DataMnemonic]] = (
DataMnemonic.STANDARD_TALLIES,
DataMnemonic.STANDARD_TALLIES_ANGLE,
DataMnemonic.FIP,
Expand Down Expand Up @@ -355,9 +355,9 @@ class Data(_card.Card):
DataMnemonic.SUPERIMPOSED_MESH_TALLY_A,
DataMnemonic.SUPERIMPOSED_MESH_TALLY_B,
DataMnemonic.LATTICE_SPEED_TALLY_ENHANCEMENT,
}
)

VARIENCE_MNEMONICS: Final[set[DataMnemonic]] = {
VARIENCE_MNEMONICS: Final[tuple[DataMnemonic]] = (
DataMnemonic.IMPORTANCE,
DataMnemonic.VARIANCE_REDUCATION_CONTROL,
DataMnemonic.WEIGHT_WINDOW_ENERGIES,
Expand All @@ -381,9 +381,9 @@ class Data(_card.Card):
DataMnemonic.PHOTON_PRODUDCTION_BIASING,
DataMnemonic.SECONDARY_PARTICLE_BIASING,
DataMnemonic.PHOTON_WEIGHT,
}
)

MICELLANEOUS_MNEMONICS: Final[set[DataMnemonic]] = {
MICELLANEOUS_MNEMONICS: Final[tuple[DataMnemonic]] = (
DataMnemonic.NPS,
DataMnemonic.COMPUTER_TIME_CUTOFF,
DataMnemonic.PERCISION_CUTOFF,
Expand All @@ -403,7 +403,7 @@ class Data(_card.Card):
DataMnemonic.ZC,
DataMnemonic.ZD,
DataMnemonic.FILE,
}
)

@staticmethod
def from_mcnp(source: str):
Expand Down
19 changes: 4 additions & 15 deletions src/pymcnp/files/ptrac/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ def __init__(
if numbers is None:
raise errors.McnpError(errors.McnpCode.INVALID_HEADER_NUMBERS)

for number in numbers:
if number is None:
for entry in numbers:
if entry is None:
raise errors.McnpError(errors.McnpCode.INVALID_HEADER_NUMBERS)

if ids is None:
raise errors.McnpError(errors.McnpCode.INVALID_HEADER_IDS)

for id_ in ids:
if id_ is None:
for entry in ids:
if entry is None:
raise errors.McnpError(errors.McnpCode.INVALID_HEADER_IDS)

self.code: Final[str] = code
Expand All @@ -156,17 +156,6 @@ def __init__(
self.numbers: Final[tuple[int]] = numbers
self.ids: Final[tuple[int]] = ids

def __str__(self):
out = f' Program: {self.code} ; Version:({self.version} , {self.code_date}) ; Current Date:{self.run_date} {self.run_time}\n'
out += f' {self.title}\n'
for k, v in self.settings.items():
out += f" {k.name}: {' '.join(str(val) for val in v)}\n"
for k in self.ids:
out += f' IDS: {k}\n'
for k in self.numbers:
out += f' Numbers: {k}\n'
return out

@staticmethod
def from_mcnp(source: str) -> tuple[Header, str]:
"""
Expand Down
28 changes: 28 additions & 0 deletions src/pymcnp/files/utils/_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ def from_mcnp(source: str):
def to_mcnp(self) -> str:
raise NotImplementedError

def __eq__(a, b):
"""
Compares ``PyMcnpObject`` objects for equality.
"""

return repr(a) == repr(b)

def __str__(self):
"""
Stingifies ``PyMcnpObject``.
"""

return self.to_mcnp()

def __repr__(self):
"""
Stringifies ``PyMcnpObject`` for debugging.
"""

return f"<{self.__class__.__name__} {' '.join(f'{attribute}={self.__dict__[attribute]}' for attribute in self.__dict__)}>"


class PyMcnpKeyword(PyMcnpObject, enum.Enum):
"""
Expand All @@ -42,6 +63,13 @@ def to_mcnp(self) -> str:

return self.value

def __repr__(self):
"""
Stringifies ``PyMcnpObject`` for debugging.
"""

return f'<{self.__class__.__name__} {self.value}>'


class PyMcnpFileObject(PyMcnpObject):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/pymcnp/files/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,6 @@ def __str__(self) -> str:
hint += ''

if hint:
return f'\n\033[31;4;1mMcnpError[{self.code.name}]\033[0m: {head}\n|\n| {self.info.__repr__()}\n|\n| \033[35;4mHint\033[0m: {hint}\n|'
return f'\n\033[31;4;1mMcnpError[{self.code.name}]\033[0m: {head}\n|\n| {repr(self.info)}\n|\n| \033[35;4mHint\033[0m: {hint}\n|'
else:
return f'\n\033[31;4;1mMcnpError[{self.code.name}]\033[0m: {head}\n|\n| {self.info.__repr__()}\n|'
return f'\n\033[31;4;1mMcnpError[{self.code.name}]\033[0m: {head}\n|\n| {repr(self.info)}\n|'
15 changes: 0 additions & 15 deletions src/pymcnp/files/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,6 @@ def to_mcnp(self) -> str:
else f'{self.z:03}{self.a:03}'
)

def __str__(self):
return self.to_mcnp()


class Particle(_object.PyMcnpObject, enum.StrEnum):
"""
Expand Down Expand Up @@ -462,12 +459,6 @@ def to_mcnp(self):

return str(self.value)

def __str__(self):
return str(self.value)

def __repr__(self):
return f'<McnpInteger({self.value}) at {id(self)!r}>'

def __eq__(a, b: McnpInteger | int):
return a.value == b.value if isinstance(b, McnpInteger) else a.value == b

Expand Down Expand Up @@ -592,12 +583,6 @@ def to_mcnp(self):

return str(self.value)

def __str__(self):
return str(self.value)

def __repr__(self):
return f'<McnpReal({self.value}) at {id(self)!r}>'

def __eq__(a, b: McnpReal | float):
return a.value == b.value if isinstance(b, McnpReal) else a.value == b

Expand Down

0 comments on commit 8051e05

Please sign in to comment.