Skip to content

Commit f8ff7db

Browse files
Joe Jevnikllllllllll
authored andcommitted
MAINT: move some string formatting tools out of testing
1 parent 98221e8 commit f8ff7db

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

zipline/testing/predicates.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from zipline.lib.labelarray import LabelArray
5252
from zipline.testing.core import ensure_doctest
5353
from zipline.utils.compat import getargspec, mappingproxy
54+
from zipline.utils.formatting import s
5455
from zipline.utils.functional import dzip_exact, instance
5556
from zipline.utils.math_utils import tolerant_equals
5657

@@ -175,25 +176,6 @@ def filter_kwargs(f, kwargs):
175176
return keyfilter(op.contains(keywords(f)), kwargs)
176177

177178

178-
def _s(word, seq, suffix='s'):
179-
"""Adds a suffix to ``word`` if some sequence has anything other than
180-
exactly one element.
181-
182-
word : str
183-
The string to add the suffix to.
184-
seq : sequence
185-
The sequence to check the length of.
186-
suffix : str, optional.
187-
The suffix to add to ``word``
188-
189-
Returns
190-
-------
191-
maybe_plural : str
192-
``word`` with ``suffix`` added if ``len(seq) != 1``.
193-
"""
194-
return word + (suffix if len(seq) != 1 else '')
195-
196-
197179
def _fmt_path(path):
198180
"""Format the path for final display.
199181
@@ -432,17 +414,17 @@ def _check_sets(result, expected, msg, path, type_):
432414
if result != expected:
433415
if result > expected:
434416
diff = result - expected
435-
msg = 'extra %s in result: %r' % (_s(type_, diff), diff)
417+
msg = 'extra %s in result: %r' % (s(type_, diff), diff)
436418
elif result < expected:
437419
diff = expected - result
438-
msg = 'result is missing %s: %r' % (_s(type_, diff), diff)
420+
msg = 'result is missing %s: %r' % (s(type_, diff), diff)
439421
else:
440422
in_result = result - expected
441423
in_expected = expected - result
442424
msg = '%s only in result: %s\n%s only in expected: %s' % (
443-
_s(type_, in_result),
425+
s(type_, in_result),
444426
in_result,
445-
_s(type_, in_expected),
427+
s(type_, in_expected),
446428
in_expected,
447429
)
448430
raise AssertionError(

zipline/utils/formatting.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
def s(word, seq, suffix='s'):
2+
"""Adds a suffix to ``word`` if some sequence has anything other than
3+
exactly one element.
4+
5+
Parameters
6+
----------
7+
word : str
8+
The string to add the suffix to.
9+
seq : sequence
10+
The sequence to check the length of.
11+
suffix : str, optional.
12+
The suffix to add to ``word``
13+
14+
Returns
15+
-------
16+
maybe_plural : str
17+
``word`` with ``suffix`` added if ``len(seq) != 1``.
18+
"""
19+
if len(seq) == 1:
20+
return word
21+
22+
return word + suffix
23+
24+
25+
def plural(singular, plural, seq):
26+
"""Selects a singular or plural word based on the length of a sequence.
27+
28+
Parameters
29+
----------
30+
singlular : str
31+
The string to use when ``len(seq) == 1``.
32+
plural : str
33+
The string to use when ``len(seq) != 1``.
34+
seq : sequence
35+
The sequence to check the length of.
36+
37+
Returns
38+
-------
39+
maybe_plural : str
40+
Either ``singlular`` or ``plural``.
41+
"""
42+
if len(seq) == 1:
43+
return singular
44+
45+
return plural

0 commit comments

Comments
 (0)