graviti.portex.template#

Template base class.

Module Contents#

Classes#

PortexExternalType

The base class of Portex external type.

Functions#

template(name, content, package = packages.locals)

Generate a Portex external type according to the input template.

Attributes#

graviti.portex.template.EXTERNAL_TYPE_TO_CONTAINER[source]#
class graviti.portex.template.PortexExternalType(*args, **kwargs)[source]#

Bases: graviti.portex.base.PortexType

The base class of Portex external type.

Parameters
  • args (Any) –

  • kwargs (Any) –

property internal_type(self)[source]#

Get the internal type of the PortexExternalType.

Returns

The internal type of the PortexExternalType.

Return type

graviti.portex.base.PortexType

to_pyarrow(self)[source]#

Convert the Portex type to the corresponding builtin PyArrow DataType.

Returns

The corresponding builtin PyArrow DataType.

Return type

pyarrow.DataType

to_builtin(self)[source]#

Expand the top level of the Portex external type to Portex builtin type.

Returns

The expanded Portex builtin type.

Return type

graviti.portex.builtin.PortexBuiltinType

get_keys(self, type_name=None)[source]#

Get the keys to locate all data, or only get keys of one type if type_name is given.

Parameters

type_name (Optional[str]) – The name of the target PortexType.

Returns

A list of keys to locate the data.

Return type

List[Tuple[str, Ellipsis]]

graviti.portex.template.template(name, content, package=packages.locals)[source]#

Generate a Portex external type according to the input template.

Parameters
  • name (str) – The class name of the Portex external type.

  • content (Dict[str, Any]) – A dict indicates a Portex template.

  • package (graviti.portex.package.Package[Any]) – The package this template belongs to.

Returns

A subclass of PortexExternalType.

Return type

Type[PortexExternalType]

Examples

>>> import graviti.portex as pt
>>> from graviti.portex.template import template
>>>
>>> vector_template = {
...     "type": "template",
...     "parameters": [
...         {
...             "name": "coords",
...             "default": {"type": "int32"},
...         },
...         {
...             "name": "labels",
...             "default": None,
...         },
...     ],
...     "declaration": {
...         "type": "record",
...         "fields": [
...             {
...                 "name": "x",
...                 "+": "$coords",
...             },
...             {
...                 "name": "y",
...                 "+": "$coords",
...             },
...             {
...                 "name": "label",
...                 "exist_if": "$labels",
...                 "type": "enum",
...                 "values": "$labels",
...             },
...         ],
...     },
... }
>>> Vector = template("Vector", vector_template)
>>> vector2d_int = Vector()
>>> vector2d_int
Vector()
>>> vector2d_int.internal_type
record(
  fields={
    'x': int32(),
    'y': int32(),
  },
)
>>>
>>> vector2d_float = Vector(pt.float32())
>>> vector2d_float
Vector(
  coords=float32(),
)
>>> vector2d_float.internal_type
record(
  fields={
    'x': float32(),
    'y': float32(),
  },
)
>>>
>>> labeled_vector = Vector(labels=["visble", "occluded"])
>>> labeled_vector
Vector(
  labels=['visble', 'occluded'],
)
>>> labeled_vector.internal_type
record(
  fields={
    'x': int32(),
    'y': int32(),
    'label': enum(
      values=['visble', 'occluded'],
    ),
  },
)