mirror of https://github.com/mahmoud/boltons.git
* Adding lstrip, rstrip, strip functions * Adding tests for lstrip, rstrip, and strip * Adding strip to documenation. Fixing python2 compatibility * Fixing strip doc test * Fixing another strip doc test * Adding additional testing for lstrip, rstrip, and strip. Removing unnecessary import of sys
This commit is contained in:
parent
3d55169c27
commit
e79511e6e7
|
@ -177,6 +177,100 @@ def split_iter(src, sep=None, maxsplit=None):
|
|||
return
|
||||
|
||||
|
||||
def lstrip(iterable, strip_value=None):
|
||||
"""Strips values from the beginning of an iterable. Stripped items will
|
||||
match the value of the argument strip_value. Functionality is analigous
|
||||
to that of the method str.lstrip. Returns a list.
|
||||
|
||||
>>> lstrip(['Foo', 'Bar', 'Bam'], 'Foo')
|
||||
['Bar', 'Bam']
|
||||
|
||||
"""
|
||||
return list(lstrip_iter(iterable, strip_value))
|
||||
|
||||
|
||||
def lstrip_iter(iterable, strip_value=None):
|
||||
"""Strips values from the beginning of an iterable. Stripped items will
|
||||
match the value of the argument strip_value. Functionality is analigous
|
||||
to that of the method str.lstrip. Returns a generator.
|
||||
|
||||
>>> list(lstrip_iter(['Foo', 'Bar', 'Bam'], 'Foo'))
|
||||
['Bar', 'Bam']
|
||||
|
||||
"""
|
||||
iterator = iter(iterable)
|
||||
for i in iterator:
|
||||
if i != strip_value:
|
||||
yield i
|
||||
break
|
||||
for i in iterator:
|
||||
yield i
|
||||
|
||||
|
||||
def rstrip(iterable, strip_value=None):
|
||||
"""Strips values from the end of an iterable. Stripped items will
|
||||
match the value of the argument strip_value. Functionality is analigous
|
||||
to that of the method str.rstrip. Returns a list.
|
||||
|
||||
>>> rstrip(['Foo', 'Bar', 'Bam'], 'Bam')
|
||||
['Foo', 'Bar']
|
||||
|
||||
"""
|
||||
return list(rstrip_iter(iterable,strip_value))
|
||||
|
||||
|
||||
def rstrip_iter(iterable, strip_value=None):
|
||||
"""Strips values from the end of an iterable. Stripped items will
|
||||
match the value of the argument strip_value. Functionality is analigous
|
||||
to that of the method str.rstrip. Returns a generator.
|
||||
|
||||
>>> list(rstrip_iter(['Foo', 'Bar', 'Bam'], 'Bam'))
|
||||
['Foo', 'Bar']
|
||||
|
||||
"""
|
||||
iterator = iter(iterable)
|
||||
for i in iterator:
|
||||
if i == strip_value:
|
||||
cache = list()
|
||||
cache.append(i)
|
||||
broken = False
|
||||
for i in iterator:
|
||||
if i == strip_value:
|
||||
cache.append(i)
|
||||
else:
|
||||
broken = True
|
||||
break
|
||||
if not broken: # Return to caller here because the end of the
|
||||
return # iterator has been reached
|
||||
for t in cache:
|
||||
yield t
|
||||
yield i
|
||||
|
||||
|
||||
def strip(iterable, strip_value=None):
|
||||
"""Strips values from the beginning and end of an iterable. Stripped items
|
||||
will match the value of the argument strip_value. Functionality is
|
||||
analigous to that of the method str.strip. Returns a list.
|
||||
|
||||
>>> strip(['Fu', 'Foo', 'Bar', 'Bam', 'Fu'], 'Fu')
|
||||
['Foo', 'Bar', 'Bam']
|
||||
|
||||
"""
|
||||
return list(strip_iter(iterable,strip_value))
|
||||
|
||||
|
||||
def strip_iter(iterable,strip_value=None):
|
||||
"""Strips values from the beginning and end of an iterable. Stripped items
|
||||
will match the value of the argument strip_value. Functionality is
|
||||
analigous to that of the method str.strip. Returns a generator.
|
||||
|
||||
>>> list(strip_iter(['Fu', 'Foo', 'Bar', 'Bam', 'Fu'], 'Fu'))
|
||||
['Foo', 'Bar', 'Bam']
|
||||
|
||||
"""
|
||||
return rstrip_iter(lstrip_iter(iterable,strip_value),strip_value)
|
||||
|
||||
|
||||
def chunked(src, size, count=None, **kw):
|
||||
"""Returns a list of *count* chunks, each with *size* elements,
|
||||
generated from iterable *src*. If *src* is not evenly divisible by
|
||||
|
|
|
@ -18,6 +18,12 @@ present in the standard library.
|
|||
|
||||
.. autofunction:: split
|
||||
.. autofunction:: split_iter
|
||||
.. autofunction:: strip
|
||||
.. autofunction:: strip_iter
|
||||
.. autofunction:: lstrip
|
||||
.. autofunction:: lstrip_iter
|
||||
.. autofunction:: rstrip
|
||||
.. autofunction:: rstrip_iter
|
||||
.. autofunction:: chunked
|
||||
.. autofunction:: chunked_iter
|
||||
.. autofunction:: pairwise
|
||||
|
|
|
@ -509,3 +509,29 @@ def test_chunked_bytes():
|
|||
from boltons.iterutils import chunked
|
||||
|
||||
assert chunked(b'123', 2) in (['12', '3'], [b'12', b'3'])
|
||||
|
||||
|
||||
def test_lstrip():
|
||||
from boltons.iterutils import lstrip
|
||||
|
||||
assert lstrip([0,1,0,2,0,3,0],0) == [1,0,2,0,3,0]
|
||||
assert lstrip([0,0,0,1,0,2,0,3,0],0) == [1,0,2,0,3,0]
|
||||
assert lstrip([]) == []
|
||||
|
||||
|
||||
|
||||
def test_rstrip():
|
||||
from boltons.iterutils import rstrip
|
||||
|
||||
assert rstrip([0,1,0,2,0,3,0],0) == [0,1,0,2,0,3]
|
||||
assert rstrip([0,1,0,2,0,3,0,0,0],0) == [0,1,0,2,0,3]
|
||||
assert rstrip([]) == []
|
||||
|
||||
|
||||
def test_strip():
|
||||
from boltons.iterutils import strip
|
||||
|
||||
assert strip([0,1,0,2,0,3,0],0) == [1,0,2,0,3]
|
||||
assert strip([0,0,0,1,0,2,0,3,0,0,0],0) == [1,0,2,0,3]
|
||||
assert strip([]) == []
|
||||
|
||||
|
|
Loading…
Reference in New Issue