Skip to content

Commit fbd2d41

Browse files
committed
Use pathlib.Path instead of str for files
I've only recently discovered that a pathlib.Path is not just the directory, it can also be a file.
1 parent 421eac0 commit fbd2d41

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

pynch/ame_mass_parse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Extract the data from the AME mass file."""
22
import logging
3+
import pathlib
34

45
import pandas as pd
56

@@ -12,7 +13,7 @@ class AMEMassParser(AMEMassFile):
1213
The format is known but I don't think python can easily parse it.
1314
"""
1415

15-
def __init__(self, filename: str, year: int):
16+
def __init__(self, filename: pathlib.Path, year: int):
1617
"""Set the file to read and table year."""
1718
self.filename = filename
1819
self.year = year

pynch/ame_reaction_1_parse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Extract the data from the first reaction file."""
22
import logging
3+
import pathlib
34

45
import pandas as pd
56

@@ -12,7 +13,7 @@ class AMEReactionParserOne(AMEReactionFileOne):
1213
The format is known but I don't think python can easily parse it.
1314
"""
1415

15-
def __init__(self, filename: str, year: int):
16+
def __init__(self, filename: pathlib.Path, year: int):
1617
"""Set the file to read and table year."""
1718
self.filename = filename
1819
self.year = year

pynch/ame_reaction_2_parse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Extract the date from the second reaction file."""
22
import logging
3+
import pathlib
34

45
import pandas as pd
56

@@ -12,7 +13,7 @@ class AMEReactionParserTwo(AMEReactionFileTwo):
1213
The format is known but I don't think python can easily parse it.
1314
"""
1415

15-
def __init__(self, filename: str, year: int):
16+
def __init__(self, filename: pathlib.Path, year: int):
1617
"""Set the file to read and table year."""
1718
self.filename = filename
1819
self.year = year

pynch/mass_table.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self):
2727
self.full_data = self._combine_all_data()
2828
self._do_indexing()
2929

30-
def _get_nubase_datafile(self, year: int) -> str:
30+
def _get_nubase_datafile(self, year: int) -> pathlib.Path:
3131
"""Use the given year to locate the nubase mass table file and return the absolute path."""
3232
nubase_mass = self.data_path / pathlib.Path(str(year))
3333
nubase_mass = nubase_mass.resolve()
@@ -38,12 +38,12 @@ def _get_nubase_datafile(self, year: int) -> str:
3838
nubase_mass = nubase_mass / "nubtab12.asc"
3939
elif year == 2016:
4040
nubase_mass = nubase_mass / "nubase2016.txt"
41-
elif year == 2020:
41+
else: # year == 2020:
4242
nubase_mass = nubase_mass / "nubase_1.mas20"
4343

4444
return nubase_mass
4545

46-
def _get_ame_datafiles(self, year: int) -> typing.Tuple[str, str, str]:
46+
def _get_ame_datafiles(self, year: int) -> typing.Tuple[pathlib.Path, pathlib.Path, pathlib.Path]:
4747
"""Use the given year to locate the 3 AME data file and return the absolute path."""
4848
data_dir = self.data_path / pathlib.Path(str(year))
4949
data_dir = data_dir.resolve()
@@ -60,7 +60,7 @@ def _get_ame_datafiles(self, year: int) -> typing.Tuple[str, str, str]:
6060
ame_mass = data_dir / "mass16.txt"
6161
ame_reaction_1 = data_dir / "rct1-16.txt"
6262
ame_reaction_2 = data_dir / "rct2-16.txt"
63-
elif year == 2020:
63+
else: # year == 2020:
6464
ame_mass = data_dir / "mass.mas20"
6565
ame_reaction_1 = data_dir / "rct1.mas20"
6666
ame_reaction_2 = data_dir / "rct2.mas20"
@@ -107,7 +107,7 @@ def _combine_all_data(self) -> pd.DataFrame:
107107
df.loc[(df.Symbol == "C") & (df.A == 12), "NubaseRelativeError"] = 0.0
108108
df.loc[(df.Symbol == "C") & (df.A == 12), "AMERelativeError"] = 0.0
109109

110-
# 198Au has a typo in it's decay mode in the 2012 table. It is down as '-'
110+
# 198Au has a typo in it's decay mode in the 2012 table. It is recorded as '-'
111111
df.loc[(df.A == 198) & (df.Z == 79) & (df.TableYear == 2012), 'Decay'] = "B-"
112112

113113
return df

pynch/nubase_parse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Extract the data from the nubse file."""
22
import logging
3+
import pathlib
34
import re
45
import typing
56

@@ -14,7 +15,7 @@ class NubaseParser(NubaseFile):
1415
A collection of functions to parse the weird format of the NUBASE file.
1516
"""
1617

17-
def __init__(self, filename: str, year: int):
18+
def __init__(self, filename: pathlib.Path, year: int):
1819
"""Set the file to read and the table year."""
1920
self.filename = filename
2021
self.year = year

0 commit comments

Comments
 (0)