General supporting functions
Functions:
Exit with a message (message) and a delay of t seconds.
Example:
>>> delayed_exit()
Exiting...
Flatten a nested list.
Parameters:
l: List (may be nested to an arbitrary depth)
If the type of l is not in ltypes, then it is placed in a list.
ltypes: Tuple (not list) of accepted indexable types
Example:
>>> flatten_list([1, [2, 3, [4]]])
[1, 2, 3, 4]
Format the scientific notation in a numeric string to HTML, LaTeX, or Unicode.
Parameters:
num_str: A numeric string
If the string does not contain ‘e’ (no scientific notation), then it is returned directly.
code: Format code
Examples:
>>> format_e('2.0e5', 'H')
2.0×10<sup>5</sup>
>>> print(format_e('5e-10', 'L'))
5 \times 10^{-10}
Return 1) the contents of a parenthetical expression and 2) everything after it.
The parenthetical expression starts at the beginning of expr and may be nested.
Example:
>>> get_group("((abc)*d)*2")
('(abc)*d', '*2')
Return a list of the names of a package and its subpackages.
This only works if the package has a __path__ attribute, which is not the case for some (all?) of the built-in packages.
Example:
>>> from natu import groups
>>> for package in list_packages(groups):
... print(package)
natu.groups
natu.groups.acceleration
natu.groups.amount
natu.groups.angle
natu.groups.area
natu.groups.charge
natu.groups.conductance
natu.groups.constants
natu.groups.current
natu.groups.dimensionless
natu.groups.energy
natu.groups.force
natu.groups.frequency
natu.groups.length
natu.groups.magnetic_flux
natu.groups.magnetic_flux_density
natu.groups.mass
natu.groups.potential
natu.groups.power
natu.groups.pressure
natu.groups.resistance
natu.groups.si
natu.groups.temperature
natu.groups.time
natu.groups.velocity
natu.groups.volume
Return a set of filenames that match a pathname pattern.
Unlike Python’s glob.glob() function, this function runs an additional expansion on matches that are directories.
Parameters:
pathnames: String or set of strings used to match files or folders
This may contain Unix shell-style patterns:
Character(s) Role * Matches everything ? Matches any single character [seq] Matches any character in seq [!seq] Matches any char not in seq
extensions: Filename pattern or set of patterns that should be used to match files in any directories generated from pathnames
These may also use the shell-style patterns above.
Example:
>>> import natu >>> from os.path import dirname, join >>> dname = dirname(natu.__file__) >>> multiglob([join(dname, '*d*'), join(dname, '*/*d*')], {'*.py'}) {'...natu/_decorators.py', '...natu/config/derived.ini', '...natu/groups/conductance.py', '...natu/groups/dimensionless.py', '...natu/groups/magnetic_flux_density.py'}
Perform a list of replacements on the text in a list of files.
Parameters:
fnames: Filename or list of filenames
rpl: List of replacements to make
Each entry is a tuple of (src, dest), where dest is a string that will replace the src string. Each string can use Python’s re expressions; see https://docs.python.org/2/library/re.html for details.
Example:
>>> fname = 'temp.txt'
>>> with open(fname, 'w') as f:
... __ = f.write("apple orange banana")
>>> replace([fname], [('ba(.*)', r'ba\1\1')])
>>> with open(fname, 'r') as f:
... print(f.read())
apple orange banananana
Ask a yes/no question and return True if the answer is ‘Y’ or ‘y’.
Parameters:
Example:
>>> if yes("Do you want to print hello (y/n)?"):
... print("hello")