Enum & EnumArray

class openfisca_core.indexed_enums.Enum(name)[source]

Enum based on enum34, whose items have an index.

classmethod encode(array)[source]

Encode a string numpy array, an enum item numpy array, or an int numpy array into an EnumArray. See EnumArray.decode for decoding.

Parameters

array (numpy.ndarray) – Array of string identifiers, or of enum items, to encode.

Returns

An EnumArray encoding the input array values.

Return type

EnumArray

For instance:

>>> string_identifier_array = asarray(['free_lodger', 'owner'])
>>> encoded_array = HousingOccupancyStatus.encode(string_identifier_array)
>>> encoded_array[0]
2  # Encoded value
>>> free_lodger = HousingOccupancyStatus.free_lodger
>>> owner = HousingOccupancyStatus.owner
>>> enum_item_array = asarray([free_lodger, owner])
>>> encoded_array = HousingOccupancyStatus.encode(enum_item_array)
>>> encoded_array[0]
2  # Encoded value
class openfisca_core.indexed_enums.EnumArray(input_array: numpy.int_, possible_values: Optional[Type[Enum]] = None)[source]

NumPy array subclass representing an array of enum items.

EnumArrays are encoded as int arrays to improve performance

decode()[source]

Return the array of enum items corresponding to self.

For instance:

>>> enum_array = household('housing_occupancy_status', period)
>>> enum_array[0]
>>> 2  # Encoded value
>>> enum_array.decode()[0]
<HousingOccupancyStatus.free_lodger: 'Free lodger'>

Decoded value: enum item

Return type

object_

decode_to_str()[source]

Return the array of string identifiers corresponding to self.

For instance:

>>> enum_array = household('housing_occupancy_status', period)
>>> enum_array[0]
>>> 2  # Encoded value
>>> enum_array.decode_to_str()[0]
'free_lodger'  # String identifier
Return type

str_