Characteristics

The galactic.characteristics module defines essential classes for representing characteristics:

  • Characteristic for the superclass of all characteristics

  • Valued for characteristics defined by a data

  • Named for named characteristics

  • Key for characteristics whose calls to an individual will be retrieved by a key access

  • Property for characteristics whose calls to an individual will be retrieved by a property access

  • Transformation for characteristics that hold a transform operation

  • Composite for characteristics which contain a collection of other Characteristic instances

  • Conversion for characteristics that execute a transform operation

  • Sum for characteristics that execute a sum operation

  • Comparison for characteristics that need a strict boolean flag used in comparison

  • Set for characteristics that contains a set of values

class galactic.characteristics.Characteristic(*args, **kwargs)

Bases: typing.Callable

The Characteristic class is the super class of all characteristics. It is designed for subclassing.

A characteristic can be called on an individual. It returns a data of this individual.

__call__(individual=None)

Make this characteristic callable. This method should be overridden in sub-classes.

Keyword Arguments

individual – Any python object

Returns

The individual characteristic

Return type

object

galactic.characteristics.identity = <galactic.characteristics.Characteristic object>

The identity characteristic.

class galactic.characteristics.Valued(*args, data=None, **kwargs)

Bases: galactic.characteristics.Characteristic

The Valued class is used to hold a data.

data

The data hold by the instance.

Example

>>> from galactic.characteristics import Valued
>>> a = Valued(data=5)
>>> print(a)
5
>>> a({})
5
>>>
__init__(*args, data=None, **kwargs)

Initialise a Valued instance.

Parameters
  • *args – Unused but useful for multi-class inheritance.

  • **kwargs – Unused but useful for multi-class inheritance.

Keyword Arguments

data – Any python object

__call__(individual=None)

Make this characteristic callable.

Keyword Arguments

individual – Any python object

Returns

The data hold by this individual.

Return type

object

class galactic.characteristics.Named(*args, name=None, **kwargs)

Bases: galactic.characteristics.Characteristic

The Named class is used to represent named characteristics. Is is designed for subclassing.

name

The name hold by the instance. It is used to name Characteristic instances.

Type

str

__init__(*args, name=None, **kwargs)

Initialise a Named instance.

Parameters
  • *args – Unused but useful for multi-class inheritance.

  • **kwargs – Unused but useful for multi-class inheritance.

Keyword Arguments

name – A name (usually a string)

class galactic.characteristics.Composite(*args, **kwargs)

Bases: galactic.characteristics.Characteristic

The Composite class is used as a container of embedded Characteristic instances. It is designed for subclassing.

characteristics

The tuple of Characteristic instances hold by this instance.

Type

tuple

__init__(*args, **kwargs)

Initialise a Composite instance.

Parameters
  • *args (tuple) – A collection of Characteristic instances

  • **kwargs – Unused but useful for multi-class inheritance.

Raises

TypeError – If a characteristic is not an instance of Characteristic

class galactic.characteristics.Key(characteristic: galactic.characteristics.Characteristic = <galactic.characteristics.Characteristic object>, **kwargs)

Bases: galactic.characteristics.Named, galactic.characteristics.Composite

The Key class is used to represent key characteristics.

name

The name hold by the instance.

Type

str

characteristics

The tuple of Characteristic instances hold by this instance.

Type

tuple

Example

>>> from galactic.characteristics import Key
>>> a = Key(name="x")
>>> print(a)
x
>>> a({"x": 5})
5
>>> a({})
>>> a=Key(name="y", characteristic=Key(name="x"))
>>> a({"x": {"y": 5}})
5
__init__(characteristic: galactic.characteristics.Characteristic = <galactic.characteristics.Characteristic object>, **kwargs)

Initialize a Key instance.

Keyword Arguments

characteristic (Characteristic) – The underlying characteristic.

__call__(individual=None)

Make this characteristic callable.

Keyword Arguments

individual – Any python object

Returns

The individual key using an array access or None if the individual does not have this key.

Return type

object

class galactic.characteristics.Property(characteristic: galactic.characteristics.Characteristic = <galactic.characteristics.Characteristic object>, **kwargs)

Bases: galactic.characteristics.Named, galactic.characteristics.Composite

The Property class is used to represent property characteristics.

name

The name hold by the instance.

Type

str

characteristics

The tuple of Characteristic instances hold by this instance.

Type

tuple

__init__(characteristic: galactic.characteristics.Characteristic = <galactic.characteristics.Characteristic object>, **kwargs)

Initialize a Property instance.

Keyword Arguments

characteristic (Characteristic) – The underlying characteristic.

__call__(individual=None)

Make this characteristic callable.

Keyword Arguments

individual – Any python object

Returns

The individual property using a property access or None if the individual does not have this property.

Return type

object

class galactic.characteristics.Transformation(*args, transform=<function Transformation.<lambda>>, **kwargs)

Bases: galactic.characteristics.Characteristic

The Transformation class is used to hold a transform operation. It is designed for subclassing.

transform

The transform operation hold by the instance. It is used to apply transformation on values.

Type

Callable

__init__(*args, transform=<function Transformation.<lambda>>, **kwargs)

Initialise a Transformation instance.

Parameters
  • *args – Unused but useful for multi-class inheritance.

  • **kwargs – Unused but useful for multi-class inheritance.

Keyword Arguments

transform – A transform operation.

class galactic.characteristics.Conversion(characteristic: galactic.characteristics.Characteristic, **kwargs)

Bases: galactic.characteristics.Composite, galactic.characteristics.Transformation, galactic.characteristics.Valued

The Conversion class is used to convert a characteristic call to a specific type.

characteristics

The characteristics (tuple of Characteristic instances) hold by this instance.

Type

tuple

transform

The transform operation hold by the instance.

Type

Callable

data

The data hold by the instance.

Example

>>> from galactic.characteristics import Conversion, Key
>>> a = Conversion(Key(name="x"), transform=float, data="nan")
>>> print(a)
x
>>> a({"x": "5"})
5.0
>>> a(4)
nan
__init__(characteristic: galactic.characteristics.Characteristic, **kwargs)

Initialise a Conversion instance.

Parameters

characteristic (Characteristic) – The embedded characteristics

Keyword Arguments
__call__(individual=None)

Make this characteristic callable.

Keyword Arguments

individual – Any python object

Returns

the individual data converted by the transform operation or the default transformed data if a TypeError or a ValueError is raised.

Return type

object

class galactic.characteristics.Sum(*args, start=0, **kwargs)

Bases: galactic.characteristics.Composite, galactic.characteristics.Transformation, galactic.characteristics.Valued

The Sum class is used to define a sum over other Characteristic instances.

start

Initial value for the sum. It is used as a beginning for the sum calculus.

characteristics

The characteristics (tuple of Characteristic instances) hold by this instance.

Type

tuple

transform

The transform operation hold by the instance.

Type

Callable

data

The data hold by the instance.

Example

>>> from galactic.characteristics import Key, Sum
>>> s = Sum(Key(name="x"), Key(name="y"), start=[], data=None)
>>> print(s)
(x+y)
>>> s({})
>>> s({"x": [1]})
>>> s({"x": [1], "y": [2]})
[1, 2]
>>>

Example

>>> from galactic.characteristics import Key, Sum
>>> s = Sum(Key(name="x"), Key(name="y"), start=0, data="nan", transform=float)
>>> print(s)
(x+y)
>>> s({})
nan
>>> s({"x": 1})
nan
>>> s({"x": 1, "y": 2})
3.0
>>>
__init__(*args, start=0, **kwargs)

Initialise a Sum instance.

Parameters
Keyword Arguments
__call__(individual=None)

Make this characteristic callable.

Parameters

individual – Any python object

Returns

the sum of all Characteristic instance calls on the individual or the default transformed data if a TypeError or a ValueError is raised.

Return type

object

class galactic.characteristics.Comparison(*args, strict=False, **kwargs)

Bases: galactic.characteristics.Characteristic

The Comparison class can be inherited to hold strict property. It is designed for subclassing.

strict

The strict property hold by this instance. It is a boolean flag used for strict (or not) Comparison.

Type

bool

__init__(*args, strict=False, **kwargs)

Initialise a Comparison instance.

Parameters
  • *args – Unused but useful for multi-class inheritance.

  • **kwargs – Unused but useful for multi-class inheritance.

Keyword Arguments

strict – Is the comparison strict?

class galactic.characteristics.Set(*args, values=None, **kwargs)

Bases: galactic.characteristics.Characteristic

A Set characteristics can be inherited to hold a set of values property. It is designed for subclassing.

values

The values hold by this instance. It is used to hold a collection of values.

Type

Container

__init__(*args, values=None, **kwargs)

Initialise a Set instance.

Parameters
  • *args – Unused but useful for multi-class inheritance.

  • **kwargs – Unused but useful for multi-class inheritance.

Keyword Arguments

values (Container, Hashable) – A container to be compared with

Raises
galactic.characteristics.get_classes()

Get all classes defined in this module.

Returns

dict – A mapping from names to classes.

Return type

dict