Skip to content

Commit

Permalink
Remove sorting on attrdict key nesting (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
brynpickering committed Feb 29, 2024
1 parent 41f9060 commit c8bafb0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ Both the index and the values of the timeseries (both being date strings) should

|changed| `inbuilt` math -> `pre-defined` math and `custom` math -> `pre-defined` math in the documentation.

|changed| Calliope attribute dictionaries (AttrDicts) no longer sort dictionary keys on `union`. Key order is now: original dictionary key order + any new keys being added in the order they appear in the new dictionary.

|fixed| Dimensions with numeric data can be defined in tabular data _or_ YAML and will appear as numeric in the processed Calliope model input dataset.
If all dimension data can be coerced to a numeric data type (e.g. `["10", 100, "-1"]`), then it _will_ be coerced (e.g., `[10, 100, -1]`).

### Internal changes

|new| `py.typed` file so that mypy recognises Calliope as a typed library when it is imported as a dependency.

|fixed| Spelling of Black config option `skip-magic-trailing-comma`.

## 0.7.0.dev2 (2024-01-26)

v0.7 includes a major change to how Calliope internally operates.
Expand Down
4 changes: 2 additions & 2 deletions src/calliope/attrdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def to_yaml(self, path=None):

def keys_nested(self, subkeys_as="list"):
"""
Returns all keys in the AttrDict, sorted, including the keys of
Returns all keys in the AttrDict, including the keys of
nested subdicts (which may be either regular dicts or AttrDicts).
If ``subkeys_as='list'`` (default), then a list of
Expand All @@ -366,7 +366,7 @@ def keys_nested(self, subkeys_as="list"):
"""
keys = []
for k, v in sorted(self.items()):
for k, v in self.items():
# Check if dict instance (which AttrDict is too),
# and for non-emptyness of the dict
if isinstance(v, dict) and v:
Expand Down
18 changes: 15 additions & 3 deletions tests/test_core_attrdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def regular_dict(self):
d = {
"a": 1,
"b": 2,
"c": {"x": "foo", "y": "bar", "z": {"I": 1, "II": 2}},
"d": None,
"c": {"x": "foo", "y": "bar", "z": {"I": 1, "II": 2}},
}
return d

Expand Down Expand Up @@ -238,12 +238,12 @@ def test_as_dict_flat(self, attr_dict):
def test_keys_nested_as_list(self, attr_dict):
d = attr_dict
dd = d.keys_nested()
assert dd == ["a", "b", "c.x", "c.y", "c.z.I", "c.z.II", "d"]
assert dd == ["a", "b", "d", "c.x", "c.y", "c.z.I", "c.z.II"]

def test_keys_nested_as_dict(self, attr_dict):
d = attr_dict
dd = d.keys_nested(subkeys_as="dict")
assert dd == ["a", "b", {"c": ["x", "y", {"z": ["I", "II"]}]}, "d"]
assert dd == ["a", "b", "d", {"c": ["x", "y", {"z": ["I", "II"]}]}]

def test_union(self, attr_dict):
d = attr_dict
Expand Down Expand Up @@ -279,6 +279,18 @@ def test_union_empty_dicts(self, attr_dict):
d.union(d_new)
assert len(d.baz.bar.keys()) == 0

def test_union_order_retained(self, attr_dict):
d_new = AttrDict({"a": 10, "e": {"b": 1, "a": 2}, "A": -1, "c.z.II": 20})
attr_dict.union(d_new, allow_override=True)
assert attr_dict == {
"a": 10,
"b": 2,
"d": None,
"c": {"x": "foo", "y": "bar", "z": {"I": 1, "II": 20}},
"e": {"b": 1, "a": 2},
"A": -1,
}

def test_del_key_single(self, attr_dict):
attr_dict.del_key("c")
assert "c" not in attr_dict
Expand Down

0 comments on commit c8bafb0

Please sign in to comment.