Skip to content

Commit

Permalink
New create_material_variables=True flag
Browse files Browse the repository at this point in the history
  • Loading branch information
aminusfu committed Oct 16, 2024
1 parent c94645f commit 3a58aee
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions scrunch/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,17 @@ class DatasetVariablesMixin(Mapping):
Handles dataset variable iteration in a dict-like way
"""

create_material_variables = True
"""New variables created by methods of this class are usually defined by
an expression which is evaluated on the server. If create_material_variables
is True (the default), such an expression is evaluated eagerly, and its
output at that moment is stored as a "material" variable: part of the schema.
Changes to any of the input variables will not update the material variable.
If False, the expression itself is stored instead and re-evaluated on each
read; the resulting "derived" variable is an artifact rather than part of
the schema, and will change if any of its inputs change.
"""

def __getitem__(self, item):
"""
Returns a Variable() instance, `item` can be either a variable alias,
Expand Down Expand Up @@ -749,7 +760,9 @@ def _var_create_reload_return(self, payload):
helper function for POSTing to variables, reload
the catalog of variables and return newly created var
"""
new_var = self.resource.variables.create(payload)
new_var = self.resource.variables.create(
dict(payload, derived=not self.create_material_variables)
)
# needed to update the variables collection
self._reload_variables()
# return an instance of Variable
Expand Down Expand Up @@ -1226,7 +1239,7 @@ def create_single_response(self, categories, name, alias, description='',
payload = shoji_entity_wrapper(dict(
alias=alias,
name=name,
expr=expr,
derivation=expr,
description=description,
notes=notes))
return self._var_create_reload_return(payload)
Expand Down Expand Up @@ -1261,7 +1274,7 @@ def rollup(self, variable_alias, name, alias, resolution, description='',
payload = shoji_entity_wrapper(dict(
alias=alias,
name=name,
expr=expr,
derivation=expr,
description=description,
notes=notes))

Expand Down Expand Up @@ -3041,7 +3054,7 @@ def edit_categorical(self, categories, rules):
more_args = process_expr(more_args, self.dataset)
# epression value building
expr = dict(function='case', args=args + more_args)
payload = shoji_entity_wrapper(dict(expr=expr))
payload = shoji_entity_wrapper(dict(derivation=expr))
# patch the variable with the new payload
resp = self.resource.patch(payload)
self._reload_variables()
Expand Down

0 comments on commit 3a58aee

Please sign in to comment.