Variables#
The policyengine_core.variables module contains the definition of Variable, which can have a value for each entity instance in a simulation, for each period in a simulation, dependent on the parameters in that period, as well as other entity instances.
Variable#
- class policyengine_core.variables.variable.Variable(baseline_variable=None)#
Bases:
objectA variable of the legislation.
- check_defined_for_variable(defined_for_variable: Variable) None#
Reject a
defined_forvariable that cannot be compared with zero.calculatekeeps this variable’s formula result where thedefined_forvariable is greater than zero, and uses the default value elsewhere. That needs abool,intorfloatvariable: an Enum array allows only==and!=, andstrand date arrays cannot be compared with a number, so the comparison raised aTypeErrorin the middle of a calculation. For an Enum condition, define aboolvariable that compares it with a member and name that variable instead.
- check_uprating_value_type()#
Reject
upratingon a variable whose values cannot be multiplied.Uprating multiplies the latest earlier input by a ratio of index values. Enum arrays allow only
==and!=, andstrand date arrays cannot be multiplied by a float, soupratingon such a variable raised aTypeErrorthe first time a later period was uprated. The check covers anupratinginherited from a baseline variable too, since the inherited one is whatcalculateuses.It runs when the variable is defined and when
upratingis assigned. A class that declaresupratingitself (even asNone) replaces the property that checks assignments, socalculateruns it again before it uprates.
- clone()#
Return a copy that preserves the variable’s merged state.
Uses
empty_clone+ a__dict__copy (theTaxBenefitSystem.clonepattern) rather than re-running__init__, so a variable registered via a reform’supdate_variablekeeps the attributes it inherited from its baseline (value_type,entity,formulas,baseline_variable, …).formulasandmetadataare copied so mutating them on the clone does not affect the original; other containers (adds/subtracts) are shared, as they were before.
- get_explicit_computation_modes()#
Computation modes the class declares itself.
Excludes attributes inherited from a baseline variable when a reform updates it: a reform may, for example, redeclare a formula variable with
adds, and the inherited baseline formulas keep their runtime precedence rather than constituting a mixed-mode authoring error.
- get_formula(period=None)#
Returns the formula used to compute the variable at the given period.
If no period is given and the variable has several formula, return the oldest formula.
- Returns:
Formula used to compute the variable
- classmethod get_introspection_data(tax_benefit_system)#
Get instrospection data about the code of the variable.
- Returns:
(comments, source file path, source code, start line number)
- Return type:
tuple
- is_input_variable()#
Returns True if the variable is an input variable.
- parse_formula_name(attribute_name)#
Returns the starting date of a formula based on its name.
Valid dated name formats are : ‘formula’, ‘formula_YYYY’, ‘formula_YYYY_MM’ and ‘formula_YYYY_MM_DD’ where YYYY, MM and DD are a year, month and day.
- By convention, the starting date of:
formula is 0001-01-01 (minimal date in Python)
formula_YYYY is YYYY-01-01
formula_YYYY_MM is YYYY-MM-01