MemoryContext

The galactic.context.memory module give the ability to define Context that resides in memory.

class galactic.context.memory.MemoryContext(**kwargs)[source]

The MemoryContext class is designed to define contexts in memory. It inherits of all the behavior from the Context class and allows direct creation and modification of a context.

It’s possible to create a context without nothing.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext()
>>> print(context)
{'population': [], 'model': {}}

It’s possible to create a context specifying the model definition.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext(definition={'mybool': bool, 'myint': int})
>>> print(context)
{'population': [], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}

It’s possible to create a context specifying the model definition and the list of individual identifiers.

Example

>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals=['0', '1']
... )
>>> print(context)
{'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}

It’s possible to create a context specifying the model definition and the individual values.

Example

>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals={'0': {'mybool': True}, '1':{'myint': 1}}
... )
>>> {ident: str(context.population[ident]) for ident in context.population}
{'0': "{'mybool': True, 'myint': 0}", '1': "{'mybool': False, 'myint': 1}"}

New in version 0.0.1.

__init__(**kwargs)[source]

Initialise a context in memory.

Keyword Arguments:
 
  • definition (Mapping[str, type]) – definition of the context by a mapping from name of attributes to their type
  • individuals (Union[Iterable[str], Mapping[str, Mapping[str, object]]]) – initial iterable of individual identifiers or a mapping from individual identifiers to individual values
Raises:
  • KeyError – if an attribute is not in the definition
  • ValueError – if a value does not correspond to an attribute type
  • TypeError – if the definition or if the individuals parameter are not of the correct type

New in version 0.0.1.

class galactic.context.memory.MemoryModel(context: galactic.context.memory.MemoryContext, definition: typing.Mapping[str, typing.type])[source]

The MemoryModel class is designed to define models that resides in memory. It inherits of all the behavior from the Model class.

It’s possible to change or to set attribute values.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals=['0', '1']
... )
>>> model = context.model
>>> model['mybool'] = int
>>> model['myint2'] = int
>>> print(context.population['0'])
{'mybool': 0, 'myint': 0, 'myint2': 0}

It’s possible to delete an attribute using its name.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals=['0', '1']
... )
>>> model = context.model
>>> del model['mybool']
>>> {ident: str(context.population[ident]) for ident in context.population}
{'0': "{'myint': 0}", '1': "{'myint': 0}"}

New in version 0.0.1.

__init__(context: galactic.context.memory.MemoryContext, definition: typing.Mapping[str, typing.type])[source]

Initialise a model in memory.

Parameters:

New in version 0.0.1.

class galactic.context.memory.MemoryPopulation(context: galactic.context.memory.MemoryContext, identifiers: typing.Iterable[str])[source]

The MemoryPopulation class is designed to define populations that resides in memory. It inherits of all the behavior from the Population class.

It’s possible to change or to set individual values.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals=['0', '1']
... )
>>> population = context.population
>>> population['0'] = {'mybool': True}
>>> population['2'] = {'myint': 1}
>>> print(population['0'])
{'mybool': True, 'myint': 0}
>>> print(population['2'])
{'mybool': False, 'myint': 1}

It’s possible to delete an individual using its identifier.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals=['0', '1']
... )
>>> population = context.population
>>> del population['0']
>>> {ident: str(context.population[ident]) for ident in context.population}
{'1': "{'mybool': False, 'myint': 0}"}

New in version 0.0.1.

__init__(context: galactic.context.memory.MemoryContext, identifiers: typing.Iterable[str])[source]

Initialise a population in memory.

Parameters:

New in version 0.0.1.

class galactic.context.memory.MemoryIndividual(population: galactic.context.memory.MemoryPopulation, identifier: str)[source]

The MemoryIndividual is designed to define individuals that resides in memory. It inherits of all the behavior from the Individual class.

It’s possible to modify a value for an individual using an attribute name.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals=['0', '1']
... )
>>> individual = context.population['0']
>>> individual['mybool'] = True
>>> individual['myint'] = 1
>>> {ident: str(context.population[ident]) for ident in context.population}
{'0': "{'mybool': True, 'myint': 1}", '1': "{'mybool': False, 'myint': 0}"}

New in version 0.0.1.

__init__(population: galactic.context.memory.MemoryPopulation, identifier: str)[source]

Initialise an individual.

Parameters:
  • population (MemoryPopulation) – the population
  • identifier (str) – the individual identifier

New in version 0.0.1.

class galactic.context.memory.MemoryAttribute(model: galactic.context.memory.MemoryModel, name: str, cls: type)[source]

The MemoryAttribute is designed to define attributes that resides in memory. It inherits of all the behavior from the Attribute class.

It’s possible to modify a value for an attribute using an individual identifier.

Example

>>> from galactic.context.memory import MemoryContext
>>> context = MemoryContext(
...     definition={'mybool': bool, 'myint': int},
...     individuals=['0', '1']
... )
>>> attribute = context.model['myint']
>>> attribute['0'] = 3
>>> attribute['1'] = 4
>>> {ident: str(context.population[ident]) for ident in context.population}
{'0': "{'mybool': False, 'myint': 3}", '1': "{'mybool': False, 'myint': 4}"}

New in version 0.0.1.

__init__(model: galactic.context.memory.MemoryModel, name: str, cls: type)[source]

Initialise an attribute.

Parameters:
  • model (MemoryModel) – the underlying model
  • name (str) – the attribute name
  • cls (type) – the attribute type

New in version 0.0.1.