next up previous index
Next: Using Interaction Sets in Up: Opus Variables Previous: Using Arguments in Variable   Index


Expression Names and Aliasing

The expression library includes a name field for each expression. This is used to bind the name to the corresponding expression. When using such expressions intermixed with Python code, another mechanism is needed to give a name to an expression - this is done with something much like an assignment statement, for example:

lnpop = ln(urbansim.gridcell.population)

This is treated as an expression, which can occur in the list of expressions given to compute_variables or in an aliases.py file (see below). The value of the new alias is returned as the value of the expression if it's the last item on the list of variables and expressions.

It is convenient, and often more efficient (Section 13.11), to gather all the expressions and aliases for a particular package and dataset into one place. When using XML-based configurations, the expression library component of the XML configurations supports this nicely. In older parts of the code using dictionary-based configurations, the optional aliases.py file supports this functionality instead. This file should define a single variable aliases to be a list of expressions, each of which should define an alias. This file is then placed in the same directory as variables for that package and dataset. For example, to define aliases relevant to urbansim.gridcell, put an aliases.py file into the urbansim.gridcell directory. These aliases can then be referred to using the fully-qualified name of the alias. When finding a variable referenced by a fully-qualified name, the system first searches the aliases file (if present), and then the variable definitions in the appropriate directory.

As an example, the directory opus_core.test_agent contains one variable definition, for the variable income_times_2. (This directory and variable are used for unit tests for opus_core.) The file aliases.py in that same directory contains the following:

aliases = [
    'income_times_5 = 5*opus_core.test_agent.income',
    'income_times_10 = 5*opus_core.test_agent.income_times_2'
    ]

The first alias refers to a primary attribute (test_agent.income), and the second to the variable. These aliases can then be referred to using a fully-qualified name, in exactly the same way as a variable, for example

opus_core.test_agent.income_times_5.

See the unit tests in opus_core.variables.expression_tests.aliases_file for examples of using these aliases.


next up previous index
Next: Using Interaction Sets in Up: Opus Variables Previous: Using Arguments in Variable   Index
info (at) urbansim.org