本文整理汇总了Python中string.Formatter.get_value方法的典型用法代码示例。如果您正苦于以下问题:Python Formatter.get_value方法的具体用法?Python Formatter.get_value怎么用?Python Formatter.get_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类string.Formatter
的用法示例。
在下文中一共展示了Formatter.get_value方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwds):
# Let default get_value() handle ints
if not isinstance(key, str):
return Formatter.get_value(self, key, args, kwds)
# HACK, we allow defining strings via fields to allow
# conversions
if key[:2] == 's|':
return key[2:]
if key[:2] == 't|':
# title mode ("TITLE ATTR")
include_title = True
elif key[:2] == 'a|':
# plain attribute mode ("ATTR")
include_title = False
else:
# No special field, have default get_value() get it
return Formatter.get_value(self, key, args, kwds)
key = key[2:]
(title, value) = self._nlattr(key)
if include_title:
if not title:
title = key # fall back to key as title
value = kw(title) + ' ' + value
return value
示例2: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwargs):
if '|' in key:
for kk in key.split('|'):
out = Formatter.get_value(self, kk, args, kwargs)
if out is not None and len(out) > 0:
return out
out = Formatter.get_value(self, key, args, kwargs)
if failOnEmpty and (out is None or len(out) == 0):
raise ValueError("Cannot resolve %s" % key)
return out
示例3: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwds):
if isinstance(key, str):
try:
return kwds[key]
except KeyError:
return self.context[key]
else:
return Formatter.get_value(key, args, kwds)
示例4: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwargs):
try:
return Formatter.get_value(self, key, args, kwargs)
except KeyError:
if self.allow_empty:
logger.debug("Found uninitialized key %s, replaced with empty string", key)
return ""
raise
示例5: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwds):
if isinstance(key, str):
if key:
try:
# Check explicitly passed arguments first
return kwds[key]
except KeyError:
return self.namespace[key]
else:
raise ValueError("zero length field name in format")
else:
return Formatter.get_value(self, key, args, kwds)
示例6: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwargs):
# try regular formatting first:
try:
return Formatter.get_value(self, key, args, kwargs)
except Exception:
pass
# next, look in user_ns and builtins:
for container in (self.shell.user_ns, __builtins__):
if key in container:
return container[key]
# nothing found, put error message in its place
return "<ERROR: '%s' not found>" % key
示例7: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwds):
"""
When values are being swapped in a formatted string, if an exact match,
the key is replaced by the value provided. If no values exist for a
given key, simply return the variable name as its value
Args:
key (str or other): The expected name for the key in the formatted string
args (any): The args to put in place of key
kwds (iterable): key/value pairs
Returns:
str: The format-replaced string with all of the keys replaced
"""
if isinstance(key, str):
try:
return kwds[key]
except KeyError:
return key
else:
Formatter.get_value(key, args, kwds)
示例8: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwargs):
try:
# XXX: .mpe Hack: format OrderedDict as regular dict on variable-expansion
if is_complex(key, args, kwargs):
return json.dumps(kwargs[key])
else:
return Formatter.get_value(self, key, args, kwargs)
except KeyError:
if self.allow_empty:
logger.debug(
'Found uninitialized key %s, replaced with empty string',
key
)
return ''
raise
示例9: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwds):
if isinstance(key, str):
if key:
try:
# Check explicitly passed arguments first
return kwds[key]
except KeyError:
pass
try:
# we deliberately do not call hasattr() first - hasattr()
# silently catches exceptions from properties.
return getattr(self.instance, key)
except AttributeError:
pass
return self.instance[key]
else:
raise ValueError("zero length field name in format")
else:
return Formatter.get_value(self, key, args, kwds)
示例10: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, *args, **kwargs):
try:
return Formatter.get_value(self, *args, **kwargs)
except KeyError:
return ''
示例11: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwds):
if isinstance(key, basestring) and key not in kwds:
return self.default
return Formatter.get_value(self, key, args, kwds)
示例12: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwargs):
# Try standard formatting, then return 'unknown key'
try:
return Formatter.get_value(self, key, args, kwargs)
except KeyError:
return self.default
示例13: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwds):
if isinstance(key, str):
return kwds.get(key, self.default)
else:
Formatter.get_value(key, args, kwds)
示例14: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, key, args, kwargs):
try:
return Formatter.get_value(self, key, args, kwargs)
except KeyError:
return MISSING_KEY
示例15: get_value
# 需要导入模块: from string import Formatter [as 别名]
# 或者: from string.Formatter import get_value [as 别名]
def get_value(self, field_name, args, kwds):
v = Formatter.get_value(self, field_name, args, kwds)
if v is None:
return EmptyNone
return v