Parse SDMX file formats

Parse SDMX file formats

SDMX-ML

sdmx supports the several types of SDMX-ML messages.

SDMXML v2.1 reader.

class sdmx.reader.xml.Reader[source]
annotable(cls, elem, **kwargs)[source]

Create a AnnotableArtefact of cls from elem and kwargs.

Collects all parsed <com:Annotation>.

content_types: List[str] = ['application/xml', 'text/xml', 'application/vnd.sdmx.genericdata+xml;version=2.1', 'application/vnd.sdmx.structurespecificdata+xml;version=2.1', 'application/vnd.sdmx.generictimeseriesdata+xml;version=2.1', 'application/vnd.sdmx.structurespecifictimeseriesdata+xml;version=2.1', 'application/vnd.sdmx.structure+xml;version=2.1', 'application/vnd.sdmx.schema+xml;version=2.1', 'application/vnd.sdmx.genericmetadata+xml;version=2.1', 'application/vnd.sdmx.structurespecificmetadata+xml;version=2.1']

List of HTTP content types handled by the reader.

classmethod detect(content)[source]

Detect whether the reader can handle content.

Returns

True if the reader can handle the content.

Return type

bool

get_single(cls_or_name: Union[Type, str], id: Optional[str] = None, subclass: bool = False) Optional[Any][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 is given, only return an IdentifiableArtefact with the matching ID.

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 hierachy, maintainable() calls nameable(), which in turn calls identifiable(), 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 to True. Subsequent calls with the same object ID will return references to the same object.

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: Union[Type, str])[source]

Get the object at the top of stack cls_or_name without removing it.

pop_all(cls_or_name: Union[Type, str], subclass=False) Iterable[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_resolved_ref(cls_or_name: Union[Type, str])[source]

Pop a reference to cls_or_name and resolve it.

pop_single(cls_or_name: Union[Type, str])[source]

Pop a single object from the stack for cls_or_name and return.

push(stack_or_obj, obj=None)[source]

Push an object onto a stack.

read_message(source, dsd: Optional[sdmx.model.DataStructureDefinition] = None) sdmx.message.Message[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

Message

resolve(ref)[source]

Resolve the Reference instance ref, returning the referred object.

stash(*stacks)[source]

Temporarily hide all objects in the given stacks.

suffixes: List[str] = ['.xml']

List of file name suffixes handled by the reader.

unstash()[source]

Restore the objects hidden by the last stash() call to their stacks.

Calls to stash() and unstash() should be matched 1-to-1; if the latter outnumber the former, this will raise KeyError.

SDMX-JSON

class sdmx.reader.json.Reader[source]

Read SDMX-JSON and expose it as instances from sdmx.model.

content_types: List[str] = ['application/vnd.sdmx.draft-sdmx-json+json', 'draft-sdmx-json', 'text/json', 'application/vnd.sdmx.data+json;version=1.0.0', 'application/vnd.sdmx.structure+json;version=1.0.0']

List of HTTP content types handled by the reader.

classmethod detect(content)[source]

Detect whether the reader can handle content.

Returns

True if the reader can handle the content.

Return type

bool

read_dataset(root, ds_key)[source]
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

Message

read_obs(root, series_key=None, base_key=None)[source]
suffixes: List[str] = ['.json']

List of file name suffixes handled by the reader.

Reader API

sdmx.reader.READERS = [<class 'sdmx.reader.json.Reader'>, <class 'sdmx.reader.xml.Reader'>]

Reader classes

sdmx.reader.detect_content_reader(content)[source]

Return a reader class for content.

The BaseReader.detect() method for each class in READERS 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_content_type(ctype)[source]

Return a reader class for HTTP content type content.

Raises

ValueError – If no reader class matches.

See also

BaseReader.content_types

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
  • filename_or_obj (str or PathLike or file) –

  • format ('XML' or 'JSON', optional) –

  • dsd (DataStructureDefinition) – For “structure-specific” format`=``XML` messages only.

class sdmx.reader.base.BaseReader[source]
content_types: List[str] = []

List of HTTP content types handled by the reader.

classmethod detect(content: bytes) bool[source]

Detect whether the reader can handle content.

Returns

True if the reader can handle the content.

Return type

bool

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

Message

suffixes: List[str] = []

List of file name suffixes handled by the reader.

classmethod supports_content_type(value: str) bool[source]

True if the reader can handle content/media type value.

classmethod supports_suffix(value: str) bool[source]

True if the reader can handle files with suffix value.