From b689b8b6b2313d5557316fe5b1476aae782f58f8 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Wed, 27 Nov 2024 22:15:48 +0100 Subject: [PATCH 1/3] drop Python2 crumbs 1/2 --- periodictable/activation.py | 2 -- periodictable/core.py | 1 - periodictable/formulas.py | 1 - 3 files changed, 4 deletions(-) diff --git a/periodictable/activation.py b/periodictable/activation.py index 9bf130c..bfecddc 100644 --- a/periodictable/activation.py +++ b/periodictable/activation.py @@ -222,8 +222,6 @@ # 58Ni (n,alpha) -from __future__ import division, print_function - from math import exp, log, expm1 import os diff --git a/periodictable/core.py b/periodictable/core.py index 8294294..09d3d28 100644 --- a/periodictable/core.py +++ b/periodictable/core.py @@ -55,7 +55,6 @@ periodic table with custom values for the attributes. """ -from __future__ import print_function __docformat__ = 'restructuredtext en' __all__ = ['delayed_load', 'define_elements', 'get_data_path', diff --git a/periodictable/formulas.py b/periodictable/formulas.py index 75e07d0..7209dda 100644 --- a/periodictable/formulas.py +++ b/periodictable/formulas.py @@ -3,7 +3,6 @@ """ Chemical formula parser. """ -from __future__ import division, print_function from copy import copy from math import pi, sqrt From 91af03a41d91679249c2467307b72d9e14234fb3 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Wed, 27 Nov 2024 22:21:40 +0100 Subject: [PATCH 2/3] remove more Python2 syntax --- periodictable/activation.py | 6 +++--- periodictable/core.py | 10 +++++----- periodictable/cromermann.py | 2 +- periodictable/fasta.py | 2 +- periodictable/formulas.py | 2 +- periodictable/magnetic_ff.py | 2 +- periodictable/nsf.py | 2 +- periodictable/xsf.py | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/periodictable/activation.py b/periodictable/activation.py index bfecddc..7d698e2 100644 --- a/periodictable/activation.py +++ b/periodictable/activation.py @@ -251,7 +251,7 @@ def IAEA1987_isotopic_abundance(iso): except AttributeError: return 0 -class Sample(object): +class Sample: """ Sample properties. @@ -452,7 +452,7 @@ def sorted_activity(activity_pair): return sorted(activity_pair, key=lambda x: (x[0].isotope, x[0].daughter)) -class ActivationEnvironment(object): +class ActivationEnvironment: """ Neutron activation environment. @@ -745,7 +745,7 @@ def activity(isotope, mass, env, exposure, rest_times): return result -class ActivationResult(object): +class ActivationResult: r""" *isotope* : diff --git a/periodictable/core.py b/periodictable/core.py index 09d3d28..84f5675 100644 --- a/periodictable/core.py +++ b/periodictable/core.py @@ -151,7 +151,7 @@ def setfn(el, value): setattr(Ion, p, prop) # Define the element names from the element table. -class PeriodicTable(object): +class PeriodicTable: """ Defines the periodic table of the elements with isotopes. Individidual elements are accessed by name, symbol or atomic number. @@ -411,7 +411,7 @@ def list(self, *props, **kw): # print "format", format, "args", L # raise -class IonSet(object): +class IonSet: def __init__(self, element_or_isotope): self.element_or_isotope = element_or_isotope self.ionset = {} @@ -424,7 +424,7 @@ def __getitem__(self, charge): self.ionset[charge] = Ion(self.element_or_isotope, charge) return self.ionset[charge] -class Ion(object): +class Ion: """ Periodic table entry for an individual ion. @@ -459,7 +459,7 @@ def __reduce__(self): self.element.number, self.charge) -class Isotope(object): +class Isotope: """ Periodic table entry for an individual isotope. @@ -487,7 +487,7 @@ def __reduce__(self): self.element.number, self.isotope) -class Element(object): +class Element: """ Periodic table entry for an element. diff --git a/periodictable/cromermann.py b/periodictable/cromermann.py index 6b575ae..ea18a8d 100644 --- a/periodictable/cromermann.py +++ b/periodictable/cromermann.py @@ -110,7 +110,7 @@ def fxrayatstol(symbol, stol, charge=None): return rv -class CromerMannFormula(object): +class CromerMannFormula: """ Cromer-Mann formula for x-ray scattering factors. Coefficient storage and evaluation. diff --git a/periodictable/fasta.py b/periodictable/fasta.py index 8b4372b..7110d0e 100644 --- a/periodictable/fasta.py +++ b/periodictable/fasta.py @@ -98,7 +98,7 @@ def isotope_substitution(formula, source, target, portion=1): return formula.replace(source, target, portion=portion) # TODO: allow Molecule to be used as compound in formulas.formula() -class Molecule(object): +class Molecule: """ Specify a biomolecule by name, chemical formula, cell volume and charge. diff --git a/periodictable/formulas.py b/periodictable/formulas.py index 7209dda..31ad42e 100644 --- a/periodictable/formulas.py +++ b/periodictable/formulas.py @@ -305,7 +305,7 @@ def formula(compound=None, density=None, natural_density=None, return Formula(structure=structure, name=name, density=density, natural_density=natural_density) -class Formula(object): +class Formula: """ Simple chemical formula representation. diff --git a/periodictable/magnetic_ff.py b/periodictable/magnetic_ff.py index c9bbb63..eb4acff 100644 --- a/periodictable/magnetic_ff.py +++ b/periodictable/magnetic_ff.py @@ -34,7 +34,7 @@ def formfactor_n(jn, q): return s_sq * (A * exp(-a*s_sq) + B * exp(-b*s_sq) + C * exp(-c*s_sq) + D) -class MagneticFormFactor(object): +class MagneticFormFactor: """ Magnetic form factor for the ion. diff --git a/periodictable/nsf.py b/periodictable/nsf.py index a0d2558..87f404e 100644 --- a/periodictable/nsf.py +++ b/periodictable/nsf.py @@ -327,7 +327,7 @@ def _CHECK_scattering_potential(sld): return (ENERGY_FACTOR/pi) * asarray(sld) _4PI_100 = 4*np.pi/100 -class Neutron(object): +class Neutron: r""" Neutron scattering factors are attached to each element in the periodic table for which values are available. If no information is available, diff --git a/periodictable/xsf.py b/periodictable/xsf.py index eb52b92..eae2591 100644 --- a/periodictable/xsf.py +++ b/periodictable/xsf.py @@ -243,7 +243,7 @@ def xray_energy(wavelength): """ return planck_constant/electron_volt*speed_of_light/np.asarray(wavelength)*1e7 -class Xray(object): +class Xray: """ X-ray scattering properties for the elements. Refer help(periodictable.xsf) from command prompt for details. From a7e11f851fcb14d558abb59504774c6529e8047f Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Wed, 27 Nov 2024 22:27:07 +0100 Subject: [PATCH 3/3] replace custom @require_keywords decorator with modern syntax --- periodictable/formulas.py | 8 ++--- periodictable/nsf.py | 11 +++--- periodictable/util.py | 71 --------------------------------------- periodictable/xsf.py | 16 +++------ 4 files changed, 12 insertions(+), 94 deletions(-) diff --git a/periodictable/formulas.py b/periodictable/formulas.py index 31ad42e..5940f2b 100644 --- a/periodictable/formulas.py +++ b/periodictable/formulas.py @@ -14,7 +14,7 @@ from .core import default_table, isatom, isisotope, ision, change_table from .constants import avogadro_number -from .util import require_keywords, cell_volume +from .util import cell_volume PACKING_FACTORS = dict(cubic=pi/6, bcc=pi*sqrt(3)/8, hcp=pi/sqrt(18), fcc=pi/sqrt(18), diamond=pi*sqrt(3)/16) @@ -518,8 +518,7 @@ def volume(self, *args, **kw): packing_factor = PACKING_FACTORS[packing_factor.lower()] return V/packing_factor*1e-24 - @require_keywords - def neutron_sld(self, wavelength=None, energy=None): + def neutron_sld(self, *, wavelength=None, energy=None): """ Neutron scattering information for the molecule. @@ -543,8 +542,7 @@ def neutron_sld(self, wavelength=None, energy=None): return neutron_sld(self.atoms, density=self.density, wavelength=wavelength, energy=energy) - @require_keywords - def xray_sld(self, energy=None, wavelength=None): + def xray_sld(self, *, energy=None, wavelength=None): """ X-ray scattering length density for the molecule. diff --git a/periodictable/nsf.py b/periodictable/nsf.py index 87f404e..63abfe1 100644 --- a/periodictable/nsf.py +++ b/periodictable/nsf.py @@ -188,7 +188,7 @@ from .core import Element, Isotope, default_table from .constants import (avogadro_number, planck_constant, electron_volt, neutron_mass, atomic_mass_constant) -from .util import require_keywords, parse_uncertainty +from .util import parse_uncertainty __all__ = ['init', 'Neutron', 'neutron_energy', 'neutron_wavelength', @@ -483,8 +483,7 @@ def scattering_by_wavelength(self, wavelength): sigma_s = _4PI_100*abs(b_c)**2 # 1 barn = 1 fm^2 1e-2 barn/fm^2 return b_c, sigma_s - @require_keywords - def sld(self, wavelength=ABSORPTION_WAVELENGTH): + def sld(self, *, wavelength=ABSORPTION_WAVELENGTH): r""" Returns scattering length density for the element at natural abundance and density. @@ -505,8 +504,7 @@ def sld(self, wavelength=ABSORPTION_WAVELENGTH): return None, None, None return self.scattering(wavelength=wavelength)[0] - @require_keywords - def scattering(self, wavelength=ABSORPTION_WAVELENGTH): + def scattering(self, *, wavelength=ABSORPTION_WAVELENGTH): r""" Returns neutron scattering information for the element at natural abundance and density. @@ -650,8 +648,7 @@ def init(table, reload=False): # TODO: split incoherent into spin and isotope incoherence (eq 17-19 of Sears) # TODO: require parsed compound rather than including formula() keywords in api # Note: docs and function prototype are reproduced in __init__ -@require_keywords -def neutron_scattering(compound, density=None, +def neutron_scattering(compound, *, density=None, wavelength=None, energy=None, natural_density=None, table=None): r""" diff --git a/periodictable/util.py b/periodictable/util.py index 2f9c25e..e729547 100644 --- a/periodictable/util.py +++ b/periodictable/util.py @@ -91,74 +91,3 @@ def cell_volume(a=None, b=None, c=None, alpha=None, beta=None, gamma=None): cgamma = cos(radians(gamma)) if gamma is not None else calpha V = a*b*c*sqrt(1 - calpha**2 - cbeta**2 - cgamma**2 + 2*calpha*cbeta*cgamma) return V - -def require_keywords(function): - r""" - Decorator which forces all keyword arguments to the function to be - explicitly named. - - For example: - - >>> @require_keywords - ... def fn(a, b, c=3): pass - >>> fn(1, 2, 3) - Traceback (most recent call last): - ... - TypeError: name=value required for c - >>> fn(1, 2, c=6) - >>> fn(b=1, a=2, c=6) - - Variable arguments are not currently supported: - - >>> @require_keywords - ... def fn(a, b, c=6, *args, **kw): pass - Traceback (most recent call last): - ... - NotImplementedError: only named arguments for now - - .. Note:: The call signature is not preserved. - - We can't preserve the function signature for the call since the only - way we can count the number of non-keyword arguments is to - use the \*args, \*\*kw call style. Python 3+ provides the '\*' call - signature element which will force all keywords after '\*' to be named. - """ - import functools - try: - from inspect import signature - getargspec = _getargspec_from_signature - except ImportError: # CRUFT: py 2.7 support - from inspect import getargspec - - args, vararg, varkwd, defaults = getargspec(function) - if defaults is None: - defaults = [] - named_args = args[:-len(defaults)] - named_kwds = args[-len(defaults):] - # Keep it simple for now - if vararg or varkwd: - raise NotImplementedError("only named arguments for now") - @functools.wraps(function) - def _require_kwds(*args, **kw): - if len(args) > len(named_args): - raise TypeError("name=value required for "+", ".join(named_kwds)) - return function(*args, **kw) - return _require_kwds - -def _getargspec_from_signature(function): - """ - Reproduce getargspec() interface using newer signature protocol - """ - from inspect import signature - - args, vararg, varkwd, defaults = [], None, None, [] - sig = signature(function) - for p in sig.parameters.values(): - args.append(p.name) - if p.default is not p.empty: - defaults.append(p.default) - if p.kind is p.VAR_POSITIONAL: - vararg = p.name - elif p.kind is p.VAR_KEYWORD: - varkwd = p.name - return args, vararg, varkwd, defaults diff --git a/periodictable/xsf.py b/periodictable/xsf.py index eae2591..a1a5950 100644 --- a/periodictable/xsf.py +++ b/periodictable/xsf.py @@ -193,7 +193,6 @@ from .constants import ( avogadro_number, planck_constant, speed_of_light, electron_volt, electron_radius) -from .util import require_keywords def xray_wavelength(energy): r""" @@ -271,8 +270,7 @@ def _gettable(self): return self._table sftable = property(_gettable, doc="X-ray scattering factor table (E,f1,f2)") - @require_keywords - def scattering_factors(self, energy=None, wavelength=None): + def scattering_factors(self, *, energy=None, wavelength=None): """ X-ray scattering factors f', f''. @@ -337,8 +335,7 @@ def f0(self, Q): charge=self.element.charge) return f - @require_keywords - def sld(self, wavelength=None, energy=None): + def sld(self, *, wavelength=None, energy=None): r""" X ray scattering length density. @@ -382,8 +379,7 @@ def sld(self, wavelength=None, energy=None): return rho, irho # Note: docs and function prototype are reproduced in __init__ -@require_keywords -def xray_sld(compound, density=None, natural_density=None, +def xray_sld(compound, *, density=None, natural_density=None, wavelength=None, energy=None): """ Compute xray scattering length densities for molecules. @@ -435,8 +431,7 @@ def xray_sld(compound, density=None, natural_density=None, return rho, irho -@require_keywords -def index_of_refraction(compound, density=None, natural_density=None, +def index_of_refraction(compound, *, density=None, natural_density=None, energy=None, wavelength=None): """ Calculates the index of refraction for a given compound @@ -470,8 +465,7 @@ def index_of_refraction(compound, density=None, natural_density=None, wavelength=wavelength) return 1 - wavelength**2/(2*pi)*(f1 + f2*1j)*1e-6 -@require_keywords -def mirror_reflectivity(compound, density=None, natural_density=None, +def mirror_reflectivity(compound, *, density=None, natural_density=None, energy=None, wavelength=None, angle=None, roughness=0): """