Skip to content

Commit

Permalink
Factor out exclude_unset
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed May 29, 2024
1 parent df5c5df commit 3d86f73
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions bw_interface_schemas/lci.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from datetime import datetime


class Source(BaseModel):
class Parsimonius(BaseModel):
"""Change defaule `model_dump` behaviour to not export unset values by default"""

def model_dump(self, exclude_unset=True, *args, **kwargs):
return super().model_dump(*args, exclude_unset=exclude_unset, **kwargs)


class Source(Parsimonius):
"""A data source, such as a publication or field measurement.
Very preliminary."""
Expand All @@ -14,12 +21,22 @@ class Source(BaseModel):
title: str
doi: Optional[str] = None

def model_dump(self, exclude_unset=True, *args, **kwargs):
"""Make `exclude_unset` True by default."""
return super().model_dump(*args, exclude_unset=exclude_unset, **kwargs)

class Edge(Parsimonius):
"""An quantitative edge linking two nodes in the graph."""
source: 'Node'
target: 'Node'
amount: float
uncertainty_type: Optional[int] = None
loc: Optional[float] = None
scale: Optional[float] = None
shape: Optional[float] = None
minimum: Optional[float] = None
maximum: Optional[float] = None
negative: Optional[bool] = None


class Node(BaseModel):
class Node(Parsimonius):
# Combination of database and code uniquely identifies a node
code: str
database: str
Expand All @@ -35,15 +52,12 @@ class Node(BaseModel):
references: Optional[list[Source]] = None
# Was previously classifications - we want a more generic categorical set
tags: Optional[dict[str, JsonValue]] = None
exchanges: list[Edge] = []

model_config = ConfigDict(
extra="allow",
)

def model_dump(self, exclude_unset=True, *args, **kwargs):
"""Make `exclude_unset` True by default."""
return super().model_dump(*args, exclude_unset=exclude_unset, **kwargs)


class Process(Node):
"""A generic process, possibly multi-functional. Does not have a reference product.
Expand Down

0 comments on commit 3d86f73

Please sign in to comment.