clookup
The CLookup is a classic example of trying to solve a very complex data problem with a very complex data structure. It does not entirely succeed, but it works well enough as a stopgap until I have a chance to give an honest crack at a graph database.
Flow characterization is the very complex data problem: the "quantity relation" maps a large, diverse set of inputs: - flowable (substance) - reference quantity (in our data system, tied to flowable) - query quantity - context / compartment - location / locale to a numeric output, namely the amount of the query quantity that corresponds to a unit of the reference quantity. Technically, this amount also has uncertainty / other quantitative characteristics.
The idea behind a CLookup is that it contains all known characterizations for a given flowable [substance] with respect to a given quantity. The CLookup is selected by specifying the flowable and the quantity, and then the CLookup is used to retrieve a set of Characterization objects for a given context (hence the 'C' in 'CLookup'). The characterization already stores a mapping of locale to factor, and also stores the flowable (with its native reference quantity used to interpret the factor).
So it solves a very narrow portion of the problem and leaves a lot to outside code.
The dream would be to design a graph database that held all of these parameters and magically obtained all the factors that applied to a given query-- that graph database would replace the current Term Manager and everything else under its hood. But first we will learn to walk...
CLookup
Bases: object
A CLookup is a kind of fuzzy dictionary that maps context to best-available characterization. A given CLookup is associated with a single quantity and a specific flowable. The query then provides the compartment and returns either: a set of best-available characterizations; a single characterization according to a selection rule; or a characterization factor (float) depending on which method is used.
__getitem__(item)
Returns
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
|
required |
Returns:
Type | Description |
---|---|
|
find(item, dist=1, return_first=True, origin=None)
Hunt for a matching compartment. 'dist' param controls the depth of search: dist = 0: equivalent to getitem dist = 1: also check compartment's children (subcompartments), to any depth, returning all CFs encountered (unless return_first is True, in which case all CFs from the first nonempty compartment found are returned) dist = 2: also check compartment's parents (excluding root=Null context) dist = 3: also check all compartment's parents until root. Useful for finding unit conversions. By default (dist==1), checks compartment self and children. Returns a set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
a Compartment |
required | |
dist |
how far to search (with limits) (default: 1= compartment + children) |
1
|
|
return_first |
stop hunting as soon as a cf is found |
True
|
|
origin |
[None] if present, only return cfs whose origins match the specification |
None
|
Returns:
Type | Description |
---|---|
a list of characterization factors that meet the query criteria, ordered by increasing dist |
serialize_for_origin(origin, values=False)
Note: In the event that the CLookup includes multiple CFs for the same flowable and the same origin, only the first (at random) will be included, because originally I had disallowed multiple CFs for the same origin.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
origin |
|
required | |
values |
|
False
|
Returns:
Type | Description |
---|---|
|
SCLookup
Bases: CLookup
A Strict CLookup that permits only one CF to be stored per compartment and raises an error if an additional one is added.