Skip to content

Commit

Permalink
Formatted Code, Codelist, Concepts and ConceptScheme.
Browse files Browse the repository at this point in the history
  • Loading branch information
javihern98 committed Apr 10, 2024
1 parent 7718a15 commit 75991b4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 51 deletions.
20 changes: 20 additions & 0 deletions src/pysdmx/model/__base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Annotation(Struct, frozen=True, omit_defaults=True):
url: The URL of the annotation.
type: The type of the annotation.
"""

id: Optional[str] = None
title: Optional[str] = None
text: Optional[str] = None
Expand Down Expand Up @@ -67,6 +68,7 @@ class IdentifiableArtefact(AnnotableArtefact):
uri: The URI of the artefact.
urn: The URN of the artefact.
"""

id: str
uri: Optional[str] = None
urn: Optional[str] = None
Expand All @@ -86,6 +88,7 @@ class NameableArtefact(IdentifiableArtefact):
name: The name of the artefact.
description: The description of the artefact.
"""

name: Optional[str] = None
description: Optional[str] = None

Expand All @@ -99,6 +102,7 @@ class VersionableArtefact(NameableArtefact):
valid_from: The date from which the artefact is valid.
valid_to: The date to which the artefact is valid.
"""

version: str = "1.0"
valid_from: Optional[datetime] = None
valid_to: Optional[datetime] = None
Expand All @@ -116,6 +120,7 @@ class MaintainableArtefact(VersionableArtefact):
structure_url: The URL of the structure.
maintainer: The maintainer of the artefact.
"""

is_final: bool = False
is_external_reference: bool = False
service_url: str = None
Expand All @@ -138,6 +143,7 @@ class Agency(MaintainableArtefact):
Attributes:
contacts: The contact of the agency.
"""

contacts = Sequence["Contact"]


Expand All @@ -151,6 +157,7 @@ class Item(NameableArtefact):
parent: The parent of the item.
children: The children of the item.
"""

scheme: "ItemScheme" = None
parent: Optional["Item"] = None
children: Sequence["Item"] = ()
Expand All @@ -165,9 +172,22 @@ class ItemScheme(MaintainableArtefact):
items: The list of items in the scheme.
is_partial: Whether the scheme is partial.
"""

items: Sequence["Item"] = ()
is_partial: bool = False

def __iter__(self) -> Iterator[Item]:
"""Return an iterator over the list of items."""
yield from self.items

def __len__(self) -> int:
"""Return the number of codes in the codelist."""
return len(self.items)

def __getitem__(self, id_: str) -> Optional[Item]:
"""Return the code identified by the supplied ID."""
out = list(filter(lambda item: item.id == id_, self.items))
if len(out) == 0:
return None
else:
return out[0]
27 changes: 4 additions & 23 deletions src/pysdmx/model/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from msgspec import Struct

from pysdmx.model.__base import ItemScheme


class Code(Struct, frozen=True, omit_defaults=True):
"""A code, such as a country code in the list of ISO 3166 codes.
Expand Down Expand Up @@ -49,7 +51,7 @@ def __str__(self) -> str:
return out


class Codelist(Struct, frozen=True, omit_defaults=True):
class Codelist(ItemScheme):
"""An immutable collection of codes, such as the ISO 3166 country codes.
A codelist is **maintained by its agency**, typically, an organisation
Expand All @@ -75,30 +77,9 @@ class Codelist(Struct, frozen=True, omit_defaults=True):
codes: The list of codes in the codelist.
"""

id: str
name: str
agency: str
description: Optional[str] = None
version: str = "1.0"
codes: Sequence[Code] = ()
items: Sequence[Code] = ()
sdmx_type: Literal["codelist", "valuelist"] = "codelist"

def __iter__(self) -> Iterator[Code]:
"""Return an iterator over the list of codes."""
yield from self.codes

def __len__(self) -> int:
"""Return the number of codes in the codelist."""
return len(self.codes)

def __getitem__(self, id_: str) -> Optional[Code]:
"""Return the code identified by the supplied ID."""
out = list(filter(lambda code: code.id == id_, self.codes))
if len(out) == 0:
return None
else:
return out[0]


class HierarchicalCode(Struct, frozen=True, omit_defaults=True):
"""A code, as used in a hierarchy.
Expand Down
33 changes: 5 additions & 28 deletions src/pysdmx/model/concept.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from msgspec import Struct

from pysdmx.model.__base import Item, ItemScheme
from pysdmx.model.code import Codelist


Expand Down Expand Up @@ -114,7 +115,7 @@ def __str__(self) -> str:
return ", ".join(out)


class Concept(Struct, frozen=True, omit_defaults=True):
class Concept(Item):
"""A concept (aka **variable**), such as frequency, reference area, etc.
Concepts are used to **describe the relevant characteristics** of a
Expand Down Expand Up @@ -144,11 +145,8 @@ class Concept(Struct, frozen=True, omit_defaults=True):
which the codes are taken.
"""

id: str
dtype: DataType = DataType.STRING
facets: Optional[Facets] = None
name: Optional[str] = None
description: Optional[str] = None
codes: Optional[Codelist] = None
enum_ref: Optional[str] = None

Expand All @@ -162,7 +160,7 @@ def __str__(self) -> str:
return ", ".join(out)


class ConceptScheme(Struct, frozen=True, omit_defaults=True):
class ConceptScheme(ItemScheme):
"""An immutable collection of concepts.
A concept scheme is **maintained by its agency**, typically, an
Expand All @@ -181,28 +179,7 @@ class ConceptScheme(Struct, frozen=True, omit_defaults=True):
description: Additional descriptive information about the scheme
(e.g. "The set of concepts in the SDMX Glossary").
version: The scheme version (e.g. 2.0)
concepts: The list of concepts in the scheme.
items: The list of concepts in the scheme.
"""

id: str
name: str
agency: str
description: Optional[str] = None
version: str = "1.0"
concepts: Sequence[Concept] = ()

def __iter__(self) -> Iterator[Concept]:
"""Return an iterator over the list of concepts."""
yield from self.concepts

def __len__(self) -> int:
"""Return the number of concepts in the concept scheme."""
return len(self.concepts)

def __getitem__(self, id_: str) -> Optional[Concept]:
"""Return the concept identified by the given ID."""
out = list(filter(lambda concept: concept.id == id_, self.concepts))
if len(out) == 0:
return None
else:
return out[0]
items: Sequence[Concept] = ()

0 comments on commit 75991b4

Please sign in to comment.