Utility functions¶
-
jsonstat.
from_file
(filename)[source]¶ read a file containing a jsonstat format and return the appropriate object
Parameters: filename – file containing a jsonstat Returns: a JsonStatCollection, JsonStatDataset or JsonStatDimension object example
>>> import os, jsonstat >>> filename = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada-col.json") >>> o = jsonstat.from_file(filename) >>> type(o) <class 'jsonstat.collection.JsonStatCollection'>
-
jsonstat.
from_url
(url, pathname=None)[source]¶ download an url and return the downloaded content.
see
jsonstat.download()
for how to use pathname parameter.Parameters: - url – ex.: http://json-stat.org/samples/oecd-canada.json
- pathname – If
pathname
is defined the contents of the url
will be stored into the file
<cache_dir>/pathname
Ifpathname
is None the filename will be automatic generated. Ifpathname
is an absolute path cache_dir will be ignored.Returns: the contents of url To set dir where to store downloaded file see
jsonstat.cache_dir()
. Cache expiration policy can be customizedexample:
>>> import jsonstat >>> # cache_dir = os.path.normpath(os.path.join(jsonstat.__fixtures_dir, "json-stat.org")) >>> # download external content into the /tmp dir so next downloads can be faster >>> uri = 'http://json-stat.org/samples/oecd-canada.json' >>> jsonstat.cache_dir("/tmp") '/tmp' >>> o = jsonstat.from_url(uri) >>> print(o) JsonstatCollection contains the following JsonStatDataSet: +-----+----------+ | pos | dataset | +-----+----------+ | 0 | 'oecd' | | 1 | 'canada' | +-----+----------+
-
jsonstat.
from_json
(json_data)[source]¶ transform a json structure into jsonstat objects hierarchy
Parameters: json_data – data structure (dictionary) representing a json Returns: a JsonStatCollection, JsonStatDataset or JsonStatDimension object >>> import json, jsonstat >>> from collections import OrderedDict >>> json_string_v1 = '''{ ... "oecd" : { ... "value": [1], ... "dimension" : { ... "id": ["one"], ... "size": [1], ... "one": { "category": { "index":{"2010":0 } } } ... } ... } ... }''' >>> json_data = json.loads(json_string_v1, object_pairs_hook=OrderedDict) >>> jsonstat.from_json(json_data) JsonstatCollection contains the following JsonStatDataSet: +-----+---------+ | pos | dataset | +-----+---------+ | 0 | 'oecd' | +-----+---------+
-
jsonstat.
from_string
(json_string)[source]¶ parse a jsonstat string and return the appropriate object
Parameters: json_string – string containing a json Returns: a JsonStatCollection, JsonStatDataset or JsonStatDimension object
-
jsonstat.
cache_dir
(cached_dir=u'', time_to_live=None)[source]¶ Manage the directory
cached_dir
where downloaded files are storedwithout parameter return the
cached_dir
directory with a parameters set the directoryParameters: - cached_dir –
- time_to_live –
-
jsonstat.
download
(url, pathname=None)[source]¶ download a url and return the downloaded content``
Parameters: - url – ex.: http://json-stat.org/samples/oecd-canada.json
- pathname – If
pathname
is defined the contents of the url
will be stored into the file
<cache_dir>/pathname
Ifpathname
is None the filename will be automatic generated. Ifpathname
is an absolute path cache_dir will be ignored.Returns: the contents of url To set dir where to store downloaded file see
jsonstat.cache_dir()
. Cache expiration policy can be customized