Skip to content

Commit

Permalink
python2/3 compatibility for doctests...
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianvf committed Sep 16, 2015
1 parent 5ad5965 commit 56d85f0
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions scrapi/base/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def CONSTANT(x):
''' Takes a value, returns a function that always returns that value
Useful inside schemas for defining constants
>>> print(CONSTANT('hello')('my', 'name', verb='is'))
hello
>>> print(CONSTANT(['example', 'values'])())
['example', 'values']
>>> CONSTANT(7)('my', 'name', verb='is')
7
>>> CONSTANT([123, 456])()
[123, 456]
'''
def inner(*y, **z):
return x
Expand Down Expand Up @@ -101,8 +101,13 @@ def updated_schema(old, new):
>>> old, new = {'name': 'ric', 'job': None}, {'name': 'Rick'}
>>> updated = updated_schema(old, new)
>>> print(sorted(updated.items())) # Dicts are unsorted, need to sort to test
[('job', None), ('name', 'Rick')]
>>> len(updated.keys())
2
>>> print(updated['name'])
Rick
>>> updated['job'] is None
True
'''
d = deepcopy(old)
for key, value in new.items():
Expand Down Expand Up @@ -291,12 +296,21 @@ def inner(*args, **kwargs):
def coerce_to_list(thing):
''' If a value is not already a list or tuple, puts that value in a length 1 list
>>> print(coerce_to_list('hello'))
['hello']
>>> print(coerce_to_list(['hello']))
['hello']
>>> print(coerce_to_list(('hello', 'goodbye')))
['hello', 'goodbye']
>>> niceties = coerce_to_list('hello')
>>> len(niceties)
1
>>> print(niceties[0])
hello
>>> niceties2 = coerce_to_list(['hello'])
>>> niceties2 == niceties
True
>>> niceties3 = (coerce_to_list(('hello', 'goodbye')))
>>> len(niceties3)
2
>>> print(niceties3[0])
hello
>>> print(niceties3[1])
goodbye
'''
if not (isinstance(thing, list) or isinstance(thing, tuple)):
return [thing]
Expand Down

0 comments on commit 56d85f0

Please sign in to comment.