예제 #1
0
def get_observation(composite: Dict,
                    period: str,
                    variable: Variable,
                    treat_missing_as_null=False) -> Optional[Any]:
    """Get the value of a temporal variable for a particular observation period."""
    path: List = [period] + list(variable.absolute_path)
    return nesteddicts.get(composite, path, accept_none=treat_missing_as_null)
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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)
예제 #5
0
def _do_get_test(data: Dict,
                 spec: List[str],
                 default: Any = None,
                 expected: Optional[str] = "expected",
                 accept_none: bool = False) -> None:
    actual: Any = nesteddicts.get(data,
                                  spec,
                                  default=default,
                                  accept_none=accept_none)
    assert actual == expected
예제 #6
0
def get_value(composite: Dict, invariant_variable: Variable) -> Optional[Any]:
    path = ["invariant"] + list(invariant_variable.absolute_path)
    value = nesteddicts.get(composite, path)
    return value
예제 #7
0
def test_get_not_nested(default: Optional[str]):
    spec: List[str] = ["a", "b", "c"]
    data: Dict = {"a": {"b": "I'm supposed to be nested, but I'm not"}}
    with raises(nesteddicts.IncompleteNestingError):
        nesteddicts.get(data, spec, default=default)
예제 #8
0
def test_empty_spec_returns_self():
    data: Dict = {"a": {"b": {"c": "expected"}}}
    spec: List[str] = []
    expected: Dict = data
    actual: Any = nesteddicts.get(data, spec)
    assert actual == expected
예제 #9
0
def get_all_observations(composite: Dict,
                         variable: Variable) -> Iterator[Tuple[str, Any]]:
    """Iterate over all observations of a temporal variable from this composite."""
    var_path: List = list(variable.absolute_path)
    for period in get_periods(composite):
        yield period, nesteddicts.get(composite, [period] + var_path)
예제 #10
0
def get_property(composite: Dict, prop: Variable) -> Optional[Any]:
    """Get an invariant "variable" (property) from this composite."""
    path = ["invariant"] + list(prop.absolute_path)
    return nesteddicts.get(composite, path)