本文整理匯總了Python中collections.ChainMap方法的典型用法代碼示例。如果您正苦於以下問題:Python collections.ChainMap方法的具體用法?Python collections.ChainMap怎麽用?Python collections.ChainMap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類collections
的用法示例。
在下文中一共展示了collections.ChainMap方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: substitute
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def substitute(self, *args, **kws):
if len(args) > 1:
raise TypeError('Too many positional arguments')
if not args:
mapping = kws
elif kws:
mapping = ChainMap(kws, args[0])
else:
mapping = args[0]
# Helper function for .sub()
def convert(mo):
# Check the most common path first.
named = mo.group('named') or mo.group('braced')
if named is not None:
val = mapping[named]
# We use this idiom instead of str() because the latter will
# fail if val is a Unicode containing non-ASCII characters.
return '%s' % (val,)
if mo.group('escaped') is not None:
return self.delimiter
if mo.group('invalid') is not None:
self._invalid(mo)
raise ValueError('Unrecognized named group in pattern',
self.pattern)
return self.pattern.sub(convert, self.template)
示例2: _unify_values
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def _unify_values(self, section, vars):
"""Create a sequence of lookups with 'vars' taking priority over
the 'section' which takes priority over the DEFAULTSECT.
"""
sectiondict = {}
try:
sectiondict = self._sections[section]
except KeyError:
if section != self.default_section:
raise NoSectionError(section)
# Update with the entry specific variables
vardict = {}
if vars:
for key, value in vars.items():
if value is not None:
value = str(value)
vardict[self.optionxform(key)] = value
return _ChainMap(vardict, sectiondict, self._defaults)
示例3: count
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def count(start=0, step=1):
"""
``itertools.count`` in Py 2.6 doesn't accept a step
parameter. This is an enhanced version of ``itertools.count``
for Py2.6 equivalent to ``itertools.count`` in Python 2.7+.
"""
while True:
yield start
start += step
########################################################################
### ChainMap (helper for configparser and string.Template)
### From the Py3.4 source code. See also:
### https://github.com/kkxue/Py2ChainMap/blob/master/py2chainmap.py
########################################################################
示例4: __init__
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def __init__(self, **kwargs) -> None:
default_kwargs = {
'id': next(self.discord_id),
'name': 'role',
'position': 1,
'colour': discord.Colour(0xdeadbf),
'permissions': discord.Permissions(),
}
super().__init__(**collections.ChainMap(kwargs, default_kwargs))
if isinstance(self.colour, int):
self.colour = discord.Colour(self.colour)
if isinstance(self.permissions, int):
self.permissions = discord.Permissions(self.permissions)
if 'mention' not in kwargs:
self.mention = f'&{self.name}'
示例5: unwatch_command
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def unwatch_command(self, ctx: Context, user: FetchedMember, *, reason: str) -> None:
"""
Ends the active nomination of the specified user with the given reason.
Providing a `reason` is required.
"""
active_nomination = await self.bot.api_client.get(
self.api_endpoint,
params=ChainMap(
self.api_default_params,
{"user__id": str(user.id)}
)
)
if not active_nomination:
await ctx.send(":x: The specified user does not have an active nomination")
return
[nomination] = active_nomination
await self.bot.api_client.patch(
f"{self.api_endpoint}/{nomination['id']}",
json={'end_reason': reason, 'active': False}
)
await ctx.send(f":white_check_mark: Messages sent by {user} will no longer be relayed")
self._remove_user(user.id)
示例6: safe_substitute
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def safe_substitute(self, *args, **kws):
if len(args) > 1:
raise TypeError('Too many positional arguments')
if not args:
mapping = kws
elif kws:
mapping = ChainMap(kws, args[0])
else:
mapping = args[0]
# Helper function for .sub()
def convert(mo):
named = mo.group('named') or mo.group('braced')
if named is not None:
try:
# We use this idiom instead of str() because the latter
# will fail if val is a Unicode containing non-ASCII
return '%s' % (mapping[named],)
except KeyError:
return mo.group()
if mo.group('escaped') is not None:
return self.delimiter
if mo.group('invalid') is not None:
return mo.group()
raise ValueError('Unrecognized named group in pattern',
self.pattern)
return self.pattern.sub(convert, self.template)
########################################################################
# the Formatter class
# see PEP 3101 for details and purpose of this class
# The hard parts are reused from the C implementation. They're exposed as "_"
# prefixed methods of str.
# The overall parser is implemented in _string.formatter_parser.
# The field name parser is implemented in _string.formatter_field_name_split
示例7: __init__
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def __init__(self, *maps):
'''Initialize a ChainMap by setting *maps* to the given mappings.
If no mappings are provided, a single empty dictionary is used.
'''
self.maps = list(maps) or [{}] # always at least one map
示例8: fromkeys
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def fromkeys(cls, iterable, *args):
'Create a ChainMap with a single dict created from the iterable.'
return cls(dict.fromkeys(iterable, *args))
示例9: copy
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def copy(self):
'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
return self.__class__(self.maps[0].copy(), *self.maps[1:])
示例10: new_child
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def new_child(self, m=None): # like Django's Context.push()
'''
New ChainMap with a new map followed by all previous maps. If no
map is provided, an empty dict is used.
'''
if m is None:
m = {}
return self.__class__(m, *self.maps)
示例11: __init__
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def __init__(self, *maps):
'''Initialize a ChainMap by setting *maps* to the given mappings.
If no mappings are provided, a single empty dictionary is used.
'''
self.maps = list(maps) or [{}] # always at least one map
示例12: fromkeys
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def fromkeys(cls, iterable, *args):
'Create a ChainMap with a single dict created from the iterable.'
return cls(dict.fromkeys(iterable, *args))
示例13: copy
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def copy(self):
'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
return self.__class__(self.maps[0].copy(), *self.maps[1:])
示例14: new_child
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def new_child(self, m=None): # like Django's Context.push()
'''New ChainMap with a new map followed by all previous maps.
If no map is provided, an empty dict is used.
'''
if m is None:
m = {}
return self.__class__(m, *self.maps)
示例15: parents
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import ChainMap [as 別名]
def parents(self): # like Django's Context.pop()
'New ChainMap from maps[1:].'
return self.__class__(*self.maps[1:])