473,385 Members | 1,593 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,385 software developers and data experts.

icmp - should this go in itertools?

Hi all,

This is a little function to compare two iterators:

def icmp(a, b):
for xa in a:
try:
xb = b.next()
d = cmp(xa, xb)
if (d != 0):
return d
except StopIteration:
return 1
try:
b.next()
return -1
except StopIteration:
return 0

It's modelled after the way cmp treats lists - if a and b are lists,
icmp(iter(a), iter(b)) should always be the same as cmp(a, b).

Is this any good? Would it be any use? Should this be added to itertools?

tom

--
I content myself with the Speculative part [...], I care not for the
Practick. I seldom bring any thing to use, 'tis not my way. Knowledge
is my ultimate end. -- Sir Nicholas Gimcrack
Nov 26 '05 #1
5 1656
Tom Anderson <tw**@urchin.earth.li> wrote:
It's modelled after the way cmp treats lists - if a and b are lists,
icmp(iter(a), iter(b)) should always be the same as cmp(a, b).

Is this any good? Would it be any use? Should this be added to itertools?


Whatever happens, please name it something other than icmp. When I read
"icmp", I think "Internet Control Message Protocol".
Nov 26 '05 #2
Tom Anderson wrote:
Hi all,

This is a little function to compare two iterators:

def icmp(a, b):
for xa in a:
try:
xb = b.next()
d = cmp(xa, xb)
if (d != 0):
return d
except StopIteration:
return 1
try:
b.next()
return -1
except StopIteration:
return 0

It's modelled after the way cmp treats lists - if a and b are lists,
icmp(iter(a), iter(b)) should always be the same as cmp(a, b).

Is this any good? Would it be any use? Should this be added to itertools?


Whilst not a total itertools-expert myself, I have one little objection
with this: the comparison won't let me know how many items have been
consumed. And I end up with two streams that lack some common prefix
plus one field. I'm just not sure if there is any usecase for that.

However, _if_ there is one, I'm all for adding it to itertools - it
seems to be in the appropriate spirit.

Regards,

Diez
Nov 26 '05 #3
On Fri, 25 Nov 2005 21:58:00 -0500, Roy Smith <ro*@panix.com> wrote:
Tom Anderson <tw**@urchin.earth.li> wrote:
It's modelled after the way cmp treats lists - if a and b are lists,
icmp(iter(a), iter(b)) should always be the same as cmp(a, b).

Is this any good? Would it be any use? Should this be added to itertools?


Whatever happens, please name it something other than icmp. When I read
"icmp", I think "Internet Control Message Protocol".


Me too, but I see that as an added bonus ;-). OTOH, if this is part of
itertools or some other module, "itertools.cmp" would be a more logical
name. (On the third hand, as someone else said, I don't see the use case for
this.)

/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
Nov 26 '05 #4
On Fri, 25 Nov 2005, Roy Smith wrote:
Tom Anderson <tw**@urchin.earth.li> wrote:
It's modelled after the way cmp treats lists - if a and b are lists,
icmp(iter(a), iter(b)) should always be the same as cmp(a, b).

Is this any good? Would it be any use? Should this be added to itertools?


Whatever happens, please name it something other than icmp. When I read
"icmp", I think "Internet Control Message Protocol".


Heh! That's a good point. The trouble is, icmp is clearly the Right Thing
to call it from the point of view of itertools, continuing the pattern of
imap, ifilter, izip etc. Wouldn't it be clear from context that this was
nothing to do with ICMP?

tom

--
Gotta treat 'em mean to make 'em scream.
Nov 26 '05 #5
On Sat, 26 Nov 2005, Diez B. Roggisch wrote:
Tom Anderson wrote:
Is this any good? Would it be any use? Should this be added to itertools?
Whilst not a total itertools-expert myself, I have one little objection
with this: the comparison won't let me know how many items have been
consumed. And I end up with two streams that lack some common prefix
plus one field.


Good point. It would probably only be useful if you didn't need to do
anything with the iterators afterwards.

One option - which is somewhat icky - would be to encode that in the
return value; if n is the number of items read from both iterators, then
if the first argument is smaller, the return value is -n, and if the
second is smaller, it's n. The trouble is that you couldn't be sure
exactly how many items had been read from the larger iterator - it could
be n, if the values in the iterators differ, or n+1, if the values were
the same but the larger one was longer.
I'm just not sure if there is any usecase for that.


I used it in my ordered dictionary implementation; it was a way of
comparing two 'virtual' lists that are lazily generated on demand.

I'll go away and think about this more.

tom

--
Gotta treat 'em mean to make 'em scream.
Nov 26 '05 #6

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

Similar topics

13
by: Bob Rivers | last post by:
Hi, I was doing a research looking for Java solutions that are able to do "ping". I found old messages (and not so old ones), telling that Java was not able to do that, because the API...
0
by: Andreas Pauley | last post by:
Hi, I'd like to generate my own icmp packets from within python. Typically this would include sending an ECHO_REQUEST and checking if I get an ECHO_REPLY back. But I would also like to...
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...
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...
0
by: T.C.McQueen | last post by:
Does anyone know how to receive an ICMP-Router Advertisement Message using Raw/ICMP - Sockets in Multicast -Mode with .NET I tried it several times differently but it does not work I think it's...
0
by: billiejoex | last post by:
Hi all. The source below is a simple icmp sniffer made with pcapy. To decode packets I'm using the EthDecoder() function that returns a rapresentation of the packet including ICMP type, ICMP code,...
1
by: Fotis | last post by:
hi there i have two questions 1)Is there a library class for ICMP packet creation ,management etc to use with C# 2)I have created a socket of icmp type and after sending a packet i call the...
0
by: Ed | last post by:
I've attached some VB.NET code I've hacked together (some taken from MS examples & newsgroup postings) that will perform a ping or IcmpSendEcho using the icmp.dll (see this for more info:...
0
by: NeoIndigo | last post by:
Hi guys, It's my first time trying to create a packet in C++. So, what i have done so far are: 1. I have opened a packet level raw socket. Using this command: sd =...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.