Skip to content

Commit

Permalink
fix: added generics to epdx.convert_ilcd
Browse files Browse the repository at this point in the history
  • Loading branch information
ocni-dtu committed Feb 17, 2024
1 parent 34fdaa9 commit 538ae5e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 7 additions & 4 deletions packages/python/src/epdx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Type, TypeVar

from .epdx import *
from .pydantic import *
Expand All @@ -7,8 +8,10 @@
if hasattr(epdx, "__all__"):
__all__ = epdx.__all__

T = TypeVar("T", str, dict, EPD)

def convert_ilcd(data: str | dict, *, as_type: str = "dict"):

def convert_ilcd(data: str | dict, *, as_type: Type[T] = dict) -> T:
"""
Converts a json formatted ILCD+EPD data into EPDx
Expand All @@ -23,11 +26,11 @@ def convert_ilcd(data: str | dict, *, as_type: str = "dict"):
except Exception as err:
raise ParsingException(err)

if as_type == "str":
if as_type == str:
return _epd
elif as_type == "dict":
elif as_type == dict:
return json.loads(_epd)
elif as_type == "pydantic":
elif as_type == EPD:
return EPD(**json.loads(_epd))
else:
raise NotImplemented("Currently only 'dict', 'str' and 'pydantic' is implemented as_type.")
Expand Down
Binary file modified packages/python/src/epdx/epdx.abi3.so
Binary file not shown.
15 changes: 12 additions & 3 deletions packages/python/tests/test_parse.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import json

import pytest

import epdx


def test_parse_ilcd(datafix_dir):
ilcd_file = datafix_dir / "f63ac879-fa7d-4f91-813e-e816cbdf1927.json"
epd = epdx.convert_ilcd(ilcd_file.read_text())
epd = epdx.convert_ilcd(ilcd_file.read_text(), as_type=dict)

assert isinstance(epd, dict)


def test_parse_ilcd_dict_input(datafix_dir):
ilcd_file = datafix_dir / "f63ac879-fa7d-4f91-813e-e816cbdf1927.json"
epd = epdx.convert_ilcd(json.loads(ilcd_file.read_text()))

assert isinstance(epd, dict)

Expand All @@ -17,13 +26,13 @@ def test_parse_empty():

def test_parse_ilcd_str(datafix_dir):
ilcd_file = datafix_dir / "f63ac879-fa7d-4f91-813e-e816cbdf1927.json"
epd = epdx.convert_ilcd(ilcd_file.read_text(), as_type='str')
epd = epdx.convert_ilcd(ilcd_file.read_text(), as_type=str)

assert isinstance(epd, str)


def test_parse_ilcd_pydantic(datafix_dir):
ilcd_file = datafix_dir / "f63ac879-fa7d-4f91-813e-e816cbdf1927.json"
epd = epdx.convert_ilcd(ilcd_file.read_text(), as_type='pydantic')
epd = epdx.convert_ilcd(ilcd_file.read_text(), as_type=epdx.EPD)

assert isinstance(epd, epdx.EPD)

0 comments on commit 538ae5e

Please sign in to comment.