Skip to content

Commit

Permalink
Allow pre-Python 3.9 versions of importlib.resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Aug 3, 2023
1 parent 2ccfef1 commit d623085
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions src/pyvalem/atom_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"""

import csv
import platform

try:
import importlib.resources as pkg_resources
Expand All @@ -46,6 +47,8 @@
# noinspection PyUnresolvedReferences
import importlib_resources as pkg_resources

PYTHON3_VERSION = int(platform.python_version_tuple()[1])


class Atom:
"""Class representing an atom instance.
Expand Down Expand Up @@ -172,16 +175,12 @@ def float_or_none(f):
return None


# list of all the element symbols recognised by pyvalem:
element_symbols = []
# pre-built mapping between element symbols and Atom instances:
atoms = {}
# Atom data is from Meija et al., "Atomic weights of the elements 2013
# (IUPAC Technical Report)", Pure Appl. Chem. 88(3), 265-291, 2016.
# See https://ciaaw.org/atomic-weights.htm
with pkg_resources.files("pyvalem").joinpath("_data_atomic_weights.txt").open(
"r", encoding="utf8"
) as fi:
def read_atom_data(fi, atoms):
"""
Read atom data from open file handle fi into dictionary atoms, keyed by
element symbol.
"""

reader = csv.reader(fi, delimiter=",")
header = ["Symbol", "Name", "Z", "atomic_weight", "atomic_weight_unc"]
for row in reader:
Expand All @@ -193,17 +192,34 @@ def float_or_none(f):
element_symbol = atom_args[0]
element_symbols.append(element_symbol)
atoms[element_symbol] = Atom(*atom_args)
return atoms


# list of all the element symbols recognised by pyvalem:
element_symbols = []
# pre-built mapping between element symbols and Atom instances:
atoms = {}
# Atom data is from Meija et al., "Atomic weights of the elements 2013
# (IUPAC Technical Report)", Pure Appl. Chem. 88(3), 265-291, 2016.
# See https://ciaaw.org/atomic-weights.htm
if PYTHON3_VERSION < 9:
# NB Python 3.8 and below use open_text:
with pkg_resources.open_text("pyvalem", "_data_atomic_weights.txt") as fi:
read_atom_data(fi, atoms)
else:
# NB Python 3.9 and above use importlib.resources.files:
with pkg_resources.files("pyvalem").joinpath("_data_atomic_weights.txt").open(
"r", encoding="utf8"
) as fi:
read_atom_data(fi, atoms)


def read_isotope_data(fi, isotopes):
"""
Read in isotope mass data from open file handle fi into dictionary isotopes,
keyed by isotope symbol (e.g. "46Ti").
"""

# pre-built mapping between element isotopic symbols and Isotope instances:
isotopes = {}
# Isotope data is from the AME2016 Atomic Mass Evaluation reports,
# Huang et al., "The Ame2016 atomic mass evaluation (I)", Chinese Physics C41,
# 030002 (2017); Wang et al., "The Ame2016 atomic mass evaluation (II)",
# Chinese Physics C41, 030003 (2017).
# See http://amdc.impcas.ac.cn/masstables/Ame2016/mass16.txt
with pkg_resources.files("pyvalem").joinpath("_data_isotope_masses.txt").open(
"r", encoding="utf8"
) as fi:
reader = csv.reader(fi, delimiter=",")
header = ["Z", "A", "Symbol", "mass", "mass_unc", "estimated_flag"]
iso_attribs = [
Expand All @@ -229,3 +245,22 @@ def float_or_none(f):
iso_symbol = "{:d}{:s}".format(iso_kwargs["mass_number"], iso_kwargs["symbol"])
iso_kwargs["symbol"] = iso_symbol
isotopes[iso_symbol] = Isotope(**iso_kwargs)


# pre-built mapping between element isotopic symbols and Isotope instances:
isotopes = {}
# Isotope data is from the AME2016 Atomic Mass Evaluation reports,
# Huang et al., "The Ame2016 atomic mass evaluation (I)", Chinese Physics C41,
# 030002 (2017); Wang et al., "The Ame2016 atomic mass evaluation (II)",
# Chinese Physics C41, 030003 (2017).
# See http://amdc.impcas.ac.cn/masstables/Ame2016/mass16.txt
if PYTHON3_VERSION < 9:
# NB Python 3.8 and below use open_text:
with pkg_resources.open_text("pyvalem", "_data_isotope_masses.txt") as fi:
read_isotope_data(fi, isotopes)
else:
# NB Python 3.9 and above use importlib.resources.files:
with pkg_resources.files("pyvalem").joinpath("_data_isotope_masses.txt").open(
"r", encoding="utf8"
) as fi:
read_isotope_data(fi, isotopes)

0 comments on commit d623085

Please sign in to comment.