Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hierarchy Filter (first pull request) #226

Merged
merged 23 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c9a8bc4
First Commit with Hierarchy Filter
GretchenSchowalter Feb 28, 2023
65b284e
updated codelist.py to include hierarchy filter
GretchenSchowalter Feb 28, 2023
5876b2b
Commiting the hierarchy filter with docstrings
GretchenSchowalter Mar 1, 2023
8a5d962
Hierarchy filter now returns RegionCodeList instance
GretchenSchowalter Mar 6, 2023
792c8a8
Update codelist.py
GretchenSchowalter Mar 6, 2023
36d16af
Fixing extra whitespaces and lengthy ValueError message.
GretchenSchowalter Mar 6, 2023
92633f9
Editing to add whitespaces
GretchenSchowalter Mar 6, 2023
209a5f0
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
e0d6b34
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
d6734c8
changed call to just "filter"
GretchenSchowalter Mar 6, 2023
4839eea
Removing extra whitespaces (stickler)
GretchenSchowalter Mar 6, 2023
dd52a98
Removed period at the end of match string in the filter value error test
GretchenSchowalter Mar 6, 2023
36031e3
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
ec15039
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
c1fcb01
adding list of available filter hierarchy options
GretchenSchowalter Mar 6, 2023
8c56c6b
Updating to reflect the name change in codelist.py
GretchenSchowalter Mar 6, 2023
234928e
Updated ValueError to include available options
GretchenSchowalter Mar 7, 2023
da7eb0f
fixing conflicts updating test and codelist
GretchenSchowalter Mar 7, 2023
8fee65b
update files to match flake8/black standards
GretchenSchowalter Mar 7, 2023
8d0c2c9
Adding RegionCodeList and VariableCodeList to codelist.rst
GretchenSchowalter Mar 9, 2023
4dd2d1a
Added line of space in codelist.py
GretchenSchowalter Mar 10, 2023
1006b3d
Made error message simpler, and added def hierarchy(self) placeholder
GretchenSchowalter Mar 15, 2023
614e30b
Made hierarchy method a property and added a test
GretchenSchowalter Mar 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)



here = Path(__file__).parent.absolute()


Expand Down Expand Up @@ -524,7 +525,7 @@ def from_directory(cls, name: str, path: Path, file_glob_pattern: str = "**/*"):
with open(yaml_file, "r", encoding="utf-8") as stream:
_code_list = yaml.safe_load(stream)

# a "region" codelist assumes a top-level key to be used as attribute
# a "region" codelist assumes a top-level category to be used as attribute
for top_level_cat in _code_list:
for top_key, _codes in top_level_cat.items():
for item in _codes:
Expand All @@ -542,3 +543,28 @@ def from_directory(cls, name: str, path: Path, file_glob_pattern: str = "**/*"):
mapping[code.name] = code

return cls(name=name, mapping=mapping)

GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
def hierarchy_filter(self, hierarchy: str) -> "RegionCodeList":
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
"""A filter that returns the components within a provided hierarchy.

Parameters
----------
hierarchy : str
The name of the hierarchy whose components you are looking for.

Raises
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
------
ValueError
If User inputs a typo or a hierarchy not compatible with the given model.
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved

Returns
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
-------
RegionCodeList
Returns a list of the component regions of the inputted hierarchy.
"""

mapping = {k: v for (k, v) in self.mapping.items() if v.hierarchy == hierarchy}
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
if mapping:
return RegionCodeList(name=f"{hierarchy}", mapping=mapping)
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
else:
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(f"No hierarchy found for {hierarchy}.")
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
Loading