obspy.core.util.obspy_types.Enum

class Enum(enums, replace={})[source]

Bases: object

Enumerated type (enum) implementation for Python.

Parameters:replace (dict, optional) – Dictionary of keys which are replaced by values.

Example

>>> from obspy.core.util import Enum
>>> units = Enum(["m", "s", "m/s", "m/(s*s)", "m*s", "other"])

There are different ways to access the correct enum values:

>>> print(units.get('m/s'))
m/s
>>> print(units['S'])
s
>>> print(units.OTHER)
other
>>> print(units[3])
m/(s*s)
>>> units.xxx  # doctest: +ELLIPSIS
Traceback (most recent call last):
...
AttributeError: 'xxx'

Changing enum values will not work:

>>> units.m = 5  # doctest: +ELLIPSIS
Traceback (most recent call last):
...
NotImplementedError
>>> units['m'] = 'xxx'  # doctest: +ELLIPSIS
Traceback (most recent call last):
...
NotImplementedError

Calling with a value will either return the mapped enum value or None:

>>> print(units("M*s"))
m*s
>>> units('xxx')
>>> print(units(5))
other

The following enum allows replacing certain entries:

>>> units2 = Enum(["m", "s", "m/s", "m/(s*s)", "m*s", "other"],
...               replace={'meter': 'm'})
>>> print(units2('m'))
m
>>> print(units2('meter'))
m

Attributes

__dict__
__doc__
__isabstractmethod__
__module__
__weakref__ list of weak references to the object (if defined)

Public Methods

get
items
iteritems
keys
values

Private Methods

Warning

Private methods are mainly for internal/developer use and their API might change without notice.

_repr_pretty_

Special Methods

__call__ Call self as a function.
__contains__
__dir__ Default dir() implementation.
__format__ Default object formatter.
__getattr__
__getitem__
__init__ Initialize self.
__init_subclass__ This method is called when a class is subclassed.
__new__ Create and return a new object.
__reduce__ Helper for pickle.
__reduce_ex__ Helper for pickle.
__repr__
>>> enum = Enum(["c", "a", "b"])
__setattr__ Implement setattr(self, name, value).
__setitem__ Implement setattr(self, name, value).
__sizeof__ Size of object in memory, in bytes.
__str__
>>> enum = Enum(["c", "a", "b"])
__subclasshook__ Abstract classes can override this to customize issubclass().