Python UserDict 模块,DictMixin() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用UserDict.DictMixin()。
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,JSON with autojson or other castfilters.
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, **tplvars)
return result
return wrapper
return decorator
def view(tpl_name, **tplvars)
return result
return wrapper
return decorator
def __setattr__(self, key, value):
if key in ('_config', '_prefix'):
self.__dict__[key] = value
return
depr('Attribute assignment is deprecated.') #0.12
if hasattr(DictMixin, key):
raise AttributeError('Read-only attribute.')
if key in self and self[key] and isinstance(self[key], self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def view(tpl_name,JSON with autojson or other castfilters.
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def visit_column(self, delta):
if isinstance(delta, DictMixin):
column = delta.result_column
table = self._to_table(delta.table)
else:
column = delta
table = self._to_table(column.table)
self.recreate_table(table,column,delta)
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def __setattr__(self, self.__class__):
raise AttributeError('Non-empty namespace attribute.')
self[key] = value
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def _to_index(index, table=None, engine=None):
"""Return if instance of Index,else construct new with Metadata"""
if isinstance(index, sqlalchemy.Index):
return index
# Given: index name; table name required
table = _to_table(table, engine)
ret = sqlalchemy.Index(index)
ret.table = table
return ret
# python3: if we just use:
#
# class ColumnDelta(DictMixin,sqlalchemy.schema.SchemaItem):
# ...
#
# We get the following error:
# TypeError: Metaclass conflict: the Metaclass of a derived class must be a
# (non-strict) subclass of the Metaclasses of all its bases.
#
# The complete inheritance/Metaclass relationship list of ColumnDelta can be
# summarized by this following dot file:
#
# digraph test123 {
# ColumnDelta -> MutableMapping;
# MutableMapping -> Mapping;
# Mapping -> {Sized Iterable Container};
# {Sized Iterable Container} -> ABCMeta[style=dashed];
#
# ColumnDelta -> SchemaItem;
# SchemaItem -> {SchemaEventTarget Visitable};
# SchemaEventTarget -> object;
# Visitable -> {VisitableType object} [style=dashed];
# VisitableType -> type;
# }
#
# We need to use a Metaclass that inherits from all the Metaclasses of
# DictMixin and sqlalchemy.schema.SchemaItem. Let's call it "MyMeta".
def view(tpl_name, defaults)
return result
return wrapper
return decorator
def lookup(self, hostname):
"""
Find a hostkey entry for a given hostname or IP. If no entry is found,
C{None} is returned. Otherwise a dictionary of keytype to key is
returned. The keytype will be either C{"ssh-rsa"} or C{"ssh-dss"}.
@param hostname: the hostname (or IP) to lookup
@type hostname: str
@return: keys associated with this host (or C{None})
@rtype: dict(str,L{PKey})
"""
class SubDict (UserDict.DictMixin):
def __init__(self, hostname, entries, hostkeys):
self._hostname = hostname
self._entries = entries
self._hostkeys = hostkeys
def __getitem__(self, key):
for e in self._entries:
if e.key.get_name() == key:
return e.key
raise KeyError(key)
def __setitem__(self, val):
for e in self._entries:
if e.key is None:
continue
if e.key.get_name() == key:
# replace
e.key = val
break
else:
# add a new one
e = HostKeyEntry([hostname], val)
self._entries.append(e)
self._hostkeys._entries.append(e)
def keys(self):
return [e.key.get_name() for e in self._entries if e.key is not None]
entries = []
for e in self._entries:
for h in e.hostnames:
if (h.startswith('|1|') and (self.hash_host(hostname, h) == h)) or (h == hostname):
entries.append(e)
if len(entries) == 0:
return None
return SubDict(hostname, self)
def lookup(self,
``None`` is returned. Otherwise a dictionary of keytype to key is
returned. The keytype will be either ``"ssh-rsa"`` or ``"ssh-dss"``.
:param str hostname: the hostname (or IP) to lookup
:return: dict of `str` -> `.PKey` keys associated with this host (or ``None``)
"""
class SubDict (MutableMapping):
def __init__(self, hostkeys):
self._hostname = hostname
self._entries = entries
self._hostkeys = hostkeys
def __iter__(self):
for k in self.keys():
yield k
def __len__(self):
return len(self.keys())
def __delitem__(self, key):
for e in list(self._entries):
if e.key.get_name() == key:
self._entries.remove(e)
else:
raise KeyError(key)
def __getitem__(self, val)
self._entries.append(e)
self._hostkeys._entries.append(e)
def keys(self):
return [e.key.get_name() for e in self._entries if e.key is not None]
entries = []
for e in self._entries:
for h in e.hostnames:
if h.startswith('|1|') and not hostname.startswith('|1|') and constant_time_bytes_eq(self.hash_host(hostname, h), h) or h == hostname:
entries.append(e)
if len(entries) == 0:
return None
return SubDict(hostname, self)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。