def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
python类MutableMapping()的实例源码
def view(tpl_name, **defaults):
""" Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
def flatten_dict(d, parent_key='', sep='_'):
"""Flattens a nested dictionary.
:param d: A nested dictionary
:type d: dict or MutableMapping
:param str parent_key: The parent's key. This is a value for tail recursion, so don't set it yourself.
:param str sep: The separator used between dictionary levels
.. seealso:: http://stackoverflow.com/a/6027615
"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, (dict, MutableMapping)):
items.extend(flatten_dict(v, new_key, sep=sep).items())
elif isinstance(v, (set, list)):
items.append((new_key, ','.join(v)))
else:
items.append((new_key, v))
return dict(items)
def rec_merge(d1, d2):
"""
Update two dicts of dicts recursively,
if either mapping has leaves that are non-dicts,
the second's leaf overwrites the first's.
"""
# in Python 2, use .iteritems()!
for k, v in d1.items():
if k in d2:
# this next check is the only difference!
if all(isinstance(e, MutableMapping) for e in (v, d2[k])):
d2[k] = rec_merge(v, d2[k])
if isinstance(v, list):
d2[k].extend(v)
# we could further check types and merge as appropriate here.
d3 = d1.copy()
d3.update(d2)
return d3
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
def _flatten_dict(dict_, parent_key="", sep="."):
"""Flattens a nested dictionary. Namedtuples within
the dictionary are converted to dicts.
Args:
dict_: The dictionary to flatten.
parent_key: A prefix to prepend to each key.
sep: Separator between parent and child keys, a string. For example
{ "a": { "b": 3 } } will become { "a.b": 3 } if the separator is ".".
Returns:
A new flattened dictionary.
"""
items = []
for key, value in dict_.items():
new_key = parent_key + sep + key if parent_key else key
if isinstance(value, collections.MutableMapping):
items.extend(_flatten_dict(value, new_key, sep=sep).items())
elif isinstance(value, tuple) and hasattr(value, "_asdict"):
dict_items = collections.OrderedDict(zip(value._fields, value))
items.extend(_flatten_dict(dict_items, new_key, sep=sep).items())
else:
items.append((new_key, value))
return dict(items)
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
return result
return wrapper
return decorator
def merge_dicts(d1: dict, d2: dict) -> dict:
"""
Update two dicts of dicts recursively,
if either mapping has leaves that are non-dicts,
the second's leaf overwrites the first's.
"""
# in Python 2, use .iteritems()!
for k, v in d1.items():
if k in d2:
# this next check is the only difference!
if all(isinstance(e, MutableMapping) for e in (v, d2[k])):
d2[k] = merge_dicts(v, d2[k])
if isinstance(v, list):
d2[k].extend(v)
# we could further check types and merge as appropriate here.
d3 = d1.copy()
d3.update(d2)
return d3
def preprocess_vars(a):
'''
Ensures that vars contained in the parameter passed in are
returned as a list of dictionaries, to ensure for instance
that vars loaded from a file conform to an expected state.
'''
if a is None:
return None
elif not isinstance(a, list):
data = [ a ]
else:
data = a
for item in data:
if not isinstance(item, MutableMapping):
raise AnsibleError("variable files must contain either a dictionary of variables, or a list of dictionaries. Got: %s (%s)" % (a, type(a)))
return data
def _preprocess_vars(self, a):
'''
Ensures that vars contained in the parameter passed in are
returned as a list of dictionaries, to ensure for instance
that vars loaded from a file conform to an expected state.
'''
if a is None:
return None
elif not isinstance(a, list):
data = [ a ]
else:
data = a
for item in data:
if not isinstance(item, MutableMapping):
raise AnsibleError("variable files must contain either a dictionary of variables, or a list of dictionaries. Got: %s (%s)" % (a, type(a)))
return data
def _validate_mutable_mappings(a, b):
"""
Internal convenience function to ensure arguments are MutableMappings
This checks that all arguments are MutableMappings or raises an error
:raises AnsibleError: if one of the arguments is not a MutableMapping
"""
# If this becomes generally needed, change the signature to operate on
# a variable number of arguments instead.
if not (isinstance(a, MutableMapping) and isinstance(b, MutableMapping)):
myvars = []
for x in [a, b]:
try:
myvars.append(dumps(x))
except:
myvars.append(to_native(x))
raise AnsibleError("failed to combine variables, expected dicts but got a '{0}' and a '{1}': \n{2}\n{3}".format(
a.__class__.__name__, b.__class__.__name__, myvars[0], myvars[1])
)
def merge_dict(target, *sources):
"""
Merge the given list of `Mapping` objects into `target` object.
:param MutableMapping target: The mapping to receive the merge.
:param tuple sources: The list of `mapping` objects to be merged.
"""
for source in sources:
if not isinstance(target, MutableMapping):
raise TypeError('target must be a dict')
if not isinstance(source, Mapping):
raise TypeError('data must be a dict')
for key in source:
target_value = target.get(key)
source_value = source[key]
if target_value is None or source_value is None:
target[key] = source_value
elif isinstance(target_value, Mapping) and isinstance(source_value, Mapping):
merge_dict(target_value, source_value)
else:
target[key] = source_value
def _to_dict(self, o):
"""
Convert a given object to dict.
:param o: The object to be converted.
:return dict: The dict value converted.
"""
if isinstance(o, MutableMapping):
return o
if not isinstance(o, string_types):
raise TypeError('Expected str, got %s' % text_type(type(o)))
pairs = o.split(self.dict_delimiter)
d = IgnoreCaseDict()
for pair in pairs:
kv = pair.split('=')
key = kv[0].strip()
value = kv[1].strip()
d[key] = value
return d
def __init__(self, func, items, cache=True):
"""Applies the given function on the given items at the moment of data request.
On the moment one of the keys of this dict class is requested we apply the given function on the given items
and return the result of that function. The advantage of this class is that it defers an expensive operation
until it is needed.
Items added to this dictionary after creation are assumed to be final, that is, we won't run the
function on them.
Args:
func (Function): the callback function to apply on the given items at request, with signature:
.. code-block:: python
def callback(key, value)
items (collections.MutableMapping): the items on which we operate
cache (boolean): if we want to cache computed results
"""
self._func = func
self._items = copy.copy(items)
self._applied_on_key = {}
self._cache = cache
def __init__(self, items, cache=True):
"""The items should contain a list of functions that we apply at the moment of request.
On the moment one of the keys of this dict class is requested we apply the function stored in the items dict
for that key and return the result of that function.
The advantage of this class is that it defers an expensive operation until it is needed.
Items set to this dictionary are assumed to be final, that is, we won't run the function on them.
Args:
items (collections.MutableMapping): the items on which we operate, each value should
contain a function with no parameters that we run to return the results.
cache (boolean): if we want to cache computed results
"""
self._items = copy.copy(items)
self._applied_on_key = {}
self._cache = cache
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
decoder_util.py 文件源码
项目:tensorflow_end2end_speech_recognition
作者: hirofumi0810
项目源码
文件源码
阅读 93
收藏 0
点赞 0
评论 0
def _flatten_dict(dict_, parent_key="", sep="."):
"""Flattens a nested dictionary. Namedtuples within
the dictionary are converted to dicts.
Args:
dict_: The dictionary to flatten.
parent_key: A prefix to prepend to each key.
sep: Separator between parent and child keys, a string. For example
{ "a": { "b": 3 } } will become { "a.b": 3 } if the separator
is ".".
Returns:
A new flattened dictionary.
"""
items = []
for key, value in dict_.items():
new_key = parent_key + sep + key if parent_key else key
if isinstance(value, collections.MutableMapping):
items.extend(_flatten_dict(value, new_key, sep=sep).items())
elif isinstance(value, tuple) and hasattr(value, "_asdict"):
dict_items = collections.OrderedDict(zip(value._fields, value))
items.extend(_flatten_dict(
dict_items, new_key, sep=sep).items())
else:
items.append((new_key, value))
return dict(items)
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
def transform_incoming(self, son, collection):
"""Replace embedded documents with DBRefs.
"""
def transform_value(value):
if isinstance(value, collections.MutableMapping):
if "_id" in value and "_ns" in value:
return DBRef(value["_ns"], transform_value(value["_id"]))
else:
return transform_dict(SON(value))
elif isinstance(value, list):
return [transform_value(v) for v in value]
return value
def transform_dict(object):
for (key, value) in object.items():
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
def transform_outgoing(self, son, collection):
"""Replace DBRefs with embedded documents.
"""
def transform_value(value):
if isinstance(value, DBRef):
return self.database.dereference(value)
elif isinstance(value, list):
return [transform_value(v) for v in value]
elif isinstance(value, collections.MutableMapping):
return transform_dict(SON(value))
return value
def transform_dict(object):
for (key, value) in object.items():
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
# TODO make a generic translator for custom types. Take encode, decode,
# should_encode and should_decode functions and just encode and decode where
# necessary. See examples/custom_type.py for where this would be useful.
# Alternatively it could take a should_encode, to_binary, from_binary and
# binary subtype.
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator
def get_obj(value, actual_type):
actual_type = str_to_class(actual_type.decode('utf-8'))
if py3:
if actual_type is not bytes:
value = value.decode('utf-8')
else:
if actual_type is unicode:
value = value.decode('utf-8')
if actual_type in {Decimal, complex}:
value = actual_type(value)
elif actual_type is datetime.datetime:
value = datetime.datetime.strptime(value, DATETIME_FORMAT)
elif actual_type is datetime.date:
value = datetime.datetime.strptime(value, DATE_FORMAT).date()
elif actual_type in {dict, list} or isinstance(actual_type, (MutableMapping, Iterable)):
value = json.loads(value)
else:
value = actual_type(value)
return value
def __save_in_redis(self, path, value):
if isinstance(value, strings):
value = self.doformat(value)
self.red.set(path, value)
elif isinstance(value, sets):
value = self.doformat(value)
self.red.delete(path)
self.red.sadd(path, *value)
elif isinstance(value, numbers):
value = self.doformat(value)
self.red.set(path, value)
elif isinstance(value, MutableMapping):
value = self.doformat(value)
self.red.hmset(path, value)
elif isinstance(value, Iterable):
value = self.doformat(value)
self.red.delete(path)
self.red.rpush(path, *value)
else:
value = self.doformat(value, the_type="obj")
self.red.set(path, value)
def view(tpl_name, **defaults):
''' Decorator: renders a template for a handler.
The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not
process the template, but return the handler result as is.
This includes returning a HTTPResponse(dict) to get,
for instance, JSON with autojson or other castfilters.
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(tpl_name, **tplvars)
elif result is None:
return template(tpl_name, defaults)
return result
return wrapper
return decorator