Context¶
The galactic.context package defines generic classes for using contexts:
Context: a context is composed by a population and a modelPopulationa population is a container for individualsModela model is a container for attributesIndividualan individual has an identifier and valuesAttributean attribute has a name and a type
-
class
galactic.context.P¶ Generic subclass of the
Populationclass
-
class
galactic.context.X¶ Generic subclass of the
Individualclass
-
class
galactic.context.Context[source]¶ A
Contexthandles a model and a population.It’s possible to access to the context
populationattribute or to the contextmodelattribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> print(context.population) ['0', '1'] >>> print(context.model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to check if a context is not empty (both
modelandpopulationare not empty) using the python builtinbool()function.It’s possible to get a readable representation of a context using the python builtin
str()function.Example
>>> print(context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
Example
>>> bool(context) True
Contexts are container for individuals and attributes. It’s possible to know if an individual or an attribute belongs to a context using the python
inkeyword.Example
>>> context.model['mybool'] in context True >>> context.population['0'] in context True
New in version 0.0.1.
-
class
galactic.context.Population[source]¶ A
Populationis a container for individuals.It’s possible to access to the population
contextattribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> population = context.population >>> print(population.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to get a readable representation of a population.
Example
>>> print(population) ['0', '1']
It’s possible to check if a population is not empty using the python builtin
bool()function.Example
>>> bool(population) True
It’s possible to access to an individual with its identifier using the python array access construct.
Example
>>> print(population['0']) {'mybool': False, 'myint': 0}
It’s possible to check if an individual belongs to a population using the python
inkeyword.Example
>>> '0' in population True
It’s possible to iterate over a population using the python
forkeyword.Example
>>> {ident: str(individual) for ident, individual in population.items()} {'0': "{'mybool': False, 'myint': 0}", '1': "{'mybool': False, 'myint': 0}"}
It’s possible to get the length of a population using the python builtin
len()function.Example
>>> len(population) 2
New in version 0.0.1.
-
class
galactic.context.Model[source]¶ A
Modelis a container for attributes.It’s possible to access to the model
contextattribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> model = context.model >>> print(model.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to get a readable representation of a model using the python builtin
str()function.Example
>>> print(model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to check if a model is not empty using the python builtin
bool()function.Example
>>> bool(model) True
It’s possible to access to an attribute with its name using the python array access construct.
Example
>>> print(model['mybool']) {'name': 'mybool', 'type': <class 'bool'>}
It’s possible to check if an attribute belongs to a model using the python
inkeyword.Example
>>> 'mybool' in model True
It’s possible to iterate over a model using the python
forkeyword.Example
>>> {name: attribute.type for name, attribute in model.items()} {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to get the length of a population using the python builtin
len()function.Example
>>> len(model) 2
New in version 0.0.1.
-
class
galactic.context.Individual[source]¶ A
Individualis a container for values.It’s possible to access to the individual
identifierattribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> individual = context.population['0'] >>> individual.identifier '0'
It’s possible to access to the individual
contextattribute.Example
>>> print(individual.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to access to the individual
modelattribute.Example
>>> print(individual.model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to access to the individual
populationattribute.Example
>>> print(individual.population) ['0', '1']
It’s possible to get a readable representation of an individual using the python builtin
str()function.Example
>>> print(individual) {'mybool': False, 'myint': 0}
It’s possible to access to the individual values using the
value()method.Example
>>> attribute = individual.model['mybool'] >>> individual.value(attribute) False
It’s possible to access to the individual values using the python array access construct.
Example
>>> individual['mybool'] False
It’s possible to get the length of an individual using the python builtin
len()function.Example
>>> len(individual) 2
It’s possible to iterate over an individual using the python
forkeyword.Example
>>> {name: value for name, value in individual.items()} {'mybool': False, 'myint': 0}
New in version 0.0.1.
-
identifier¶ Get this individual identifier.
Returns: the individual identifier Return type: strNew in version 0.0.1.
-
population¶ Get the underlying population.
Returns: the underlying population Return type: PNew in version 0.0.1.
-
context¶ Get the underlying context.
Returns: the underlying context Return type: CNew in version 0.0.1.
-
value(attribute: A)[source]¶ Get the attribute value for this individual.
Parameters: attribute ( A) – the attributeReturns: the value Return type: objectRaises: ValueError– if the attribute does not belong to the underlying model.New in version 0.0.1.
-
-
class
galactic.context.Attribute[source]¶ A
Attributeis described by anameand atype.It’s possible to access to the attribute
identifierandtypeattributes.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> attribute = context.model['mybool'] >>> attribute.name 'mybool' >>> attribute.type <class 'bool'>
It’s possible to access to the individual
contextattribute.Example
>>> print(attribute.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to access to the individual
modelattribute.Example
>>> print(attribute.model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to access to the individual
populationattribute.Example
>>> print(attribute.population) ['0', '1']
It’s possible to get a readable representation of an attribute using the python builtin
str()function.Example
>>> print(attribute) {'name': 'mybool': 'type': <class 'bool'>}
It’s possible to access to the attribute values using the
value()method.Example
>>> individual = attribute.population['0'] >>> attribute.value(individual) False
It’s possible to access to the attribute values using the python array access construct.
Example
>>> attribute['0'] False
It’s possible to get the length of an attribute using the python builtin
len()function.Example
>>> len(attribute) 2
It’s possible to iterate over an attribute using the python
forkeyword.Example
>>> {identifier: value for identifier, value in attribute.items()} {'0': False, '1': False}
New in version 0.0.1.
-
context¶ Get the underlying context.
Returns: the underlying context Return type: CNew in version 0.0.1.
-
population¶ Get the underlying population.
Returns: the underlying population Return type: PNew in version 0.0.1.
-
value(individual: X)[source]¶ Get the individual value for this attribute.
Parameters: individual ( X) – the individualReturns: the value Return type: objectRaises: ValueError– if the individual does not belong to the underlying population.New in version 0.0.1.
-