/usr/lib/python3/dist-packages/periodictable/util.py is in python3-periodictable 1.5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | # This program is in the public domain
# Author: Paul Kienzle
"""
Helper functions
"""
def cell_volume(a=None, b=None, c=None, alpha=None, beta=None, gamma=None):
r"""
Compute cell volume from lattice parameters.
:Parameters:
*a*, *b*, *c* : float | |Ang|
Lattice spacings. *a* is required.
*b* and *c* default to *a*.
*alpha*, *beta*, *gamma* : float | |deg|
Lattice angles. *alpha* defaults to 90\ |deg|.
*beta* and *gamma* default to *alpha*.
:Returns:
*V* : float | |Ang^3|
Cell volume
:Raises:
*TypeError* : missing or invalid parameters
The following formula works for all lattice types:
.. math::
V = a b c \sqrt{1 - \cos^2 \alpha - \cos^2 \beta - \cos^2 \gamma
+ 2 \cos \alpha \cos \beta \cos \gamma}
"""
from math import cos, radians, sqrt
if a is None:
raise TypeError('missing lattice parameters')
if b is None:
b = a
if c is None:
c = a
calpha = cos(radians(alpha)) if alpha is not None else 0
cbeta = cos(radians(beta)) if beta is not None else calpha
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 inspect
import functools
args, vararg, varkwd, defaults = inspect.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
|