Types

Module containing all the data types used with Attribute classes.

class common.types.Type

The most general type, parent for all specific types.

validate(value)

Validates if value is of correct data type.

Parameters

value (Any) – value to be validated

Raises

ValueError if validation fails

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

Any

to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (Any) – value to be converted

Returns

JSON representation of the value

Return type

Any

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (Any) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

class common.types.AnyType
validate(value)

Validates if value is of correct data type.

Parameters

value (Any) – value to be validated

Raises

ValueError if validation fails

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

Any

class common.types.NoneType
class common.types.BoolType
class common.types.StringType
convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

str

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (str) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

class common.types.IntegerType
convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

int

class common.types.FloatType
convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

float

class common.types.UnionType(*of_types)
Parameters

of_types (Type) –

validate(value)

Validates if value is of correct data type.

Parameters

value (Any) – value to be validated

Raises

ValueError if validation fails

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

Any

to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (Any) – value to be converted

Returns

JSON representation of the value

Return type

Any

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (Any) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

class common.types.TupleType(*of_types)
Parameters

of_types (Type) –

validate(value)

Validates if value is of correct data type.

Parameters

value (Any) – value to be validated

Raises

ValueError if validation fails

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

tuple

to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (tuple) – value to be converted

Returns

JSON representation of the value

Return type

list

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (tuple) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

class common.types.ListType(of_type=Any)
Parameters

of_type (Type) –

validate(value)

Validates if value is of correct data type.

Parameters

value (Any) – value to be validated

Raises

ValueError if validation fails

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

list

to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (list) – value to be converted

Returns

JSON representation of the value

Return type

list

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (list) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

class common.types.DictType(keys, values)
Parameters
validate(value)

Validates if value is of correct data type.

Parameters

value (Any) – value to be validated

Raises

ValueError if validation fails

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

Any

to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (dict) – value to be converted

Returns

JSON representation of the value

Return type

dict

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (dict) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

class common.types.DateTimeType
to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (datetime.datetime) – value to be converted

Returns

JSON representation of the value

Return type

str

class common.types.ObjectType(class_)

Type representing some custom, user-defined class. It is assumed that objects of this class can be fully instantiated with a dict containing all the needed data.

Example:

>>> class Foo:
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
...
...     def __repr__(self):
...         return f'<Foo a={self.a}, b={self.b} />'

>>> type_ = ObjectType(Foo)
>>> type_.convert({'a': 123, 'b': 'bar'})
<Foo a=123, b=bar />
Parameters

class_ (TypingType[T]) –

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

T

to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (T) – value to be converted

Returns

JSON representation of the value

Return type

Dict[str, Any]

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (T) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

class common.types.MappingType(mapping_key, mapping)

Type representing some kind of mapping. It is used to determine the correct type of the object at runtime, depending on the specified field.

Example:

>>> class Foo:
...     def __init__(self, a, type):
...         self.a = a
...         self.type = type
...
...     def __repr__(self):
...         return f'<Foo a={self.a} />'

>>> class Bar:
...     def __init__(self, a, b, c, type):
...         self.abc = f'{a}{b}{c}'
...         self.type = type
...
...     def __repr__(self):
...         return f'<Bar abc={self.abc} />'

>>> type_ = MappingType('type', {
...     'foo': ObjectType(Foo),
...     'bar': ObjectType(Bar),
... })

>>> type_.convert({'type': 'foo', 'a': 123})
<Foo a=123 />

>>> type_.convert({'type': 'bar', 'a': 'a', 'b': 'b', 'c': 42})
<Bar abc=ab42 />

>>> foo = Foo(a=123, type='foo')
>>> type_.validate(foo)

>>> foo = Foo(a=123, type='bar')
>>> type_.validate(foo)
Traceback (most recent call last):
...
ValueError
Parameters
  • mapping_key (str) –

  • mapping (Dict[str, ObjectType]) –

convert(value)

Tries to convert value to self._type data type.

Parameters

value (Any) – value to be converted

Returns

possibly converted value, but it might also be just the original value

Return type

Any

validate(value)

Validates if value is of correct data type.

Parameters

value (Any) – value to be validated

Raises

ValueError if validation fails

to_json(value)

Converts the value to JSON.

Be aware, that the result is not a string, but is instead represented with built-in Python types.

Parameters

value (Any) – value to be converted

Returns

JSON representation of the value

Return type

Dict[str, Any]

value_repr(value)

Returns the string representation of the value for the self._type data type.

Parameters

value (Any) – value to be represented as a string

Returns

string representing value for this particular data type

Return type

str

common.types.new_type(type_)
Parameters

type_ (common.types.Type) –

Return type

Callable[[], common.types.Type]

common.types.NumericType()