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 22 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
30 changes: 15 additions & 15 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.. _api:

.. currentmodule:: nomenclature

API documentation
=================

.. toctree::
:maxdepth: 2

api/nomenclature
api/datastructuredefinition
api/codelist
api/regionprocessor
api/testing
.. _api:
.. currentmodule:: nomenclature
API documentation
=================
.. toctree::
:maxdepth: 2
api/nomenclature
api/datastructuredefinition
api/codelist
api/regionprocessor
api/testing
22 changes: 15 additions & 7 deletions doc/source/api/codelist.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
.. currentmodule:: nomenclature.codelist

**CodeList**
============

.. autoclass:: CodeList
:members:
.. currentmodule:: nomenclature.codelist

**CodeList**
============

.. autoclass:: CodeList
:members:


.. autoclass:: VariableCodeList
:members:


.. autoclass:: RegionCodeList
:members:
41 changes: 39 additions & 2 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def validate_items(self, items: List[str]) -> List[str]:
def replace_tags(
cls, code_list: List[Code], tag_name: str, tags: List[Code]
) -> List[Code]:

_code_list: List[Code] = []

for code in code_list:
Expand Down Expand Up @@ -524,7 +523,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 +541,41 @@ 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(self):
raise NotImplementedError("This method is not yet implemented.")

def filter(self, hierarchy: str) -> "RegionCodeList":
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
"""Return a filtered RegionCodeList object

Parameters
----------
hierarchy : str
String with filter parameter.

Raises
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
------
ValueError
Provided hierarchy is not compatible with the given model.

Returns
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
-------
:class:'RegionCodeList'

Notes
-----
The following arguments are available for filtering:

- 'hierarchy': filter by the hierarchy of each region

"""

mapping = {k: v for k, v in self.mapping.items() if v.hierarchy == hierarchy}
if mapping:
return RegionCodeList(name=self.name, mapping=mapping)
else:
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
msg: str = (
f"Filtered RegionCodeList is empty: hierarchy={hierarchy}\n"
"Use `RegionCodeList.hierarchy` for available items."
)
raise ValueError(msg)
Loading