def test_put_parent_missing(): target: Dict = {"a": {}} spec: List[str] = ["a", "b", "c"] value: str = "value" nesteddicts.put(target, spec, value) expected: Dict = {"a": {"b": {"c": "value"}}} assert expected == target
def __call__(self, composite: Dict): person_name_path = ["invariant"] + list(self.person_name_var.absolute_path) person_name: str = nesteddicts.get(composite, person_name_path) lc_name = person_name.lower() gender = self.lookups["genders"][lc_name] gender_path = ["invariant"] + list(self.gender_var.absolute_path) nesteddicts.put(composite, gender_path, gender)
def test_put_tree_exists(): target: Dict = {"a": {"b": {"c": "old value"}}} spec: List[str] = ["a", "b", "c"] value: str = "new value" nesteddicts.put(target, spec, value) expected: Dict = {"a": {"b": {"c": "new value"}}} assert expected == target
def __call__(self, composite: Dict): # IRL, you'd have to handle nulls, decide how to deal with temporal variables, etc. color_name_path = ["invariant"] + list(self.color_name_var.absolute_path) color_name = nesteddicts.get(composite, color_name_path) # At some point, I'll write a fancy retrieval/validation/assignment system, but that's not for the MVP rgb_value = self.lookups["color_names"][color_name] rgb_path = ["invariant"] + list(self.rgb_var.absolute_path) nesteddicts.put(composite, rgb_path, rgb_value)
def __call__(self, composite: Dict): template = "%s's favorite color is %s (%s). Over the observation period, %s gained %0.1f lbs." name = get_value(composite, self.person_name_var) color = get_value(composite, self.color_name_var) rgb = get_value(composite, self.rgb_var) weight_gain = get_value(composite, self.weight_gain_var) pronoun = self.get_pronoun(composite) sentence = template % (name, color, rgb, pronoun, weight_gain) print(self.sentence_var) sentence_path = ["invariant"] + list(self.sentence_var.absolute_path) nesteddicts.put(composite, sentence_path, sentence)
def __call__(self, composite: Dict): periods = set(composite.keys()) - {"invariant"} earliest = min(periods) latest = max(periods) weight_path = list(self.weight_var.absolute_path) earliest_weight_path: list = [earliest] + weight_path earliest_weight: float = nesteddicts.get(composite, earliest_weight_path) latest_weight_path: list = [latest] + weight_path latest_weight: float = nesteddicts.get(composite, latest_weight_path) # I know, should have called it "weight change." weight_gain = round(latest_weight - earliest_weight, 2) weight_gain_path = ["invariant"] + list(self.weight_gain_var.absolute_path) nesteddicts.put(composite, weight_gain_path, weight_gain)
def test_put_incomplete_nesting(): target: Dict = {"a": "b"} spec: List[str] = ["a", "b", "c"] value: str = "value" with raises(nesteddicts.IncompleteNestingError): nesteddicts.put(target, spec, value)
def put_observation(composite: Dict, period: str, variable: Variable, value: Optional[Any]) -> None: """Assign (or overwrite) the value of a variable into a particular observation.""" path: List = [period] + list(variable.absolute_path) nesteddicts.put(composite, path, value)
def put_property(composite: Dict, prop: Variable, value: Optional[Any]) -> None: """Assign (or overwrite) a value to an invariant "variable" (property) in this composite.""" path: List = ["invariant"] + list(prop.absolute_path) nesteddicts.put(composite, path, value)