Skip to content

Commit ed7a70f

Browse files
committed
utils/_object: updated metaclasses
1 parent 18cbd37 commit ed7a70f

621 files changed

Lines changed: 758 additions & 1349 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

scripts/inp_code.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,7 @@ class {CAMEL(element.name, "Option")}(Option):
300300
Represents generic INP {element.name} options.
301301
"""
302302
303-
_KEYWORD = ""
304-
_SUBCLASSES = {{}}
305-
_REGEX = re.compile(
306-
rf"{r'|'.join(GET_REGEX(option) for option in sorted(element.options, reverse=True, key=lambda scheme: len(scheme.mnemonic)))}"
307-
)
308-
309-
def __init_subclass__(cls, keyword: str):
310-
cls._KEYWORD: typing.Final[str] = keyword
311-
312-
if keyword not in cls._SUBCLASSES:
313-
cls._SUBCLASSES[keyword] = [cls]
314-
else:
315-
cls._SUBCLASSES[keyword] += [cls]
316-
317-
def __class_getitem__(cls, keyword: str):
318-
return cls._SUBCLASSES[keyword]
303+
pass
319304
'''[1:]
320305

321306

@@ -336,7 +321,7 @@ def ELEMENT(element, parent_name, depth):
336321
from {"." * depth}utils import _visualization
337322
338323
339-
class {CAMEL(element.name)}({f"{CAMEL(parent_name, 'Option')}, keyword='{element.mnemonic}'" if parent_name else "Card"}):
324+
class {CAMEL(element.name)}({CAMEL(parent_name, 'Option')}):
340325
"""
341326
Represents INP {element.name.split('_')[0]}{f" variation #{element.name.split('_')[1]}"if len(element.name.split('_')) - 1 else ""} elements.
342327

scripts/inp_data.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,7 @@ def draw(self):
26542654
ElementScheme(
26552655
name='vol',
26562656
mnemonic='vol',
2657-
regex='vol( no)?((?: {types.RealOrJump._REGEX.pattern})+?)',
2657+
regex='vol(?: (no))?((?: {types.RealOrJump._REGEX.pattern})+?)',
26582658
attributes=[
26592659
AttributeScheme(
26602660
name='no',
@@ -7841,7 +7841,7 @@ def from_formula(number: int, formulas: dict[str, float], atomic_or_weight: bool
78417841
ElementScheme(
78427842
name='fu',
78437843
mnemonic='fu',
7844-
regex='fu(\\d+)((?: {types.RealOrJump._REGEX.pattern})+?)( nt)?( c)?',
7844+
regex='fu(\\d+)((?: {types.RealOrJump._REGEX.pattern})+?)(?: (nt))?(?: (c))?',
78457845
attributes=[
78467846
AttributeScheme(
78477847
name='suffix',
@@ -8234,6 +8234,7 @@ def from_formula(number: int, formulas: dict[str, float], atomic_or_weight: bool
82348234
ElementScheme(
82358235
name='geom',
82368236
mnemonic='geom',
8237+
regex='geom(?: (xyz|rec|rzt|cyl))',
82378238
attributes=[
82388239
AttributeScheme(
82398240
name='geometry',
@@ -9979,6 +9980,7 @@ def from_formula(number: int, formulas: dict[str, float], atomic_or_weight: bool
99799980
ElementScheme(
99809981
name='file',
99819982
mnemonic='file',
9983+
regex='file(?: (asc|bin|aov|bov))',
99829984
attributes=[
99839985
AttributeScheme(
99849986
name='setting',
@@ -10015,6 +10017,7 @@ def from_formula(number: int, formulas: dict[str, float], atomic_or_weight: bool
1001510017
ElementScheme(
1001610018
name='write',
1001710019
mnemonic='write',
10020+
regex='write(?: (pos|all))',
1001810021
attributes=[
1001910022
AttributeScheme(
1002010023
name='setting',
@@ -10027,6 +10030,7 @@ def from_formula(number: int, formulas: dict[str, float], atomic_or_weight: bool
1002710030
ElementScheme(
1002810031
name='conic',
1002910032
mnemonic='conic',
10033+
regex='setting(?: (col|lin))',
1003010034
attributes=[
1003110035
AttributeScheme(
1003210036
name='setting',
@@ -10156,7 +10160,7 @@ def from_formula(number: int, formulas: dict[str, float], atomic_or_weight: bool
1015610160
ElementScheme(
1015710161
name='file',
1015810162
mnemonic='file',
10159-
regex='file( all|none)?',
10163+
regex='file (?: (all|none))?',
1016010164
attributes=[
1016110165
AttributeScheme(
1016210166
name='aa',
@@ -10351,7 +10355,7 @@ def from_formula(number: int, formulas: dict[str, float], atomic_or_weight: bool
1035110355
ElementScheme(
1035210356
name='reset',
1035310357
mnemonic='reset',
10354-
regex='reset( all|coplot)?',
10358+
regex='reset(?: (all|coplot))?',
1035510359
attributes=[
1035610360
AttributeScheme(
1035710361
name='aa',

src/pymcnp/Inp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .utils import _visualization
1313

1414

15-
class Inp(_object.McnpFile_):
15+
class Inp(_object.McnpFile):
1616
"""
1717
Represents INP files.
1818

src/pymcnp/Meshtal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .utils import _object
77

88

9-
class Meshtal(_object.McnpFile_):
9+
class Meshtal(_object.McnpFile):
1010
"""
1111
Represents MESTHAL files.
1212

src/pymcnp/Outp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .utils import _object
66

77

8-
class Outp(_object.McnpFile_):
8+
class Outp(_object.McnpFile):
99
"""
1010
Represents OUTP files.
1111

src/pymcnp/Ptrac.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .utils import _object
77

88

9-
class Ptrac(_object.McnpFile_):
9+
class Ptrac(_object.McnpFile):
1010
"""
1111
Represents PTRAC files.
1212

src/pymcnp/inp/Comment.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from ._card import Card
77
from ..utils import types
8+
from ..utils import _parser
89

910

1011
class Comment(Card):
@@ -32,6 +33,16 @@ def __init__(self, text: types.String):
3233

3334
self.text: typing.Final[types.String] = text
3435

36+
def to_mcnp(self):
37+
"""
38+
Generates INP from ``Data``.
39+
40+
Returns:
41+
INP data card.
42+
"""
43+
44+
return _parser.postprocess_continuation_line(f'c {self.text}')
45+
3546

3647
@dataclasses.dataclass
3748
class CommentBuilder:

src/pymcnp/inp/_card.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..utils import _parser
66

77

8-
class Card(_object.McnpElement_):
8+
class Card(_object.McnpNonterminal):
99
"""
1010
Represents generic INP cards.
1111
"""

src/pymcnp/inp/_option.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..utils import _parser
66

77

8-
class Option(_object.McnpElement_):
8+
class Option(_object.McnpNonterminal):
99
"""
1010
Represents generic INP cards.
1111
"""
@@ -30,11 +30,11 @@ def from_mcnp(cls, source: str):
3030

3131
source, comments = _parser.preprocess_inp(source)
3232

33-
for subcls in sorted(
34-
sum(cls._SUBCLASSES.values(), []), reverse=True, key=lambda val: len(val._KEYWORD)
35-
):
33+
subclasses = cls.__subclasses__() or [cls]
34+
35+
for subclass in subclasses:
3636
try:
37-
if (tokens := subcls._REGEX.match(source)) and tokens[0] == source:
37+
if (tokens := subclass._REGEX.match(source)) and tokens[0] == source:
3838
break
3939
else:
4040
continue
@@ -44,15 +44,15 @@ def from_mcnp(cls, source: str):
4444
raise errors.InpError(errors.InpCode.SYNTAX_OPTION, source)
4545

4646
attrs = {}
47-
for i, (name, attr) in enumerate(subcls._ATTRS.items()):
47+
for i, (name, attr) in enumerate(subclass._ATTRS.items()):
4848
if isinstance(attr, types.GenericAlias):
4949
attrs[name] = (
5050
attr.from_mcnp(tokens[i + 1], attr.__args__[0]) if tokens[i + 1] else None
5151
)
5252
else:
5353
attrs[name] = attr.from_mcnp(tokens[i + 1]) if tokens[i + 1] else None
5454

55-
return subcls(**attrs)
55+
return subclass(**attrs)
5656

5757
def to_mcnp(self):
5858
"""

src/pymcnp/inp/cell/Bflcl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ...utils import errors
99

1010

11-
class Bflcl(CellOption, keyword='bflcl'):
11+
class Bflcl(CellOption):
1212
"""
1313
Represents INP bflcl elements.
1414

0 commit comments

Comments
 (0)