diff --git a/lodstorage/__init__.py b/lodstorage/__init__.py index 906d362..49e0fc1 100644 --- a/lodstorage/__init__.py +++ b/lodstorage/__init__.py @@ -1 +1 @@ -__version__ = "0.6.0" +__version__ = "0.7.0" diff --git a/lodstorage/sample2.py b/lodstorage/sample2.py new file mode 100644 index 0000000..43ff435 --- /dev/null +++ b/lodstorage/sample2.py @@ -0,0 +1,131 @@ +""" +Created on 2024-01-21 + +@author: wf +""" +from dataclasses import dataclass, field +from datetime import date, datetime +from typing import List, Optional + +from dataclasses_json import dataclass_json + +class DateConvert: + """ + date converter + """ + @classmethod + def iso_date_to_datetime(cls,iso_date:str)->datetime.date: + date=datetime.strptime(iso_date, "%Y-%m-%d").date() if iso_date else None + return date + + +@dataclass_json +@dataclass +class Royal: + """ + A member of the royal family + """ + + name: str + wikidata_id: str + number_in_line: Optional[int] = None + born_iso_date: Optional[str] = None + died_iso_date: Optional[str] = None + last_modified_iso: str = field(init=False) + age: Optional[int] = field(init=None) + of_age: Optional[bool] = field(init=None) + wikidata_url: Optional[str] = field(init=None) + + def __post_init__(self): + """ + init calculated fields + """ + self.lastmodified = datetime.utcnow() + self.last_modified_iso = self.lastmodified.strftime("%Y-%m-%dT%H:%M:%SZ") + end_date = self.died if self.died else date.today() + self.age = int((end_date - self.born).days / 365.2425) + self.of_age = self.age >= 18 + if self.wikidata_id: + self.wikidata_url=f"https://www.wikidata.org/wiki/{self.wikidata_id}" + @property + def born(self) -> date: + """Return the date of birth from the ISO date string.""" + born_date = DateConvert.iso_date_to_datetime(self.born_iso_date) + return born_date + + + @property + def died(self) -> Optional[date]: + """Return the date of death from the ISO date string, if available.""" + died_date = DateConvert.iso_date_to_datetime(self.died_iso_date) + return died_date + +@dataclass_json +@dataclass +class Royals: + """ + A collection of Royal family members + """ + + members: List[Royal] = field(default_factory=list) + + +class Sample: + """ + Sample dataset provider + """ + + @staticmethod + def get(dataset_name: str): + """ + Get the given sample dataset name + """ + if dataset_name == "royals": + return Royals( + members=[ + Royal( + name="Elizabeth Alexandra Mary Windsor", + born_iso_date="1926-04-21", + died_iso_date="2022-09-08", + wikidata_id="Q9682", + ), + Royal( + name="Charles III of the United Kingdom", + born_iso_date="1948-11-14", + number_in_line=0, + wikidata_id="Q43274", + ), + Royal( + name="William, Duke of Cambridge", + born_iso_date="1982-06-21", + number_in_line=1, + wikidata_id="Q36812", + ), + Royal( + name="Prince George of Wales", + born_iso_date="2013-07-22", + number_in_line=2, + wikidata_id="Q13590412", + ), + Royal( + name="Princess Charlotte of Wales", + born_iso_date="2015-05-02", + number_in_line=3, + wikidata_id="Q18002970", + ), + Royal( + name="Prince Louis of Wales", + born_iso_date="2018-04-23", + number_in_line=4, + wikidata_id="Q38668629", + ), + Royal( + name="Harry Duke of Sussex", + born_iso_date="1984-09-15", + number_in_line=5, + wikidata_id="Q152316", + ), + ] + ) + else: + raise ValueError("Unknown dataset name") diff --git a/lodstorage/version.py b/lodstorage/version.py index 7ef3df9..010ee07 100644 --- a/lodstorage/version.py +++ b/lodstorage/version.py @@ -14,5 +14,5 @@ class Version(object): name = "pylodstorage" version = lodstorage.__version__ date = "2020-09-10" - updated = "2024-01-07" + updated = "2024-01-21" description = "python List of Dict (Table) Storage library" diff --git a/pyproject.toml b/pyproject.toml index 9afb3fc..954a340 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,9 @@ dependencies = [ "jsonpickle==1.5.2", "pylatexenc~=2.10", "pygments", - "dicttoxml2" + "dicttoxml2", + # https://pypi.org/project/dataclasses-json/ + "dataclasses-json>=0.6.1", ] requires-python = ">=3.9" classifiers=[ diff --git a/tests/test_rdf.py b/tests/test_rdf.py index 3904a6a..bc6ce00 100644 --- a/tests/test_rdf.py +++ b/tests/test_rdf.py @@ -3,25 +3,25 @@ @author: wf """ +from lodstorage.sample2 import Sample from tests.basetest import Basetest -from lodstorage.sample import Sample -import json + class TestTriplify(Basetest): - + """ Tests https://github.com/WolfgangFahl/pyLoDStorage/issues/57 """ + def setUp(self, debug=False, profile=True): Basetest.setUp(self, debug=debug, profile=profile) - + def test_rdf_triples(self): """ test creating RDF triples """ - lod = Sample.getRoyals() - debug=self.debug - debug=True + royals = Sample.get("royals") + debug = self.debug + debug = True if debug: - print(json.dumps(lod,indent=2,default=str)) - \ No newline at end of file + print(royals.to_json(indent=2))