Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Python 3 compatibility and reduced memory footprint on tail #9

Merged
merged 1 commit into from

2 participants

@jbvsmo
  • Fixed doctest for t on Python 3 (range converted to list).
  • Python 3's strings have the __iter__ method and that breaks t. Added type check.
  • Changed tail to avoid all the iterator in memory (only qte sized list is kept).

All tests pass on Python 2 and 3

@jbvsmo jbvsmo - Fixed doctest for "t" on Python 3 (range converted to list).
- Python 3's strings have the __iter__ method and that breaks "t". Added this restriction.
- Changed "tail" to avoid all the iterator in memory (only qte sized list is kept).

All tests pass on Python 2 and 3
78ef8a8
@JulienPalard JulienPalard merged commit 208b669 into JulienPalard:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 31, 2011
  1. @jbvsmo

    - Fixed doctest for "t" on Python 3 (range converted to list).

    jbvsmo authored
    - Python 3's strings have the __iter__ method and that breaks "t". Added this restriction.
    - Changed "tail" to avoid all the iterator in memory (only qte sized list is kept).
    
    All tests pass on Python 2 and 3
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 5 deletions.
  1. +9 −5 pipe.py
View
14 pipe.py
@@ -314,7 +314,7 @@ def stdout(x):
t
Like Haskell's operator ":"
- >>> 0 | t(1) | t(2) == range(3)
+ >>> 0 | t(1) | t(2) == range(3) | as_list
True
to_type
@@ -415,9 +415,13 @@ def take(iterable, qte):
@Pipe
def tail(iterable, qte):
"Yield qte of elements in the given iterable."
- for item in (iterable | as_list)[-qte:]:
- yield item
-
+ out = []
+ for item in iterable:
+ out.append(item)
+ if len(out) > qte:
+ out.pop(0)
+ return out
+
@Pipe
def skip(iterable, qte):
"Skip qte elements in the given iterable, then yield others."
@@ -608,7 +612,7 @@ def run_with(iterable, func):
@Pipe
def t(iterable, y):
- if hasattr(iterable,'__iter__'):
+ if hasattr(iterable,'__iter__') and not isinstance(iterable, str):
return iterable + type(iterable)([y])
else:
return [iterable, y]
Something went wrong with that request. Please try again.