コード例 #1
0
ファイル: jsonfy.py プロジェクト: gitthabet/MS-BGD
def auto_converter(tt):
    '''Auto convert attempt a number of ways of finding appropriate converters 
    to a unhandled type.
    
    The first strategy is to check if the type is supported in the 
    mod:`pyson.jsonfy_extras` module.
    
    If that is not the case, it searches for converters for the superclasses
    of `tt`.
    
    Finally, if that fails an error is raised.
    '''

    # Tries to import a converter from jsonfy_ext.* modules
    name = type2name(tt)
    try:
        load_modules('pyson.jsonfy_extras.ext_' + name)
    except AttributeError:
        pass
    try:
        return CONVERTERS[tt]
    except KeyError:
        pass

    # Tries to use a converter for a base class (hint: this often fails!)
    for base_t in tt.mro()[:-1]:
        try:
            return CONVERTERS[base_t]
        except KeyError:
            pass
    else:
        raise TypeError('object of type %s is not supported' % tt)
        def not_supported(obj):
            raise TypeError('object of type %s is not supported' % tt)
        return not_supported
コード例 #2
0
ファイル: unjsonfy.py プロジェクト: jludac/MS-BGD
def unjsonfy(json, tt=None, recursive=True, inplace=False, lazy=True):
    """Create object from its JSON representation
    
    Parameters
    ----------
    json : JSON-like
        JSON-like structure to be converted to a Python object.
    tt (optional): type
        Type of the output object (can be inferred from the '@type' key, if 
        ``json`` has it. 
    recursive : bool
        If False, prevents recursive application of func:`jsonfy` to child nodes 
        of the JSON input.
    """

    if tt is None:
        try:
            tt = json[u"@type"]
        except:
            if not lazy:
                raise ValueError("'tt' must be set explicity if 'json' does not have a '@type' key")
            else:
                tt = None
        else:
            tt = name2type(tt)

    # TODO: move 'inplace' and 'recursive' to the converter function
    if not inplace:
        json = deepcopy(json)
    if recursive:
        if isinstance(json, dict):
            for k, v in json.items():
                json[k] = unjsonfy(v, recursive=True, inplace=True)
        elif isinstance(json, list):
            for idx, v in enumerate(json):
                json[idx] = unjsonfy(v, recursive=True, inplace=True)

    # Compute the result from the converter function
    try:
        converter = CONVERTERS[tt]
    except KeyError:
        # Tries to load converters from unjsonfy_extras
        try:
            load_modules("pyson.unjsonfy_extras.ext_" + type2name(tt))
        except AttributeError:
            pass

        try:
            converter = CONVERTERS[tt]
        except KeyError:
            if tt is None:
                return json
            raise ValueError("there is no known converter to type %s" % tt)

    return converter(json)
コード例 #3
0
def unjsonfy(json, tt=None, recursive=True, inplace=False, lazy=True):
    '''Create object from its JSON representation
    
    Parameters
    ----------
    json : JSON-like
        JSON-like structure to be converted to a Python object.
    tt (optional): type
        Type of the output object (can be inferred from the '@type' key, if 
        ``json`` has it. 
    recursive : bool
        If False, prevents recursive application of func:`jsonfy` to child nodes 
        of the JSON input.
    '''

    if tt is None:
        try:
            tt = json[u'@type']
        except:
            if not lazy:
                raise ValueError("'tt' must be set explicity if 'json' does not have a '@type' key")
            else:
                tt = None
        else:
            tt = name2type(tt)

    #TODO: move 'inplace' and 'recursive' to the converter function 
    if not inplace:
        json = deepcopy(json)
    if recursive:
        if isinstance(json, dict):
            for k, v in json.items():
                json[k] = unjsonfy(v, recursive=True, inplace=True)
        elif isinstance(json, list):
            for idx, v in enumerate(json):
                json[idx] = unjsonfy(v, recursive=True, inplace=True)

    # Compute the result from the converter function
    try:
        converter = CONVERTERS[tt]
    except KeyError:
        # Tries to load converters from unjsonfy_extras
        try: load_modules('pyson.unjsonfy_extras.ext_' + type2name(tt))
        except AttributeError: pass

        try:
            converter = CONVERTERS[tt]
        except KeyError:
            if tt is None:
                return json
            raise ValueError('there is no known converter to type %s' % tt)

    return converter(json)
コード例 #4
0
def auto_converter(tt):
    '''Auto convert attempt a number of ways of finding appropriate converters 
    to a unhandled type.
    
    The first strategy is to check if the type is supported in the 
    mod:`pyson.jsonfy_extras` module.
    
    If that is not the case, it searches for converters for the superclasses
    of `tt`.
    
    Finally, if that fails an error is raised.
    '''

    # Tries to import a converter from jsonfy_ext.* modules
    name = type2name(tt)
    try:
        load_modules('pyson.jsonfy_extras.ext_' + name)
    except AttributeError:
        pass
    try:
        return CONVERTERS[tt]
    except KeyError:
        pass

    # Tries to use a converter for a base class (hint: this often fails!)
    for base_t in tt.mro()[:-1]:
        try:
            return CONVERTERS[base_t]
        except KeyError:
            pass
    else:
        raise TypeError('object of type %s is not supported' % tt)

        def not_supported(obj):
            raise TypeError('object of type %s is not supported' % tt)

        return not_supported