10.7. Utilities

Small module of helper functions and classes.

10.7.1. Unit Conversions

Utility functions shared for two or more modules

class cherab.core.utility.conversion.AmuToKg

Converts from amu to kg

class cherab.core.utility.conversion.AngstromToNm

Converts from Angstroms to nm.

class cherab.core.utility.conversion.BaseFactorConversion

Base class for conversion based on factor

classmethod inv(x)

Inverse conversion

classmethod to(x)

Direct conversion

class cherab.core.utility.conversion.Cm3ToM3

Converts from cm3 to m3

class cherab.core.utility.conversion.EvAmuToMS

Converts from eV/amu to velocity (m/s)

classmethod inv(x)

Inverse conversion

classmethod to(x)

Direct conversion

class cherab.core.utility.conversion.EvToJ

Converts from eV to Jules

class cherab.core.utility.conversion.PerCm3ToPerM3

Converts from cm-3 to m-3

class cherab.core.utility.conversion.PhotonToJ

Converts from photon to Jules

classmethod inv(x, wavelength)

Inverse conversion; wavelength in nm

classmethod to(x, wavelength)

Direct conversion; wavelength in nm

10.7.2. Notify

class cherab.core.utility.notify.Notifier

Allows objects to broadcast notifications to observing objects.

This object implements a version of the observer pattern. Objects wishing to be notified may register a callback function with the notifier. The callbacks will be called when the notify method of the Notifier is called.

The primary purpose of this class is to permit cache control between disconnected objects. To speed up calculations, objects may cache the results of a calculation involving a source object. If the source object data changes, the caches of the dependent objects must be invalidated otherwise stale data may be used in subsequent calculations.

Callbacks are assumed to have no arguments.

This object holds weak references to callbacks. If an observing object has registered a method as a callback and that object is subsequently deleted, the callback will be automatically removed from the list of registered callbacks. The Notifier will not prevent referenced objects being garbage collected.

class cherab.core.utility.notify.NotifyingList(*args, **kwargs)

A list that reports changes to its contents.

The NotifyingList class is a subclass of the builtin list type. It extends the list type to add a Notifier object that generates notifications whenever the list contents are modified. A notifier attribute is provided to supply access to configure the internal Notifier object.

The NotifierList implements the entire list interface. Please note however that __add__ or __mul__ operations involving a NotifyingList will return a basic builtin list.

append(p_object)

Append object to the end of the list.

clear()

Remove all items from list.

extend(iterable)

Extend list by appending elements from the iterable.

insert(index, p_object)

Insert object before index.

pop(index=-1)

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(value)

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

sort(**kwargs)

Stable sort IN PLACE.

10.7.3. Recursive Dict

class cherab.core.utility.recursivedict.RecursiveDict

A dictionary that implements a basic, automatically expanding tree.

If a key is accessed that is not defined then it is automatically populated with another RecursiveDict object. This allows the user to rapidly construct nested trees of data, with each level of the tree automatically created. The RecursiveDict is especially useful for quickly assembling configuration files. Once the RecursiveDict is populated it can be frozen by converting the tree to a nested set of basic python dictionaries.

For example:

a = RecursiveDict()
a["animal"]["bird"]["parrot"]["dead"] = True
a["tree"]["larch"] = "The larch."
b = a.freeze()

This will produce the following nested dictionary in b:

b = {
    "animal": {
        "bird": {
            "parrot": {
                "dead": True
            }
        }
    },
    "tree": {
        "larch": "The larch."
    }
}
freeze()

Returns a copy of this object with the RecursiveDicts replaced with basic python dictionaries.

classmethod from_dict(dictionary)

Returns a copy of the dictionary as a RecursiveDict.