Skip to content

Commit 9371789

Browse files
committed
pymcnp: reformatting for ruff
1 parent f3590ab commit 9371789

File tree

23 files changed

+66
-92
lines changed

23 files changed

+66
-92
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ write_to = "./src/pymcnp/version.py"
6363

6464
[tool.ruff]
6565
line-length = 100
66+
lint.ignore = ["E741", "E742"]
6667

6768
[tool.ruff.format]
6869
quote-style = "single"

src/pymcnp/Outp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def from_mcnp(cls, source: str):
4141
``Outp``.
4242
"""
4343

44-
lines = types.Tuple(map(lambda line: types.String(line), lines.split('\n')))
44+
lines = types.Tuple(map(lambda line: types.String(line), source.split('\n')))
4545

4646
return cls(lines)
4747

@@ -53,4 +53,4 @@ def to_mcnp(self):
5353
OUTP for ``Outp``.
5454
"""
5555

56-
return '\n'.join(lines)
56+
return '\n'.join(line for line in self.lines)

src/pymcnp/Ptrac.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class Ptrac(_object.McnpFile_):
1616
"""
1717

1818
_REGEX = re.compile(
19-
rf'({ptrac.Header._REGEX.pattern})'
20-
rf'((?:{ptrac.History._REGEX.pattern})+)'
19+
rf'({ptrac.Header._REGEX.pattern})' rf'((?:{ptrac.History._REGEX.pattern})+)'
2120
)
2221

2322
def __init__(
@@ -67,7 +66,10 @@ def from_mcnp(source: str):
6766
raise errors.PtracError(errors.PtracCode.SYNTAX_PTRAC, source)
6867

6968
header = ptrac.Header.from_mcnp(tokens[1])
70-
histories = (ptrac.History.from_mcnp(match[0], header) for match in ptrac.History._REGEX.finditer(tokens[10]))
69+
histories = (
70+
ptrac.History.from_mcnp(match[0], header)
71+
for match in ptrac.History._REGEX.finditer(tokens[10])
72+
)
7173

7274
return Ptrac(header, histories)
7375

@@ -79,4 +81,8 @@ def to_mcnp(self):
7981
PTRAC for ``Ptrac``.
8082
"""
8183

82-
return self.header.to_mcnp() + '\n'.join(history.to_mcnp() for history in self.histories) + '\n'
84+
return (
85+
self.header.to_mcnp()
86+
+ '\n'.join(history.to_mcnp() for history in self.histories)
87+
+ '\n'
88+
)

src/pymcnp/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .Meshtal import Meshtal
1515
from .MeshtalFiltered import MeshtalFiltered
1616
from .MeshtalProcessed import MeshtalProcessed
17+
1718
# from . import outp
1819
from .Outp import Outp
1920

src/pymcnp/cli/_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def get_outfile(path: str | pathlib.Path, new: str, old: str) -> str:
99
path = str(path)
10-
10+
1111
if path.rsplit('.', maxsplit=1)[0] == old:
1212
path = path.rsplit('.', maxsplit=1)[1]
1313

src/pymcnp/cli/convert.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from docopt import docopt
1717

1818
from . import _io
19-
from ..Outp import Outp
2019
from ..utils import errors
2120

2221

@@ -53,13 +52,12 @@ def to_csv(self, number: int):
5352
number: Tally to read.
5453
"""
5554

56-
outp = Outp.from_mcnp_file(self.path)
55+
# outp = Outp.from_mcnp_file(self.path)
5756
# df = outp.to_dataframe()
5857

59-
path = _io.get_outfile(self.path, 'outp', 'csv')
60-
with path.open('r') as file:
61-
pass
62-
# file.write(df.to_csv())
58+
# path = _io.get_outfile(self.path, 'outp', 'csv')
59+
# with path.open('r') as file:
60+
# file.write(df.to_csv())
6361

6462
def to_parquet(self, number: int):
6563
"""
@@ -69,13 +67,12 @@ def to_parquet(self, number: int):
6967
number: Tally to read.
7068
"""
7169

72-
outp = Outp.from_mcnp_file(self.path)
70+
# outp = Outp.from_mcnp_file(self.path)
7371
# df = outp.to_dataframe()
7472

75-
path = _io.get_outfile(self.path, 'outp', 'parquet')
76-
with path.open('r') as file:
77-
pass
78-
# file.write(df.to_parquet())
73+
# path = _io.get_outfile(self.path, 'outp', 'parquet')
74+
# with path.open('r') as file:
75+
# file.write(df.to_parquet())
7976

8077

8178
def main() -> None:

src/pymcnp/cli/plot.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
import pathlib
1010

1111
from docopt import docopt
12-
from matplotlib import pyplot as plt
1312

1413
from . import _io
15-
from ..Outp import Outp
1614
from ..utils import errors
1715

1816

@@ -48,28 +46,26 @@ def to_show(self, number: int):
4846
number: Tally number.
4947
"""
5048

51-
outp = Outp.from_mcnp_file(self.path)
49+
# outp = Outp.from_mcnp_file(self.path)
5250
# df = outp.to_dataframe()
53-
54-
fig, ax = plt.subplots()
55-
56-
#ax.errorbar(
51+
# fig, ax = plt.subplots()
52+
# ax.errorbar(
5753
# df['energy'],
5854
# df['cts'],
5955
# yerr=df['error'] * df['cts'],
6056
# fmt='o',
6157
# label='Error',
6258
# color='orange',
6359
# zorder=1,
64-
#)
65-
#ax.step(df['energy'], df['cts'], color='blue', where='mid', label='Counts', zorder=2)
66-
ax.title('Energy histogram')
67-
ax.xlabel('Energy [MeV]')
68-
ax.ylabel('Counts/bin/neutron')
69-
ax.yscale('log')
70-
ax.legend()
71-
72-
return fig, ax
60+
# )
61+
# ax.step(df['energy'], df['cts'], color='blue', where='mid', label='Counts', zorder=2)
62+
# ax.title('Energy histogram')
63+
# ax.xlabel('Energy [MeV]')
64+
# ax.ylabel('Counts/bin/neutron')
65+
# ax.yscale('log')
66+
# ax.legend()
67+
68+
# return fig, ax
7369

7470
def to_pdf(self, number: int):
7571
"""

src/pymcnp/ptrac/Header.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from __future__ import annotations
1+
# noqa: E741
2+
23
import re
34
import typing
45

56
from ..utils import types
67
from ..utils import errors
7-
from ..utils import _parser
88
from ..utils import _object
99

1010

@@ -94,7 +94,7 @@ def __init__(
9494
self.l_line: typing.Final[types.Tuple[types.Integer]] = l_line
9595

9696
@staticmethod
97-
def from_mcnp(source: str) -> tuple[Header, str]:
97+
def from_mcnp(source: str):
9898
"""
9999
Generates ``Header`` from PTRAC.
100100
@@ -118,21 +118,18 @@ def from_mcnp(source: str) -> tuple[Header, str]:
118118
code_date = types.String.from_mcnp(tokens[3])
119119
run_datetime = types.String.from_mcnp(tokens[4])
120120
title = types.String.from_mcnp(tokens[5])
121-
v_line = types.Tuple([types.Real.from_mcnp(v) for v in re.split(r'\s+|\n', tokens[6].strip())])
122-
n_line = types.Tuple([types.Integer.from_mcnp(n) for n in re.split(r'\s+|\n', tokens[7].strip())])
123-
l_line = types.Tuple([types.Integer.from_mcnp(l) for l in re.split(r'\s+|\n', tokens[8].strip())])
124-
125-
return Header(
126-
code,
127-
version,
128-
code_date,
129-
run_datetime,
130-
title,
131-
v_line,
132-
n_line,
133-
l_line
121+
v_line = types.Tuple(
122+
[types.Real.from_mcnp(v) for v in re.split(r'\s+|\n', tokens[6].strip())]
123+
)
124+
n_line = types.Tuple(
125+
[types.Integer.from_mcnp(n) for n in re.split(r'\s+|\n', tokens[7].strip())]
126+
)
127+
l_line = types.Tuple(
128+
[types.Integer.from_mcnp(l) for l in re.split(r'\s+|\n', tokens[8].strip())]
134129
)
135130

131+
return Header(code, version, code_date, run_datetime, title, v_line, n_line, l_line)
132+
136133
def to_mcnp(self):
137134
"""
138135
Generates PTRAC from ``Header``.
@@ -143,22 +140,21 @@ def to_mcnp(self):
143140

144141
v_line = ' '
145142
for i, v in enumerate(self.v_line):
146-
v_line += f"{v:>12.4E}"
143+
v_line += f'{v:>12.4E}'
147144

148145
if (i + 1) % 10 == 0 and i != 0:
149146
v_line += '\n '
150147
v_line = v_line[:-1]
151148

152149
n_line = ' '
153150
for i, n in enumerate(self.n_line):
154-
n_line += f"{n:>5}"
151+
n_line += f'{n:>5}'
155152

156153
l_line = ' '
157154
for i, l in enumerate(self.l_line):
158-
l_line += f"{l:>4}"
155+
l_line += f'{l:>4}'
159156

160157
if (i + 1) % 30 == 0 and i != 0:
161158
l_line += '\n '
162159

163-
return f" -1\n{self.code:<8}{self.version:<25}{self.code_date:<9}{self.run_datetime:<18}\n{self.title:<80}\n{v_line}{n_line}\n{l_line}\n"
164-
160+
return f' -1\n{self.code:<8}{self.version:<25}{self.code_date:<9}{self.run_datetime:<18}\n{self.title:<80}\n{v_line}{n_line}\n{l_line}\n'

src/pymcnp/ptrac/History.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
from __future__ import annotations
21
import re
32
import typing
43

54
from . import history
65
from .Header import Header
76
from ..utils import types
87
from ..utils import errors
9-
from ..utils import _parser
108
from ..utils import _object
119

1210

@@ -52,7 +50,7 @@ def __init__(
5250
self.events: typing.Final[typing.Generator] = events
5351

5452
@staticmethod
55-
def from_mcnp(source: str, header: Header) -> tuple[History, str]:
53+
def from_mcnp(source: str, header: Header):
5654
"""
5755
Generates ``History`` from PTRAC.
5856

src/pymcnp/ptrac/history/EventType.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from . import _keyword
22
from ...utils import errors
3-
from ...utils import _parser
43

54

65
class EventType(_keyword.HistoryKeyword):
@@ -91,8 +90,6 @@ def from_mcnp(source: str):
9190
PtracError: SYNTAX_KEYWORD.
9291
"""
9392

94-
95-
9693
try:
9794
return EventType(source)
9895
except ValueError:

0 commit comments

Comments
 (0)