API reference

Contents

API reference#

Some parts of the API are described on separate pages:

See also the Implementation notes.

Top-level methods and classes#

Client([source, log_level])

Client for a SDMX REST web service.

Resource(value[, names, module, qualname, ...])

Enumeration of SDMX-REST API resources.

add_source(info[, id, override])

Add a new data source.

list_sources()

Return a sorted list of valid source IDs.

log

Top-level logger.

read_sdmx(filename_or_obj[, format])

Load a SDMX-ML or SDMX-JSON message from a file or file-like object.

read_url(url, **kwargs)

Request a URL directly.

to_csv(obj, *args[, path, rtype])

Convert an SDMX obj to SDMX-CSV.

to_pandas(obj, *args, **kwargs)

Convert an SDMX obj to pandas object(s).

to_xml(obj, **kwargs)

Convert an SDMX obj to SDMX-ML.

class sdmx.Client(source=None, log_level=None, **session_opts)[source]#

Client for a SDMX REST web service.

Parameters:
  • source (str or source.Source) – Identifier of a data source. If a string, must be one of the known sources in list_sources().

  • log_level (int) –

    Override the package-wide logger with one of the standard logging levels.

    Deprecated since version 2.0: Will be removed in sdmx version 3.0.

  • **session_opts – Additional keyword arguments are passed to Session.

get(resource_type: str | Resource | None = None, resource_id: str | None = None, tofile: os.PathLike | IO | None = None, use_cache: bool = False, dry_run: bool = False, **kwargs) sdmx.message.Message[source]#

Retrieve SDMX data or metadata.

(Meta)data is retrieved from the source of the current Client. The item(s) to retrieve can be specified in one of two ways:

  1. resource_type, resource_id: These give the type (see Resource) and, optionally, ID of the item(s). If the resource_id is not given, all items of the given type are retrieved.

  2. a resource object, i.e. a MaintainableArtefact: resource_type and resource_id are determined by the object’s class and id attribute, respectively.

Data is retrieved with resource_type=’data’. In this case, the optional keyword argument key can be used to constrain the data that is retrieved. Examples of the formats for key:

  1. {'GEO': ['EL', 'ES', 'IE']}: dict with dimension name(s) mapped to an iterable of allowable values.

  2. {'GEO': 'EL+ES+IE'}: dict with dimension name(s) mapped to strings joining allowable values with ‘+’, the logical ‘or’ operator for SDMX web services.

  3. '....EL+ES+IE': str in which ordered dimension values (some empty, '') are joined with '.'. Using this form requires knowledge of the dimension order in the target data resource_id; in the example, dimension ‘GEO’ is the fifth of five dimensions: '.'.join(['', '', '', '', 'EL+ES+IE']). CubeRegion.to_query_string() can also be used to create properly formatted strings.

For formats 1 and 2, but not 3, the key argument is validated against the relevant DSD, either given with the dsd keyword argument, or retrieved from the web service before the main query.

For the optional param keyword argument, some useful parameters are:

  • ‘startperiod’, ‘endperiod’: restrict the time range of data to retrieve.

  • ‘references’: control which item(s) related to a metadata resource are retrieved, e.g. references=’parentsandsiblings’.

Parameters:
  • resource_type (str or Resource, optional) – Type of resource to retrieve.

  • resource_id (str, optional) – ID of the resource to retrieve.

  • tofile (str or PathLike or file-like object, optional) – File path or file-like to write SDMX data as it is received.

  • use_cache (bool, optional) – If True, return a previously retrieved Message from cache, or update the cache with a newly-retrieved Message.

  • dry_run (bool, optional) – If True, prepare and return a requests.Request object, but do not execute the query. The prepared URL and headers can be examined by inspecting the returned object.

  • **kwargs – Other, optional parameters (below).

  • dsd (DataStructureDefinition) – Existing object used to validate the key argument. If not provided, an additional query executed to retrieve a DSD in order to validate the key.

  • force (bool) – If True, execute the query even if the source does not support queries for the given resource_type. Default: False.

  • headers (dict) – HTTP headers. Given headers will overwrite instance-wide headers passed to the constructor. Default: None to use the default headers of the source.

  • key (str or dict) – For queries with resource_type=’data’. str values are not validated; dict values are validated using make_constraint().

  • params (dict) – Query parameters. The SDMX REST web service guidelines describe parameters and allowable values for different queries. params is not validated before the query is executed.

  • provider (str) –

    ID of the agency providing the data or metadata. Default: ID of the source agency.

    An SDMX web service is a ‘data source’ operated by a specific, ‘source’ agency. A web service may host data or metadata originally published by one or more ‘provider’ agencies. Many sources are also providers. Other agencies—e.g. the SDMX Global Registry—simply aggregate (meta)data from other providers, but do not provide any (meta)data themselves.

  • resource (MaintainableArtefact subclass) – Object to retrieve. If given, resource_type and resource_id are ignored.

  • version (str) – version> of a resource to retrieve. Default: the keyword ‘latest’.

Returns:

The requested SDMX message or, if dry_run is True, the prepared request object.

Return type:

Message or Request

Raises:

NotImplementedError – If the source does not support the given resource_type and force is not True.

preview_data(flow_id, key={})[source]#

Return a preview of data.

For the Dataflow flow_id, return all series keys matching key. Uses a feature supported by some data providers that returns SeriesKeys without the corresponding Observations.

To count the number of series:

keys = sdmx.Client('PROVIDER').preview_data('flow')
len(keys)

To get a pandas object containing the key values:

keys_df = sdmx.to_pandas(keys)
Parameters:
  • flow_id (str) – Dataflow to preview.

  • key (dict, optional) – Mapping of dimension to values, where values may be a ‘+’-delimited list of values. If given, only SeriesKeys that match key are returned. If not given, preview_data is equivalent to list(client.series_keys(flow_id)).

Return type:

list of SeriesKey

series_keys(flow_id, use_cache=True)[source]#

Return all SeriesKey for flow_id.

Return type:

list

session: Session[source]#

Session for queries sent from the instance.

source: sdmx.source.Source[source]#

source.Source for requests sent from the instance.

sdmx.Request(*args, **kwargs)[source]#

Compatibility function for Client.

New in version 2.0.

Deprecated since version 2.0: Will be removed in sdmx version 3.0.

class sdmx.Resource(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Enumeration of SDMX-REST API resources.

This class merges the “resources” defined in Section V of the SDMX 2.1 and 3.0 standards; in the latter, only five values (availability, data, metadata, schema, structure) are used as the first part of a URL path; however, the choice of this first part and allowable query parameters depend on the more detailed list.

Enum member

sdmx.model class

actualconstraint

ContentConstraint

agencyscheme

AgencyScheme

allowedconstraint

ContentConstraint

attachementconstraint

AttachmentConstraint

availableconstraint

ContentConstraint

categorisation

Categorisation

categoryscheme

CategoryScheme

codelist

Codelist

conceptscheme

ConceptScheme

contentconstraint

ContentConstraint

customtypescheme

CustomTypeScheme.

data

DataSet

dataflow

Dataflow(Definition)

dataconsumerscheme

DataConsumerScheme

dataproviderscheme

DataProviderScheme

datastructure

DataStructureDefinition

hierarchicalcodelist

v21.HierarchicalCodelist.

metadata

MetadataSet.

metadataflow

Metadataflow(Definition)

metadatastructure

MetadataStructureDefinition

namepersonalisationscheme

NamePersonalisationScheme.

organisationscheme

OrganisationScheme

provisionagreement

ProvisionAgreement

rulesetscheme

RulesetScheme.

structure

Mixed.

structureset

StructureSet.

transformationscheme

TransformationScheme.

userdefinedoperatorscheme

UserdefinedoperatorScheme.

vtlmappingscheme

VTLMappingScheme.

organisationunitscheme

Not implemented.

process

Not implemented.

reportingtaxonomy

Not implemented.

schema

Not implemented.

classmethod class_name(value: Resource, default=None) str[source]#

Return the name of a sdmx.model class from an enum value.

Values are returned in lower case.

classmethod from_obj(obj)[source]#

Return an enumeration value based on the class of obj.

sdmx.add_source(info: Dict | str, id: str | None = None, override: bool = False, **kwargs) None[source]#

Add a new data source.

The info expected is in JSON format:

{
  "id": "ESTAT",
  "documentation": "http://data.un.org/Host.aspx?Content=API",
  "url": "http://ec.europa.eu/eurostat/SDMX/diss-web/rest",
  "name": "Eurostat",
  "supports": {"codelist": false, "preview": true}
}

…with unspecified values using the defaults; see Source.

Parameters:
  • info (dict-like) – String containing JSON information about a data source.

  • id (str) – Identifier for the new datasource. If None (default), then info[‘id’] is used.

  • override (bool) – If True, replace any existing data source with id. Otherwise, raise ValueError.

  • **kwargs – Optional callbacks for handle_response and finish_message hooks.

sdmx.install_schemas(schema_dir: ~pathlib.Path | None = None, version: str | ~sdmx.format.Version = Version.2.1) Path[source]#

Install SDMX-ML XML Schema documents for use with validate_xml().

Parameters:
  • schema_dir (Path, optional) – The directory where XSD schemas will be downloaded to. Default: a subdirectory named sdmx/version within the platformdirs.user_cache_path().

  • version (str or Version, optional) – The SDMX-ML schema version to install. One of Version["2.1"] (default), Version["3.0.0"], or str equivalent.

Returns:

The path containing the installed schemas. If schema_dir is given, the return value is identical to the parameter.

Return type:

Path

sdmx.list_sources()[source]#

Return a sorted list of valid source IDs.

These can be used to create Client instances.

sdmx.log = <Logger sdmx (WARNING)>[source]#

Top-level logger.

New in version 0.4.

sdmx.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.

sdmx.read_url(url, **kwargs)[source]#

Request a URL directly.

sdmx.to_csv(obj, *args, path: ~os.PathLike | None = None, rtype: ~typing.Type[str | ~pandas.core.frame.DataFrame] = <class 'str'>, **kwargs) None | str | DataFrame[source]#

Convert an SDMX obj to SDMX-CSV.

With rtype = DataFrame, the returned object is not necessarily in SDMX-CSV format. In particular, writing this to file using pandas.DataFrame.to_csv() will yield invalid SDMX-CSV, because pandas includes a CSV column corresponding to the index of the data frame. You must pass index=False to disable this behaviour. With rtype = str or when giving path, this is done automatically.

Parameters:
  • path (os.PathLike, optional) – Path to write an SDMX-CSV file. If given, nothing is returned.

  • rtype – Return type; see below. Pass literally str or pd.DataFrame; not an instance of either class.

  • kwargs – Keyword arguments passed to dataset().

Returns:

Raises:

NotImplementedError – If obj is any class except DataSet; this is the only class for which the SDMX-CSV standard describes a format.

See also

sdmx.writer.csv.

sdmx.to_pandas(obj, *args, **kwargs)[source]#

Convert an SDMX obj to pandas object(s).

See sdmx.writer.pandas.

sdmx.to_xml(obj, **kwargs)[source]#

Convert an SDMX obj to SDMX-ML.

Parameters:

kwargs – Passed to lxml.etree.to_string(), e.g. pretty_print = True.

Raises:

NotImplementedError – If writing specific objects to SDMX-ML has not been implemented in sdmx.

sdmx.validate_xml(msg: ~pathlib.Path | ~typing.IO, schema_dir: ~pathlib.Path | None = None, version: str | ~sdmx.format.Version = Version.2.1) bool[source]#

Validate and SDMX message against the XML Schema (XSD) documents.

The XML Schemas must first be installed or validation will fail. See sdmx.install_schemas() to download the schema files.

Parameters:
  • msg – A SDMX-ML Message formatted XML file.

  • schema_dir – The directory to XSD schemas used to validate the message.

  • version – The SDMX-ML schema version to validate against. One of 2.1 or 3.0.

Returns:

True if validation passed. False otherwise.

Return type:

bool

format: SDMX file formats#

This information is used across other modules including sdmx.reader, sdmx.client, and sdmx.writer.

class sdmx.format.Flag(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: IntFlag

Flag values for information about MediaType:

  • data: True if this format contains (meta)data. False if it contains (meta)data structures.

  • meta: True if this format contains metadata (or metadata structures). False otherwise.

  • ss: True if this format contains data that is structure-specific. This distinction is only relevant before SDMX 3.0.

  • ts: True if this format contains time-series data. This distinction is only relevant before SDMX 3.0.

data = 1[source]#
meta = 2[source]#
ss = 4[source]#
ts = 8[source]#
sdmx.format.MEDIA_TYPES = [application/vnd.sdmx.generic+xml; version=2.1, application/vnd.sdmx.genericdata+xml; version=2.1, application/vnd.sdmx.genericmetadata+xml; version=2.1, application/vnd.sdmx.generictimeseriesdata+xml; version=2.1, application/vnd.sdmx.schema+xml; version=2.1, application/vnd.sdmx.structure+xml; version=2.1, application/vnd.sdmx.structurespecificdata+xml; version=2.1, application/vnd.sdmx.structurespecificmetadata+xml; version=2.1, application/vnd.sdmx.structurespecifictimeseriesdata+xml; version=2.1, application/xml; version=2.1, text/xml; version=2.1, application/vnd.sdmx.data+xml; version=3.0.0, application/vnd.sdmx.structure+xml; version=3.0.0, application/vnd.sdmx.metadata+xml; version=2.0.0, application/vnd.sdmx.data+json; version=1.0.0, application/vnd.sdmx.data+json; version=2.0.0, application/vnd.sdmx.structure+json; version=1.0.0, application/vnd.sdmx.structure+json; version=2.0.0, application/vnd.sdmx.metadata+json; version=2.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, application/vnd.sdmx.data+csv; version=1.0.0, application/vnd.sdmx.metadata+csv; version=2.0.0][source]#

SDMX formats. Each record is an instance of Format.

class sdmx.format.MediaType(label: str, base: ~typing.Literal['csv', 'json', 'xml'], _version: dataclasses.InitVar[typing.Union[str, sdmx.format.Version]], flags: ~sdmx.format.Flag = Flag.None, full: str | None = None)[source]#

Bases: object

Structure of elements in MEDIA_TYPES.

The str() of a MediaType is generally of the form:

application/vnd.sdmx.{label}+{base};version={version}

…unless full is provided, in which case label and base are ignored.

base: Literal['csv', 'json', 'xml'][source]#

The base media type or file format.

flags: Flag = 0[source]#
full: str | None = None[source]#

Specify the full media type string.

property is_data: bool[source]#
property is_meta: bool[source]#
property is_structure_specific: bool[source]#
property is_time_series: bool[source]#
label: str[source]#

Distinguishing part of the media type.

match(value: str, strict: bool = False) bool[source]#

True if value matches the current MediaType.

version: Version[source]#

Format version.

sdmx.format.f[source]#

alias of Flag

sdmx.format.list_media_types(**filters) List[MediaType][source]#

Return the string for each item in MEDIA_TYPES matching filters.

SDMX-JSON#

Information about the SDMX-JSON file format.

SDMX-ML#

message: SDMX messages#

Classes for SDMX messages.

Message and related classes are not defined in the SDMX information model, but in the SDMX-ML standard.

sdmx also uses DataMessage to encapsulate SDMX-JSON data returned by data sources.

class sdmx.message.DataMessage(version: ~sdmx.format.Version = Version.2.1, header: ~sdmx.message.Header = <factory>, footer: ~sdmx.message.Footer | None = None, response: requests.Response | None = None, data: ~typing.List[~sdmx.model.common.BaseDataSet] = <factory>, dataflow: ~sdmx.model.common.BaseDataflow | None = None, observation_dimension: ~sdmx.model.common._AllDimensions | ~sdmx.model.common.DimensionComponent | ~typing.List[~sdmx.model.common.DimensionComponent] | None = None)[source]#

Bases: Message

SDMX Data Message.

Note

A DataMessage may contain zero or more DataSet, so data is a list. To retrieve the first (and possibly only) data set in the message, access the first element of the list: msg.data[0].

compare(other, strict=True)[source]#

Return True if self is the same as other.

Two DataMessages are the same if:

Parameters:

strict (bool, optional) – Passed to compare().

data: List[BaseDataSet][source]#

list of DataSet.

dataflow: BaseDataflow | None = None[source]#

DataflowDefinition that contains the data.

observation_dimension: _AllDimensions | DimensionComponent | List[DimensionComponent] | None = None[source]#

The “dimension at observation level”.

property structure[source]#

DataStructureDefinition used in the dataflow.

property structure_type: Type[Structure][source]#

Structure subtype describing the contained (meta)data.

class sdmx.message.ErrorMessage(version: ~sdmx.format.Version = Version.2.1, header: ~sdmx.message.Header = <factory>, footer: ~sdmx.message.Footer | None = None, response: requests.Response | None = None)[source]#

Bases: Message

class sdmx.message.Footer(severity: str | None = None, text: ~typing.List[~sdmx.model.internationalstring.InternationalString] = <factory>, code: int | None = None)[source]#

Bases: object

Footer of an SDMX-ML message.

SDMX-JSON messages do not have footers.

code: int | None = None[source]#
compare(other, strict=True)[source]#

Return True if self is the same as other.

Two Footers are the same if their code, severity, and text are equal.

Parameters:

strict (bool, optional) – Passed to compare().

severity: str | None = None[source]#
text: List[InternationalString][source]#

The body text of the Footer contains zero or more blocks of text.

class sdmx.message.Header(error: str | None = None, extracted: datetime | None = None, id: str | None = None, prepared: datetime | None = None, reporting_begin: datetime | None = None, reporting_end: datetime | None = None, receiver: Agency | None = None, sender: Agency | None = None, source: InternationalStringDescriptor = None, test: bool = False)[source]#

Bases: object

Header of an SDMX-ML message.

SDMX-JSON messages do not have headers.

compare(other, strict=True)[source]#

Return True if self is the same as other.

Two Headers are the same if their corresponding attributes are equal.

Parameters:

strict (bool, optional) – Passed to compare().

error: str | None = None[source]#

(optional) Error code for the message.

extracted: datetime | None = None[source]#

Date and time at which the data was extracted.

id: str | None = None[source]#

Identifier for the message.

prepared: datetime | None = None[source]#

Date and time at which the message was generated.

receiver: Agency | None = None[source]#

Intended recipient of the message, e.g. the user’s name for an authenticated service.

reporting_begin: datetime | None = None[source]#

Start of the time period covered by a DataMessage.

reporting_end: datetime | None = None[source]#

End of the time period covered by a DataMessage.

sender: Agency | None = None[source]#

The Agency associated with the data Source.

source: InternationalStringDescriptor = None[source]#
test: bool = False[source]#
class sdmx.message.Message(version: sdmx.format.Version = <Version.2.1: 2.1>, header: sdmx.message.Header = <factory>, footer: Optional[sdmx.message.Footer] = None, response: Optional[ForwardRef('requests.Response')] = None)[source]#

Bases: object

compare(other, strict=True)[source]#

Return True if self is the same as other.

Two Messages are the same if their header and footer compare equal.

Parameters:

strict (bool, optional) – Passed to compare().

footer: Footer | None = None[source]#

(optional) Footer instance.

header: Header[source]#

Header instance.

response: requests.Response | None = None[source]#

requests.Response instance for the response to the HTTP request that returned the Message. This is not part of the SDMX standard.

version: Version = 2.1[source]#

SDMX version.

class sdmx.message.MetadataMessage(version: ~sdmx.format.Version = Version.2.1, header: ~sdmx.message.Header = <factory>, footer: ~sdmx.message.Footer | None = None, response: requests.Response | None = None, data: ~typing.List[~sdmx.model.common.BaseDataSet] = <factory>, dataflow: ~sdmx.model.common.BaseDataflow | None = None, observation_dimension: ~sdmx.model.common._AllDimensions | ~sdmx.model.common.DimensionComponent | ~typing.List[~sdmx.model.common.DimensionComponent] | None = None)[source]#

Bases: DataMessage

SDMX Metadata Message.

property structure_type: Type[Structure][source]#

Structure subtype describing the contained (meta)data.

class sdmx.message.StructureMessage(version: sdmx.format.Version = <Version.2.1: 2.1>, header: sdmx.message.Header = <factory>, footer: Optional[sdmx.message.Footer] = None, response: Optional[ForwardRef('requests.Response')] = None, categorisation: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.Categorisation] = None, category_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.CategoryScheme] = None, codelist: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.Codelist] = None, hierarchical_codelist: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.v21.HierarchicalCodelist] = None, hierarchy: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.v30.Hierarchy] = None, concept_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.ConceptScheme] = None, constraint: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.BaseConstraint] = None, dataflow: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.BaseDataflow] = None, metadatastructure: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.BaseMetadataStructureDefinition] = None, metadataflow: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.BaseMetadataflow] = None, structure: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.BaseDataStructureDefinition] = None, structureset: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.v21.StructureSet] = None, organisation_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.OrganisationScheme] = None, provisionagreement: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.ProvisionAgreement] = None, custom_type_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.CustomTypeScheme] = None, name_personalisation_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.NamePersonalisationScheme] = None, ruleset_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.RulesetScheme] = None, vtl_mapping_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.VTLMappingScheme] = None, transformation_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.TransformationScheme] = None, user_defined_operator_scheme: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.common.UserDefinedOperatorScheme] = None, valuelist: sdmx.dictlike.DictLikeDescriptor[str, sdmx.model.v30.ValueList] = None)[source]#

Bases: Message

add(obj: IdentifiableArtefact)[source]#

Add obj to the StructureMessage.

categorisation: DictLikeDescriptor[str, Categorisation] = None[source]#

Collection of Categorisation.

category_scheme: DictLikeDescriptor[str, CategoryScheme] = None[source]#

Collection of CategoryScheme.

codelist: DictLikeDescriptor[str, Codelist] = None[source]#

Collection of Codelist.

compare(other, strict=True)[source]#

Return True if self is the same as other.

Two StructureMessages compare equal if DictLike.compare() is True for each of the object collection attributes.

Parameters:

strict (bool, optional) – Passed to DictLike.compare().

concept_scheme: DictLikeDescriptor[str, ConceptScheme] = None[source]#

Collection of ConceptScheme.

constraint: DictLikeDescriptor[str, BaseConstraint] = None[source]#

Collection of ContentConstraint.

custom_type_scheme: DictLikeDescriptor[str, CustomTypeScheme] = None[source]#

Collection of CustomTypeScheme.

dataflow: DictLikeDescriptor[str, BaseDataflow] = None[source]#

Collection of Dataflow(Definition).

get(obj_or_id: str | IdentifiableArtefact) IdentifiableArtefact | None[source]#

Retrieve obj_or_id from the StructureMessage.

Parameters:

obj_or_id (str or IdentifiableArtefact) – If an IdentifiableArtefact, return an object of the same class and id; if str, an object with this ID.

Returns:

Raises:

ValueError – if obj_or_id is a string and there are ≥2 objects (of different classes) with the same ID.

hierarchical_codelist: DictLikeDescriptor[str, HierarchicalCodelist] = None[source]#

Collection of HierarchicalCodelist.

hierarchy: DictLikeDescriptor[str, Hierarchy] = None[source]#

Collection of v30.Hierarchy.

iter_collections()[source]#

Iterate over collections.

metadataflow: DictLikeDescriptor[str, BaseMetadataflow] = None[source]#

Collection of Metadataflow(Definition).

metadatastructure: DictLikeDescriptor[str, BaseMetadataStructureDefinition] = None[source]#

MetadataStructureDefinition <.BaseMetadataStructureDefinition>.

Type:

Collection of

Type:

class

name_personalisation_scheme: DictLikeDescriptor[str, NamePersonalisationScheme] = None[source]#

Collection of NamePersonalisationScheme.

objects(cls)[source]#

Get a reference to the attribute for objects of type cls.

For example, if cls is the class DataStructureDefinition (not an instance), return a reference to structure.

organisation_scheme: DictLikeDescriptor[str, OrganisationScheme] = None[source]#

Collection of OrganisationScheme.

provisionagreement: DictLikeDescriptor[str, ProvisionAgreement] = None[source]#

Collection of ProvisionAgreement.

ruleset_scheme: DictLikeDescriptor[str, RulesetScheme] = None[source]#

Collection of RulesetScheme.

structure: DictLikeDescriptor[str, BaseDataStructureDefinition] = None[source]#

Collection of DataStructureDefinition.

structureset: DictLikeDescriptor[str, StructureSet] = None[source]#

Collection of StructureSet.

transformation_scheme: DictLikeDescriptor[str, TransformationScheme] = None[source]#

Collection of TransformationScheme.

user_defined_operator_scheme: DictLikeDescriptor[str, UserDefinedOperatorScheme] = None[source]#

Collection of UserDefinedOperatorScheme.

valuelist: DictLikeDescriptor[str, ValueList] = None[source]#

Collection of ValueList (SDMX 3.0 only).

vtl_mapping_scheme: DictLikeDescriptor[str, VTLMappingScheme] = None[source]#

Collection of VTLMappingScheme.

rest: SDMX-REST standard#

class sdmx.rest.URL(source: sdmx.source.Source, resource_type: Resource, **kwargs)[source]#

Bases: URL

Utility class to build SDMX-REST API v1.5.0 URLs.

handle_availability()[source]#

Handle URL parameters for availability endpoints.

handle_data()[source]#

Handle URL parameters for data endpoints.

handle_metadata()[source]#

Handle URL parameters for metadata endpoints.

Raises:

NotImplementedError – Although it is described in the standards, there are no known SDMX REST data sources that support this API endpoint to confirm behaviour. If you are aware of one, please open an issue.

handle_registration()[source]#

Handle URL parameters for registration endpoints.

Raises:

ValueError – Always. This type of query is not included in SDMX-REST v1.5.0.

handle_schema()[source]#

Handle URL parameters for schema endpoints.

handle_structure()[source]#

Handle URL parameters for structure endpoints.

Warning

This method currently preserves the behaviour of Client.get() as of sdmx version 2.13.1 and earlier. Namely, defaults are supplied for the ?references=… query parameter in the following cases:

  • Resource.dataflow or Resource.datastructure and a specific resource_id is given (that is, not “all”) → default ?references=all.

  • Resource.categoryscheme → default ?references=parentsandsiblings.

These differ from the SDMX-REST v1.5.0 standard, which states the default should be none in all cases. The sdmx-specific defaults may be deprecated and removed in future versions; to ensure stable behaviour, give the parameter explicitly.

rest.v21#

SDMX-REST API v1.5.0.

Note that version 1.5.0 of the REST API corresponds to version 2.1 of the overall SDMX standards. See the documentation and OpenAPI specification for further details.

sdmx.rest.v21.PARAM: Dict[str, Parameter] = {'component_id': OptionalPath(name='component_id', values=set(), default=None), 'context': PathParameter(name='context', values={'datastructure', 'metadatastructure', 'provisionagreement', 'dataflow', 'metadataflow'}, default=None), 'detail_d': QueryParameter(name='detail', values={'full', 'dataonly', 'serieskeysonly', 'nodata'}, default=None), 'detail_s': QueryParameter(name='detail', values={'full', 'allstubs', 'referencecompletestubs', 'referencepartial', 'allcompletestubs', 'referencestubs'}, default=None), 'end_period': QueryParameter(name='end_period', values=set(), default=None), 'explicit_measure': QueryParameter(name='explicit_measure', values={False, True}, default=None), 'flow': OptionalPath(name='resource_id', values=set(), default=None), 'provider': OptionalPath(name='provider', values=set(), default=None), 'references_s': QueryParameter(name='references', values={<Resource.userdefinedoperatorscheme: 'userdefinedoperatorscheme'>, <Resource.process: 'process'>, <Resource.transformationscheme: 'transformationscheme'>, 'children', 'none', <Resource.metadata: 'metadata'>, <Resource.organisationunitscheme: 'organisationunitscheme'>, <Resource.actualconstraint: 'actualconstraint'>, <Resource.provisionagreement: 'provisionagreement'>, 'all', <Resource.registration: 'registration'>, <Resource.conceptscheme: 'conceptscheme'>, <Resource.metadatastructure: 'metadatastructure'>, <Resource.organisationscheme: 'organisationscheme'>, <Resource.metadataflow: 'metadataflow'>, <Resource.categoryscheme: 'categoryscheme'>, <Resource.structureset: 'structureset'>, <Resource.datastructure: 'datastructure'>, <Resource.customtypescheme: 'customtypescheme'>, <Resource.agencyscheme: 'agencyscheme'>, <Resource.codelist: 'codelist'>, <Resource.data: 'data'>, 'descendants', <Resource.attachementconstraint: 'attachementconstraint'>, 'parents', <Resource.dataconsumerscheme: 'dataconsumerscheme'>, <Resource.dataflow: 'dataflow'>, <Resource.hierarchicalcodelist: 'hierarchicalcodelist'>, <Resource.rulesetscheme: 'rulesetscheme'>, 'parentsandsiblings', <Resource.contentconstraint: 'contentconstraint'>, <Resource.vtlmappingscheme: 'vtlmappingscheme'>, <Resource.dataproviderscheme: 'dataproviderscheme'>, <Resource.availableconstraint: 'availableconstraint'>, <Resource.allowedconstraint: 'allowedconstraint'>, <Resource.categorisation: 'categorisation'>, <Resource.namepersonalisationscheme: 'namepersonalisationscheme'>, <Resource.schema: 'schema'>, <Resource.reportingtaxonomy: 'reportingtaxonomy'>, <Resource.structure: 'structure'>}, default=None), 'start_period': QueryParameter(name='start_period', values=set(), default=None), 'version': PathParameter(name='version', values=set(), default='latest')}[source]#

v1.5.0-specific path and query parameters.

class sdmx.rest.v21.URL(source: sdmx.source.Source, resource_type: Resource, **kwargs)[source]#

Bases: URL

Utility class to build SDMX-REST API v1.5.0 URLs.

handle_availability()[source]#

Handle URL parameters for availability endpoints.

handle_data()[source]#

Handle URL parameters for data endpoints.

handle_metadata()[source]#

Handle URL parameters for metadata endpoints.

Raises:

NotImplementedError

Although it is described in the standards, there are no known SDMX REST data sources that support this API endpoint to confirm behaviour. If you are aware of one, please open an issue.

handle_registration()[source]#

Handle URL parameters for registration endpoints.

Raises:

ValueError – Always. This type of query is not included in SDMX-REST v1.5.0.

handle_schema()[source]#

Handle URL parameters for schema endpoints.

handle_structure()[source]#

Handle URL parameters for structure endpoints.

Warning

This method currently preserves the behaviour of Client.get() as of sdmx version 2.13.1 and earlier. Namely, defaults are supplied for the ?references=… query parameter in the following cases:

  • Resource.dataflow or Resource.datastructure and a specific resource_id is given (that is, not “all”) → default ?references=all.

  • Resource.categoryscheme → default ?references=parentsandsiblings.

These differ from the SDMX-REST v1.5.0 standard, which states the default should be none in all cases. The sdmx-specific defaults may be deprecated and removed in future versions; to ensure stable behaviour, give the parameter explicitly.

rest.v30#

SDMX-REST API v2.1.0.

Note that version 2.1.0 of the REST API corresponds to version 3.0.0 of the overall SDMX standards. See the documentation and OpenAPI specification for further details.

sdmx.rest.v30.PARAM: Dict[str, Parameter] = {'attributes': QueryParameter(name='attributes', values=set(), default=None), 'c': QueryParameter(name='c', values=set(), default=None), 'component_id': PathParameter(name='component_id', values=set(), default=None), 'context': PathParameter(name='context', values={'datastructure', 'metadataprovisionagreement', 'metadatastructure', 'provisionagreement', 'dataflow', 'metadataflow'}, default=None), 'context_d': PathParameter(name='context', values={'provisionagreement', 'datastructure', '*', 'dataflow'}, default='*'), 'detail_m': QueryParameter(name='detail', values={'full', 'allstubs'}, default=None), 'detail_s': QueryParameter(name='detail', values={'full', 'allstubs', 'referencecompletestubs', 'raw', 'referencepartial', 'allcompletestubs', 'referencestubs'}, default=None), 'measures': QueryParameter(name='measures', values=set(), default=None), 'provider_id': PathParameter(name='provider_id', values=set(), default=None), 'references_s': QueryParameter(name='references', values={<Resource.userdefinedoperatorscheme: 'userdefinedoperatorscheme'>, <Resource.process: 'process'>, <Resource.transformationscheme: 'transformationscheme'>, 'children', 'none', <Resource.metadata: 'metadata'>, <Resource.organisationunitscheme: 'organisationunitscheme'>, <Resource.actualconstraint: 'actualconstraint'>, <Resource.provisionagreement: 'provisionagreement'>, 'all', <Resource.registration: 'registration'>, <Resource.conceptscheme: 'conceptscheme'>, <Resource.metadatastructure: 'metadatastructure'>, <Resource.organisationscheme: 'organisationscheme'>, <Resource.metadataflow: 'metadataflow'>, <Resource.categoryscheme: 'categoryscheme'>, <Resource.structureset: 'structureset'>, <Resource.datastructure: 'datastructure'>, <Resource.customtypescheme: 'customtypescheme'>, <Resource.agencyscheme: 'agencyscheme'>, <Resource.codelist: 'codelist'>, <Resource.data: 'data'>, 'descendants', <Resource.attachementconstraint: 'attachementconstraint'>, 'parents', <Resource.dataconsumerscheme: 'dataconsumerscheme'>, <Resource.dataflow: 'dataflow'>, <Resource.hierarchicalcodelist: 'hierarchicalcodelist'>, <Resource.rulesetscheme: 'rulesetscheme'>, 'parentsandsiblings', 'ancestors', <Resource.contentconstraint: 'contentconstraint'>, <Resource.vtlmappingscheme: 'vtlmappingscheme'>, <Resource.dataproviderscheme: 'dataproviderscheme'>, <Resource.availableconstraint: 'availableconstraint'>, <Resource.allowedconstraint: 'allowedconstraint'>, <Resource.categorisation: 'categorisation'>, <Resource.namepersonalisationscheme: 'namepersonalisationscheme'>, <Resource.schema: 'schema'>, <Resource.reportingtaxonomy: 'reportingtaxonomy'>, <Resource.structure: 'structure'>}, default=None), 'updated_after': QueryParameter(name='update_after', values=set(), default=None), 'updated_before': QueryParameter(name='update_before', values=set(), default=None), 'version': PathParameter(name='version', values=set(), default='+')}[source]#

v2.1.0-specific path and query parameters.

class sdmx.rest.v30.URL(source: sdmx.source.Source, resource_type: Resource, **kwargs)[source]#

Bases: URL

Utility class to build SDMX-REST API v2.1.0 URLs.

handle_availability()[source]#

Handle URL parameters for availability endpoints.

handle_data()[source]#

Handle URL parameters for data endpoints.

handle_metadata()[source]#

Handle URL parameters for metadata endpoints.

handle_registration()[source]#

Handle URL parameters for registration endpoints.

handle_structure()[source]#

Handle URL parameters for structure endpoints.

session: HTTP sessions and responses#

class sdmx.session.Session(timeout=30.1, **kwargs)[source]#

requests.Session subclass with optional caching.

If requests_cache is installed, this class inherits from CachedSession and caches responses.

Parameters:
Raises:

TypeError – if requests_cache is not installed and any parameters are passed.

class sdmx.session.ResponseIO(response, tee: IO | os.PathLike | None = None)[source]#

Buffered wrapper for requests.Response with optional file output.

ResponseIO wraps a requests.Response object’s ‘content’ attribute, providing a file-like object from which bytes can be read() incrementally.

Parameters:
  • response (requests.Response) – HTTP response to wrap.

  • tee (binary, writable :py:class:`io.BufferedIOBase`, defaults to io.BytesIO()) – tee is exposed as self.tee and not closed explicitly.

urn: Uniform Resource Names (URNs) for SDMX objects#

sdmx.urn.URN = re.compile('urn:sdmx:org\\.sdmx\\.infomodel\\.(?P<package>[^\\.]*)\\.(?P<class>[^=]*)=((?P<agency>[^:]*):)?(?P<id>[^\\(]*)(\\((?P<version>[\\d\\.]*)\\))?(\\.(?P<item_id>.*))?')[source]#

Regular expression for URNs.

sdmx.urn.make(obj, maintainable_parent=None, strict=False)[source]#

Create an SDMX URN for obj.

If obj is not MaintainableArtefact, then maintainable_parent must be supplied in order to construct the URN.

sdmx.urn.match(value: str) Dict[str, str][source]#

Match URN in value, returning a dict with the match groups.

Utilities and internals#

class sdmx.util.MaybeCachedSession(name, bases, dct)[source]#

Bases: type

Metaclass to inherit from requests_cache.CachedSession, if available.

If requests_cache is not installed, returns requests.Session as a base class.

sdmx.util.compare(attr, a, b, strict: bool) bool[source]#

Return True if a.attr == b.attr.

If strict is False, None is permissible as a or b; otherwise,

sdmx.util.direct_fields(cls) Iterable[Field][source]#

Return the data class fields defined on cls or its class.

This is like the __fields__ attribute, but excludes the fields defined on any parent class(es).

sdmx.util.only(iterator: Iterator) Any[source]#

Return the only element of iterator, or None.

sdmx.util.parse_content_type(value: str) Tuple[str, Dict[str, Any]][source]#

Return content type and parameters from value.

Modified from requests.util.

DictLike collections#

class sdmx.dictlike.DictLike(*args, **kwargs)[source]#

Bases: dict, MutableMapping[KT, VT]

Container with features of dict, attribute access, and validation.

compare(other, strict=True)[source]#

Return True if self is the same as other.

Two DictLike instances are identical if they contain the same set of keys, and corresponding values compare equal.

Parameters:

strict (bool, optional) – Passed to compare() for the values.

copy()[source]#

Return a copy of the DictLike.

update(other)[source]#

Update the DictLike with elements from other, validating entries.

classmethod with_types(key_type, value_type)[source]#

Construct a new DictLike with the given key_type and value_type.

class sdmx.dictlike.DictLikeDescriptor[source]#

Bases: Generic[KT, VT]

Descriptor for DictLike attributes on dataclasses.

sdmx.dictlike.summarize_dictlike(dl, maxwidth=72)[source]#

Return a string summary of the DictLike contents.

Structure expressions in Item descriptions#

Parse structure information from item description.

parse_item_description(item[, locale])

Parse the description of item for a structure expression.

parse_item(itemscheme[, id])

Parse a structure expression for the item in itemscheme with the given id.

parse_all(itemscheme, **kwargs)

Parse structure expressions for every item in itemscheme.

Note

The code in this module does not perform calculations or operations on data using the parsed structure expressions. User code should use the returned information to determine which operations should be performed.

sdmx.util.item_structure.OPS = {'+': <built-in function add>, '-': <built-in function sub>, '=': <built-in function eq>}[source]#

Operators understood by parse_item_description()

sdmx.util.item_structure.parse_all(itemscheme: ItemScheme, **kwargs) Dict[str, List[Tuple[Callable, Item | str]]][source]#

Parse structure expressions for every item in itemscheme.

Parameters:

kwargs – Passed to parse_item_description() via parse_item().

Returns:

Keys are references to the items of itemscheme. Values are the results of parse_item(), i.e. possibly empty.

Return type:

dict of (Item list)

sdmx.util.item_structure.parse_item(itemscheme: ~sdmx.model.common.ItemScheme, id=<class 'str'>, **kwargs) List[Tuple[Callable, Item | str]][source]#

Parse a structure expression for the item in itemscheme with the given id.

In addition to the behaviour of parse_item_description(), parse_item() replaces—where possible—the operand str IDs with references to specific other Items in the itemscheme.

Where not possible, the operand is returned as-is. For example, in an expression like:

A = B + C - D (until 2022) - E

B, C, and E may resolve to references to particular other items, while the string “D (until 2022)” will be returned as-is, and not further parsed as a reference to an item with ID D. (This functionality may be added in a future version of sdmx.)

Parameters:

kwargs – Passed to parse_item_description().

Returns:

Where possible, operand is a reference to an existing item in itemscheme; otherwise the same str as returned by parse_item_description().

Return type:

list of tuple of (operator, operand)

sdmx.util.item_structure.parse_item_description(item: Item, locale: str | None = None) List[Tuple[Callable, str]][source]#

Parse the description of item for a structure expression.

A common—but non-standard—SDMX usage is that Items in ItemSchemes contain structure expressions in their description. These may resemble:

A = B + C - D

…indicating that data for A can be computed by adding data for B and C and subtracting data for D. In this usage, A matches the IdentifiableArtefact.id of the item, and B, C, and D are usually the IDs of other Items in the same ItemScheme.

Another form is:

B + C - D

In this case, the left-hand side and “=” are omitted.

parse_item_description() parses these expressions, returning a list of tuples. In each tuple, the first element gives the operation to be applied (in the above, examples, implicitly add() for “B”), and the second element is the ID of the operand.

Other descriptions are not (yet) supported, including:

  • Multi-line descriptions, in which the structure expression occurs on one line.

  • Structure expressions that appear on lines with other text, for example:

    Some text; A = B + C - D
    
Parameters:

locale (str) – Use this localization of item’s description, instead of the default (DEFAULT_LOCALE or the sole localization).

Returns:

The list is empty if:

  • item has no description.

  • The description does not contain a structure expression.

  • An expression like “A = B + …” exists, but A does not match the ID of item.

Return type:

list of tuple of (operator, str)

Example

>>> from sdmx.model.common import Code
>>> from sdmx.util.item_structure import parse_item_description
>>> c = Code(id="A", description="A = B + C - D")
>>> parse_item_description(c)
[(<function _operator.add(a, b, /)>, 'B'),
 (<function _operator.add(a, b, /)>, 'C'),
 (<function _operator.sub(a, b, /)>, 'D')]