altimeter.core.graph.field package

Submodules

altimeter.core.graph.field.base module

Base classes for Fields. Fields define how individual elements of input JSON are parsed into a LinkCollection.

class Field

Bases: abc.ABC

Abstract base class for all fields

abstract parse(data, context)

Parse data into a LinkCollection using this field’s definition.

Return type

LinkCollection

class SubField

Bases: altimeter.core.graph.field.base.Field

SubFields are fields which must have a non-anonymous parent Field.

get_parent_alti_key(data, context)

Get the alti_key of the parent of this SubField.

Parameters
  • data (Any) – field data

  • context (Dict[str, Any]) –

    contains auxiliary information which can be passed through the parse process.

    Returns:

    alti_key of parent of this SubField

Return type

str

altimeter.core.graph.field.dict_field module

Dict Fields represent fields which consist of dict-like data.

class AnonymousDictField(source_key, *fields, optional=False, nullable=False)

Bases: altimeter.core.graph.field.base.Field

An AnonymousDictField is a DictField where the source_key of the field is discarded and not used as a name in the resulting graph. See Examples below for more clarity.

Parameters
  • source_key (str) – Name of the key in the input JSON

  • fields (Field) – fields inside this DictField

  • optional (bool) – Whether this key is optional. Defaults to False.

  • nullable (bool) – Whether this field’s value can be null.

Examples

A dict containing 3 ScalarFields
>>> from altimeter.core.graph.field.scalar_field import ScalarField
>>> input = {"Person": {"FirstName": "Bob", "LastName": "Smith"}}
>>> field = AnonymousDictField("Person", ScalarField("FirstName"), ScalarField("LastName"))
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'})}
parse(data, context)

Parse this field and return a LinkCollection.

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) –

    context dict containing data from higher level parsing code.

    Returns:

    LinkCollection

    Raises:

    DictFieldSourceKeyNotFoundException if self.source_key is not in data. DictFieldValueNotADictException if the data does not appear to represent a dict.

Return type

LinkCollection

class AnonymousEmbeddedDictField(*fields)

Bases: altimeter.core.graph.field.base.Field

An AnonymousEmbeddedDictField is a EmbeddedDictField where the source_key of the parent field is discarded and not used as a name in the resulting graph. See Examples below for more clarity.

Parameters

fields (Field) – fields inside this DictField

Examples

A ListField containing an AnonymousEmbeddedDictField with two ScalarFields:
>>> from altimeter.core.graph.field.list_field import ListField
>>> from altimeter.core.graph.field.scalar_field import ScalarField
>>> input = {"People": [{"FirstName": "Bob", "LastName": "Smith"},                         {"FirstName": "Alice", "LastName": "Smith"}]}
>>> field = ListField("People", AnonymousEmbeddedDictField(ScalarField("FirstName"),                                  ScalarField("LastName")))
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'}, {'pred': 'first_name', 'obj': 'Alice'}, {'pred': 'last_name', 'obj': 'Smith'})}
parse(data, context)

Parse this field and return a list of Links.

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) – context dict containing data from higher level parsing code.

Return type

LinkCollection

Returns

LinkCollection

Raises

DictFieldValueNotADictException if the data does not appear to represent a dict.

class DictField(source_key, *fields, alti_key=None, optional=False)

Bases: altimeter.core.graph.field.base.Field

A DictField is a field where the input is a JSON object containing a key (source_key) where the corresponding value is a dictionary.

Examples

A dictionary containing two ScalarFields:
>>> from altimeter.core.graph.field.scalar_field import ScalarField
>>> input = {"Person": {"FirstName": "Bob", "LastName": "Smith"}}
>>> field = DictField("Person", ScalarField("FirstName"), ScalarField("LastName"))
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'multi_links': ({'pred': 'person', 'obj': {'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'})}},)}
Parameters
  • source_key (str) – Name of the key in the input JSON

  • fields (Field) – fields inside this DictField

  • alti_key (Optional[str]) – Optional key name to be used in the graph. By default this is set to the source key converted to snake case.

  • optional (bool) – Whether this key is optional. Defaults to False.

parse(data, context)

Parse this field and return a LinkCollection.

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) –

    context dict containing data from higher level parsing code.

    Returns:

    LinkCollection

    Raises:

    DictFieldSourceKeyNotFoundException if self.source_key is not in data. DictFieldValueNotADictException if the data does not appear to represent a dict.

Return type

LinkCollection

class EmbeddedDictField(*fields)

Bases: altimeter.core.graph.field.base.SubField

An EmbeddedDictField is a field where the input is a JSON object. Generally this field is used inside a ListField.

Parameters

fields (Field) – fields inside this DictField

Examples

A ListField containing an EmbeddedDictField with two ScalarFields:
>>> from altimeter.core.graph.field.list_field import ListField
>>> from altimeter.core.graph.field.scalar_field import ScalarField
>>> input = {"People": [{"FirstName": "Bob", "LastName": "Smith"},                         {"FirstName": "Alice", "LastName": "Smith"}]}
>>> field = ListField("People", EmbeddedDictField(ScalarField("FirstName"),                                  ScalarField("LastName")))
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'multi_links': ({'pred': 'people', 'obj': {'simple_links': ({'pred': 'first_name', 'obj': 'Bob'}, {'pred': 'last_name', 'obj': 'Smith'})}}, {'pred': 'people', 'obj': {'simple_links': ({'pred': 'first_name', 'obj': 'Alice'}, {'pred': 'last_name', 'obj': 'Smith'})}})}
parse(data, context)

Parse this field and return a LinkCollection.

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) – context dict containing data from higher level parsing code.

Return type

LinkCollection

Returns

LinkCollection

Raises
  • DictFieldSourceKeyNotFoundException if self.source_key is not in data.

  • DictFieldValueNotADictException if the data does not appear to represent a dict.

altimeter.core.graph.field.exceptions module

Field parsing related exceptions.

exception DictFieldSourceKeyNotFoundException

Bases: altimeter.core.exceptions.AltimeterException

The source_key of a DictField was not found

exception DictFieldValueIsNullException

Bases: altimeter.core.exceptions.AltimeterException

The value of a non-nullable DictField was null

exception DictFieldValueNotADictException

Bases: altimeter.core.exceptions.AltimeterException

A DictField does not contain a dict.

exception InvalidParentKeyException

Bases: altimeter.core.exceptions.AltimeterException

A ParentKey is invalid.

exception ParentKeyMissingException

Bases: altimeter.core.exceptions.AltimeterException

A required ParentKey is missing.

exception ResourceLinkFieldSourceKeyNotFoundException

Bases: altimeter.core.exceptions.AltimeterException

The source_key of a ResourceLinkField is not found

exception ResourceLinkFieldValueNotAStringException

Bases: altimeter.core.exceptions.AltimeterException

The value of a ResourceLinkField is not a string.

exception ScalarFieldSourceKeyNotFoundException

Bases: altimeter.core.exceptions.AltimeterException

The source_key of a ScalarField is not found

exception ScalarFieldValueNotAScalarException

Bases: altimeter.core.exceptions.AltimeterException

The value of a ScalarField is not a string, bool, int or float.

exception TagsFieldMissingTagsKeyException

Bases: altimeter.core.exceptions.AltimeterException

A TagsField data is missing key ‘Tags’

altimeter.core.graph.field.list_field module

List Fields represent fields which consist of list data.

class AnonymousListField(source_key, field, optional=False, allow_scalar=False)

Bases: altimeter.core.graph.field.base.Field

An AnonymousListField is a ListField where the source_key of the field is discarded and not used as a name in the resulting graph. See Examples below for more clarity.

Parameters
  • source_key (str) – Name of the key in the input JSON

  • field (Field) – Field object representing the type that is contained in this list.

  • optional (bool) – Whether this key is optional. Defaults to False.

  • allow_scalar (bool) – Whether this field can sometimes contain a scalar rather than a list - if this is set to True the scalar will be treated as a list of one. Defaults to False.

Examples

A DictField containing an AnonymousListField:
>>> from altimeter.core.graph.field.dict_field import DictField
>>> from altimeter.core.graph.field.scalar_field import EmbeddedScalarField
>>> input = {"Biota": {"Animals": ["cow", "pig", "human"],                                   "Plants": ["tree", "fern"]}}
>>> field = DictField("Biota", AnonymousListField("Animals", EmbeddedScalarField()))
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'multi_links': ({'pred': 'biota', 'obj': {'simple_links': ({'pred': 'biota', 'obj': 'cow'}, {'pred': 'biota', 'obj': 'pig'}, {'pred': 'biota', 'obj': 'human'})}},)}
parse(data, context)

Parse this field and return a list of Links.

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) –

    context dict containing data from higher level parsing code.

    Returns:

    LinkCollection

    Raises:

    ListFieldSourceKeyNotFoundException if self.source_key is not in data. ListFieldValueNotAListException if the data does not appear to represent a list.

Return type

LinkCollection

class ListField(source_key, sub_field, alti_key=None, optional=False, allow_scalar=False)

Bases: altimeter.core.graph.field.base.Field

A ListField is a field where the input is a JSON object containing a key (source_key) where the corresponding value is a list of homogeneous items.

Examples

A list of strings:
>>> from altimeter.core.graph.field.scalar_field import EmbeddedScalarField
>>> input = {"Animals": ["cow", "pig", "human"]}
>>> field = ListField("Animals", EmbeddedScalarField())
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'simple_links': ({'pred': 'animals', 'obj': 'cow'}, {'pred': 'animals', 'obj': 'pig'}, {'pred': 'animals', 'obj': 'human'})}
A list of dicts:
>>> from altimeter.core.graph.field.dict_field import EmbeddedDictField
>>> from altimeter.core.graph.field.scalar_field import ScalarField
>>> input = {"People": [{"Name": "Bob", "Age": 49}, {"Name": "Sue", "Age": 42}]}
>>> field = ListField("People", EmbeddedDictField(ScalarField("Name"),                                  ScalarField("Age")))
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'multi_links': ({'pred': 'people', 'obj': {'simple_links': ({'pred': 'name', 'obj': 'Bob'}, {'pred': 'age', 'obj': 49})}}, {'pred': 'people', 'obj': {'simple_links': ({'pred': 'name', 'obj': 'Sue'}, {'pred': 'age', 'obj': 42})}})}
Parameters
  • source_key (str) – Name of the key in the input JSON

  • sub_field (SubField) – SubField object representing the type that is contained in this list.

  • alti_key (Optional[str]) – Optional key name to be used in the graph. By default this is set to the source key converted to snake case.

  • optional (bool) – Whether this key is optional. Defaults to False.

  • allow_scalar (bool) – Whether this field can sometimes contain a scalar rather than a list - if this is set to True the scalar will be treated as a list of one. Defaults to False.

parse(data, context)

Parse this field and return a LinkCollection

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) –

    context dict containing data from higher level parsing code.

    Returns:

    LinkCollection

    Raises:

    ListFieldSourceKeyNotFoundException if self.source_key is not in data. ListFieldValueNotAListException if the data does not appear to represent a list.

Return type

LinkCollection

altimeter.core.graph.field.scalar_field module

Scalar Fields represent field that contain strings, bools or numbers.

class EmbeddedScalarField

Bases: altimeter.core.graph.field.base.SubField

An EmbeddedScalarField is a field where the input is a string, number or bool. An EmbeddedScalarField assumes the key of the enclosing field.

Examples

An EmbeddedScalarField inside a ListField::
>>> from altimeter.core.graph.field.list_field import ListField
>>> input = {"Animal": ["Value1", "Value2"]}
>>> field = ListField("Animal", EmbeddedScalarField())
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'simple_links': ({'pred': 'animal', 'obj': 'Value1'}, {'pred': 'animal', 'obj': 'Value2'})}
parse(data, context)

Parse this field and return a LinkCollection.

Parameters
  • data (Union[str, bool, int, float]) – scalar value

  • context (Dict[str, Any]) – context dict containing data from higher level parsing code.

Return type

LinkCollection

Returns

LinkCollection

class ScalarField(source_key, alti_key=None, optional=False, default_value=None)

Bases: altimeter.core.graph.field.base.Field

A ScalarField is a field where the input is a JSON object and the corresponding value is a string, number or bool.

Examples

A ScalarField with a string value::
>>> input = {"FieldName": "Value"}
>>> field = ScalarField("FieldName")
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'simple_links': ({'pred': 'field_name', 'obj': 'Value'},)}
A ScalarField with a string value and an alti_key specified::
>>> input = {"FieldName": "Value"}
>>> field = ScalarField("FieldName", alti_key="custom_name")
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'simple_links': ({'pred': 'custom_name', 'obj': 'Value'},)}
An optional ScalarField with no default value::
>>> input = {"SomeOtherFieldName": "Value"}
>>> field = ScalarField("FieldName", optional=True)
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{}
An optional ScalarField with a default value::
>>> input = {"SomeOtherFieldName": "Value"}
>>> field = ScalarField("FieldName", optional=True, default_value="DefaultValue")
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'simple_links': ({'pred': 'field_name', 'obj': 'DefaultValue'},)}
Parameters
  • source_key (str) – Name of the key in the input JSON

  • alti_key (Optional[str]) – Optional key name to be used in the graph. By default this is set to the source key converted to snake case.

  • optional (bool) – Whether this key is optional. Defaults to False.

  • default_value (Union[str, bool, int, float, None]) – A default value to use if the source_key is not present.

parse(data, context)

Parse this field and return a LinkCollection

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) –

    context dict containing data from higher level parsing code.

    Returns:

    LinkCollection

Return type

LinkCollection

altimeter.core.graph.field.tags_field module

Tags Fields represent AWS Tags.

class TagsField(optional=True)

Bases: altimeter.core.graph.field.base.Field

A TagsField is a field used to parse data in the form

{‘Tags’: [{‘Key’: ‘tag_key’, ‘Value’: ‘tag_value’}, …]}

Examples

>>> input = {"Tags": [{"Key": "Name", "Value": "Jerry"},                               {"Key": "DOB", "Value": "1942-08-01"}]}
>>> field = TagsField()
>>> link_collection = field.parse(data=input, context={})
>>> print(link_collection.dict(exclude_unset=True))
{'tag_links': ({'pred': 'Name', 'obj': 'Jerry'}, {'pred': 'DOB', 'obj': '1942-08-01'})}
Parameters

optional (bool) – Whether this key is optional. Defaults to False.

parse(data, context)

Parse this field and return a list of Links.

Parameters
  • data (Dict[str, Any]) – dictionary of data to parse

  • context (Dict[str, Any]) –

    context dict containing data from higher level parsing code.

    Returns:

    List of TagLink objects, one for each tag.

Return type

LinkCollection

altimeter.core.graph.field.util module

Grab-bag functions for Field parsing

camel_case_to_snake_case(name)

Convert a string from CamelCase into snake_case. :type name: str :param name: string to convert

Return type

str

Returns

snake cased string

Module contents