Related to #387.
Two changes needed to support categorical dates (as far as I can tell):
Variable._MUTABLE_ATTRIBUTES
- Add
date to set of mutable attributes
class Variable(ReadOnly, DatasetSubvariablesMixin):
"""
A pycrunch.shoji.Entity wrapper that provides variable-specific methods.
DatasetSubvariablesMixin provides for subvariable interactions.
"""
_MUTABLE_ATTRIBUTES = {'name', 'description', 'uniform_basis',
'view', 'notes', 'format', 'derived', 'date'}
_IMMUTABLE_ATTRIBUTES = {'id', 'alias', 'type', 'discarded'}
Variable.add_category:
- Add
date=None kwarg
- If
date is not None, add date to new_category (which I've also separated out in the below example)
def add_category(self, id, name, numeric_value, missing=False, before_id=False, date=None):
if self.resource.body['type'] not in CATEGORICAL_TYPES:
raise TypeError(
"Variable of type %s do not have categories"
% self.resource.body.type)
if self.resource.body.get('derivation'):
raise TypeError("Cannot add categories on derived variables. Re-derive with the appropriate expression")
categories = self.resource.body['categories']
new_category = {
'id': id,
'missing': missing,
'name': name,
'numeric_value': numeric_value
}
if date is not None:
new_category['date'] = date
if before_id:
# only accept int type
assert isinstance(before_id, int)
# see if id exist
try:
self.categories[before_id]
except:
raise AttributeError('before_id not found: {}'.format(before_id))
new_categories = []
for category in categories:
if category['id'] == before_id:
new_categories.append(new_category)
new_categories.append(category)
categories = new_categories
else:
categories.append(new_category)
resp = self.resource.edit(categories=categories)
self._reload_variables()
return resp
Related to #387.
Two changes needed to support categorical dates (as far as I can tell):
Variable._MUTABLE_ATTRIBUTESdateto set of mutable attributesVariable.add_category:date=Nonekwargdate is not None, adddatetonew_category(which I've also separated out in the below example)