mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-10-06 11:57:29 +02:00
Update utils.str.format to support specifying an 'and' string.
This commit is contained in:
parent
28cb5abc65
commit
9099a1a934
@ -127,7 +127,7 @@ def _getSep(s):
|
|||||||
def _getSplitterRe(s):
|
def _getSplitterRe(s):
|
||||||
separator = _getSep(s)
|
separator = _getSep(s)
|
||||||
return re.compile(r'(?<!\\)%s' % re.escape(separator))
|
return re.compile(r'(?<!\\)%s' % re.escape(separator))
|
||||||
|
|
||||||
def perlReToPythonRe(s):
|
def perlReToPythonRe(s):
|
||||||
"""Converts a string representation of a Perl regular expression (i.e.,
|
"""Converts a string representation of a Perl regular expression (i.e.,
|
||||||
m/^foo$/i or /foo|bar/) to a Python regular expression.
|
m/^foo$/i or /foo|bar/) to a Python regular expression.
|
||||||
@ -342,7 +342,7 @@ def format(s, *args, **kwargs):
|
|||||||
f: float
|
f: float
|
||||||
b: form of the verb 'to be' (takes an int)
|
b: form of the verb 'to be' (takes an int)
|
||||||
h: form of the verb 'to have' (takes an int)
|
h: form of the verb 'to have' (takes an int)
|
||||||
L: commaAndify (takes a list of strings)
|
L: commaAndify (takes a list of strings or a tuple of ([strings], and))
|
||||||
p: pluralize (takes a string)
|
p: pluralize (takes a string)
|
||||||
q: quoted (takes a string)
|
q: quoted (takes a string)
|
||||||
n: nItems (takes a 2-tuple of (n, item) or a 3-tuple of (n, between, item))
|
n: nItems (takes a 2-tuple of (n, item) or a 3-tuple of (n, between, item))
|
||||||
@ -365,7 +365,17 @@ def format(s, *args, **kwargs):
|
|||||||
elif char == 'h':
|
elif char == 'h':
|
||||||
return has(args.pop())
|
return has(args.pop())
|
||||||
elif char == 'L':
|
elif char == 'L':
|
||||||
return commaAndify(args.pop())
|
t = args.pop()
|
||||||
|
if isinstance(t, list):
|
||||||
|
return commaAndify(t)
|
||||||
|
elif isinstance(t, tuple) and len(t) == 2:
|
||||||
|
if not isinstance(t[0], list):
|
||||||
|
raise ValueError, 'Invalid list for %%L in format: %s' % t
|
||||||
|
if not isinstance(t[1], basestring):
|
||||||
|
raise ValueError, 'Invalid string for %%L in format: %s' % t
|
||||||
|
return commaAndify(t[0], And=t[1])
|
||||||
|
else:
|
||||||
|
raise ValueError, 'Invalid value for %%L in format: %s' % t
|
||||||
elif char == 'p':
|
elif char == 'p':
|
||||||
return pluralize(args.pop())
|
return pluralize(args.pop())
|
||||||
elif char == 'q':
|
elif char == 'q':
|
||||||
@ -390,5 +400,5 @@ def format(s, *args, **kwargs):
|
|||||||
return _formatRe.sub(sub, s)
|
return _formatRe.sub(sub, s)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise ValueError, 'Extra format chars in format spec: %r' % s
|
raise ValueError, 'Extra format chars in format spec: %r' % s
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user