Read SDMX file formats#
SDMX-ML#
sdmx.reader.xml
supports the several types of SDMX-ML messages.
Implementation details:
The collections of
StructureMessage
(e.g.StructureMessage.codelist
) are implemented bysdmx
asDictLike
, withstr
keys, for convenience; the standard would imply these could be other collections, such as a simplelist
. The format of the keys in each collection depends on the content of the message parsed byreader.xml
:Simply
{object.id}
(IdentifiableArtefact.id
) of the contained objects, if these are unique;Otherwise
{maintainer.id}:{object.id}
(using theAgency
id) if these are unique;Otherwise
{maintainer.id}:{object.id}({object.version})
(using theVersionableArtefact.version
).
This ensures that all objects in a parsed message are accessible.
SDMX-ML v2.1 reader.
- class sdmx.reader.xml.v21.DispatchingReader(name, bases, dct)[source]#
Populate the parser, format, and model attributes of
Reader
.
- exception sdmx.reader.xml.v21.NotReference[source]#
Raised when the elem passed to
Reference
is not a reference.
- class sdmx.reader.xml.v21.Reader[source]#
SDMX-ML 2.1 reader.
- class Reference(reader, elem, cls_hint=None)[source]#
Temporary class for references.
cls, id, version, and agency_id are always for a MaintainableArtefact.
If the reference target is a MaintainableArtefact (maintainable is True), target_cls and target_id are identical to cls and id, respectively.
If the target is not maintainable, target_cls and target_id describe it.
cls_hint is an optional hint for when the object is instantiated, i.e. a more specific override for cls/target_cls.
- annotable(cls, elem, **kwargs)[source]#
Create a AnnotableArtefact of cls from elem and kwargs.
Collects all parsed <com:Annotation>.
- classmethod end(names: str, only: bool = True)[source]#
Decorator for a function that parses “end” events for XML elements.
- format: ClassVar = <module 'sdmx.format.xml.v21' from '/home/docs/checkouts/readthedocs.org/user_builds/sdmx1/envs/latest/lib/python3.12/site-packages/sdmx/format/xml/v21.py'>[source]#
- get_single(cls_or_name: Type | str, id: str | None = None, version: str | None = None, subclass: bool = False) Any | None [source]#
Return a reference to an object while leaving it in its stack.
Always returns 1 object. Returns
None
if no matching object exists, or if 2 or more objects meet the conditions.If id (and version) is/are given, only return an IdentifiableArtefact with the matching ID (and version).
If cls_or_name is a class and subclass is
True
; check all objects in the stack cls_or_name or any stack for a subclass of this class.
- identifiable(cls, elem, **kwargs)[source]#
Create a IdentifiableArtefact of cls from elem and kwargs.
- maintainable(cls, elem, **kwargs)[source]#
Create or retrieve a MaintainableArtefact of cls from elem and kwargs.
Following the SDMX-IM class hierarchy,
maintainable()
callsnameable()
, which in turn callsidentifiable()
, etc. (Since no concrete class is versionable but not maintainable, no separate method is created, for better performance). For all of these methods:Already-parsed items are removed from the stack only if elem is not
None
.kwargs (e.g. ‘id’) take precedence over any values retrieved from attributes of elem.
If elem is None,
maintainable()
returns a MaintainableArtefact with the is_external_reference attribute set toTrue
. Subsequent calls with the same object ID will return references to the same object.
- model: ClassVar = <module 'sdmx.model.v21' from '/home/docs/checkouts/readthedocs.org/user_builds/sdmx1/envs/latest/lib/python3.12/site-packages/sdmx/model/v21.py'>[source]#
- nameable(cls, elem, **kwargs)[source]#
Create a NameableArtefact of cls from elem and kwargs.
Collects all parsed
InternationalString
localizations of <com:Name> and <com:Description>.
- peek(cls_or_name: Type | str)[source]#
Get the object at the top of stack cls_or_name without removing it.
- pop_all(cls_or_name: Type | str, subclass=False) Sequence [source]#
Pop all objects from stack cls_or_name and return.
If cls_or_name is a class and subclass is
True
; return all objects in the stack cls_or_name or any stack for a subclass of this class.
- pop_single(cls_or_name: Type | str)[source]#
Pop a single object from the stack for cls_or_name and return.
- classmethod possible_reference(cls_hint: type | None = None, unstash: bool = False)[source]#
Decorator for a function where the elem parsed may be a Reference.
Before calling the decorated function, attempt to parse the elem as a
Reference
. If successful, return the reference instead of calling the function. If elem does not contain a reference, call the decorated function.
- class sdmx.reader.xml.v21.Reference(reader, elem, cls_hint=None)[source]#
Temporary class for references.
cls, id, version, and agency_id are always for a MaintainableArtefact.
If the reference target is a MaintainableArtefact (maintainable is True), target_cls and target_id are identical to cls and id, respectively.
If the target is not maintainable, target_cls and target_id describe it.
cls_hint is an optional hint for when the object is instantiated, i.e. a more specific override for cls/target_cls.
- sdmx.reader.xml.v21.add_localizations(target: InternationalString, values: list) None [source]#
Add localized strings from values to target.
- sdmx.reader.xml.v21.add_mds_events(reader: Reader, mds: MetadataStructureDefinition)[source]#
Add parser events for structure-specific metadata.
- sdmx.reader.xml.v21.end(names: str, only: bool = True)[source]#
Decorator for a function that parses “end” events for XML elements.
- sdmx.reader.xml.v21.matching_class(cls)[source]#
Filter condition; see
get_single()
andpop_all()
.
- sdmx.reader.xml.v21.possible_reference(cls_hint: type | None = None, unstash: bool = False)[source]#
Decorator for a function where the elem parsed may be a Reference.
Before calling the decorated function, attempt to parse the elem as a
Reference
. If successful, return the reference instead of calling the function. If elem does not contain a reference, call the decorated function.
- sdmx.reader.xml.v21.start(names: str, only: bool = True)[source]#
Decorator for a function that parses “start” events for XML elements.
SDMX-ML 3.0.0 reader.
SDMX-JSON#
SDMX-JSON v2.1 reader
- class sdmx.reader.json.Reader[source]#
Read SDMX-JSON and expose it as instances from
sdmx.model
.- media_types: List[MediaType] = [application/vnd.sdmx.data+json; version=1.0.0, application/vnd.sdmx.structure+json; version=1.0.0, application/vnd.sdmx.draft-sdmx-json+json; version=1.0.0, draft-sdmx-json; version=1.0.0, text/json; version=1.0.0][source]#
List of media types handled by the reader.
- read_message(source, dsd=None)[source]#
Read message from source.
- Parameters:
source (
file-like
) – Message content.dsd (
DataStructureDefinition
, optional) – DSD for aid in reading source.
- Returns:
An instance of a Message subclass.
- Return type:
SDMX-CSV#
Reader API#
- sdmx.reader.READERS = [<class 'sdmx.reader.json.Reader'>, <class 'sdmx.reader.xml.Reader'>][source]#
Reader classes
- sdmx.reader.detect_content_reader(content)[source]#
Return a reader class for content.
The
BaseReader.detect()
method for each class inREADERS
is called; if a reader signals that it is compatible with content, then that class is returned.- Raises:
ValueError – If no reader class matches.
- sdmx.reader.get_reader_for_media_type(value)[source]#
Return a reader class for HTTP content/media type value.
- Raises:
ValueError – If no reader class matches.
See also
BaseReader.media_type
- sdmx.reader.get_reader_for_path(path)[source]#
Return a reader class for file path.
- Raises:
ValueError – If no reader class matches.
See also
BaseReader.suffixes
- sdmx.reader.read_sdmx(filename_or_obj, format=None, **kwargs)[source]#
Load a SDMX-ML or SDMX-JSON message from a file or file-like object.
- Parameters:
format (
'XML'
or'JSON'
, optional) –dsd (
DataStructureDefinition
) – For “structure-specific” format`=``XML` messages only.
- class sdmx.reader.base.BaseReader[source]#
-
- classmethod handles_media_type(value: str) bool [source]#
True
if the reader can handle content/media type value.
- abstract read_message(source, dsd=None)[source]#
Read message from source.
- Parameters:
source (
file-like
) – Message content.dsd (
DataStructureDefinition
, optional) – DSD for aid in reading source.
- Returns:
An instance of a Message subclass.
- Return type: