Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from ocni-dtu/chrk/error_epds
Browse files Browse the repository at this point in the history
Chrk/error epds
  • Loading branch information
ocni-dtu authored Feb 18, 2024
2 parents ef2ea7d + 7c43544 commit a2818d4
Show file tree
Hide file tree
Showing 7 changed files with 2,066 additions and 17 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)
10 changes: 5 additions & 5 deletions src/epd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ enum Standard {

impl From<&String> for Standard {
fn from(value: &String) -> Self {
if value.to_ascii_lowercase().contains("15804") {
Standard::EN15804A1
} else if value.to_ascii_lowercase() == "en 15804+a2" {
if value.to_ascii_lowercase().contains("15804+a2") {
Standard::EN15804A2
} else if value.to_ascii_lowercase().contains("15804") {
Standard::EN15804A1
} else { Standard::UNKNOWN }
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ impl<'de> Deserialize<'de> for EPD {

let (gwp, odp, ap, ep, pocp, adpe, adpf) = collect_from_lcia_result(&helper.lcia_results.lcia_result);

let (declared_unit, conversions, pere, perm, pert, penre, penrm, penrt, sm ,rsf, nrsf, fw, hwd, nhwd, rwd, cru, mfr, mer, eee, eet) = collect_from_exchanges(&helper.exchanges.exchange);
let (declared_unit, conversions, pere, perm, pert, penre, penrm, penrt, sm, rsf, nrsf, fw, hwd, nhwd, rwd, cru, mfr, mer, eee, eet) = collect_from_exchanges(&helper.exchanges.exchange);

Ok(EPD {
id: helper.process_information.data_set_information.uuid,
Expand Down Expand Up @@ -387,5 +387,5 @@ fn collect_from_exchanges(exchanges: &Vec<Exchange>) -> (Unit, Vec<Conversion>,
};
}

(declared_unit, conversions, pere, perm, pert, penre, penrm, penrt, sm ,rsf, nrsf, fw, hwd, nhwd, rwd, cru, mfr, mer, eee, eet)
(declared_unit, conversions, pere, perm, pert, penre, penrm, penrt, sm, rsf, nrsf, fw, hwd, nhwd, rwd, cru, mfr, mer, eee, eet)
}
2 changes: 1 addition & 1 deletion src/ilcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct Compliance {
pub struct ReferenceToDescription {
pub short_description: Vec<ValueLang>,
pub _type: String,
pub ref_object_id: String,
// pub ref_object_id: Option<String>,
pub version: Option<String>
}

Expand Down
2,038 changes: 2,038 additions & 0 deletions tests/datafixtures/023f3b97-976a-41c4-b0f1-5357b9dc5b3e.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ mod tests {
let contents = fs::read_to_string(file_path).expect("Should have been able to read the file");

match parse::parse_ilcd(contents) {
Ok(epd) => {
Ok(())
}
Ok(_) => Ok(()),
Err(error) => Err(error.to_string())
}
}
Expand All @@ -36,6 +34,7 @@ mod tests {
ilcd_0b4c397d: "0b4c397d-c7a1-4ceb-9718-184334f6364e.json"
ilcd_0e0c4e37: "0e0c4e37-b7e6-4a4f-b1c9-d36da0aa16f5.json"
ilcd_0e9fd868: "0e9fd868-9656-489e-be6c-8251b3d43283.json"
ilcd_023f3b97: "023f3b97-976a-41c4-b0f1-5357b9dc5b3e.json"
}

#[test]
Expand All @@ -47,7 +46,7 @@ mod tests {

match parse::parse_ilcd(contents) {
Ok(_) => Err(String::from("Did not fail")),
Err(error) => Ok(())
Err(_) => Ok(())
}
}
}

0 comments on commit a2818d4

Please sign in to comment.