Subscription donations to PSF
Hi all, I asked the PSF folks if they plan on offering a subscription donation option, and they do eventually, if there's enough interest. If you have any interest in donating a small amount (preferably >= US$5) to the PSF on a regular basis (monthly?), then please respond here in the newsgroup or otherwise communicate this interest to the PSF so they have a feel for how many people would take advantage of the donation option if they offered it. For those not familiar with the PSF, plenty of info can be found here: http://python.org/psf/ Thanks! Alan McIntyre -- http://mail.python.org/mailman/listinfo/python-list
Re: changing __call__ on demand
Thanks; I didn't read close enough. :) -- Alan McIntyre ESRG LLC http://www.esrgtech.com Michael Hoffman wrote: Alan McIntyre wrote: >>>class test(object): ...def __call1(self): ...print 1 ...__call__ = __call1 Is that what you were looking for? That still only allows him to have one call function per class. -- http://mail.python.org/mailman/listinfo/python-list
Re: changing __call__ on demand
I tried this: >>>class test(object): ... def __call1(self): ... print 1 ... __call__ = __call1 ... >>>t = test() >>>t() 1 >>> Is that what you were looking for? -- Alan McIntyre ESRG LLC http://www.esrgtech.com Stefan Behnel wrote: Hi! This somewhat puzzles me: Python 2.4 (#1, Feb 3 2005, 16:47:05) [GCC 3.3.4 (pre 3.3.5 20040809)] on linux2 Type "help", "copyright", "credits" or "license" for more information. .>>> class test(object): ... def __init__(self): ... self.__call__ = self.__call1 ... def __call1(self): ... print 1 ... def __call__(self): ... print 2 ... .>>> t = test() .>>> t() 2 If I take out the __call__ method completely and only set it in __init__, I get a TypeError saying that test is not callable. I want to use this in order to provide different implementations based on the object configuration. Calculating the right function to call is non-trivial and calls are frequent, so I want to change __call__ in order to run the right function directly. I know, I could use another level of indirection: def __call__(self): self.the_right_method() and then set the_right_method accordingly, but I find that somewhat sub-optimal. Is there a way to change __call__ after class creation? Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: listerator clonage
Cyril, Here's some code that (I think) does what you want: l = [1, 7, 3, 4, 3, 2, 1] s, dups = set(), set() for x in i: if x in s: dups.add(x) s.add(x) print dups I'm sure there are more elegant ways to do it, but this seemed to be the most straightforward way I could think of. Hope this helps, Alan McIntyre ESRG LLC http://www.esrgtech.com Cyril BAZIN wrote: Hello, I want to build a function which return values which appear two or more times in a list: So, I decided to write a little example which doesn't work: #l = [1, 7, 3, 4, 3, 2, 1] #i = iter(l) #for x in i: #j = iter(i) #for y in j: #if x == y: #print x In thinked that the instruction 'j= iter(i)' create a new iterator 'j' based on 'i' (some kind of clone). I wrote this little test which show that 'j = iter(i)' is the same as 'j = i' (that makes me sad): #l = [1, 7, 3, 4, 2] #i = iter(l) #j = iter(i) #k = i #i, j, k (, , ) Just in order to test, I wrote these little test: #l = [1, 7, 3, 4, 2] #i = iter(l) #import pickle #j = pickle.loads(pickle.dumps(i)) Traceback (most recent call last): File "", line 1, in ? File "C:\Python24\lib\pickle.py", line 1386, in dumps Pickler(file, protocol, bin).dump(obj) File "C:\Python24\lib\pickle.py", line 231, in dump self.save(obj) File "C:\Python24\lib\pickle.py", line 313, in save rv = reduce(self.proto) File "C:\Python24\lib\copy_reg.py", line 69, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle listiterator objects #import copy #j = copy.copy(i) Traceback (most recent call last): File "", line 1, in ? File "C:\Python24\lib\copy.py", line 95, in copy return _reconstruct(x, rv, 0) File "C:\Python24\lib\copy.py", line 320, in _reconstruct y = callable(*args) File "C:\Python24\lib\copy_reg.py", line 92, in __newobj__ return cls.__new__(cls, *args) TypeError: object.__new__(listiterator) is not safe, use listiterator.__new__() So, I would like to know if there is a way to 'clone' a 'listiterator' object. I know that is possible in Java for example... If it is impossible, have you better ideas to find duplicate entries in a list... Thanks, Cyril -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda
e wrote: Question: WHAT IS LAMBDA? I can't figure out what it does from any documentation i've found anywhere. i doubt i need it but i still want to know what the heck it is/does/fixes/whatever! Here's something: http://docs.python.org/tut/node6.html#SECTION00675 Hope this helps, Alan McIntyre ESRG LLC http://www.esrgtech.com -- http://mail.python.org/mailman/listinfo/python-list
Re: "Collapsing" a list into a list of changes
Thanks to everybody that responded; I appreciate all the input, even if I didn't respond to all of it individually. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble converting hex to decimal?
Earl, Try this: >>> ord('\x00') 0 or: >>> import struct >>> struct.unpack('b', '\x00') (0,) If you're needing to pull values out of multiple bytes (shorts, longs, floats, etc.), have a look at the struct module. Here's an example: >>> struct.unpack('f', '\x00\x00(B') (42.0,) Hope this helps, Alan Earl Eiland wrote: I'm trying to process the IP packet length field, as recorded by pcap (Ethereal) and recovered using pcapy. When I slice out those bytes, I get a value that shows in '\x00' format, rather than '0x00'. Neither int() nor eval() are working. How do I handle this? Earl Eiland -- http://mail.python.org/mailman/listinfo/python-list
Re: "Collapsing" a list into a list of changes
Alex, Wow, that method turns out to be the fastest so far in a simple benchmark on Python2.3 (on my machine, of course, YMMV); it takes 14% less time than the one that I deemed most straightforward. :) Thanks, Alan Alex Martelli wrote: H, what role does the enumeration play here? I don't see how you're using it, at all. Why not just: def collapse(iterable): it = iter(iterable) lastitem = it.next() yield lastitem for item in it: if item != lastitem: yield item lastitem = item that's basically just the same as your code but without the strangeness of making an enumerate for the purpose of ignoring what the enumerate adds to an ordinary iterator. Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: "Collapsing" a list into a list of changes
Tony, Actually I only want to remove a certain kind of duplication; if an item occurs twice - say like this: [1,1,1,2,2,2,1,1,1], then I need to keep the order and occurrence of the individual values: [1,2,1]. Using a dict as you proposed loses the order of occurrence, as well as multiple occurrences of groups of the same item. If I didn't need those two qualities of the list to be preserved, though, I think I'd use something like your solution (if I was using a Python older than 2.3) or Steve Coats' solution posted above using Set. Thanks! Alan Tony wrote: Here is a version using dictionary properties, ie no duplication of keys. def condense(l): d={} for item in l: d[item]=1 l=d.keys() return l Cheers Tony -- http://mail.python.org/mailman/listinfo/python-list
Re: "Collapsing" a list into a list of changes
Steve, Yeah, in this particular application the ordering and reoccurrence of a value in a non-contiguous way does matter; if those two things weren't required I think the method you suggested would be a good way to remove the duplicates. Thanks! Coates, Steve (ACHE) wrote: It's not _exactly_ what you asked for but it may be enough... Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. from sets import Set l = [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5] s = Set(l) s Set([0, 1, 2, 3, 4, 5]) l2 = list(s) l2 [0, 1, 2, 3, 4, 5] I say it's not exactly what you wanted because I don't think the ordering of l2 is necessarily the same as l. That may or may not be a problem for you. Regards Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: string issue
Wow, that's cool; I'd never seen that before. :) Thanks, Steve.. Steve Holden wrote: You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... regards Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: string issue
I think it's because you're modifying the list as you're iterating over it. Try this: import time ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', '127.0.0.1', '255.0.0.0', '255', '128.173.255.34'] ips_new = [] for ip in ips: if '255' not in ip: ips_new.append(ip) print ips_new Or this: ips_new = [ip for ip in ips if '255' not in ip] print ips_new Hope this helps, Alan McIntyre http://www.esrgtech.com rbt wrote: Either I'm crazy and I'm missing the obvious here or there is something wrong with this code. Element 5 of this list says it doesn't contain the string 255, when that's *ALL* it contains... why would it think that??? import time ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', '127.0.0.1', '255.0.0.0', '255', '128.173.255.34'] for ip in ips: if '255' in ip: try: print "Removing", ip ips.remove(ip) except Exception, e: print e print ips time.sleep(5) Someone tell me I'm going crazy ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: empty classes as c structs?
Christopher, I've found myself doing the same thing. You could do something like this: blah = type('Struct', (), {})() blah.some_field = x I think I'd only do this if I needed to construct objects at runtime based on information that I don't have at compile time, since the two lines of code for your empty class would probably be more recognizable to more people. If this usage of type() strikes anyone as inappropriate please let me know, because I really don't know. :) Christopher J. Bottaro wrote: I find myself doing the following very often: class Struct: pass ... blah = Struct() blah.some_field = x blah.other_field = y ... Is there a better way to do this? Is this considered bad programming practice? I don't like using tuples (or lists) because I'd rather use symbolic names, rather than numeric subscripts. Also, I don't like having to declare the empty Struct class everytime I want to do this (which is very often). Feedback is appreciated, thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: "Collapsing" a list into a list of changes
Jack, I'm not using 2.4 yet; still back in 2.3x. :) Thanks for the examples, though - they are clear enough that I will probably use them when I upgrade. Thanks, Alan Jack Diederich wrote: If you are using python2.4, import itertools as it [x[0] for (x) in it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5])] [0, 1, 2, 3, 2, 4, 5] Since this is 2.4 you could also return a generator expression. def iter_collapse(myList): ... return (x[0] for (x) in it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5])) ... i = iter_collapse([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]) i list(i) [0, 1, 2, 3, 2, 4, 5] -Jack -- http://mail.python.org/mailman/listinfo/python-list
Re: "Collapsing" a list into a list of changes
I think you're right; sometimes I'm susceptible to the "I can do that in one line of code" temptation. :) Since this current bit of code will probably end up in something that's going to be maintained, I will probably stick with the straightforward method just to be nice to the maintainer (especially if it's me!). Jeremy Bowers wrote: I think that's pretty elegant; I read it and immediately understood what you were doing. There may be some performance tweaks you could make if you were doing this to large lists, and my instincts say to re-write it as an iterator if you use it a lot like: for item in collapse(yourList): but other than that which may not even apply, "straightforward" is generally a *good* thing, don't you think? :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: "Collapsing" a list into a list of changes
Your first example is along the lines of what I was thinking when I said "elegant." :) I was looking for something that I could drop into one or two lines of code; I may not do that if I'm writing code that will have to be maintained, but it's still nice to know how to do it. Thanks :) Alan Steven Bethard wrote: Well, this does about the same thing, but using enumerate and a list comprehension: py> lst = [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5] py> [item for i, item in enumerate(lst) if i == 0 or item != lst[i-1]] [0, 1, 2, 3, 2, 4, 5] Similar code that doesn't check 'if i == 0' each time through: py> itr = enumerate(lst) py> itr.next() (0, 0) py> [lst[0]] + [item for i, item in itr if item != lst[i-1]] [0, 1, 2, 3, 2, 4, 5] I don't know if either of these is really more elegant though... Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python for a new beginner
Lisa Horton wrote: I hear that Python is one of the easiest languages to learn. It is easier than PHP or Pearl? Is it as useful as those two? I am attracted to Python as a first language, but I just want to be sure I will be able to use it. Opinions, thoughts, thanks! I haven't done much with Perl, but I did try to learn enough PHP a couple of years ago to do some web development. It wasn't too bad, but it seemed tough to get started for some reason. My experience with Python wasn't like that - it didn't seem to take very long at all to get comfortable with it and start using it for real projects at work (maybe 2-3 months of learning it in my spare time). I still use Python for >90% of my projects at work. In over 2 years, I haven't run into any situations where I had good reason to choose anything else. The stuff built into the language and the libraries that come as part of the standard Python distribution have made my life a lot easier than it ever was when I was only using C/C++. Of course, your mileage may vary, depending on what sort of programming you'd like to do. :) Hope this helps, Alan McIntyre http://www.esrgtech.com -- http://mail.python.org/mailman/listinfo/python-list
"Collapsing" a list into a list of changes
Hi all, I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my original list is [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want to end up with [0,1,2,3,2,4,5]. Here is the way I'm doing this now: def straightforward_collapse(myList): collapsed = [myList[0]] for n in myList[1:]: if n != collapsed[-1]: collapsed.append(n) return collapsed Is there an elegant way to do this, or should I just stick with the code above? Thanks, Alan McIntyre http://www.esrgtech.com -- http://mail.python.org/mailman/listinfo/python-list