All,
As you know, by python iter we can use iter.next() to get the next item of data. take a list for example:
l = [x for x in range(100)]
itl = iter(l)
itl.next() # 0
itl.next() # 1
Now I want a buffer can store *general iter pointed data * slice in fixed size, use above list iter to demo my question.
class IterPage(iter, size):
# class code here
itp = IterPage(itl, 5)
what I want is
print itp.first() # [0,1,2,3,4]
print itp.next() # [5,6,7,8,9]
print itp.prev() # [0,1,2,3,4]
len(itp) # 20 # 100 item / 5 fixed size = 20
print itp.last() # [96,97,98,99,100]
for y in itp: # iter may not support "for" and len(iter) then something alike code also needed here
print y
[0,1,2,3,4]
[5,6,7,8,9]
...
[96,97,98,99,100]
it is not a homework, but as a beginner of the python know little about to design an iter class, could someone share me how to code the class "IterPage" here?
Also, by below answers I found if the raw data what I want to slice is very big, for example a 8Giga text file or a 10^100 records table on a database, it may not able to read all of them into a list - I have no so much physical memories. Take the snippet in python document for example:
http://docs.python.org/library/sqlite3.html#
>>> c = conn.cursor()
>>> c.execute('select * from stocks order by price')
>>> for row in c:
... print row
...
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.14)
(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)
(u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0)
If here we've got about 10^100 records, In that case, it it possible only store line/records I want by this class with itp = IterPage(c, 5)
? if I invoke the itp.next() the itp can just fetch next 5 records from database?
Thanks!
PS: I got an approach in below link: http://code.activestate.com/recipes/577196-windowing-an-iterable-with-itertools/
and I also found someone want to make a itertools.iwindow() function however it is just been rejected. http://mail.python.org/pipermail/python-dev/2006-May/065304.html