Re: Unpacking byte strings from a file of unknown size
On Mon, Oct 27, 2008 at 4:29 PM, Mark [EMAIL PROTECTED] wrote: Hi; I'm trying to use the struct.unpack to extract an int, int, char struct info from a file. I'm more accustomed to the file.readlines which works well in a 'for' construct (ending loop after reaching EOF). # This does OK at fetching one 10-byte string at a time: # (4, 4, 2 ascii chars representing hex) info1, info2, info3 = struct.unpack('IIH', myfile.read(10)) # Now to do the entire file, putting into a loop just gives error: # TypeError: 'int' object is not iterable for info1, info2, info3 in struct.unpack('IIH', myfile.read(10)): In trying to shoehorn this into a 'for' loop I've been unsuccessful. I also tried other variations that also didn't work but no point wasting space. Using Python 2.5, WinXP Thx, Mark -- http://mail.python.org/mailman/listinfo/python-list I usually do something like: s = myfile.read(10) while len(s) == 10: info1, info2, info3 = struct.unpack('IIH', s) s = myfile.read(10) #might want to check that len(s) == 0 here -- http://mail.python.org/mailman/listinfo/python-list
Re: struct.pack behavior
For efficiency reasons many CPUs require particular primitive data types (integers/pointers of various sizes) to be placed in memory at particular boundaries. For example, shorts (H above, usually two bytes and probably always so in the struct module) are often required to be on even addresses, and longer objects to be on 4 or 8 byte boundaries. This allows for much more efficient memory access on many platforms (of course the rules depend on the platform). Although RAM _appears_ to the random access to arbitrary bytes, the underlying hardware will often fetch chunks of bytes in parallel. If a number spanned the boundaries of such a chunk it would require two fetch cycles instead of one. So this is avoided for performance reasons. So, packing HB puts a short at offset 0 (even) and then a byte. Conversely, packing BH puts a byte at offset zero but puts the short at offset 2 (to be even), leaving a gap after the byte to achieve this, thus the 4 byte size of the result (byte, gap, short). This layout procedure is called alignment. Cheers, -- Cameron Simpson [EMAIL PROTECTED] DoD#743 http://www.cskk.ezoshosting.com/cs/ Thanks for taking the time to type a detailed, helpful response, Cameron. Much appreciated! -- http://mail.python.org/mailman/listinfo/python-list
struct.pack behavior
Can anyone explain to me why struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2) gives 4 bytes? -Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: struct.pack behavior
On Wed, Jun 25, 2008 at 7:03 PM, John Machin [EMAIL PROTECTED] wrote: On Jun 26, 9:00 am, Steven Clark [EMAIL PROTECTED] wrote: Can anyone explain to me why struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2) gives 4 bytes? Alignment -- read the manual. -- http://mail.python.org/mailman/listinfo/python-list If the manual is the help files for the struct module, I've read it several times over. I understand endianness; I don't understand alignment. Could anyone give a less cryptic / terse answer? -- http://mail.python.org/mailman/listinfo/python-list
Re: value is in list?
Hello , following scenario list_current = [ welcome, search, done, result] list_ldap = [ welcome, hello] result: list_toadd = [ hello] by words said , i want to check if list item from list_ldap exists in list_current if not i want to add it to list_toadd. Thanks! D. list_toadd = [i for i in list_ldap if i not in list_current] seems to work. I'm sure there's a way to do it with set objects as well. -Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: can't assign to literal
On Tue, Jun 10, 2008 at 5:30 PM, maehhheeyy [EMAIL PROTECTED] wrote: On Jun 10, 1:21 pm, Matimus [EMAIL PROTECTED] wrote: On Jun 10, 12:53 pm, maehhheeyy [EMAIL PROTECTED] wrote: this is stopping my program from running properly. is there something wrong in my code when that happens? yes Post your code, or at least the full error message if you want more details. Matt for 1 in oids, vals head_oids: SyntaxError: can't assign to literal -- http://mail.python.org/mailman/listinfo/python-list Wow. http://catb.org/~esr/faqs/smart-questions.html -- http://mail.python.org/mailman/listinfo/python-list
Re: can't assign to literal
for 1 in oids, vals head_oids: SyntaxError: can't assign to literal -- 1 is a literal, you can't assign it to something. Are you trying to use it as a variable name? -- http://mail.python.org/mailman/listinfo/python-list
Re: scope of optional arguments
http://www.ferg.org/projects/python_gotchas.html#contents_item_6 On Mon, May 19, 2008 at 10:30 AM, cseja [EMAIL PROTECTED] wrote: If I call print walk([1,2,3], []) print walk([5,6,7]) I get [1, 2, 3] [4, 5, 6] but when I call print walk([1,2,3]) print walk([5,6,7]) I get [1, 2, 3] [1, 2, 3, 4, 5, 6] at stdout, where def walk(seq, result = []): for item in seq: result.append(item) return result Does that mean that the scope of optional arguments is global if they aren't used and local if they are (or am I missing something here)? Regards, CS -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Data structure recommendation?
I believe the best way to implement this would be a binary search (bisect?) on the actual times, which would be O(log N). Though since they are timestamps they should be monotonically increasing, in which case at least you don't have to go to the expense of sorting them. Some kind of hash function won't hack it, since the purpose of a hash function is to map a large number of (possibly) evenly-distributed (potential) keys as nearly as possible randomly across a much smaller set of actual values. You might try messing around with reducing the precision of the numbers to home in on a gross region, but I am not convinced that does anything other than re-spell binary search if carried to extremes. Thanks all for your comments, which basically confirmed for me that there is no magic bullet in this situation. To add an additional wrinkle to the problem, I know a priori that for sequential calls to foo.get(x), 95% of the time, x is likely to be very close to the previous x. In other words, you might see foo.get(3.9), foo.get(3.8), foo.get(3.7), etc. (think VCR controls, rewinding). It seems to me because of this, saving state and doing a linear search from the previous location can actually be faster than a binary search. But you would pay a big penalty when x jumps by a lot. Hrm, a balancing act... Regarding monotonically increasing timestamps: I had initially assumed so, but I may want to allow adding events back in time. What is the most efficient way to keep the list sorted after each put()? Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
list.sort(): heaviest item?
If I have a list of items of mixed type, can I put something into it such that after a list.sort(), is guaranteed to be at the end of the list? Looking at http://www.python.org/doc/2.3.5/ref/comparisons.html Most other types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program. makes me unsure. It looks like None always ends up at the start (lightest), but I want the opposite (heaviest). -Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: list.sort(): heaviest item?
You can pass a cmp-function that will always make one object being greater than all others. Diez -- Yeah, I figured it out 2 minutes after I posted, d'oh! class Anvil(object): def __cmp__(self. other): return 1 Sorry for the wasted space. -- http://mail.python.org/mailman/listinfo/python-list
Re: Data structure recommendation?
bisect is definitely the way to go. You should take care with floating point precision, though. One way to do this is to choose a number of digits of precision that you want, and then internally to your class, multiply the keys by 10**precision and truncate, so that you are working with ints internal to the Foo class. Thanks for the reply. Can you explain how I could be bitten by floating point precision here? I'm familiar with howwhy 1.3*3 != 3.9, etc., but I'm not sure how it applies here, or what you are gaining by converting to int. What do you guys think of this approach which uses tuples: from bisect import insort_right, bisect_right class Heavy(object): def __cmp__(self, other): return 1 heavy = Heavy() class Foo(object): def __init__(self): self.data = [] def __setitem__(self, k, v): #if k's are the same, will be sorted by v's. may or may not be desireable insort_right(self.data, (k, v)) def __getitem__(self, k): i = bisect_right(self.data, (k, heavy)) if i == 0: return None else: return self.data[i-1][1] def main(): foo = Foo() assert(foo[1.5] == None) foo[1.3] = 'a' foo[2.6] = 'b' assert(foo[1.2999] == None) assert(foo[1.3] == 'a') assert(foo[1.5] == 'a') assert(foo[2.6] == 'b') assert(foo[7.8] == 'b') foo[5.0] = 'c' assert(foo[7.8] == 'c') print('Foo checks passed.') if __name__ == '__main__': main() -- http://mail.python.org/mailman/listinfo/python-list
Data structure recommendation?
Hi all- I'm looking for a data structure that is a bit like a dictionary or a hash map. In particular, I want a mapping of floats to objects. However, I want to map a RANGE of floats to an object. This will be used for timestamped storage / lookup, where the float represents the timestamp. get(x) should return the object with the newest (biggest) timestamp y = x, if it exists. Example: foo = Foo() foo.get(1.5) - None foo.put(1.3, 'a') foo.put(2.6, 'b') foo.get(1.5) - 'a' foo.get(7.8) - 'b' foo.put(5.0, 'c') foo.get(7.8) - 'c' In otherwords, by the end here, for foo.get(x), x 1.3 maps to None, 1.3 = x 2.6 maps to 'a', 2.6 = x 5.0 maps to 'b', 5.0 = x maps to 'c'. I know that foo.get() will be called many times for each foo.put(). Is there any way to achieve O(1) performance for foo.get(), maybe via some kind of hash function? Or is the best thing to use some kind of binary search? Thanks for any advice. -Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: ord function problem from newbie
print sum([ord(ch)-96 for ch in small]) On Mon, Mar 17, 2008 at 11:28 PM, [EMAIL PROTECTED] wrote: I'm trying to convert a name into a numerical value that is not consistent with ANSCII values. In my case, I convert all to lowercase, then try to sum the value of the letters entered by the user, can't get it to add them. Here is what I have. By the way, the values I need to use is: a=1, b=2, c=3, etc... I'm trying to subtract 96 from the ANSCII value, then total. import string def main(): print This program calculates the numeric value of a name with which print you could look up online as to what that value represents. print # Get name to calculate name = raw_input(Please type a name: ) small = string.lower(name) print Here is the calculated value: print small for ch in small: v = ord(ch)-96 print v main() Looks like this: This program calculates the numeric value of a name with which you could look up online as to what that value represents. Please type a name: David Here is the calculated value: david 4 1 22 9 4 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Genetic Algorithm
Why not make chromosome itself a class? class BasicChromosome(object): def __init__(self, data): self.data = data def crossover(self): [stuff here] You can subclass this as needed, altering the crossover method as necessary. ...perhaps I didn't understand your question. -Steven On Jan 27, 2008 6:35 PM, Wildemar Wildenburger [EMAIL PROTECTED] wrote: Max wrote: In GAs, you operate on a Population of solutions. Each Individual from the Population is a potential solution to the problem you're optimizing, and Individuals have what's called a chromosome - a specification of what it contains. For example, common chromosomes are bit strings, lists of ints/floats, permutations...etc. I'm stuck on how to implement the different chromosomes. I have a Population class, which is going to contain a list of Individuals. Each individual will be of a certain chromosome. I envision the chromosomes as subclasses of an abstract Individual class, perhaps all in the same module. I'm just having trouble envisioning how this would be coded at the population level. Presumably, when a population is created, a parameter to its __init__ would be the chromosome type, but I don't know how to take that in Python and use it to specify a certain class. I'm not sure I'm following you here. So a chromosome is bit of functionality, right? So basically it is a function. So my advice would be to write these functions and store it to the indivuals-list like so: class Population(object): def __init__(self, *individuals): self.individuals = list(individuals) Then you can say: p = Population(indiv1, indiv2, indiv3) for individual in p.individual: individual(whatever_your_problem) (Don't know if this is the way GA's are supposed to work) You can also create callable classes (that is, classes that implement the __call__ method), and use instances of these as the individuals. For example you can create a Permutation class that returns a permutation (defined in it's __init__()) when it's __call__ method is called. (Am I making sense?) This is just generic advice, maybe this helps and maybe it doesn't at all. :) I'm doing something similar with my crossover methods, by specifying them as functions in a module called Crossover, importing that, and defining crossover_function = getattr(Crossover, %s_crossover % xover) Where xover is a parameter defining the type of crossover to be used. I'm hoping there's some similar trick to accomplish what I want to do with chromosomes - or maybe I'm going about this completely the wrong way, trying to get Python to do something it's not made for. Any help/ feedback would be wonderful. This isn't too bad, but for such things dictionaries are your Go-To datatype. Just have a dictionary of xover-functions handy and call the thusly: crossover_function = Crossover.function[xover] Thanks, Max Martin If that helps :) regards /W -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question on Classes
On Jan 10, 2008 4:54 PM, Fredrik Lundh [EMAIL PROTECTED] wrote: Adrian Wood wrote: I can call man.state() and then woman.state() or Person.state(man) and Person.state(woman) to print the status of each. This takes time and space however, and becomes unmanageable if we start talking about a large number of objects, and unworkable if there is an unknown number. What I'm after is a way to call the status of every instance of Man, without knowing their exact names or number. I've gone through the relevant parts of the online docs, tried to find information elsewhere online, and looked for code samples, but the ionformation either isn't there, or just isn't clicking with me. I've tried tracking the names of each object in a list, and even creating each object within a list, but don't seem to be able to find the right syntax to make it all work. For a start, how about: class Person: ... your class ... persons = [] man = Person() persons.add(man) woman = Person() persons.add(woman) for p in persons: print p, p.state() Once you've gotten this to work, you can, if you want, look into moving the list maintenance into the class itself (i.e. let the __init__ function add the person to the list, and provide a destroy method that removes it from the list). And once you've gotten that to work, you can look at the weakref module for more elegant ways to handle destruction. But one step at a time... /F -- http://mail.python.org/mailman/listinfo/python-list if you can keep all instances in a list, as Fredrik showed, it's easy. Otherwise you can do something like: class Person: people = [] def __init__(self, name): self.name = name Person.people.append(self) def print_state(self): print self.name some_folks = [Person('Bob'), Person('Mary')] other_guy = Person('Frank') for p in Person.people: p.print_state() This would keep the people from ever being garbage collected, but this may not be a problem for you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question on Classes
l = [] l.append(man) l.append(woman) # Print the state. for item in l: print item.state() Small, off-topic nitpick: please don't use l (lower-case el) as a variable name. From http://www.python.org/dev/peps/pep-0008/: Naming Conventions Names to Avoid Never use the characters `l' (lowercase letter el), `O' (uppercase letter oh), or `I' (uppercase letter eye) as single character variable names. In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use `l', use `L' instead. -- http://mail.python.org/mailman/listinfo/python-list
Re: 2D Game Development in Python
On Dec 20, 2007 10:30 PM, Terry Reedy [EMAIL PROTECTED] wrote: PatrickMinnesota [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | I think I need at least this: 2D graphics, sound, input (kbd, mouse, | joystick maybe), some IPC might be nice (Stuff like: Sockets, TCP, | UDP, pipes, msg queues, shared memory). The IPC stuff would only be | used if I decide to allow some multi-player over a network. | | I've been playing with Pygame some in my late night hobby time. I'm | wondering what else I should be looking at since I'm not all that | impressed with Pygame so far. Maybe it is the right library, but | maybe it's not. Please don't point me to a list of choices. I've | seen all the lists. I've done my reading. What I don't have is | actual testimonials by people who have used a chunk of code to program | an animated 2D game and had a great experience. Someone recently posted on the pygame list that he had tried the rather new pyglet (which wraps ogre) and was switching many games to that. It is new enough that it might not be on 'all the lists' yet. But no, I have not tried it yet. -- http://mail.python.org/mailman/listinfo/python-list Pyglet does NOT, to my knowledge, wrap ogre. Pyglet is a lean-and-mean standalone library, and gets a big, big thumbs up from me. Check it out. http://pyglet.org/ 2D sprites can be accomplished pretty easily within its openGL framework. -- http://mail.python.org/mailman/listinfo/python-list
Using 'property' in evolving code
Hi all- I was reading http://dirtsimple.org/2004/12/python-is-not-java.html, in particular the part about getters and setters are evil: In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters. I understand the idea behind this, but how does this transition work in actuality? Lets say I define class Foo initially as... class Foo(object): def __init__(self, x): self.x = x def get_sqrt(self): return sqrt(self.x) Fine. I can read from x, and write to x: def test(): f = Foo(9) print 'sqrt of', f.x, 'is', f.get_sqrt() f.x = 16 print 'sqrt of', f.x, 'is', f.get_sqrt() Let's say later on I decide that f.get_sqrt will be called far more often than f.x is changed, and that it therefore makes sense to store the actual sqrt in the class, rather than calling math.sqrt each time f.get_sqrt is called. So I rewrite Foo: class Foo(object): def __init__(self, x): self._x = x self._my_root = sqrt(x) def _set_x(self, x): self._x = x self._my_root = sqrt(x) def get_sqrt(self): return self._my_root x = property(lambda self: self._x, _set_x) External to the class, everything behaves as before, except with the benefit of not having to wait for slow sqrt each time. self.x is now a property rather than an attribute. Internal to the class, I have to search replace all old occurences of self.x with self._x? Otherwise there is a collision between f.x the attribute and f.x the property? Am I understanding this correctly? Thanks! -Steven -- http://mail.python.org/mailman/listinfo/python-list