Skip to content

Commit

Permalink
Update examples to use types/ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Jan 9, 2025
1 parent e19e7ed commit 4a87763
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
8 changes: 4 additions & 4 deletions examples/example_p1.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
{
"relation_type": "IS_PART_OF",
"related_identifier": "https://example.org/collection",
"datetime_log": "2024-02-19T00:00:00Z"
"datetime_log": "2024-02-19T00:00:00+00:00"
},
{
"relation_type": "IS_REFERENCED_BY",
"related_identifier": "https://example.org/referenced",
"datetime_log": "2024-02-19T00:00:00Z"
"datetime_log": "2024-02-19T00:00:00+00:00"
}
],
"change_log": [
{
"datetime_log": "2024-02-19T00:00:00Z",
"datetime_log": "2024-02-19T00:00:00+00:00",
"has_agent": {
"name": "Data Fuzzi",
"email": "[email protected]",
Expand All @@ -44,7 +44,7 @@
"description": "Registration completed."
},
{
"datetime_log": "2024-05-15T15:51:15Z",
"datetime_log": "2024-05-15T15:51:15+00:00",
"has_agent": {
"name": "Data Fuzzi",
"email": "[email protected]",
Expand Down
13 changes: 9 additions & 4 deletions examples/example_p1_dataclasses.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import sys
from datetime import datetime
from linkml_runtime.dumpers import json_dumper
from pid4cat_model.datamodel import pid4cat_model as p4c

# Demonstrate the use of Python DataClass-based model

if sys.version_info < (3, 11):
raise RuntimeError("Python 3.11 or higher is required due to the use of datetime.fromisoformat")

p1_Agent = p4c.Agent(
name="Data Fuzzi",
email="[email protected]",
Expand All @@ -15,12 +20,12 @@
p4c.PID4CatRelation(
relation_type=p4c.RelationType.IS_PART_OF,
related_identifier="https://example.org/collection",
datetime_log="2024-02-19T00:00:00Z",
datetime_log=datetime.fromisoformat("2024-02-19T00:00:00Z"),
),
p4c.PID4CatRelation(
relation_type=p4c.RelationType.IS_REFERENCED_BY,
related_identifier="https://example.org/referenced",
datetime_log="2024-02-19T00:00:00Z",
datetime_log=datetime.fromisoformat("2024-02-19T00:00:00Z"),
),
]

Expand All @@ -43,13 +48,13 @@

p1_log = [
p4c.LogRecord(
datetime_log="2024-02-19T00:00:00Z",
datetime_log=datetime.fromisoformat("2024-02-19T00:00:00Z"),
has_agent=p1_Agent,
changed_field=p4c.ChangeLogField.STATUS,
description="Registration completed.",
),
p4c.LogRecord(
datetime_log="2024-05-15T15:51:15Z",
datetime_log=datetime.fromisoformat("2024-05-15T15:51:15Z"),
has_agent=p1_Agent,
changed_field=p4c.ChangeLogField.RESOURCE_INFO,
description="as requested in issue #234",
Expand Down
17 changes: 11 additions & 6 deletions examples/example_p1_pydantic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import sys
from datetime import datetime
from pathlib import Path
from linkml_runtime.dumpers import json_dumper, yaml_dumper
from pid4cat_model.datamodel import pid4cat_model_pydantic as p4c

# Demonstrate the use of Pydantic models

if sys.version_info < (3, 11):
raise RuntimeError("Python 3.11 or higher is required due to the use of datetime.fromisoformat")

p1_Agent = p4c.Agent(
name="Data Fuzzi",
email="[email protected]",
Expand All @@ -16,12 +21,12 @@
p4c.PID4CatRelation(
relation_type=p4c.RelationType.IS_PART_OF,
related_identifier="https://example.org/collection",
datetime_log="2024-02-19T00:00:00Z",
datetime_log=datetime.fromisoformat("2024-02-19T00:00:00Z"),
),
p4c.PID4CatRelation(
relation_type=p4c.RelationType.IS_REFERENCED_BY,
related_identifier="https://example.org/referenced",
datetime_log="2024-02-19T00:00:00Z",
datetime_log=datetime.fromisoformat("2024-02-19T00:00:00Z"),
),
]

Expand All @@ -44,13 +49,13 @@

p1_log = [
p4c.LogRecord(
datetime_log="2024-02-19T00:00:00Z",
datetime_log=datetime.fromisoformat("2024-02-19T00:00:00Z"),
has_agent=p1_Agent,
changed_field=p4c.ChangeLogField.STATUS,
description="Registration completed.",
),
p4c.LogRecord(
datetime_log="2024-05-15T15:51:15Z",
datetime_log=datetime.fromisoformat("2024-05-15T15:51:15Z"),
has_agent=p1_Agent,
changed_field=p4c.ChangeLogField.RESOURCE_INFO,
description="as requested in issue #234",
Expand All @@ -73,12 +78,12 @@

print(p1)

print(yaml_dumper.dumps(c))

print(json_dumper.dumps(p1))

# write json to file
script_folder = Path(__file__).parent

with open(script_folder / "example_p1.json", "w", encoding="utf-8") as f:
f.write(json_dumper.dumps(p1))

print(yaml_dumper.dumps(c))

0 comments on commit 4a87763

Please sign in to comment.