natu.util

General supporting functions

Functions:

  • delayed_exit() - Exit with a message and a delay.
  • flatten_list() - Flatten a nested list.
  • format_e(): Format the scientific notation in a numeric string to HTML, LaTeX, or Unicode.
  • get_group() - Return a tuple with the contents of a parenthetical expression and everything after it.
  • list_packages() - Return a list of the names of a module and its subpackages.
  • multiglob() - Return a set of filenames that match sets of pathnames and extensions.
  • num2super() - Convert a number to Unicode superscript.
  • replace() - Perform a list of replacements on the text in a list of files.
  • str2super() - Convert a numeric string to Unicode superscript.
  • yes() - Ask a yes/no question and return True if the answer is ‘Y’ or ‘y’.
natu.util.delayed_exit(message=u'Exiting...', t=0.5)

Exit with a message (message) and a delay of t seconds.

Example:

>>> delayed_exit() 
Exiting...
natu.util.flatten_list(l, ltypes=(<type 'list'>, <type 'tuple'>))

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]
natu.util.format_e(num_str, code)

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

    ‘H’ is for HTML, ‘L’ is for LaTeX, and ‘U’ is for Unicode.

Examples:

>>> format_e('2.0e5', 'H')
2.0&times;10<sup>5</sup>
>>> print(format_e('5e-10', 'L'))
5 \times 10^{-10}
natu.util.get_group(expr)

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')
natu.util.list_packages(package)

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
natu.util.multiglob(pathnames, extensions=set([u'*.*']))

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'}
natu.util.num2super(num)

Convert a number to Unicode superscript.

natu.util.replace(fnames, rpls)

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
natu.util.str2super(num_str)

Convert a numeric string to Unicode superscript.

natu.util.yes(question)

Ask a yes/no question and return True if the answer is ‘Y’ or ‘y’.

Parameters:

  • question: String representing the question to the user

Example:

>>> if yes("Do you want to print hello (y/n)?"): 
...     print("hello")