Skip to content

Commit

Permalink
Merge pull request #97 from a-detiste/R2.0-pre
Browse files Browse the repository at this point in the history
remove Python2 compatibility
  • Loading branch information
pkienzle authored Dec 3, 2024
2 parents 3a1cbf2 + a7e11f8 commit 134aed2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 112 deletions.
8 changes: 3 additions & 5 deletions periodictable/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@
# 58Ni (n,alpha)


from __future__ import division, print_function

from math import exp, log, expm1
import os

Expand Down Expand Up @@ -253,7 +251,7 @@ def IAEA1987_isotopic_abundance(iso):
except AttributeError:
return 0

class Sample(object):
class Sample:
"""
Sample properties.
Expand Down Expand Up @@ -454,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.
Expand Down Expand Up @@ -755,7 +753,7 @@ def activity(isotope, mass, env, exposure, rest_times):

return result

class ActivationResult(object):
class ActivationResult:
r"""
*isotope* :
Expand Down
11 changes: 5 additions & 6 deletions periodictable/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -153,7 +152,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.
Expand Down Expand Up @@ -413,7 +412,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 = {}
Expand All @@ -426,7 +425,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.
Expand Down Expand Up @@ -461,7 +460,7 @@ def __reduce__(self):
self.element.number,
self.charge)

class Isotope(object):
class Isotope:
"""
Periodic table entry for an individual isotope.
Expand Down Expand Up @@ -489,7 +488,7 @@ def __reduce__(self):
self.element.number,
self.isotope)

class Element(object):
class Element:
"""
Periodic table entry for an element.
Expand Down
2 changes: 1 addition & 1 deletion periodictable/cromermann.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion periodictable/fasta.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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.
Expand Down
11 changes: 4 additions & 7 deletions periodictable/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
Chemical formula parser.
"""
from __future__ import division, print_function

from copy import copy
from math import pi, sqrt
Expand All @@ -15,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)
Expand Down Expand Up @@ -306,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.
Expand Down Expand Up @@ -519,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.
Expand All @@ -544,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.
Expand Down
2 changes: 1 addition & 1 deletion periodictable/magnetic_ff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions periodictable/nsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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',
Expand Down Expand Up @@ -330,7 +330,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,
Expand Down Expand Up @@ -486,8 +486,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.
Expand All @@ -508,8 +507,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.
Expand Down Expand Up @@ -665,8 +663,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"""
Expand Down
71 changes: 0 additions & 71 deletions periodictable/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 6 additions & 12 deletions periodictable/xsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -243,7 +242,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.
Expand Down Expand Up @@ -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''.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 134aed2

Please sign in to comment.