def create_from_description(description): """ Instantiate a new object from a description. Args: description (dict): A description of the object. Returns: A new instance of the described object """ if isinstance(description, dict): if '@type' in description: name = description['@type'] for describable in get_inheritors(Describable): if describable.__name__ == name: return describable.__new_from_description__(description) raise TypeError('No describable class "{}" found!'.format(name)) else: return {k: create_from_description(v) for k, v in description.items()} elif (isinstance(description, (bool, float, type(None))) or isinstance(description, six.integer_types) or isinstance(description, six.string_types)): return description elif isinstance(description, list): return [create_from_description(d) for d in description] raise TypeError('Invalid description of type {}'.format(type(description)))
def get_layer_class_from_typename(typename): layer_classes = get_inheritors(Layer) for layer_class in layer_classes: if typename == layer_class.__name__: return layer_class else: raise TypeError("Layer-type '{}' unknown!".format(typename))
def test_get_inheritors(): class A(object): pass class B(A): pass class C(B): pass class D(A): pass class E(object): pass assert get_inheritors(A) == {B, C, D}