Utillity functions and classes

This module contains utility functions and classes.

ase.utils.opencew(filename, world=None)[source]

Create and open filename exclusively for writing.

If master cpu gets exclusive write access to filename, a file descriptor is returned (a dummy file descriptor is returned on the slaves). If the master cpu does not get write access, None is returned on all processors.

ase.utils.gcd(x, y) → int

greatest common divisor of x and y

ase.utils.seterr(**kwargs)[source]

Set how floating-point errors are handled.

See np.seterr() for more details.

ase.utils.plural(n, word)[source]

Use plural for n!=1.

>>> plural(0, 'egg'), plural(1, 'egg'), plural(2, 'egg')
('0 eggs', '1 egg', '2 eggs')
ase.utils.formula_hill(numbers)[source]

Convert list of atomic numbers to a chemical formula as a string.

Elements are alphabetically ordered with C and H first.

ase.utils.formula_metal(numbers)[source]

Convert list of atomic numbers to a chemical formula as a string.

Elements are alphabetically ordered with metals first.

ase.utils.convert_string_to_fd(name, world=None)[source]

Create a file-descriptor for text output.

Will open a file for writing with given name. Use None for no output and ‘-‘ for sys.stdout.

class ase.utils.timing.Timer(print_levels=1000)[source]

Timer object.

Use like this:

timer = Timer()
timer.start('description')
# do something
timer.stop()

or:

with timer('description'):
    # do something

To get a summary call:

timer.write()
class ase.utils.timing.timer(name)[source]

Decorator for timing a method call.

Example:

from ase.utils.timing import timer, Timer

class A:
    def __init__(self):
        self.timer = Timer()

    @timer('Add two numbers')
    def add(self, x, y):
        return x + y