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, or an enum item numpy array, into an EnumArray. See EnumArray.decode for decoding.

Parameters:array (numpy.ndarray) – Numpy 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 = numpy.asarray(['free_lodger', 'owner'])
>>> encoded_array = HousingOccupancyStatus.encode(string_identifier_array)
>>> encoded_array[0]
>>> 2  # Encoded value
>>> enum_item_array = numpy.asarray([HousingOccupancyStatus.free_lodger, HousingOccupancyStatus.owner])
>>> encoded_array = HousingOccupancyStatus.encode(enum_item_array)
>>> encoded_array[0]
>>> 2  # Encoded value
class openfisca_core.indexed_enums.EnumArray[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

>>> 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