473,378 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

itertools.count(-3)

itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>>from itertools import count, islice
list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>>def count2(n=0):
.... while True:
.... yield n
.... n += 1
....
>>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.

Bye,
bearophile

Sep 21 '06 #1
2 1746

bearophileH...@lycos.com wrote:
itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>from itertools import count, islice
list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>def count2(n=0):
... while True:
... yield n
... n += 1
...
>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.
Seems like a regression to me. itertools was documented as taking a
sequence of integers, not necessarily positive integers. It worked on
Python 2.4.

Looking at the source (from 2.5 rc2), it looks like they accidentally
used PyInt_FromSize_t rather than PyInt_FromSSize_t in the count
iterator. size_t is unsigned, of course, hence the large negative
numbers.
Carl Banks

Sep 21 '06 #2
On Thu, Sep 21, 2006 at 08:29:11AM -0700, Carl Banks wrote:
>
bearophileH...@lycos.com wrote:
itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>>from itertools import count, islice
>>list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>>def count2(n=0):
... while True:
... yield n
... n += 1
...
>>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.

Seems like a regression to me. itertools was documented as taking a
sequence of integers, not necessarily positive integers. It worked on
Python 2.4.

Looking at the source (from 2.5 rc2), it looks like they accidentally
used PyInt_FromSize_t rather than PyInt_FromSSize_t in the count
iterator. size_t is unsigned, of course, hence the large negative
numbers.
Nuts, that was me. I'll fix and add some tests.

-Jack
Sep 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Williams | last post by:
Is there any interest in adding a "Haskellish" take function to itertools? I know its easy enough to roll my own, I've been using: take = lambda iter, n: which works, but I was wondering if...
10
by: Jeremy Fincher | last post by:
Sometimes I find myself simply wanting the length of an iterator. For example, to collect some (somewhat useless ;)) statistics about a program of mine, I've got code like this: objs =...
6
by: Robert Brewer | last post by:
def warehouse(stock, factory=None): """warehouse(stock, factory=None) -> iavailable, iremainder. Iterate over stock, yielding each value. Once the 'stock' sequence is exhausted, the factory...
1
by: anton muhin | last post by:
Hello, everybody! Trying to solve the problem in the subj, I found that I miss some iterator-related tools. Mostly consequental application of the same function to some argument (if I'm not...
1
by: Steven Bethard | last post by:
Is there a reason that itertools.islice doesn't support None arguments for start and step? This would be handy for use with slice objects: >>> r = range(20) >>> s1 = slice(2, 10, 2) >>> s2 =...
18
by: Ville Vainio | last post by:
For quick-and-dirty stuff, it's often convenient to flatten a sequence (which perl does, surprise surprise, by default): ]]] -> One such implementation is at ...
21
by: Steven Bethard | last post by:
Jack Diederich wrote: > > itertools to iter transition, huh? I slipped that one in, I mentioned > it to Raymond at PyCon and he didn't flinch. It would be nice not to > have to sprinkle 'import...
41
by: rurpy | last post by:
The code below should be pretty self-explanatory. I want to read two files in parallel, so that I can print corresponding lines from each, side by side. itertools.izip() seems the obvious way to...
23
by: Mathias Panzenboeck | last post by:
I wrote a few functions which IMHO are missing in python(s itertools). You can download them here: http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104 A short...
17
by: Raymond Hettinger | last post by:
I'm considering deprecating these two functions and would like some feedback from the community or from people who have a background in functional programming. * I'm concerned that use cases for...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.