Skip to content
Snippets Groups Projects
Commit d71fa9c3 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Implement the JSON import and export of taxonomy data

parent 070e9d4e
2 merge requests!37Resolve "Implement the JSON import and export of taxonomy data",!35Draft: Resolve "[Epic] Changes to taxonomy-lib for the 25.01 release"
Pipeline #83399 passed
......@@ -19,6 +19,8 @@
from __future__ import annotations
import re
import json
from copy import deepcopy
from math import floor
from taxonomylib.constants import (
......@@ -77,6 +79,46 @@ class Taxonomy:
else:
self._taxonomy_dict = taxonomy_dict
def get_dict(self) -> dict[str, str]:
"""
Returns the internal dictionary with taxonomy data.
Returns:
dict [str, str]:
Copy of internal dictionary with taxonomy data.
"""
return deepcopy(self._taxonomy_dict)
@classmethod
def load_json(cls, json_taxonomy: str) -> Taxonomy:
"""
Initializes a `Taxonomy` object with reading the provided taxonomy data from a JSON
string into the `_taxonomy_dict` dictionary.
Args:
json_taxonomy (str):
Taxonomy data in JSON format.
Returns:
A `Taxonomy` object with taxonomy data from the taxonomy string.
"""
taxonomy = cls(json.loads(json_taxonomy))
taxonomy.is_valid()
return taxonomy
def get_json(self) -> str:
"""
Creates a JSON string from the internal taxonomy dictionary.
Returns:
str:
A representation of the internal taxonomy dictionary as JSON string.
"""
return json.dumps(self._taxonomy_dict, indent=4)
@classmethod
def load_taxonomy_string(cls, taxonomy_string: str) -> Taxonomy:
"""
......@@ -249,7 +291,11 @@ class Taxonomy:
"""
Checks if the taxonomy data is valid. Checks for the following attributes are
implemented:
- height attribute.
- `HEI` attribute.
- `HIM` attribute.
- `HEB` attribute.
- date attribute.
- roof attribute.
"""
self._is_valid_hei_attribute()
......
......@@ -470,3 +470,36 @@ def test_check_for_occupancy_match():
f"The resulting tag of {test_dataset[0]} and {test_dataset[1]} should be "
f"{test_dataset[2][1]} but {result} was returned."
)
def test_load_get_json():
"""
Tests if the `load_json()` and `get_json()` functions correctly export a JSON string from
the dictionary of taxonomy data and read it back in correctly. For this the taxonomy data is
provided as a standard taxonomy string, read into the internal dictionary, exported to JSON,
read back in from JSON and exported again to the standard string. If the string is recreated
identically, the test passes.
"""
test_datasets = [
"CR+CIP/LFINF+CDL+LFC:15.0/HBET:4-7/BPD/EWMA/RSH1/FC",
"CR/LDUAL+CDN/HBET:3-5/SOS",
"CR/LWAL+CDM+DUL/HBET:31-/RES",
"MUR+STRUB+MOM/LWAL+CDN/HBET:3-6/COM",
"S/LFBR/HBET:1-3/YBET:1976-1978/SOS",
"W+WLI/LWAL+CDM/HBET:1-2/RES1/RWO3",
"MUR/LWAL+CDL/HBET:1-2/RES2A/EWMA",
"CR+CIP/LWAL+CDN/HBET:1-3/RES4",
"CR+CIP/LFM+CDM/HBET:1-3/RES2D",
"CR+PC/LWAL+CDM/COM3",
"S+SL/LFM+CDM/COM2",
"MUR/LWAL+CDN/HBET:1-2/COM5/EWMA",
"CR+CIP/LWAL+CDM/HBET:1-3/IND2",
"S+SL/LFM+CDN/IND2",
]
for test_dataset in test_datasets:
taxonomy = Taxonomy.load_taxonomy_string(test_dataset)
json_string = taxonomy.get_json()
taxonomy = Taxonomy.load_json(json_string)
taxonomy_string = taxonomy.get_standard_string()
assert taxonomy_string == test_dataset
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment