oemof.solph.plumbing

Helpers to fit scalar values into sequences.

class oemof.solph._plumbing.Apply(converter, default=<object object>)[source]

Bases: object

Apply converter to values assigned to this attribute.

Whenever a value is assigned to an attribute that uses this descriptor, what’s actually stored is the result of converter(value), e.g.:

>>> class Inverter:
...     inverted = Apply(lambda x: -x)
...
>>> one = Inverter()
>>> one.inverted = 1
>>> one.inverted
-1
unset = <object object>
class oemof.solph._plumbing.SequenceDict(dict=None, /, **kwargs)[source]

Bases: UserDict

Convert each value to a sequence upon insertion.

A drop-in replacement class for dict`s, that calls `sequence on values before they are inserted and stores the result.

oemof.solph._plumbing.sequence(iterable_or_scalar, length=None)[source]

Casts iterable_or_scalar to an object that allows element access.

Technically, it checks if an object is iterable or scalar and returns a numpy array if the object is an iterable or the length is given, or a _FakeSequence if the length cannot be infered. This way, we can (possibly) define a sequence of unknown length if needed.

Parameters:

iterable_or_scalar (iterable or None or int or float)

Examples

>>> y = sequence([1,2,3,4,5,6,7,8,9,10,11])
>>> y[0]
np.int64(1)
>>> y[10]
np.int64(11)
>>> import pandas as pd
>>> s1 = sequence(pd.Series([1,5,9]))
>>> s1[2]
np.int64(9)
>>> x = sequence(10)
>>> x[0]
10
>>> x[10]
10
oemof.solph._plumbing.valid_sequence(sequence, length: int) → bool[source]

Checks if an object has the given length

This is needed as we have _FakeSequence which is assumed to have every possible length, thus __len__ is not defined for it.