Skip to content

Commit

Permalink
adds sample2 and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Jan 21, 2024
1 parent 0f0b87f commit ebdffe7
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lodstorage/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6.0"
__version__ = "0.7.0"
131 changes: 131 additions & 0 deletions lodstorage/sample2.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion lodstorage/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
18 changes: 9 additions & 9 deletions tests/test_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

print(royals.to_json(indent=2))

0 comments on commit ebdffe7

Please sign in to comment.