obspy.core.event.base._event_type_class_factory

_event_type_class_factory(class_name, class_attributes=[], class_contains=[])[source]

Class factory to unify the creation of all the types needed for the event handling in ObsPy.

The types oftentimes share attributes and setting them manually every time is cumbersome, error-prone and hard to do consistently. The classes created with this method will inherit from AttribDict.

Usage to create a new class type:

The created class will assure that any given (key, type) attribute pairs will always be of the given type and will attempt to convert any given value to the correct type and raise an error otherwise. This happens to values given during initialization as well as values set when the object has already been created. A useful type is Enum if you want to restrict the acceptable values.

>>> from obspy.core.util import Enum
>>> MyEnum = Enum(["a", "b", "c"])
>>> class_attributes = [                 ("resource_id", ResourceIdentifier),                 ("creation_info", CreationInfo),                 ("some_letters", MyEnum),                 ("some_error_quantity", float, ATTRIBUTE_HAS_ERRORS),                 ("description", str)]

Furthermore the class can contain lists of other objects. There is not much to it so far. Giving the name of the created class is mandatory.

>>> class_contains = ["comments"]
>>> TestEventClass = _event_type_class_factory("TestEventClass",                 class_attributes=class_attributes,                 class_contains=class_contains)
>>> assert(TestEventClass.__name__ == "TestEventClass")

Now the new class type can be used.

>>> test_event = TestEventClass(resource_id="event/123456",                 creation_info={"author": "obspy.org", "version": "0.1"})

All given arguments will be converted to the right type upon setting them.

>>> test_event.resource_id
ResourceIdentifier(id="event/123456")
>>> print(test_event.creation_info)
CreationInfo(author='obspy.org', version='0.1')

All others will be set to None.

>>> assert(test_event.description is None)
>>> assert(test_event.some_letters is None)

If the resource_id attribute of the created class type is set, the object the ResourceIdentifier refers to will be the class instance.

>>> assert(id(test_event) ==             id(test_event.resource_id.get_referred_object()))

They can be set later and will be converted to the appropriate type if possible.

>>> test_event.description = 1
>>> assert(test_event.description == "1")

Trying to set with an inappropriate value will raise an error.

>>> test_event.some_letters = "d" 
Traceback (most recent call last):
    ...
ValueError: Setting attribute "some_letters" failed. ...

If you pass ATTRIBUTE_HAS_ERRORS as the third tuple item for the class_attributes, a error (type QuantityError) will be be created that will be named like the attribute with “_errors” appended.

>>> assert(hasattr(test_event, "some_error_quantity_errors"))
>>> test_event.some_error_quantity_errors  
QuantityError(...)