Пример #1
0
 def __init__(self, cache, http_cfg):
     default_cfg = dict(stream=True, timeout=30.1)
     for it in default_cfg.items():
         http_cfg.setdefault(*it)
     self.config = DictLike(http_cfg)
     if cache:
         requests_cache.install_cache(**cache)
Пример #2
0
 def international_str(self, name, sdmxobj):
     '''
     return DictLike of xml:lang attributes. If node has no attributes,
     assume that language is 'en'.
     '''
     # Get language tokens like 'en', 'fr'...
     elem_attrib = self._paths['int_str_names'](sdmxobj._elem, name=name)
     values = self._paths['int_str_values'](sdmxobj._elem, name=name)
     # Unilingual strings have no attributes. Assume 'en' instead.
     if not elem_attrib:
         elem_attrib = ['en']
     return DictLike(zip(elem_attrib, values))
Пример #3
0
    def write(self, source=None, rows=None, **kwargs):
        '''
        Transfform structural metadata, i.e. codelists, concept-schemes,
        lists of dataflow definitions or category-schemes  
        from a :class:`pandasdmx.model.StructureMessage` instance into a pandas DataFrame.
        This method is called by :meth:`pandasdmx.api.Response.write` . It is not
        part of the public-facing API. Yet, certain kwargs are 
        propagated from there.

        Args:
            source(pandasdmx.model.StructureMessage): a :class:`pandasdmx.model.StructureMessage` instance.

            rows(str): sets the desired content 
                to be extracted from the StructureMessage.
                Must be a name of an attribute of the StructureMessage. The attribute must
                be an instance of `dict` whose keys are strings. These will be
                interpreted as ID's and used for the MultiIndex of the DataFrame
                to be returned. Values can be either instances of `dict` such as for codelists and categoryscheme, 
                or simple nameable objects
                such as for dataflows. In the latter case, the DataFrame will have a flat index.  
                (default: depends on content found in Message. 
                Common is 'codelist')
            columns(str, list): if str, it denotes the attribute of attributes of the
                values (nameable SDMX objects such as Code or ConceptScheme) that will be stored in the
                DataFrame. If a list, it must contain strings
                that are valid attibute values. Defaults to: ['name', 'description']
            lang(str): locale identifier. Specifies the preferred 
                language for international strings such as names.
                Default is 'en'.
        '''

        # Set convenient default values for args
        # is rows a string?
        if rows is not None and not isinstance(rows, (list, tuple)):
            rows = [rows]
            return_df = True
        elif isinstance(rows, (list, tuple)) and len(rows) == 1:
            return_df = True
        else:
            return_df = False
        if rows is None:
            rows = [i for i in self._row_content if hasattr(source, i)]
        # Generate the DataFrame or -Frames and store them in a DictLike with
        # content-type names as keys
        frames = DictLike(
            {r: self._make_dataframe(source, r, **kwargs) for r in rows})
        if return_df:
            # There is only one item. So return the only value.
            return frames.any()
        else:
            return frames
Пример #4
0
 def read_identifiables(self, cls,  sdmxobj, offset=None):
     '''
     If sdmxobj inherits from dict: update it  with modelized elements.
     These must be instances of model.IdentifiableArtefact,
     i.e. have an 'id' attribute. This will be used as dict keys.
     If sdmxobj does not inherit from dict: return a new DictLike.
     '''
     path = self._paths[cls]
     if offset:
         try:
             base = self._paths[offset](sdmxobj._elem)[0]
         except IndexError:
             return None
     else:
         base = sdmxobj._elem
     result = {e.get('id'): cls(self, e) for e in path(base)}
     if isinstance(sdmxobj, dict):
         sdmxobj.update(result)
     else:
         return DictLike(result)
Пример #5
0
 def header_error(self, sdmxobj):
     try:
         return DictLike(sdmxobj._elem.Error.attrib)
     except AttributeError:
         return None