From d62308551859ef70adfb6f36da11b80f2a5ffb04 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 3 Aug 2023 19:01:23 +0000 Subject: [PATCH] Allow pre-Python 3.9 versions of importlib.resources --- src/pyvalem/atom_data.py | 75 +++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/src/pyvalem/atom_data.py b/src/pyvalem/atom_data.py index 0f2efdd..0197456 100644 --- a/src/pyvalem/atom_data.py +++ b/src/pyvalem/atom_data.py @@ -38,6 +38,7 @@ """ import csv +import platform try: import importlib.resources as pkg_resources @@ -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. @@ -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: @@ -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 = [ @@ -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)