Repository: RemDelaporteMathurin/h-transport-materials

Issue:
Export to JSON
I guess this could be a method of PropertiesGroup

Discussion:

Patch:
diff --git a/h_transport_materials/materials/vanadium_alloy.py b/h_transport_materials/materials/vanadium_alloy.py
index 3448398..0c6a282 100644
--- a/h_transport_materials/materials/vanadium_alloy.py
+++ b/h_transport_materials/materials/vanadium_alloy.py
@@ -13,7 +13,7 @@ hashizume_diffusivity = Diffusivity(
 
 klepikov_solubility = Solubility(
     units="m-3 Pa-1/2",
-    data_T=[673, 773, 873, 973, 1073],
+    data_T=[673.0, 773.0, 873.0, 973.0, 1073.0],
     data_y=[1.62e20, 9.84e19, 5.65e19, 4.91e19, 2.94e19],  # taken from Table 2
     isotope="H",
     source="klepikov_hydrogen_2000",
@@ -29,4 +29,3 @@ for prop in properties:
     prop.material = "v4cr4ti"
 
 htm.database += properties
-
diff --git a/h_transport_materials/properties_group.py b/h_transport_materials/properties_group.py
index c23b30a..88a6df9 100644
--- a/h_transport_materials/properties_group.py
+++ b/h_transport_materials/properties_group.py
@@ -1,4 +1,5 @@
 import numpy as np
+import json
 from pybtex.database import BibliographyData
 
 from h_transport_materials.fitting import fit_arhenius
@@ -113,3 +114,25 @@ class PropertiesGroup(list):
         """
 
         self.bibdata.to_file(filename)
+
+    def export_to_json(self, filename: str):
+        keys = [
+            "material",
+            "pre_exp",
+            "act_energy",
+            "isotope",
+            "author",
+            "source",
+            "range",
+            "doi",
+            "units",
+        ]
+
+        data = []
+        for prop in self:
+
+            prop_dict = {key: getattr(prop, key) for key in keys if hasattr(prop, key)}
+            data.append(prop_dict)
+
+        with open(filename, "w") as outfile:
+            json.dump(data, outfile, indent=4)
diff --git a/h_transport_materials/property.py b/h_transport_materials/property.py
index a6ff5dd..1e6679d 100644
--- a/h_transport_materials/property.py
+++ b/h_transport_materials/property.py
@@ -91,15 +91,27 @@ class Property:
 
         self._range = value
 
+    @property
+    def doi(self):
+        self._doi = None
+        if self.bibsource:
+            if "doi" in self.bibsource.fields:
+                self._doi = self.bibsource.fields["doi"]
+
+        return self._doi
+
+    @doi.setter
+    def doi(self, value):
+        self._doi = value
+
     @property
     def nb_citations(self):
         # if nb_citations doesn't already exist, compute it
         if self._nb_citations is None:
             if self.bibsource is None:
                 self.nb_citations = 0
-            elif "doi" in self.bibsource.fields:
-                doi = self.bibsource.fields["doi"]
-                self.nb_citations = get_nb_citations(doi)
+            elif self.doi:
+                self.nb_citations = get_nb_citations(self.doi)
             else:
                 self.nb_citations = 0
         return self._nb_citations


Test patch:
diff --git a/tests/test_properties_group.py b/tests/test_properties_group.py
index 060299e..ab6c629 100644
--- a/tests/test_properties_group.py
+++ b/tests/test_properties_group.py
@@ -1,5 +1,6 @@
 import h_transport_materials as htm
 import numpy as np
+import json
 import pytest
 from pybtex.database import BibliographyData
 
@@ -124,3 +125,26 @@ def test_export_bib():
         ]
     )
     my_group.export_bib("out.bib")
+
+
+def test_export_to_json():
+    # build
+
+    my_group = htm.database
+
+    # run
+
+    my_group.export_to_json("out.json")
+
+    # test
+    with open("out.json") as json_file:
+        data_in = json.load(json_file)
+
+    for prop_file, prop_ref in zip(data_in, my_group):
+        for key, val in prop_file.items():
+            if hasattr(prop_ref, key):
+                if isinstance(val, list):
+                    for item1, item2 in zip(val, getattr(prop_ref, key)):
+                        assert item1 == item2
+                else:
+                    assert getattr(prop_ref, key) == val
diff --git a/tests/test_property.py b/tests/test_property.py
index 7d9a023..c70b217 100644
--- a/tests/test_property.py
+++ b/tests/test_property.py
@@ -80,3 +80,9 @@ def test_range_cannot_be_negative():
         htm.Property(range=(-1, 2))
     with pytest.raises(ValueError, match="range must be stricly positive"):
         htm.Property(range=(0, 2))
+
+
+def test_has_doi():
+    """Checks that Property has a doi attribute"""
+    prop = htm.Property(source=source_bib_from_file)
+    assert prop.doi