spinedb_api.parameter_value
Parameter values in a Spine DB can be of different types (see Parameter value format). For each of these types, this module provides a Python class to represent values of that type.
type |
Python class |
---|---|
|
|
|
|
|
|
|
|
|
|
|
The module also provides the functions to_database()
and from_database()
to translate between instances of the above classes and their DB representation (namely, the value and type fields
that would go in the parameter_value
table).
For example, to write a Python object into a parameter value in the DB:
# Create the Python object
parsed_value = TimeSeriesFixedResolution(
"2023-01-01T00:00", # start
"1D", # resolution
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0], # values
ignore_year=False,
repeat=False,
)
# Translate it to value and type
value, type_ = to_database(parsed_value)
# Add a parameter_value to the DB with that value and type
with DatabaseMapping(url) as db_map:
db_map.add_parameter_value_item(
entity_class_name="cat",
entity_byname=("Tom",),
parameter_definition_name="number_of_lives",
alternative_name="Base",
value=value,
type=type_,
)
db_map.commit_session("Tom is living one day at a time")
The value can be accessed as a Python object using the parsed_value
field:
# Get the parameter_value from the DB
with DatabaseMapping(url) as db_map:
pval_item = db_map.get_parameter_value_item(
entity_class_name="cat",
entity_byname=("Tom",),
parameter_definition_name="number_of_lives",
alternative_name="Base",
)
value = pval_item["parsed_value"]
In the rare case where a manual conversion from a DB value to Python object is needed,
use from_database()
:
# Obtain value and type
value, type_ = pval_item["value"], pval_item["type"]
# Translate value and type to a Python object manually
parsed_value = from_database(value, type_)
Module Contents
Classes
Base for all classes representing parameter values. |
|
A parameter value of type 'date_time'. A point in time. |
|
A parameter value of type 'duration'. An extension of time. |
|
Base for all classes representing indexed parameter values. |
|
A parameter value of type 'array'. A one dimensional array with zero based indexing. |
|
A parameter value of type 'time_pattern'. |
|
Base for all classes representing 'time_series' parameter values. |
|
A parameter value of type 'time_series'. |
|
A parameter value of type 'time_series'. |
|
A parameter value of type 'map'. A mapping from key to value, where the values can be other instances |
Functions
|
Converts a parameter value from the DB into a Python object. |
|
Converts a Python object representing a parameter value into their DB representation. |
- spinedb_api.parameter_value.from_database(value, type_=None)[source]
Converts a parameter value from the DB into a Python object.
- Parameters:
- Returns:
a Python object representing the parameter value.
- Return type:
ParameterValue
, float, str, bool or None
- spinedb_api.parameter_value.to_database(parsed_value)[source]
Converts a Python object representing a parameter value into their DB representation.
- class spinedb_api.parameter_value.ParameterValue[source]
Base for all classes representing parameter values.
- class spinedb_api.parameter_value.DateTime(value=None)[source]
Bases:
ParameterValue
A parameter value of type ‘date_time’. A point in time.
- class spinedb_api.parameter_value.Duration(value=None)[source]
Bases:
ParameterValue
A parameter value of type ‘duration’. An extension of time.
- Parameters:
value (str or
Duration
orrelativedelta
) – the duration value.
- class spinedb_api.parameter_value.IndexedValue(values, value_type=None, index_name='')[source]
Bases:
ParameterValue
Base for all classes representing indexed parameter values.
- get_nearest(index)[source]
Returns the value at the nearest index to the given one.
- Parameters:
index (any) – The index.
- Returns:
The value.
- Return type:
any
- class spinedb_api.parameter_value.Array(values, value_type=None, index_name='')[source]
Bases:
IndexedValue
A parameter value of type ‘array’. A one dimensional array with zero based indexing.
- class spinedb_api.parameter_value.TimePattern(indexes, values, index_name='')[source]
Bases:
IndexedValue
A parameter value of type ‘time_pattern’. A mapping from time patterns strings to numerical values.
- class spinedb_api.parameter_value.TimeSeries(values, ignore_year, repeat, index_name='')[source]
Bases:
IndexedValue
Base for all classes representing ‘time_series’ parameter values.
- class spinedb_api.parameter_value.TimeSeriesFixedResolution(start, resolution, values, ignore_year, repeat, index_name='')[source]
Bases:
TimeSeries
A parameter value of type ‘time_series’. A mapping from time stamps to numerical values, with fixed durations between the time stamps.
When getting the indexes the durations are applied cyclically.
Currently, there is no support for the ignore_year and repeat options other than having getters for their values.
- Parameters:
start (str or
datetime
ordatetime64
) – the first time stampresolution (str,
relativedelta
, list) – duration(s) between the time stamps.values (Sequence) – the values in the time-series.
ignore_year (bool) – True if the year should be ignored.
repeat (bool) – True if the series is repeating.
index_name (str) – index name.
- class spinedb_api.parameter_value.TimeSeriesVariableResolution(indexes, values, ignore_year, repeat, index_name='')[source]
Bases:
TimeSeries
A parameter value of type ‘time_series’. A mapping from time stamps to numerical values with arbitrary time steps.
- class spinedb_api.parameter_value.Map(indexes, values, index_type=None, index_name='')[source]
Bases:
IndexedValue
A parameter value of type ‘map’. A mapping from key to value, where the values can be other instances of
ParameterValue
.- Parameters: