Re: Hamming Distance

2008-06-27 Thread Jared Grubb
Matimus, I was surprised that "lazy" was the algorithm that won your time tests, and I saw a way to improve it even better (algorithm is O(# ones in number) rather than O(# bits in number)) def lazy2(a, b, bits=32): x = (a ^ b) & ((1 << bits) - 1) tot = 0 while x: tot += 1

Re: extend getattr()

2008-06-27 Thread Jared Grubb
You could overload __getattr__ (might have to play around a bit to make sure any possible AttributeError's look right, but the basic idea is here) class A(object): # ... def __getattr__(self, name): try: return object.__getattribute__(self, name) except AttributeError:

Re: Partition list with predicate

2008-04-23 Thread Jared Grubb
> pred = EveryOtherOne() >>> lst = [1,2,2,1] >>> extracted = [ obj for obj in lst if pred(obj) ] >>> extracted [2, 1] >>> lst = [ obj for obj in lst if obj not in extracted ] >>> lst [] On Wed, Apr 23, 2008 at 6:14 PM, Brian <[EMAIL PROTECTED]> wro

Re: Partition list with predicate

2008-04-23 Thread Jared Grubb
classes to handle. I haven't found a function that will both remove objects from a list, but save the ones that do get removed. Jared On 23 Apr 2008, at 10:15, Tim Golden wrote: Jared Grubb wrote: I want a function that removes values from a list if a predicate evaluates to True. The b

Partition list with predicate

2008-04-23 Thread Jared Grubb
I want a function that removes values from a list if a predicate evaluates to True. The best I could come up with is: def extract(lst, pred): idx = 0 ret = [] for obj in lst[:]: if pred(obj): ret.append(obj) lst.pop(idx) else: idx +=

Re: for-else

2008-03-05 Thread Jared Grubb
I think bearophile makes an excellent point. I also have a hard time remembering what else does. I have always pictured that the "normal" behavior of a for loop is to get through all the items. In special circumstances, it is necessary to break out early. Therefore, it FEELS like the else loop shou

Re: Adding properties to an instance

2008-02-06 Thread Jared Grubb
Er, instead of "getattr(self,...) you gotta do "object.__getattr__(self,...)" and same for setattr and delattr. Dumb error on my part. (Otherwise you get infinite recursion!) On Feb 6, 2008 12:43 PM, Jared Grubb <[EMAIL PROTECTED]> wrote: > Here's one way of doing

Re: Adding properties to an instance

2008-02-06 Thread Jared Grubb
Here's one way of doing what you're asking... I would suggest using __getattribute__ and __setattr__ to dispatch the methods to the custom class you invent that holds all those properties. For example (I simplified your makeprops into __init__ just to keep the example short, but you can probably s

Re: boolean decisions

2008-02-05 Thread Jared Grubb
Quine-McCluskey isn't too bad to do once or twice by hand, but if you change even one row in your dataset, you'll have to repeat the ENTIRE Q-M algorithm. It gets very tedious. For your application, I'd just use a hash table. You dont need the reduced form of your data, you just need a look-up tabl

Re: Binary file Pt 1 - Only reading some

2008-02-04 Thread Jared Grubb
You should look into the struct module. For example, you could do the same thing via (using the variable names you used before): header_str = info.read(13) a,b,c,d,e = struct.unpack("6s4sBBB", header_str) After that, you will probably be able to get the integers by (doing it one at a time... read'

Re: char string 2 hex string

2008-01-31 Thread Jared Grubb
You could also do: "".join(['%02x' % ord(c) for c in 'AAA']) On 31 Jan 2008, at 14:09, Paul Rubin wrote: Antonio Chay <[EMAIL PROTECTED]> writes: "AAA" should be "414141" 'AAA'.encode('hex') -- http://mail.python.org/mailman/listinfo/python-list On 31 Jan 2008, at 14:05, Antonio Chay wrot

Iterate through slots

2008-01-15 Thread Jared Grubb
How can I iterate through the slots of a class, including those it inherits from parent classes? class C(object): __slots__ = ['a'] class D(C): __slots__ = ['b'] >>> d = D() >>> d.__slots__ ['b'] >>> d.a = 1 # this works, so slots inherit properly >>> d.b = 2 >>> d.c = 3 # this doesnt work,