Skip to content

Commit

Permalink
:update: docstring and return type of the function slice in case of d…
Browse files Browse the repository at this point in the history
…ict iterable in the module iterable.
  • Loading branch information
b3j0f committed Nov 8, 2015
1 parent 46abc45 commit 49e1cca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ False
'3'

>>> sliceit(od, -2, -1)
OrderedDict([('3', 4)])
['3']

Path
----
Expand Down
38 changes: 22 additions & 16 deletions b3j0f/utils/iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ def last(iterable, default=None):

def itemat(iterable, index):
"""Try to get the item at index position in iterable after iterate on
iterable items."""
iterable items.
:param iterable: object which provides the method __getitem__ or __iter__.
:param int index: item position to get.
"""

result = None

Expand Down Expand Up @@ -194,7 +198,15 @@ def itemat(iterable, index):
return result

def sliceit(iterable, lower=0, upper=None):
"""Apply a slice on input iterable."""
"""Apply a slice on input iterable.
:param iterable: object which provides the method __getitem__ or __iter__.
:param int lower: lower bound from where start to get items.
:param int upper: upper bound from where finish to get items.
:return: sliced object of the same type of iterable if not dict, or specific
object. otherwise, simple list of sliced items.
:rtype: Iterable
"""

if upper is None:
upper = len(iterable)
Expand All @@ -203,7 +215,7 @@ def sliceit(iterable, lower=0, upper=None):
result = iterable[lower: upper]

except TypeError: # if iterable does not implement the slice method
values = []
result = []

if lower < 0: # ensure lower is positive
lower += len(iterable)
Expand All @@ -223,20 +235,14 @@ def sliceit(iterable, lower=0, upper=None):

else:
if index >= lower:
values.append(value)

if isinstance(iterable, dict):
result = {}
result.append(value)

for value in values:
result[value] = iterable[value]

else:
iterablecls = iterable.__class__
try:
result = iterablecls(values)
iterablecls = iterable.__class__
if not(isinstance(result, iterablecls) or issubclass(iterablecls, dict)):
try:
result = iterablecls(result)

except:
result = values
except TypeError:
pass

return result
25 changes: 10 additions & 15 deletions b3j0f/utils/test/iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def __getslice__(self, lower, upper):

def __cmp__(self, other):

return len(self.value).__cmp__(len(other))
valuetocmp = other.value if isinstance(other, Test) else other

return len(self.value).__cmp__(len(valuetocmp))

self._assertvalue(Test)

Expand Down Expand Up @@ -281,7 +283,9 @@ def _assertvalue(self, _type):
# test empty iterable
empty = _type()
value = sliceit(iterable=empty)
self.assertEqual(value, empty)
if isdict:
empty = []
self.assertEqual(empty, value)

# test with not empty iterable
randlist = _randlist()
Expand Down Expand Up @@ -311,17 +315,13 @@ def _assertvalue(self, _type):

else:

val = {} if isdict else []
val = []

index = lower

for index in range(lower, upper):

item = itemat(iterable, index)
if isdict:
val[item] = iterable[item]
else:
val.append(item)
val.append(item)

if not isdict:
val = _type(val)
Expand All @@ -343,18 +343,13 @@ def _assertvalue(self, _type):

else:

val = {} if isdict else []
val = []

index = lower

for index in range(lower, upper):
item = itemat(iterable, index)

if isdict:
val[item] = iterable[item]

else:
val.append(item)
val.append(item)

if not isdict:
val = _type(val)
Expand Down

0 comments on commit 49e1cca

Please sign in to comment.