Re: Python proficiency test
In message [EMAIL PROTECTED], Richard Jones [EMAIL PROTECTED] writes Kent Johnson wrote: I recently helped create an on-line Python proficiency test. The publisher of the test is looking for beta testers to try the test and give feedback. If you are interested, here is an announcement from the publisher: Had a look. In between my browser blocking a popup on every page and the registration asking far more details that I felt necessary, I stopped before looking at the actual test. Richard Likewise, I wanted to see a sample test (for any subject) but couldn't do that without a long-winded registration procedure. Regards Ian -- Ian Parker -- http://mail.python.org/mailman/listinfo/python-list
Re: Type signature
Yacao Wang wrote: Hi, I'm a newbie to Python. I've recently read some books about this language and none of them have answered my question. As a dynamically-typed language Python doesn't need any form of type signature which makes the syntax very clean and concise. However, type signatures are not only a kind of information provided for the compiler, but also for the programmer, or more important, for the programmer. Without it, we have to infer the return type or required agument types of a function, Reset your brain. Functions don't have required argument types, anything implementing the interface they use will work. e.g.: class a(object): def getVal(self): return a_val class b(object): def getVal(self): return b_val def someFunc(valObj): return valObj.getVal().upper() someFunc can take objects of class a or class b or any other class that has a getVal method returning something with an upper method (getVal doesn't even have to return a string as long as what it returns has an upper method). a and b don't share an inheritance hierarchy, either. There is no type signature, there's just which methods/attributes are used by the function. Limiting it to only working on specified types is unnecessarily restrictive. Haskell can also determine type information dynamically Haskell does static type inference at compile time, not dynamic typing. It's a completely different programming model (shared with ML, among others) As I understand, Python relies too much on run-time type-checking, that is, whenever you give the wrong type, you just end up with an exception, which is logically correct, but not that useful as type signatures. Dynamic typing is different from static typing, you're right, but it's not worse. You probably want to google for duck typing think about the implications on polymorphism. -- http://mail.python.org/mailman/listinfo/python-list
[Newbie] List from a generator function
Hi all, I'm sure there is a better way to do this: [random.choice(possible_notes) for x in range(length)] Regards, Ernesto -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
In [EMAIL PROTECTED], Ernesto García García wrote: I'm sure there is a better way to do this: [random.choice(possible_notes) for x in range(length)] There is at least a better way to ask the question. The subject has nothing to do with the body of your post. Or am I missing something? What is `length`? Do you want unique elements from `possible_notes`? Then you could use `random.sample()`. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Type signature
Yacao Wang wrote: Hi, I'm a newbie to Python. I've recently read some books about this language and none of them have answered my question. As a dynamically-typed language Python doesn't need any form of type signature which makes the syntax very clean and concise. However, type signatures are not only a kind of information provided for the compiler, but also for the programmer, or more important, for the programmer. Without it, we have to infer the return type or required agument types of a function, and this can't be done without seeing the implementation of it, and sometimes it is still difficult to extract the above information even if the implementation is available. Haskell can also determine type information dynamically, but it still supports and recommends the programming style with type signatures, which makes the code very readable and maitainable. As I understand, Python relies too much on run-time type-checking, that is, whenever you give the wrong type, you just end up with an exception, which is logically correct, but not that useful as type signatures. Any ideas on this issue? -- Alex Hi Yacao/Alex, Try these: How to duck type? - the psychology of static typing in Ruby http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/100511 3-31-04 I'm Over It http://66.102.9.104/search?q=cache:6XW473VSflcJ:www.mindview.net/WebLog/log-0053+%2B%22duck+typing%22+%2B+%22static+typing%22+%2Bpythonhl=engl=ukct=clnkcd=3client=firefox-a It seems that the latent or duck typing, used in dynamic languages is counter-intuitve to those from a static typing background. Nevertheless, it does work, and work well. - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: range() is not the best way to check range?
On 2006-07-21, Paul Boddie [EMAIL PROTECTED] wrote: Regardless of whether myslice inherits from object or not, there's no persuading the interpreter that it is a genuine slice, and remember that we can't subclass slice (for some reason unknown). So, it would appear that the interpreter really wants instances from some specific set of types (presumably discoverable by looking at list_subscript in listobject.c) rather than some objects conforming to some interface or protocol, and perhaps it is implemented this way for performance reasons. In any case, in the core of Python some types/classes are more equal than others, and for whatever reason the duck typing breaks down - a case of malbik endar [1] that you just have to be aware of, I suppose. Is there any chance this will iever change? Is there any chance the start:stop:step notation will ever be considered an atom? -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: function v. method
On 2006-07-21, Bruno Desthuilliers [EMAIL PROTECTED] wrote: Antoon Pardon wrote: On 2006-07-21, fuzzylollipop [EMAIL PROTECTED] wrote: danielx wrote: (snip) if you prefix with a single underscore, that tells the user, DON'T MESS WITH ME FROM OUTSIDE! I AM AN IMPLEMENTATION DETAIL! Personnaly I don't like this convention. To bad for you. I'll survive. It isn't clear enough. Oh yes ? Suppose I am writing my own module, I use an underscore, to mark variables which are an implementation detail for my module. Now I need to import an other module in my module and need access to an implementation variable from that module. So now I have variables with an underscore which have two different meanings: 1) This is an implemantation detail of this module, It is the users of my module who have to be extra carefull using it. 2) This is an implemantation detail of the other module, I should be extra carefull using it. Either you imported with the from othermodule import * form (which you shouldn't do), and you *don't* have the implementation of othermodule, or your used the import othermodule form, in which case it's pretty obvious which names belongs to othermodule. As far as I understand the _name convention is often defended with the argument that it stands out. Now if you have to go and look at the import statements to make a disticntion, then it seems that the way _names standout isn't that usefull. And I find variable starting or ending with an underscore ugly. :-) Too bad for you. Choose another language then... PHP, Perl, Ruby ?-) Not a very strong argument. Whether or not someone has a legitimat point of criticism against Python or some of the conventions used. You can always reply: Too bad, better choose another language then. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: function v. method
On 2006-07-21, fuzzylollipop [EMAIL PROTECTED] wrote: Antoon Pardon wrote: Suppose I am writing my own module, I use an underscore, to mark variables which are an implementation detail for my module. Now I need to import an other module in my module and need access to an implementation variable from that module. So now I have variables with an underscore which have two different meanings: you don't understand what implementation detail means, it means it is NOT part of the public API and no client code should ever use it. If you reference _vara in your code and it is in someone elses module you don't understand YOU ARE NOT SUPPOSED TO DO THAT! Why do you assume that in my example the other module is not understood? 1) This is an implemantation detail of this module, It is the users of my module who have to be extra carefull using it. Users of your module should NEVER KNOW any of the _ or __ stuff exists to begin with. 2) This is an implemantation detail of the other module, I should be extra carefull using it. You should NEVER use it. Well that may be your view, but AFAICS it is not the view of the python community. Because each time some mechanism is proposed for real private variable, people oppose it, they want people to have access to what are supposed to be private variables. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
I'm sure there is a better way to do this: [random.choice(possible_notes) for x in range(length)] There is at least a better way to ask the question. The subject has nothing to do with the body of your post. Or am I missing something? Sorry, I should have explained better. I just want to build a fix length list made up of elements generated by a function, in this case random.choice(). I don't like my list comprehension, because I'm using that dumb variable x and the range() list. Ernesto -- http://mail.python.org/mailman/listinfo/python-list
Re: Python proficiency test
I managed to take the test,though not it's entirety. I am still far being proficient in Python,but I really liked the thought-provoking questions in there. thanks so much. Kiran Satya On 7/23/06, Ian Parker [EMAIL PROTECTED] wrote: In message [EMAIL PROTECTED], Richard Jones [EMAIL PROTECTED] writes Kent Johnson wrote: I recently helped create an on-line Python proficiency test. The publisher of the test is looking for beta testers to try the test and give feedback. If you are interested, here is an announcement from the publisher: Had a look. In between my browser blocking a popup on every page and the registration asking far more details that I felt necessary, I stopped before looking at the actual test. Richard Likewise, I wanted to see a sample test (for any subject) but couldn't do that without a long-winded registration procedure. Regards Ian -- Ian Parker -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Coding style
On 2006-07-21, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On 21 Jul 2006 12:00:43 GMT, Antoon Pardon [EMAIL PROTECTED] declaimed the following in comp.lang.python: So we have code with certain shudder characteristics. And instead of trying to help the OP with his problem, some people react to the shudder and come with all sort of comments that might be true if the code as shown was production code, but which totally miss the point the code was illustrating and thus aren't much helpfull. You miss one factor -- the specification of what the code sample is supposed to handle. Showing a sample of code which may reproduce an apparent problem, without stating /all/ the requirements of the data/process, leaves the reviewer in the state of assuming all the conditions are explicit in the code. No that leaves the reviewer in the state of assuming all *relevant* conditions are explicit in the code. And in the case of your sample -- that just isn't true. The try/except block is too general, and seems to imply that the exception might be raised in either (or both) foo() or bar() Which in this case is totally irrelevant. The try/except block was not part of the code that would have been posetd to the newsgroup. -- it is not clear that the try/except is being used to detect an unstated exit condition requirement. Again irrelevant. This was an example where someone had trouble with his code (the try/except statement) which he would then reduce to a minimal piece of code that would still show the trouble some behaviour (Just the then part of an if statement) which he would then post to the list. All trouble with the so called original code is totally irrelevant to the point I was trying to make and the more you try to critisize it the more you are an illustration of the point I am trying to make. This remark was just under that example code in my originle article: This code makes the distinction between the three possibilities, whether it is a good way or not I won't discuss, this is just meant as an illustration. What didn't you understand about the code just meant to be an illustration. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
Ernesto García García [EMAIL PROTECTED] writes: [random.choice(possible_notes) for x in range(length)] There is at least a better way to ask the question. The subject has nothing to do with the body of your post. Or am I missing something? Sorry, I should have explained better. I just want to build a fix length list made up of elements generated by a function, in this case random.choice(). I don't like my list comprehension, because I'm using that dumb variable x and the range() list. Use xrange instead of range. If you want to do it with no variables, hmmm: from itertools import islice, starmap, repeat import random possible_notes = range(12) length = 9 print list(islice(starmap(random.choice, repeat((possible_notes,))), length)) ## working on region in file /usr/tmp/python-21885hGZ... [10, 0, 6, 7, 8, 1, 9, 6, 11] Maybe you're sorry you asked ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
Ernesto García García wrote: I'm sure there is a better way to do this: [random.choice(possible_notes) for x in range(length)] Note that generator has a fixed meaning in Python: http://www.python.org/dev/peps/pep-0255/ For generators you can use list(itertools.islice(gen()), length) What you need then would be a way to turn an ordinary function into a generator: def make_gen(fun, *args, **kw): ... def gen(): ... while 1: ... yield fun(*args, **kw) ... return gen() ... from random import choice from itertools import islice length = 7 sample = abcde list(islice(make_gen(choice, sample), length)) ['e', 'b', 'b', 'a', 'b', 'b', 'a'] Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Coding style
On 2006-07-21, Gerhard Fiedler [EMAIL PROTECTED] wrote: On 2006-07-21 09:00:43, Antoon Pardon wrote: So we have code with certain shudder characteristics. And instead of trying to help the OP with his problem, some people react to the shudder and come with all sort of comments that might be true if the code as shown was production code, but which totally miss the point the code was illustrating and thus aren't much helpfull. In general, I'd say that in this case the example was not well-chosen. After such a shudder removal, a poster should IMO review what caused the shudder, and rephrase the original problem without the shudder. The shudder is not with the poster. The poster can't predict how the readers will react. That might take a few iterations, but in general, either one or more of the shudder removals actually address the OP's issue, maybe without any individual helper understanding all of the problem (due to incomplete example code), or the iterations lead to a code that's shudder free and still shows the original problem -- now usually in a clearer form, because free of other issues that are not relevant to the OP's point. I doubt that. My experience is that you have more chance of being helped by people who try to understand where the OP is trying to go form than from those who react to style issues. I have never seem a bug go away because someone suggested to write if Number: instead of if Number != 0: E.g. if someone makes a suggestion that is valid with the example code I posted but not with the real code I have, I need to post an updated example code that introduces a restriction similar to the one I actually have, which then correctly invalidates the formerly valid suggestion -- and helps getting more helpful responses. Not necessarily. A valid suggestion doesn't imply a relevant suggestion. If someone come with code like: if Number != 0: And someone suggests that the python idiom is if Number: Then that suggestion is valid, but it also is irrelevant. A bug doesn't disappear because the latter is the python idiom for the former. This is a process that in the past has taught me a lot (not Python, but that's just because I'm too new here :). Once you get good at this process, it often helps you find the problem even before posting, because boiling code down to what the /real/ problem is can show you a lot you otherwise miss. Sure, but if you do that, you sometimes have code that deviates from the python idiom because there was originnaly a good reason for, but that isn't obvious any more because you are temporarily working with a boiled down piece of code. People making suggestions on how to make that boiled down code look more pythonic by using the python idiom are IMO not very helpfull because essentially those are just aesthetic. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Understanding Unicode encodings
Hello, For my application, I would like to execute an SQL query like this: self.dbCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum, path) VALUES ('%s', %s, %s, %s, '%s') % (track, nbr, idartist, idalbum, path)) where the different variables are returned by the libtagedit python bindings as Unicode. Every time I execute this, I get an exception like this: UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position 64: ordinal not in range(128) I tried to encode the different variables in many different encodings (latin1), but I always get an exception. Where does this ascii codec error comes from? How can I simply build this query string? Thanks in advance. Best Regards, Raphael -- http://mail.python.org/mailman/listinfo/python-list
Re: httplib, threading, wx app freezing after 4 hours
Mark rainess wrote: [...] It runs perfectly for about 4 hours, then freezes. I'm stuck. How do I debug this? [...] Can anyone suggest techniques to help me learn what is going on. By inspection: errcode is undefined; I expect you stripped the example a bit too far. If it is set to something other 200, it looks like you loop out. You are calling wx.CallAfter() from a different thread than runs the GUI. Is that documented to be safe? I've read that wxPostEvent() is is the call to use for this. Next thing to try is adding enough logging to tell exactly what statement hangs. -- --Bryan -- http://mail.python.org/mailman/listinfo/python-list
Re: httplib, threading, wx app freezing after 4 hours
[EMAIL PROTECTED] wrote: Mark rainess wrote: [...] It runs perfectly for about 4 hours, then freezes. I'm stuck. How do I debug this? [...] Can anyone suggest techniques to help me learn what is going on. By inspection: errcode is undefined; I expect you stripped the example a bit too far. If it is set to something other 200, it looks like you loop out. You are calling wx.CallAfter() from a different thread than runs the GUI. Is that documented to be safe? I've read that wxPostEvent() is is the call to use for this. Next thing to try is adding enough logging to tell exactly what statement hangs. Thanks guys, I found the problem. I had screen-saver set to None and power set to blank monitor after 30 minutes. The problem occurred after the monitor blanked. I remembered I re-flashed my bios a few weeks ago. I didn't check the bios power-management settings. I'm not going to reboot now to check because I have too much stuff open. I set power to never blank monitor. Now there is no problem. I added code to monitor for activity and to kill and restart the thread if activity stops. Now if power-management kills it, it wakes-up when the screen returns. I think using wx.CallAfter() the way I have is correct. I will check that. It does work properly though. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode encodings
[EMAIL PROTECTED] wrote: Hello, For my application, I would like to execute an SQL query like this: self.dbCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum, path) VALUES ('%s', %s, %s, %s, '%s') % (track, nbr, idartist, idalbum, path)) No, I'll bet that you'd like to run something like self.dcCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum,path) VALUES (%(track)s, %(nbr)s, %(idartist)s,%(idalbum)s,'%(path)s'), {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path}) (only without my typos). That's an improvment for a number of reasons, one of which is that the system will quote for you, for instance in idartist=John's Beer changing the single quote to two single quotes to suit SQL. Every time I execute this, I get an exception like this: UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position 64: ordinal not in range(128) I tried to encode the different variables in many different encodings (latin1), but I always get an exception. Where does this ascii codec error comes from? How can I simply build this query string? Some more information may help: is the error returned before or during the execute call? If before, then the execute() call is a distraction. If during, then what is your dB, what is it's encoding (is the dB using latin1, or does the dB only accept ascii?), and what are you using to connect to it? Jim -- http://mail.python.org/mailman/listinfo/python-list
Python for Lazarus (not Delphi)
Does anyone know if something similar to Python for Delphi does exist for lazarus? Thanks for any pointers, Uwe -- http://mail.python.org/mailman/listinfo/python-list
Re: range() is not the best way to check range?
Antoon Pardon wrote: Except that if you write your own class from scratch, you can't use it as a slice. Correct, but we were actually discussing subclassing built-in classes for use as a replacement for range/xrange. :-) It may be hard work writing all those methods in a totally new range/xrange class, but passing objects of that class around should prove satisfactory for the use of most programs. I personally doubt that it is that much hard work, especially if you stick to a reasonable selection of list capabilities, for example, rather than attempting to emulate support for every dodgy trick available to the programmer in the modern CPython arsenal. For a language that is supposed to be about duck typing I find it strange that if I make my own class with a start, stop and step attribute, that python barfs on it when I want to use it as a slice. Yes, my post showed this and gave a reference to where in the CPython source code the tests for specific types are performed. Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: function v. method
On 2006-07-22 16:32:38, danielx wrote: ...and source code... *shudders* What happened to all the goodness of abstraction? Abstraction as you seem to use it requires complete docs of the interface. Which is what you said you don't have... So the original abstractor broke the abstraction when publishing insufficient docs, not the one who looks into the sources to find out what actually happens. Absolutely. I didn't mean the user was breaking abstraction (let's not blame the victim). I was saying that we should really have more sympathy for him. I have all the sympathy in the world for him... after all, he's me :) But one reason why I try to write (and insist on as much as possible from people writing for or with me) self-documenting code is that wherever you have documentation and code separated (which is the case of API docs), you can bet (without losing) that sooner or later code and doc will diverge. This has a probability that approaches 1 :) So, I agree with you that good API docs are a good thing, as they tell me everything I need to know without having to wade through tons of implementation details that may be interesting but don't serve my immediate need (of having to use the API). But reality seems to be (and mine so far definitely is) that these docs, even the good ones, are not completely in alignment with the reality of the code. (We all know that code has bugs... and the API always describes, at best, how the code /should/ work. It never describes how it actually works, including the bugs g (this notwithstanding the bugs that have been elevated to features and henceforth been described in the API docs). So... the final authority /is/ the code. I don't see an alternative. For me, good abstraction doesn't mean I don't have to read the sources; good abstraction means (among other things) that I can read the sources easily. Gerhard -- http://mail.python.org/mailman/listinfo/python-list
building lists of dictionaries
Hi all, I tried this piece of code (FWIW, it was taken as is from a help section of mpfit, a mathematical routine for least square fitting): parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 parinfo[0]['fixed'] = 1 parinfo[4]['limited'][0] = 1 parinfo[4]['limits'][0] = 50. The first line builds a list of six dictionaries with initialised keys. I expected that the last three lines would only affect the corresponding keys of the corresponding dictionnary and that I would end up with a fully initialised list where only the 'fixed' key of the first dict would be 1, and the first values of limited and limits for dict number 4 would be 1 and 50. respectively This is not so! I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. I do not understand this behaviour... Thanks for helping a newbie. JF -- http://mail.python.org/mailman/listinfo/python-list
Re: Coding style
On 2006-07-23 06:24:09, Antoon Pardon wrote: In general, I'd say that in this case the example was not well-chosen. After such a shudder removal, a poster should IMO review what caused the shudder, and rephrase the original problem without the shudder. The shudder is not with the poster. The poster can't predict how the readers will react. Correct. But if someone with the potential to help shudders, I might just rephrase my question (the code) to suit that person's preferences. Why not? That might take a few iterations, but in general, either one or more of the shudder removals actually address the OP's issue, maybe without any individual helper understanding all of the problem (due to incomplete example code), or the iterations lead to a code that's shudder free and still shows the original problem -- now usually in a clearer form, because free of other issues that are not relevant to the OP's point. I doubt that. I'm not sure, but I've seen in the short time I've been here that a few of the regulars both have their specific shudder issues, and post helpful alternative solutions and corrections. So I think it definitely can help to address their shudder issues and get this out of the way. My experience is that you have more chance of being helped by people who try to understand where the OP is trying to go form than from those who react to style issues. No contest to that. But why not talk to the others, who in a way insist on their idiom but otherwise contribute to helpful solutions, in their idiom? I have never seem a bug go away because someone suggested to write if Number: instead of if Number != 0: Of course. I didn't mean to imply this. But if the only thing that's required from me is to rephrase my code idiom and replace the number != 0 comparisons with number, that's a small price to pay. You are right, IMO, that this stuff comes over as petty sometimes. And that it often is far away from the original question. But just as often corrections that at first seem petty lead to different ideas about implementation that the OP didn't consider. My view is: I ask for help on a public forum. I get what I get... and if I consider that someone who responded with something that's not immediately helpful has a potential to help me better, I try to get in a dialog and address what has been suggested, carving out my real problem in the idiom of that person. This is my responsibility -- after all, I'm the one who wants to learn how that person would solve my problem. Later then I can try to separate what I want to use from that solution from what I consider personal preference of the helping person. E.g. if someone makes a suggestion that is valid with the example code I posted but not with the real code I have, I need to post an updated example code that introduces a restriction similar to the one I actually have, which then correctly invalidates the formerly valid suggestion -- and helps getting more helpful responses. Not necessarily. A valid suggestion doesn't imply a relevant suggestion. If someone come with code like: if Number != 0: And someone suggests that the python idiom is if Number: Then that suggestion is valid, but it also is irrelevant. Correct. But if it helps the person who suggested it to see the original problem, now not hidden (for that person) under offending idiom, then the change was relevant -- not for the problem, but for the communication about it. It's like if you want good stock advice from your uncle, who's experienced in the matter but very formal, ask him without using the f*** word. You can use it when asking your other uncle, who's also experienced but doesn't really care about formalities. Tune in if you want a good channel... This is a process that in the past has taught me a lot (not Python, but that's just because I'm too new here :). Once you get good at this process, it often helps you find the problem even before posting, because boiling code down to what the /real/ problem is can show you a lot you otherwise miss. Sure, but if you do that, you sometimes have code that deviates from the python idiom because there was originnaly a good reason for, but that isn't obvious any more because you are temporarily working with a boiled down piece of code. People making suggestions on how to make that boiled down code look more pythonic by using the python idiom are IMO not very helpfull because essentially those are just aesthetic. I agree that mere aesthetic changes usually don't address the problem in itself. But it's just like posting here in a distant Chinese dialect won't do you any good (and neither in a distant, say, Scottish dialect :). If you want to get up a helpful communication, you need to use the right language. Here it is English and Python :) and if making my example code more pythonic (and my textual part more English) helps me creating a better
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 With this, you are creating a list with 6 references to the same list. Note that the left operand of '*' is evaluated only once before multiplying it six times. Regards, Tito -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
Jean_Francois Moulin wrote in news:[EMAIL PROTECTED] in comp.lang.python: Hi all, I tried this piece of code (FWIW, it was taken as is from a help section of mpfit, a mathematical routine for least square fitting): parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 parinfo[0]['fixed'] = 1 parinfo[4]['limited'][0] = 1 parinfo[4]['limits'][0] = 50. The first line builds a list of six dictionaries with initialised keys. I expected that the last three lines would only affect the corresponding keys of the corresponding dictionnary and that I would end up with a fully initialised list where only the 'fixed' key of the first dict would be 1, and the first values of limited and limits for dict number 4 would be 1 and 50. respectively This is not so! I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. I do not understand this behaviour... This should help: url:http://www.python.org/doc/faq/programming/#how-do-i-create-a- multidimensional-list As a TinyUrl: http://tinyurl.com/s8akj Rob. -- http://www.victim-prime.dsl.pipex.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 parinfo[0]['fixed'] = 1 parinfo[4]['limited'][0] = 1 parinfo[4]['limits'][0] = 50. The first line builds a list of six dictionaries with initialised keys. I expected that the last three lines would only affect the corresponding keys of the corresponding dictionnary and that I would end up with a fully initialised list where only the 'fixed' key of the first dict would be 1, and the first values of limited and limits for dict number 4 would be 1 and 50. respectively This is not so! I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. I do not understand this behaviour... The *6 creates multiple references to the same dictionary. Thus, when you update the dictionary through one reference/name (parinfo[0]), the things that the other entries (parinfo[1:5]) reference that changed dictionary. You're likely looking for something like parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}] for i in xrange(1,6): parinfo.append(parinfo[0].copy()) or something like parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Thus, you'd also want to change it to be something like parinfo = [ {'value':0., 'fixed':0, 'limited':[0, 0][:], 'limits':[0., 0.][:] }.copy() for i in xrange(0, 6)] where the slice operator is used to build a copy of the list for each element as well (rather than keeping a reference to the same list for each dictionary). Hopefully this makes some sense, and helps get you on your way. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
Jean_Francois Moulin wrote: Hi all, I tried this piece of code (FWIW, it was taken as is from a help section of mpfit, a mathematical routine for least square fitting): parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 The first line builds a list of six dictionaries with initialised keys. This is not so! I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. I do not understand this behaviour... Thanks for helping a newbie. JF xvec = [{'value':0}]*6 xids = [id(x) for x in xvec] print xids Should print a list of six integers, all equal. So all elements in your list are the same. Another way to construct the desired list: yvec = [{'value':0} for i in range(6)] yids = [id(y) for y in yvec] print yids The elements in this list should be all different. -- http://mail.python.org/mailman/listinfo/python-list
Re: Which Pyton Book For Newbies?
In article [EMAIL PROTECTED], [EMAIL PROTECTED] wrote: W. D. Allen wrote: I want to write a retirement financial estimating program. Python was suggested as the easiest language to use on Linux. I have some experience programming in Basic but not in Python. I have two questions: 1. What do I need to be able to make user GUIs for the program, and 2. Which book would be easiest to use to learn Python programming? I am a fairly experienced programmer and I have been reading Dive Into Python. If you have prior experience, you may find it very satisfactory. (But see a recent thread I started which points out a few small mistakes...nothing too bad over all.) If you have less programming experience, you may wish to look at Byte of Python. The great thing about Python is that there is a ton of online material to peruse... As fond as I am of Python (enough so to have been recognized for my advocacy by the community), and as often as I've lauded Python for its easy entry, I feel compelled to observe that it *is* possible to program in Basic under Linux; perhaps such an approach would particularly suit you. Have you considered, for example, URL: http://directory.google.com/Top/Computers/Programming/Languages/BASIC/ ? -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
Tim Chase wrote: parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Interesting. Cut-and-paste to my python prompt and I get parinfo[5]['limited'] [0, 0] Tried both Python 2.4.1 and 2.5 beta, Linux, GCC 4.0.2 -- http://mail.python.org/mailman/listinfo/python-list
Don't you hate it when your bed squeaks?
Don't you hate it when your bed squeaks? http://www.beyond-science.com -- http://mail.python.org/mailman/listinfo/python-list
Machine intelligence- can this program be written ??
Machine intelligence- can this program be written ?? Machine intelligence- can this program be written ?? In this game of chess it is a set of 7 chess boards The PAWN the pawn can move just like the regular game but up and down no levels The rook on the end moves all the way up and left to right back and forth The knight moves in a L shape forward and backwards but only one level step on the board back and forth The bishop moves in a x just like in regular game but can move to all levels back and forth The Queen moves just like in the regular game but can move on all levels back and forth The king can move one square at a time and to move one level back and forth NOW EVERY TIME THE GAME HAS ENDED THE 7 LEVEL BOARD IS PLACED IN A CUBE WITH BOARD ARE ADD ON EACH SIDE TO KEEP IN A CUBE AND GROWS INFINITE UNTIL THE MACHINE INTELLIGENCE ASK FOR MULTI-DIMENSIONS THE OBJECT IS: PROTECTING ALL PIECES WITH ONE OR MORE PIECES. ONLY LOSE A PIECE BY HIM TAKING ONE, AND THEN YOU TAKING ANOTHER. TRY TO MAKE IT WHERE HE CANNOT MOVE TAKE ALL CHESS PIECES -- http://mail.python.org/mailman/listinfo/python-list
cherrypy tracing back all the time
I've got a very simple script with cherrypy but for some reason the cherrypy server is constantly tracing back but it stays up, kind of, the performance etc though shows that something is wrong.import cherrypyimport threading def someFunc(): while 1: print workingthreading._start_new_thread( someFunc , () )class someClass(): def index(self): test=this is a test return test index.exposed=Truecherrypy.root=someClass()cherrypy.config.update( file=cherrypyconfig.txt )cherrypy.server.start()The tracebacks I get are:Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack 2006/07/23 17:58:46 INFO Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgi.py, line 110, in wsgiApp environ['wsgi.url_scheme'], File /usr/lib/python2.4/site-packages/cherrypy/_cpserver.py, line 227, in request requestLine, headers, rfile, scheme) File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 181, in __init__ self.parseFirstLine() File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 223, in parseFirstLine request.method, path, request.protocol = request.requestLine.split()ValueError: too many values to unpackI don't understand, all I've asked the stupid cherrypy thing to do is to print one line... -- http://mail.python.org/mailman/listinfo/python-list
Re: Which Pyton Book For Newbies?
W. D. Allen wrote: I want to write a retirement financial estimating program. Python was suggested as the easiest language to use on Linux. I have some experience programming in Basic but not in Python. I have two questions: 1. What do I need to be able to make user GUIs for the program, and 2. Which book would be easiest to use to learn Python programming? Thanks, WDA [EMAIL PROTECTED] end I'm sure you will hear this many times, but that's a great choice ;). I really think you'll like Learning Python from O'Reilly Press. The authors claim you can read the book even with no prior programming experience, which seems plausible having read it. Of course, you already have some programming experience, so it should go much more smoothly with you. Good luck finding the right book! -- http://mail.python.org/mailman/listinfo/python-list
Re: cherrypy tracing back all the time
On 23/07/06, Hari Sekhon [EMAIL PROTECTED] wrote: I've got a very simple script with cherrypy but for some reason the cherrypy server is constantly tracing back but it stays up, kind of, the performance etc though shows that something is wrong.import cherrypy import threading def someFunc(): while 1: print workingthreading._start_new_thread( someFunc , () )class someClass(): def index(self): test=this is a test return test index.exposed=Truecherrypy.root=someClass()cherrypy.config.update( file=cherrypyconfig.txt )cherrypy.server.start()The tracebacks I get are:Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack 2006/07/23 17:58:46 INFO Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgi.py, line 110, in wsgiApp environ['wsgi.url_scheme'], File /usr/lib/python2.4/site-packages/cherrypy/_cpserver.py, line 227, in request requestLine, headers, rfile, scheme) File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 181, in __init__ self.parseFirstLine() File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 223, in parseFirstLine request.method, path, request.protocol = request.requestLine.split()ValueError: too many values to unpackI don't understand, all I've asked the stupid cherrypy thing to do is to print one line... Ok, when the somefunc() that is executed by threading runs, it doesn't cause the tracing back I showed above, it occurs only when I use one of the other two functions I have in it's place in the script via threading. The thing is, those other two functions both use try excepts inside to make sure there are no exceptions generated beyond the internal function, and neither of those functions can touch anything to do with the class used as the index of the cherrypy document root. What is going on? -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested function scope problem
Bruno Desthuilliers wrote: Josiah Manson a écrit : I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only problem is that my little helper function doesn't work! It claims that a variable doesn't exist. If I move the variable declaration, it finds the variable, but can't change it. Declaring the variable global in the nested function doesn't work either. But, changing the variable in the containing scope is the whole purpose of this helper function. I'm new to python, so there is probably some solution I haven't encountered yet. Could you please suggest a nice clean solution? The offending code is below. Thanks. def breakLine(s): Break a string into a list of words and symbols. def addTok(): if len(tok) 0: if tok: An empty sequence evals to False in a boolean context. ls.append(tok) tok = '' I can't figure out why Josiah's breakLine function won't work either. I know Josiah has had his problem resolved, but I'd still like to know why his func won't work. I'd like to redirect this discussion in that direction, if I may. First point: the nested function only have access to names that exists in the enclosing namespace at the time it's defined. Coming from lisp, that doesn't make very much sense, and I'm not sure that's true. If you move the def for addTok bellow the lines that initialize the locals of breakLines, you still get the same problem. Second point: a nested function cannot rebind names from the enclosing namespace. Note that in Python, rebinding a name and modifying the object bound to a name are very distinct operations. I'm not sure that's the problem, because when I ran the debugger, the problem is with the line that says if len(tok), not the one bellow it which says tok = . Regardless, my above issue seems to be overriding this one. Third point : functions modifying their environment this way are usually considered bad form. Again, this is coming from lisp, but I don't see anything wrong with that :P. *** After some experimentation, I am completely baffeled as to why breakLine won't work. Here is an example of one of the things I did, which I believe exactly mimics what breakLine does: def outer(): ... def inner(): ... if outerLocal: ... return I hear you, 'hello world'. ... else: ... return Come again? ... outerLocal = hello world ... return inner() ... outer() I hear you, 'hello world'. As I said, I believe the line which sets tok should break (quietly), but not the line which tests tok. My experiment seems to confirm this... One thing I can understand is why the line tok = in addTok won't work. This is because when Python sees that line, it should create a new local variable in the scope of addTok. Once addTok returns, that variable is lost. That's pretty deep, now that I've thought about it... Here's a possible solution - but note that there are probably much better ways to get the same result... def breakline(line): Break a string into a list of words and symbols. class TokenList(list): def append(self, token): if token: list.append(self, token) return '' tokens = TokenList() token = '' splitters = '?()|:~,' whitespace = ' \t\n\r' specials = splitters + whitespace for char in line: if char in specials: token = tokens.append(token) if char in splitters: tokens.append(char) else: token += char tokens.append(token) return list(tokens) (snip) -- http://mail.python.org/mailman/listinfo/python-list
Re: getaddrinfo not found on SCO OpenServer 5.0.5
Martin v. Löwis wrote: [EMAIL PROTECTED] wrote: 1) I've seen mention of native vs. Python getaddrinfo implementations. If that's true, how can I force the program to use the Python one? 2) Is there an option to not use the BSD Library function? 3) Finally, is there a trick to searching for shared libaries? There is an autoconf test to determine whether getaddrinfo is available on the system. You should study that test to find out why it thinks the function is available when it is actually not. If you can't do that, you can manually edit pyconfig.h to change the outcome of configure. Regards, Martin Thank you for the suggestion. I will try it July 24 when I am back in the office. I've dug into it further and, on the surface, it appears that IPv6 was identified as available when the binary was built for OSR 5. IPv6 is not available for OSR5, so the binary was built on OSR 6 and mislabeled, or the autoconf test failed. Thanks again, Martin, for the relevant reply, Dave Harris -- http://mail.python.org/mailman/listinfo/python-list
Referring to Interpreter (Jython)
Hi everyone, I apologize if this is a bit off-topic. I am currently working on a Java-based application that is used Jython as a scripting language within the actual program. However, for one of my GUI elements, I need to pass the actual PythonInterpeter to the constructor. I know that in Java there is the this keyword which allows an object to refer to itself. Does anyone know if there is a way to use a similar type of keyword to have the PythonInterpreter refer to itself? Thanks, Wojciech P.S. If there is a more appropriate newsgroup to which I should post this, please let me know. -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode encodings
[EMAIL PROTECTED] wrote: I tried to encode the different variables in many different encodings (latin1), but I always get an exception. Where does this ascii codec error comes from? How can I simply build this query string? Raphael, The 'ascii' encoding is set in the python library file site.py (/usr/lib/python2.4/site.py on my gentoo machine) as the system default encoding for python. The solution I used to the problem you're describing was to create a sitecustomize.py file and redefine the encoding as 'utf-8'. The entire file contents look like this: ''' Site customization: change default encoding to UTF-8 ''' import sys sys.setdefaultencoding('utf-8') For more info on creating a sitecustomize.py file, read the comments in the site.py file. I use UTF-8 because I do a lot of multilingual text manipulation, but if all you're concerned about is Western European, you could also use 'latin1'. This gets you halfway there. Beyond that you need to stringify the (potentially Unicode) strings during concatenation, e.g.: self.dbCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum, path) VALUES ('%s', %s, %s, %s, '%s') % \ (str(track), nbr, idartist, idalbum, path)) (Assuming that track is the offending string.) I'm not exactly sure why this explicit conversion is necessary, as it is supposed to happen automatically, but I get the same UnicodeDecodeError error without it. Hope this helps, Cary -- http://mail.python.org/mailman/listinfo/python-list
Re: Referring to Interpreter (Jython)
Hi everyone, Ah, after some more searching and programming, I got it: PythonInterpreter py = new PythonInterpreter(); py.set(interpreter, py); Thanks, Wojciech [EMAIL PROTECTED] wrote: Hi everyone, I apologize if this is a bit off-topic. I am currently working on a Java-based application that is used Jython as a scripting language within the actual program. However, for one of my GUI elements, I need to pass the actual PythonInterpeter to the constructor. I know that in Java there is the this keyword which allows an object to refer to itself. Does anyone know if there is a way to use a similar type of keyword to have the PythonInterpreter refer to itself? Thanks, Wojciech P.S. If there is a more appropriate newsgroup to which I should post this, please let me know. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested function scope problem
On 2006-07-23 14:53:33, danielx wrote: I can't figure out why Josiah's breakLine function won't work either. I know Josiah has had his problem resolved, but I'd still like to know why his func won't work. I'd like to redirect this discussion in that direction, if I may. I think what happens is this (and this may not be expressed in the proper terms for Python): It is possible to read variables from the outer function in the inner function. But when trying to write to them, this causes that same name to be re-defined in the inner function's scope, making it a different variable. Now, in the OP's code, that caused that new variable (with scope of the inner function) to be accessed before anything was assigned to it. One obvious way is to not write to the variables from the outer scope, but rather return a value from the inner function and assign it in the outer function. But it seems there should be a way to be able to write in the inner function to variables that are defined in the outer function. First point: the nested function only have access to names that exists in the enclosing namespace at the time it's defined. Coming from lisp, that doesn't make very much sense, and I'm not sure that's true. If you move the def for addTok bellow the lines that initialize the locals of breakLines, you still get the same problem. The problem there is only secondarily a scope problem. At first it is reading a variable (the inner function scope variable tok) before anything has been assigned to it. Of course, the real problem is the secondary one: that this variable tok is a variable of scope addTok and not of scope breakLine. Second point: a nested function cannot rebind names from the enclosing namespace. Note that in Python, rebinding a name and modifying the object bound to a name are very distinct operations. I'm not sure that's the problem, because when I ran the debugger, the problem is with the line that says if len(tok), not the one bellow it which says tok = . Regardless, my above issue seems to be overriding this one. Yes, but it is the line tok = '' that seems to cause tok to be now a variable of the inner function's scope (rather than the variable tok of breakLine). After some experimentation, I am completely baffeled as to why breakLine won't work. Here is an example of one of the things I did, which I believe exactly mimics what breakLine does: def outer(): ... def inner(): ... if outerLocal: ... return I hear you, 'hello world'. ... else: ... return Come again? ... outerLocal = hello world ... return inner() ... outer() I hear you, 'hello world'. As I said, I believe the line which sets tok should break (quietly), but not the line which tests tok. My experiment seems to confirm this... The line that sets tok causes tok to be a different tok from the outer tok -- in the whole scope of the assignment. Gerhard -- http://mail.python.org/mailman/listinfo/python-list
Re: Python for Lazarus (not Delphi)
Uwe Grauer wrote: Does anyone know if something similar to Python for Delphi does exist for lazarus? Thanks for any pointers, Uwe Python for Delphi does support Lazarus since Version 3.29 http://mmm-experts.com/VersionHistory.aspx?ProductId=3 -- http://mail.python.org/mailman/listinfo/python-list
Generating all possible combination of elements in a list
Hello, I need to write scripts in which I need to generate all posible unique combinations of an integer list. Lists are a minimum 12 elements in size with very large number of possible combination(12!) I hacked a few lines of code and tried a few things from Python CookBook (http://aspn.activestate.com/ASPN/Cookbook/), but they are hell slow. Does any body know of an algorithm/library/module for python that can help me in generation of these combinations faster ONLY REQUIREMENT IS SPEED Example Problem: Generate all possible permutations for [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] [1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2] (notice an extra 2 ) eliminate some combinations based on some conditions and combine the rest of combinations. And now generate all possible combinations for resulting data set. Hope you get the idea. Thanks PS: Tried matlab/scilab. They are slower than python. -- http://mail.python.org/mailman/listinfo/python-list
How to generate geometric random numbers?
Hi all, I am a newbie to Python and would like to genereate some numbers according to geometric distribution. However, the Python Random package seems do not have implemented functionality. I am wondering is there exist any other libraries that can do this job? Thanks a lot, Da -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
Thank you guys. So the answer is to keep with the original form, perhaps with xrange. Ernesto -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate geometric random numbers?
Gerhard Fiedler wrote: On 2006-07-23 17:12:20, [EMAIL PROTECTED] wrote: I am a newbie to Python and would like to genereate some numbers according to geometric distribution. However, the Python Random package seems do not have implemented functionality. I am wondering is there exist any other libraries that can do this job? The usual way is to generate standard random numbers (linear distribution) and then apply whatever transformation you need to generate the desired distribution. That only works if there is such a transformation. The geometric distribution and many others have been implemented in numpy: http://www.scipy.org/NumPy In [1]: from numpy import random In [2]: random.geometric(0.5, size=100) Out[2]: array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1, 4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2, 1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1, 4, 2, 1, 1, 3, 1, 3, 1]) -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Interesting. Cut-and-paste to my python prompt and I get parinfo[5]['limited'] [0, 0] Tried both Python 2.4.1 and 2.5 beta, Linux, GCC 4.0.2 Of course. The expression within the list comprehension is evaluated for each iteration, so that the objects are recreated each time. The copy() for the dictionary is also not needed: parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]} for i in xrange(0,6)] parinfo [{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}] parinfo[0]['limited'] [0, 0] parinfo[0]['limited'][0]=1 parinfo [{'limited': [1, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}] Ernesto -- http://mail.python.org/mailman/listinfo/python-list
Re: How to force a thread to stop
Hi! threadicide method I like this word... Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Interesting. Cut-and-paste to my python prompt and I get parinfo[5]['limited'] [0, 0] Hmm...same behavior on my 2.3.5 here. Looks like the problem lay within the for-loop version I tried first: parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}] for i in xrange(1,6): parinfo.append(parinfo[0].copy()) which copied the elements, but when the elements were references to other lists, only the references were copied. I just tried it with the second method I suggested (the list-comprehension one you reference here) and it doesn't have the same problem as the crazy for-loop, and as shown by others, doesn't need the superflous copy() call either. That'll teach me to test both proposed ideas, rather than making assumptions. -tkc -- http://mail.python.org/mailman/listinfo/python-list
find_longest_match in SequenceMatcher
Hello, it might be too late or too hot, but i cannot work out this behaviour of find_longest_match() in difflib.SequenceMatcher: string1: releasenotesforwildmagicversion01thiscdromcontainstheinitialreleaseofthesourcecodethataccompaniesthebook3dgameenginedesign:apracticalapproachtorealtimecomputergraphicsthereareanumberofknownissuesaboutthecodeastheseissuesareaddressedtheupdatedcodewillbeavailableatthewebsitehttp://wwwmagicsoftwarecom/[EMAIL PROTECTED] string2: releasenotesforwildmagicversion02updatefromversion01toversion02ifyourcopyofthebookhasversion01andifyoudownloadedversion02fromthewebsitethenapplythefollowingdirectionsforinstallingtheupdateforalinuxinstallationseethesectionattheendofthisdocumentupdatedirectionsassumingthatthetopleveldirectoryiscalledmagicreplacebyyourtoplevelnameyoushouldhavetheversion01contentsinthislocation1deletethecontentsofmagic\include2deletethesubdirectorymagic\source\mgcapplication3deletetheobsoletefiles:amagic\source\mgc find_longest_match(0,500,0,500)=(24,43,10)=version01t What? O_o Clearly there is a longer match, right at the beginning! And then, after removal of the last character from each string (i found the limit of 500 by trial and error -- and it looks suspiciously rounded): find_longest_match(0,499,0,499)=(0,0,32)=releasenotesforwildmagicversion0 Is this the expected behaviour? What's going on? Thank you for any ideas -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode encodings
Jim wrote: [EMAIL PROTECTED] wrote: Hello, For my application, I would like to execute an SQL query like this: self.dbCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum, path) VALUES ('%s', %s, %s, %s, '%s') % (track, nbr, idartist, idalbum, path)) No, I'll bet that you'd like to run something like self.dcCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum,path) VALUES (%(track)s, %(nbr)s, %(idartist)s,%(idalbum)s,'%(path)s'), {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path}) (only without my typos). That's an improvment for a number of reasons, one of which is that the system will quote for you, for instance in idartist=John's Beer changing the single quote to two single quotes to suit SQL. self.dcCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum,path) VALUES (%(track)s, %(nbr)s, %(idartist)s,%(idalbum)s,'%(path)s'), {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path}) I see no improvement here. The OP's code is effectively:: sql = INSERT INTO track (name, ..., path) VALUES ('%s', ..., '%s') value_tuple = (track, , path) self.dcCursor.execute(sql % value_tuple) Your suggested replacement is effectively: sql = INSERT INTO track (name, ...,path) VALUES (%(track)s, ...,'%(path)s') str_fmt_dict = {'track':track, ...,'path':path} self.dcCursor.execute(sql, str_fmt_dict) Well, that won't run at all. Let's correct the presumed typo: self.dcCursor.execute(sql % str_fmt_dict) Now, the only practical difference is that you have REMOVED the OP's explicit quoting of the first column value. Changing the string formatting from the %s style to the %(column_name) style achieves nothing useful. You are presenting the system with a constant SQL string -- it is not going to get any chance to fiddle with the quoting. However the verbosity index has gone off the scale: each column name is mentioned 4 times (previously 1). I would suggest the standard default approach: sql = INSERT INTO track (name, ..., path) VALUES (?, ..., ?) value_tuple = (track, , path) self.dcCursor.execute(sql, value_tuple) The benefits of doing this include that the DBAPI layer gets to determine the type of each incoming value and the type of the corresponding DB column, and makes the appropriate adjustments, including quoting each value properly, if quoting is necessary. Every time I execute this, I get an exception like this: UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position 64: ordinal not in range(128) I tried to encode the different variables in many different encodings (latin1), but I always get an exception. Where does this ascii codec error comes from? How can I simply build this query string? Some more information may help: is the error returned before or during the execute call? If before, then the execute() call is a distraction. If during, then what is your dB, what is it's encoding (is the dB using latin1, or does the dB only accept ascii?), and what are you using to connect to it? These are very sensible questions. Some more q's for the OP: (1) What is the schema for the 'track' table? (2) I tried to encode the different variables in many different encodings (latin1) -- you say many different encodings but mention only one ... please explain and/or show a sample of the actual code of the many different attempts. (3) You said that your input values (produced by some libblahblah) were in Unicode -- are you sure? The exception that you got means that it was trying to convert *from* an 8-bit string *to* Unicode, but used the default ASCII codec (which couldn't hack it). Try doing this before the execute() call: print 'track', type(track), repr(track) ... print 'path', type(path), repr(path) and change the execute() call to three statements along the above lines, so we can see (as Jim asked) where the exception is being raised. HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Grail not downloading
Does anybody know anything about Grail? I've been unable to get at it, and I've tried on both Windows and Macintosh machines. http://grail.sourceforge.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating all possible combination of elements in a list
Mir Nazim wrote: Example Problem: Generate all possible permutations for [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] [1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2] (notice an extra 2 ) eliminate some combinations based on some conditions and combine the rest of combinations. And now generate all possible combinations for resulting data set. Hope you get the idea. Unfortunately, I don't. Why do you have two lists for which to generate permutations? Why is it important that the second list has an extra 2 (actually, not extra, but it replaces a 1)? What are some conditions by which to eliminate permutations? How to combine the remaining permutations? What is the resulting data set, and what is a possible combination of it? If the task is to produce all distinct permutations of 6 occurrences of 1 and 6 occurrences of 2, I suggest the program below. It needs produces much fewer than 12! results (namely, 924). Regards, Martin numbers = [1,2] remaining = [None, 6, 6] result = [None]*12 def permutations(index=0): if index == 12: yield result else: for n in numbers: if not remaining[n]: continue result[index] = n remaining[n] -= 1 for k in permutations(index+1): yield k remaining[n] += 1 for p in permutations(): print p -- http://mail.python.org/mailman/listinfo/python-list
Re: Grail not downloading
Dustan wrote: Does anybody know anything about Grail? I've been unable to get at it, and I've tried on both Windows and Macintosh machines. http://grail.sourceforge.net/ The files just don't exist, physically, on the server (if you have an SF account, you can check this yourself). However, it appears that the source code is still available through CVS: http://grail.cvs.sourceforge.net/grail/grail/ Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Track keyboard and mouse usage
Hi Francois, Thank you for providing me the evdev link! That was exactly what I was looking for. Instead of sudo'ing the script, I changed /dev/input/ directory to be world readable. After that, I had to change the way a file was accessed in evdev.py to: Line No: 91 #self.fd = os.open(filename, os.O_RDWR | os.O_NONBLOCK) self.fd = os.open(filename, os.O_RDONLY | os.O_NONBLOCK) Runs great. Thanks again. [EMAIL PROTECTED] wrote: Hello dfaber, I had the same problem not long ago. I tried to use the Xlib since its obvious the X server has all the events but I couldn't have the mouse events if my app was out of focus. If you have a way to do that I'm really interested. Anyway I found this to be a good introduction to Xlib: http://users.actcom.co.il/~choo/lupg/tutorials/xlib-programming/xlib-programming.html#preface Since I didn't find a way to do it I went down to the source of the events which are provided by the evdev drivers: http://en.wikipedia.org/wiki/Evdev Fortunately you can use Python to access it: http://svn.navi.cx/misc/trunk/python/evdev/ First you need to know your input devices, search the eventX in relation to your device here: cat /proc/bus/input/devices Then you can do: sudo python evdev.py /dev/input/eventX # where X is the event number in relation to your device (kb is usually zero) It works well but there is two problems with this solution: - the root access is annoying (but I'll have to try Diez suggestion) - The X event number of the mouse can change from a PC to another one (you need to check the PC first with that cat command and search for your mouse francois dfaber wrote: Hi all, I have been searching for a keyboard and mouse tracker on linux. I've read solutions (watch at sourceforge) which look at /proc/interrupts to check keyboard or mouse activity. I also read one post where watch seems to have difficulty tracking usb keyboards and mice. So, I'm out of ideas here. My goal are: 1. Check keyboard activity. I'm not interested in logging which keys are pressed or record them. 2. Monitor mouse activity. I want to know the pointer position, left-clicks, right-clicks, middle-mouse button usage. I know that these things can be done in a GUI environment. I am looking for some approach that helps me do this system-wide. Any suggestions would be welcome. Again, I am looking for trackers on Linux based machines. -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode encodings
[EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: I tried to encode the different variables in many different encodings (latin1), but I always get an exception. Where does this ascii codec error comes from? How can I simply build this query string? Raphael, The 'ascii' encoding is set in the python library file site.py (/usr/lib/python2.4/site.py on my gentoo machine) as the system default encoding for python. The solution I used to the problem you're describing was to create a sitecustomize.py file and redefine the encoding as 'utf-8'. Here is the word from on high (effbot, April 2006): (you're not supposed to change the default encoding. don't do that; it'll only cause problems in the long run). That exception is a wake-up call -- it means you don't have a clue how your 8-bit strings are encoded. You are intended to obtain a clue (case by case), and specify the encoding explicitly (case by case). Sure the current app might dump utf_8 on you. What happens if the next app dumps latin1 or cp1251 or big5 on you? This gets you halfway there. Beyond that you need to stringify the (potentially Unicode) strings during concatenation, e.g.: self.dbCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum, path) VALUES ('%s', %s, %s, %s, '%s') % \ (str(track), nbr, idartist, idalbum, path)) (Assuming that track is the offending string.) I'm not exactly sure why this explicit conversion is necessary, as it is supposed to happen automatically, but I get the same UnicodeDecodeError error without it. Perhaps if you were to supply info like which DBMS, type of the offending column in the DB, Python type of the value that *appears* to need stringification, ... we could help you too. Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re-evaluating a string?
I'm writing a program to send data over the serial port. I'm using pyserial, and I'm on WindowsXP. When I use literals I can get the data accross how I want it for example: 1 2 3 4 5 6 serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r') 1=Get devices attention 2=Select channel on device 3=Rate for movement 4=Low byte of 16 bits 5=High bytes of 16 bits 6=Carriage return signaling command is over This command works as desired. Sends the first 3 ASCII characters, then some numbers in hex followed by a carriage return. My problem is that the write() function only takes a string, and I want to substitute variables for the hex literals. I know that I can use the hex() function and it will return a string with the appropriate hex value, and I could combine this with some other literals like \\ to create my desired hex literal, but then I would need something to re-parse my string to change my ASCII text into the appropriate hex values. Any ideas on how I may do this? Any help is greatly appreciated. Thanks, Will -- http://mail.python.org/mailman/listinfo/python-list
url encode latin-1 characters
I want send latin-1 string to web server by url parameter urllib.quote return just symbol code with preceeding percent for every non-ascii character: #ustr = 'Ü' #urllib.quote(ustr) '%9A' but this seems to be wrong because server response contains my parameter and it differ from original (for example 'Ü' became '[') -- http://mail.python.org/mailman/listinfo/python-list
MULTI-DIMENTIONAL CHESS DOWNLOAD FROM DEEP SPACE
MULTI-DIMENTIONAL CHESS DOWNLOAD FROM DEEP SPACE HTTP://WWW.BEYOND-SCIENCE.COM -- http://mail.python.org/mailman/listinfo/python-list
Re: Re-evaluating a string?
serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r') [cut] My problem is that the write() function only takes a string, and I want to substitute variables for the hex literals. Well, you can try something like import types def makeString(a): ... return ''.join([type(x) != types.IntType and ... str(x) or chr(x) for x in a]) ... data = ['!SC', 1, 5, 0xFA, 0, '\r'] makeString(data) '!SC\x01\x05\xfa\x00\r' Thus, you can mix and match your desired data in a list, and then let Python intelligently smash together the string you want, so you can later pass that to your write() call. It does hiccup (read throw an exception) if you have an empty string in your list. It also falls down if you try and put in an integer constant that isn't in range(256). My advice regarding these would be don't do that. :) Alternatively, you can concoct some cousin-function to chr() that takes any old garbage along with a default, and returns either the chr() of it, unless that throws an expception, in which case you just return something like '\x00' (whatever you specified as the default). This allows you to use your favorite notation. If you like hex notation, you can use it (as in the 0xFA in the above data). If you prefer integers, you can toss them in the mix. Alternatively, you can create a suite of API wrapper functions, such as def move(rate, low, high, channel=1): serialport.write(''.join([type(x) != types.IntType and ... str(x) or chr(x) for x in ... ['!SC', channel, rate, low, high, '\r'] ... ])) (that could be uncompacted a bit for readibility's sake...) You could then just call move(5, 0xFA, 0) and the function does the heavy work for you. Might also be more readable later for other folks coming to the project (if there are others). Just a couple ideas you might want to try. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Re-evaluating a string?
bugnthecode wrote: I'm writing a program to send data over the serial port. I'm using pyserial, and I'm on WindowsXP. When I use literals I can get the data accross how I want it for example: 1 2 3 4 5 6 serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r') 1=Get devices attention 2=Select channel on device 3=Rate for movement 4=Low byte of 16 bits 5=High bytes of 16 bits 6=Carriage return signaling command is over This command works as desired. Sends the first 3 ASCII characters, then some numbers in hex followed by a carriage return. My problem is that the write() function only takes a string, and I want to substitute variables for the hex literals. I know that I can use the hex() function and it will return a string with the appropriate hex value, and I could combine this with some other literals like \\ to create my desired hex literal, but then I would need something to re-parse my string to change my ASCII text into the appropriate hex values. No need. That would be like travelling from Brooklyn to the Bronx via Walla Walla, WA. And the appropriate hex values exist only as ASCII text, anyway. The following code (a) is untested (b) assumes each of the 3 numbers (channel, rate, value) is UNsigned. channel = 1 rate = 5 value = 0xFA # or value = 250 if HARD_WAY: vhi, vlo = divmod(value, 256) command = !SC + chr(channel) + chr(rate) + chr(vlo) + chr(vhi) + \r elif MEDIUM_WAY: vhi, vlo = divmod(value, 256) command = !SC%c%c%c%c\r % (channel, rate, vlo, vhi) else: import struct command = !SC + struct.pack(BBH, channel, rate, value) + \r print repr(command) # for debugging, or if you're desperate to see some hex values :-) serialport.write(command) Do check out the manual sections relating to the struct module. If any value is signed, you definitely want to use the struct module (with b or h instead of B or H of course) instead of mucking about byte-bashing. HTH, John -- http://mail.python.org/mailman/listinfo/python-list
easy questions from python newbie
This is the first real python program I have ever worked on. What I want to do is: 1) count identical records in a cvs file 2) create a new file with quantities instead duplicate records 3) open the new file in ms-excel For example, I will start with a file like: 1001 1012 1008 1012 1001 1001 and finish with a file like: 1001,3 1008,1 1012,2 What I need to know: 1) is there a function in python that will sort a file into another file. Something like: sort file1.txt file2.txt from the DOS command line. I know there is also a similar sort funtion in Unix. 2) is there a function in python that will correctly load a csv file into an excel spreadsheet, and open that spreadsheet. 3) I will probably be working with 50 items, or less, would it be best for me to do this with a multi-diminsional array? For example: sort the file, read a rec into the array, if the next rec is the same then incr the count, otherwise add a new rec with a count of 1. Then write the array to a file? -- http://mail.python.org/mailman/listinfo/python-list
Re: Which Pyton Book For Newbies?
W. D. Allen wrote: I want to write a retirement financial estimating program. Python was suggested as the easiest language to use on Linux. I have some experience programming in Basic but not in Python. I have two questions: 1. What do I need to be able to make user GUIs for the program, and 2. Which book would be easiest to use to learn Python programming? Thanks, WDA [EMAIL PROTECTED] Tkinter is an easy to use GUI that comes with python. There's a good online manual, Tkinter reference: a GUI for Python, at http://infohost.nmt.edu/tcc/help/lang/python/tkinter.html HTH, ~Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: Track keyboard and mouse usage
Dennis Lee Bieber wrote: On 17 Jul 2006 21:00:09 -0700, dfaber [EMAIL PROTECTED] declaimed the following in comp.lang.python: Is there no clean method of accessing the keyboard device or the mouse on linux? It seems that looking at /proc/interrupts might prove to be useful for keyboard monitoring. What about checking if the left mouse button is clicked or finding the position of the cursor on the screen? ... I don't think anyone has ported raw X-protocol access to Python. Actually someone did. http://python-xlib.sourceforge.net/ It's old but it works fine. Speaks X protocol in pure python. HTH, ~Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: Re-evaluating a string?
Thanks Tim, and John for your quick responses! Tim, I tested your function and it works! Though I don't completely understand how. Could you possibly explain this? John, I test your MEDUIM_WAYand it works as well. How is it that putting the string together this way translates into a hex value to be transmitted to the serial port? When I manually tried to put a string together I couldn't get this to happen. I was trying: controlString = '!SC' + '\\' + ch.__str__() + '\\' + rate.__str__() ...etc also I noticed you added another line to the code which appears to split the low and high bytes for me? If so thanks! Could you also offer an explanation on how this works. I tried a google search and couldn't get a decent explanation. I implemented this a little differently as you can see in my Position class. Could you possibly offer any info on the benefits/drawbacks of the different methods? Thanks again to both of you for your quick responses and help. Will import serial import types class Position: def __init__(self, num): Takes the position as an int, and splits the low and high bytes into instance members. self.lowByte = num 0x00FF self.highByte = (num 0xFF00) 8 def __str__(self): Mainly for debugging purposes. Allows meaningful output when printed return 'Low: ' + self.lowByte.__str__() + '\nHigh: ' + self.highByte.__str__() def makeString(a): Takes in a list, and intelligentlly smashes everything together. Outputs everything as a hex string. Posted by: Tim Chase on comp.lang.python return ''.join([type(x) != types.IntType and str(x) or chr(x) for x in a]) def getVer(localpsc): Gets the version from the PSC. Mainly just to verify a connection localpsc.write('!SCVER?\r') localpsc.read(8) #Discard the echo! s = localpsc.read(3) print s def moveServo(localpsc, ch, rate, position): Takes in a serial object, the desired channel, the ramp rate, and the desired position of ther servo. Moves the servo to the desired postion. #localpsc.write('!SC', ch, rate, position.low, position.high, '\r') #controlString = makeString(['!SC', ch, rate, position.lowByte, position.highByte, '\r']) #localpsc.write(controlString) #localpsc.flushInput() #discard the echo! Following line from John Machin controlString = !SC%c%c%c%c\r % (ch, rate, position.lowByte, position.highByte) localpsc.write(controlString) psc = serial.Serial(1, 2400) mypos = Position(2500) moveServo(psc, 0, 5, mypos) psc.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: url encode latin-1 characters
Lurker wrote: I want send latin-1 string to web server by url parameter urllib.quote return just symbol code with preceeding percent for every non-ascii character: #ustr = 'Ü' #urllib.quote(ustr) '%9A' The latin1 encoding for U-with-umlaut is 0xDC. The cp850 encoding for the same is 0x9A. I deduce that you typed the above in a DOS command window on a Windows box. Fire up IDLE and try the same thing again. but this seems to be wrong because server response contains my parameter and it differ from original (for example 'Ü' became '[') You told it (maybe) that you were sending in latin1, then gave it 0x9A which in latin1 is a control character (SCI, single character introducer) and it gave you back a '[' ... garbage in, garbage out, perhaps? How about showing us (a) a snippet of the code you actually executed (b) the repr() of what was returned -- *NOT* a copy/paste from the screen; as shown above, a picture of U with two dots above it is not reliable information. Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Isn't there a better way?
In message [EMAIL PROTECTED], Bruno Desthuilliers wrote: Lawrence D'Oliveiro a écrit : If you're calling a number of different routines in the Processor class, all accessing the same data, then it makes perfect sense to only pass it once. Actually they are not passed. I think I'm going to plonk you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode encodings
John Machin wrote: Jim wrote: No, I'll bet that you'd like to run something like self.dcCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum,path) VALUES (%(track)s, %(nbr)s, %(idartist)s,%(idalbum)s,'%(path)s'), {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path}) (only without my typos). That's an improvment for a number of reasons, one of which is that the system will quote for you, for instance in idartist=John's Beer changing the single quote to two single quotes to suit SQL. I see no improvement here. The OP's code is effectively:: sql = INSERT INTO track (name, ..., path) VALUES ('%s', ..., '%s') value_tuple = (track, , path) self.dcCursor.execute(sql % value_tuple) Your suggested replacement is effectively: sql = INSERT INTO track (name, ...,path) VALUES (%(track)s, ...,'%(path)s') str_fmt_dict = {'track':track, ...,'path':path} self.dcCursor.execute(sql, str_fmt_dict) Well, that won't run at all. Let's correct the presumed typo: self.dcCursor.execute(sql % str_fmt_dict) I'm sorry, that wasn't a typo. I was using what the dBapi 2.0 document calls 'pyformat' (see the text under paramstyle in that document). Now, the only practical difference is that you have REMOVED the OP's explicit quoting of the first column value. Changing the string formatting from the %s style to the %(column_name) style achieves nothing useful. You are presenting the system with a constant SQL string -- it is not going to get any chance to fiddle with the quoting. However the verbosity index has gone off the scale: each column name is mentioned 4 times (previously 1). Gee, I like the dictionary; it has a lot of advantages. I would suggest the standard default approach: sql = INSERT INTO track (name, ..., path) VALUES (?, ..., ?) value_tuple = (track, , path) self.dcCursor.execute(sql, value_tuple) The benefits of doing this include that the DBAPI layer gets to determine the type of each incoming value and the type of the corresponding DB column, and makes the appropriate adjustments, including quoting each value properly, if quoting is necessary. I'll note that footnote [2] of the dBapi format indicates some preference for pyformat over the format above, called there 'qmark'. But it all depends on what the OP is using to connect to the dB; their database module may well force them to choose a paramstyle, AIUI. Anyway, the point is that to get quote escaping right, to prevent SQL injection, etc., paramstyles are better than direct string %-ing. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: easy questions from python newbie
walterbyrd wrote: This is the first real python program I have ever worked on. What I want to do is: 1) count identical records in a cvs file 2) create a new file with quantities instead duplicate records 3) open the new file in ms-excel For example, I will start with a file like: 1001 1012 1008 1012 1001 1001 and finish with a file like: 1001,3 1008,1 1012,2 What I need to know: 1) is there a function in python that will sort a file into another file. Something like: sort file1.txt file2.txt from the DOS command line. I know there is also a similar sort funtion in Unix. import os os.system('sort file1.txt file2.txt') 2) is there a function in python that will correctly load a csv file into an excel spreadsheet, and open that spreadsheet. What's with the excel files? You must be in industry. Excel opens csv files, no problem. In Mac OS X, you can do this: os.system('open -a /Applications/Excel %s' % 'my_file.csv') It probably requires a nauseating journey through a bunch of hoops to do the equivalent in window$. But you haven't specified your operating system, so I optimistically assume a best-case (OS X) and not a worst-case (window$). If worst-case, I'm truly sorry for your misfortune. 3) I will probably be working with 50 items, or less, would it be best for me to do this with a multi-diminsional array? For example: sort the file, read a rec into the array, if the next rec is the same then incr the count, otherwise add a new rec with a count of 1. Then write the array to a file? Ah, a real question. Use a dict: if adict.has_key(some_key): adict[some_key] += 1 else: adict[some_key] = 1 James -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Re-evaluating a string?
bugnthecode wrote: Thanks Tim, and John for your quick responses! Tim, I tested your function and it works! Though I don't completely understand how. Could you possibly explain this? John, I test your MEDUIM_WAYand it works as well. Now try the struct module approach. Are you 100% certain that the servo position can't be negative? What would you do if it could be negative? How is it that putting the string together this way translates into a hex value to be transmitted to the serial port? Like I tried to tell you before, there is no such thing as transmitting a hex value. You are transmitting 8-bit bytes, one bit at a time. The number 6 might be transmitted as 0110 or 0110 (the latter, IIRC -- it's been a while). The number 6 can be represented in a computer program as 6 or 0x6. As your write() function expects a string, you need to represent it in your program as a string e.g. '\x06' or chr(6) or %c % 6. Read about chr() in the manual: http://docs.python.org/lib/built-in-funcs.html Read about string formatting in the manual: http://docs.python.org/lib/typesseq-strings.html When I manually tried to put a string together I couldn't get this to happen. I was trying: controlString = '!SC' + '\\' + ch.__str__() + '\\' + rate.__str__() ...etc Ugh. FWIW, use str(foo) instead of foo.__str__() also I noticed you added another line to the code which appears to split the low and high bytes for me? If so thanks! Could you also offer an explanation on how this works. I tried a google search and couldn't get a decent explanation. I presume you mean vhi, vlo = divmod(value, 256) What did you search for? If you are on Windows: do(click on Start, hover on All Programs, hover on Python 2.4, click on Python Manuals, type in divmod, press Enter key twice) else: deduce that divmod can only be a built-in function (I didn't declare it, did I?) read web docs on built-in functions (http://docs.python.org/lib/built-in-funcs.html) I implemented this a little differently as you can see in my Position class. Could you possibly offer any info on the benefits/drawbacks of the different methods? No great difference. Better than both is to use the struct module. Thanks again to both of you for your quick responses and help. Will import serial import types class Position: It is extreme overkill, IMHO, to use a class for this trivium. def __init__(self, num): Takes the position as an int, and splits the low and high bytes into instance members. self.lowByte = num 0x00FF self.highByte = (num 0xFF00) 8 If you are sure that 0 = num = 0x, then you don't need the 0xFF00 mask. If you are unsure, then don't just silently transmit what may well be rubbish; check, or use an assertion: assert 0 = num = 0x def __str__(self): Mainly for debugging purposes. Allows meaningful output when printed FFS. Use the repr() built-in, like I suggested. return 'Low: ' + self.lowByte.__str__() + '\nHigh: ' + self.highByte.__str__() def makeString(a): Takes in a list, and intelligentlly smashes everything together. Outputs everything as a hex string. No it doesn't. Posted by: Tim Chase on comp.lang.python return ''.join([type(x) != types.IntType and str(x) or chr(x) for x in a]) def getVer(localpsc): Gets the version from the PSC. Mainly just to verify a connection localpsc.write('!SCVER?\r') localpsc.read(8) #Discard the echo! s = localpsc.read(3) print s def moveServo(localpsc, ch, rate, position): Takes in a serial object, the desired channel, the ramp rate, and the desired position of ther servo. Moves the servo to the desired postion. #localpsc.write('!SC', ch, rate, position.low, position.high, '\r') #controlString = makeString(['!SC', ch, rate, position.lowByte, position.highByte, '\r']) #localpsc.write(controlString) #localpsc.flushInput() #discard the echo! Following line from John Machin controlString = !SC%c%c%c%c\r % (ch, rate, position.lowByte, position.highByte) localpsc.write(controlString) psc = serial.Serial(1, 2400) mypos = Position(2500) moveServo(psc, 0, 5, mypos) psc.close() import struct DEBUG = True def moveServo(localpsc, ch, rate, position): Takes in a serial object, the desired channel, the ramp rate, and the desired position of the servo. Moves the servo to the desired position. assert 0 = ch = 255 # or tighter bounds from manuf's spec assert 0 = rate = 255 assert 0 = position = 65535 # aka 0x cmd = !SC + struct.pack(BBH, ch, rate, position) + \r if DEBUG: print moveServo:, repr(cmd) localpsc.write(cmd) It's that simple. If you are ever going to do more than this one script, it will pay to investigate the struct module. Python has batteries included. You don't need to find a lead mine and look up sal
Re: Re-evaluating a string?
Thanks Tim, and John for your quick responses! This is one of the better lists for getting quick (and usually helpful) responses. Tim, I tested your function and it works! Though I don't completely understand how. Could you possibly explain this? def makeString(a): ... return ''.join([type(x) != types.IntType and ... str(x) or chr(x) for x in a]) ... The boolean_expression and value1 or value2 is a common python idiom for something akin to C/C++/Java's ternary boolean_expression? value1: value2 expression. There are some gotchas (and workarounds for those gotchas) if value1 can be a false value (an empty string, a zero or empty list are good examples of this). It pretty much boils down to joining all the elements of the list that is composed from for every item in the list 'a', if it's not an int, just return the str() of it; and if it is an int, return the chr() of it. It then smashes them all together with the join() and returns the resulting string. John, I test your MEDUIM_WAYand it works as well. How is it that putting the string together this way translates into a hex value to be transmitted to the serial port? When I manually tried to put a string together I couldn't get this to happen. I was trying: controlString = '!SC' + '\\' + ch.__str__() + '\\' + rate.__str__() The string-formatting %c expects a byte and prints the ascii character relating to the byte. Also a good way to do things. Come to think of it, John had a lot of good ideas in his post. In your above code, the ch.__str__() creates a string representation of the number I presume is stored in ch. The string representation of a number is simply a string containing that number: x = 42 x.__str__() '42' Not very exciting. And generally stylistically better to use str() in favor of the __str__() method call. also I noticed you added another line to the code which appears to split the low and high bytes for me? If so thanks! Could you also offer an explanation on how this works. the divmod(x,y) function divides x by y, and returns a tuple. The first value of the tuple is the integer result of the division, and the second value of the tuple is the remainder. It's a one-step way of doing hi,lo = divmod(x,y) works like a condensing of hi = x / y lo = x % y I tried a google search and couldn't get a decent explanation. Googling the python docs for divmod should do the trick. http://www.google.com/search?q=site%3Adocs.python.org+divmod returns several helpful hits wherein you can learn more than any sane person should care to know. But the above primer should be enough to get you on your way. I think, in the case of your example string, using John's suggestion of the %c formatting is the cleanest approach. As such, I'd rework the move() function I suggested to simply be something like def move(rate,lo,hi,chan=1): return !SC%c%c%c%c\r % (chan, rate, lo, hi) where you possibly even just pass in the position parameter, and let the function do the splitting with something like def move(rate,position,chan=1) hi,lo = divmod(position 0x, 256) return !SC%c%c%c%c\r % (chan, rate, lo, hi) or optionally use the struct module to unpack them. Just a few more thoughts, -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode encodings
Jim wrote: John Machin wrote: Jim wrote: No, I'll bet that you'd like to run something like self.dcCursor.execute(INSERT INTO track (name, nbr, idartist, idalbum,path) VALUES (%(track)s, %(nbr)s, %(idartist)s,%(idalbum)s,'%(path)s'), {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path}) (only without my typos). That's an improvment for a number of reasons, one of which is that the system will quote for you, for instance in idartist=John's Beer changing the single quote to two single quotes to suit SQL. I see no improvement here. The OP's code is effectively:: sql = INSERT INTO track (name, ..., path) VALUES ('%s', ..., '%s') value_tuple = (track, , path) self.dcCursor.execute(sql % value_tuple) Your suggested replacement is effectively: sql = INSERT INTO track (name, ...,path) VALUES (%(track)s, ...,'%(path)s') str_fmt_dict = {'track':track, ...,'path':path} self.dcCursor.execute(sql, str_fmt_dict) Well, that won't run at all. Let's correct the presumed typo: self.dcCursor.execute(sql % str_fmt_dict) I'm sorry, that wasn't a typo. I was using what the dBapi 2.0 document calls 'pyformat' (see the text under paramstyle in that document). Oh yeah. My mistake. Noticed 'pyformat' years ago, thought What a good idea, found out that ODBC supports only qmark, SQLite supports only qmark, working on database conversions where the SQL was programatically generated anyway: forgot all about it. Now, the only practical difference is that you have REMOVED the OP's explicit quoting of the first column value. Changing the string formatting from the %s style to the %(column_name) style achieves nothing useful. You are presenting the system with a constant SQL string -- it is not going to get any chance to fiddle with the quoting. However the verbosity index has gone off the scale: each column name is mentioned 4 times (previously 1). Gee, I like the dictionary; it has a lot of advantages. Like tersemess? Like wide availibility? Anyway, the point is that to get quote escaping right, to prevent SQL injection, etc., paramstyles are better than direct string %-ing. And possible performance gains (the engine may avoid parsing the SQL each time). *NOW* we're on the same page of the same hymnbook, Brother Jim :-) Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate geometric random numbers?
Robert Kern wrote: Gerhard Fiedler wrote: On 2006-07-23 17:12:20, [EMAIL PROTECTED] wrote: I am a newbie to Python and would like to genereate some numbers according to geometric distribution. However, the Python Random package seems do not have implemented functionality. I am wondering is there exist any other libraries that can do this job? The usual way is to generate standard random numbers (linear distribution) and then apply whatever transformation you need to generate the desired distribution. That only works if there is such a transformation. The geometric distribution and many others have been implemented in numpy: http://www.scipy.org/NumPy In [1]: from numpy import random In [2]: random.geometric(0.5, size=100) Out[2]: array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1, 4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2, 1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1, 4, 2, 1, 1, 3, 1, 3, 1]) -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco Thanks a lot. I will try it out. But I am still surprised because the default Random package in Python can generate so few discrete random distritbuions, while it can generate quite a few continuous distribution, including some not very common one. Da -- http://mail.python.org/mailman/listinfo/python-list
Re: easy questions from python newbie
James Stroud wrote: walterbyrd wrote: This is the first real python program I have ever worked on. What I want to do is: 1) count identical records in a cvs file 2) create a new file with quantities instead duplicate records 3) open the new file in ms-excel For example, I will start with a file like: 1001 1012 1008 1012 1001 1001 and finish with a file like: 1001,3 1008,1 1012,2 What I need to know: 1) is there a function in python that will sort a file into another file. Something like: sort file1.txt file2.txt from the DOS command line. I know there is also a similar sort funtion in Unix. import os os.system('sort file1.txt file2.txt') 2) is there a function in python that will correctly load a csv file into an excel spreadsheet, and open that spreadsheet. What's with the excel files? You must be in industry. Excel opens csv files, no problem. In Mac OS X, you can do this: os.system('open -a /Applications/Excel %s' % 'my_file.csv') It probably requires a nauseating journey through a bunch of hoops to do the equivalent in window$. But you haven't specified your operating system, so I optimistically assume a best-case (OS X) and not a worst-case (window$). If worst-case, I'm truly sorry for your misfortune. Hey d00d, what's with the attit00d? All those $ signs? Do you get Excel for free on Mac OS X or does your department pay for it? Us 'doze dummies w/o a big endowment like UCLA wouldn't/couldn't afford that. Out here in the boonies we have to download the free stuff. C:\junkpython Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. import os os.system('C:\Program Files\OpenOffice.org 2.0\program\scalc foo.csv') 0 Look, d00d, no hoops. 3) I will probably be working with 50 items, or less, would it be best for me to do this with a multi-diminsional array? For example: sort the file, read a rec into the array, if the next rec is the same then incr the count, otherwise add a new rec with a count of 1. Then write the array to a file? Ah, a real question. Use a dict: if adict.has_key(some_key): Hey, d00d, ask the department sysadmin to update your Python for you, then you'll be able to use this: if some_key in adict: adict[some_key] += 1 else: adict[some_key] = 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: easy questions from python newbie
John Machin wrote: James Stroud wrote: walterbyrd wrote: This is the first real python program I have ever worked on. What I want to do is: 1) count identical records in a cvs file 2) create a new file with quantities instead duplicate records 3) open the new file in ms-excel For example, I will start with a file like: 1001 1012 1008 1012 1001 1001 and finish with a file like: 1001,3 1008,1 1012,2 What I need to know: 1) is there a function in python that will sort a file into another file. Something like: sort file1.txt file2.txt from the DOS command line. I know there is also a similar sort funtion in Unix. import os os.system('sort file1.txt file2.txt') 2) is there a function in python that will correctly load a csv file into an excel spreadsheet, and open that spreadsheet. What's with the excel files? You must be in industry. Excel opens csv files, no problem. In Mac OS X, you can do this: os.system('open -a /Applications/Excel %s' % 'my_file.csv') It probably requires a nauseating journey through a bunch of hoops to do the equivalent in window$. But you haven't specified your operating system, so I optimistically assume a best-case (OS X) and not a worst-case (window$). If worst-case, I'm truly sorry for your misfortune. Hey d00d, what's with the attit00d? All those $ signs? Do you get Excel for free on Mac OS X or does your department pay for it? Us 'doze dummies w/o a big endowment like UCLA wouldn't/couldn't afford that. Out here in the boonies we have to download the free stuff. I use FC4 and open office. I had to fire up a window$ machine to test the CSV thing, d00d. C:\junkpython Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. import os os.system('C:\Program Files\OpenOffice.org 2.0\program\scalc foo.csv') 0 Look, d00d, no hoops. Glad for you. 3) I will probably be working with 50 items, or less, would it be best for me to do this with a multi-diminsional array? For example: sort the file, read a rec into the array, if the next rec is the same then incr the count, otherwise add a new rec with a count of 1. Then write the array to a file? Ah, a real question. Use a dict: if adict.has_key(some_key): Hey, d00d, ask the department sysadmin to update your Python for you, then you'll be able to use this: if some_key in adict: adict[some_key] += 1 else: adict[some_key] = 1 Last I checked, both worked. James -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate geometric random numbers?
[EMAIL PROTECTED] writes: But I am still surprised because the default Random package in Python can generate so few discrete random distritbuions, while it can generate quite a few continuous distribution, including some not very common one. It looks pretty simple to transform the uniform distribution to the geometric distribution. The formula for its cdf is pretty simple: cdf(p,n) = (1-p)**(n-1)*p For fixed p, if the cdf is c, we get (unless I made an error), n = log(c, 1-p) - 1 So choose a uniform point c in the unit interval, run it through that formula, and round up to the nearest integer. See http://en.wikipedia.org/wiki/Geometric_distribution for more about the distribution. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate geometric random numbers?
Paul Rubin http://[EMAIL PROTECTED] writes: n = log(c, 1-p) - 1 I meantn = log(c/p, 1-p) - 1 sorry. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate geometric random numbers?
Paul Rubin wrote: Paul Rubin http://[EMAIL PROTECTED] writes: n = log(c, 1-p) - 1 I meantn = log(c/p, 1-p) - 1 sorry. import random from math import ceil, log def geometric(p): Geometric distribution per Devroye, Luc. _Non-Uniform Random Variate Generation_, 1986, p 500. http://cg.scs.carleton.ca/~luc/rnbookindex.html # p should be in (0.0, 1.0]. if p = 0.0 or p 1.0: raise ValueError(p must be in the interval (0.0, 1.0]) elif p == 1.0: # If p is exactly 1.0, then the only possible generated value is 1. # Recognizing this case early means that we can avoid a log(0.0) later. # The exact floating point comparison should be fine. log(eps) works just # dandy. return 1 # random() returns a number in [0, 1). The log() function does not # like 0. U = 1.0 - random.random() # Find the corresponding geometric variate by inverting the uniform variate. G = int(ceil(log(U) / log(1.0 - p))) return G -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate geometric random numbers?
Robert Kern [EMAIL PROTECTED] writes: G = int(ceil(log(U) / log(1.0 - p))) I usually owuld write that as int(ceil(log(U, 1.0 - p))). -- http://mail.python.org/mailman/listinfo/python-list
Re: find_longest_match in SequenceMatcher
koara wrote: Hello, it might be too late or too hot, but i cannot work out this behaviour of find_longest_match() in difflib.SequenceMatcher: string1: [snipped 500-byte string] string2: [snipped 500-byte string] find_longest_match(0,500,0,500)=(24,43,10)=version01t What? O_o Clearly there is a longer match, right at the beginning! And then, after removal of the last character from each string (i found the limit of 500 by trial and error -- and it looks suspiciously rounded): What limit? (a) My results [see below] (b) my inspection of the Python version 2.4 source for the difflib module (c) what I know of the author -- all tend to indicate that there is no hidden undocumented length limit. find_longest_match(0,499,0,499)=(0,0,32)=releasenotesforwildmagicversion0 Is this the expected behaviour? What's going on? My code: (koara.py) 8--- strg1 = rreleasenotesforwildmagicversion01thiscdromcontainstheinitialreleaseofthesourcecodethataccompaniesthebook3dgameenginedesign:apracticalapproachtorealtimecomputergraphicsthereareanumberofknownissuesaboutthecodeastheseissuesareaddressedtheupdatedcodewillbeavailableatthewebsitehttp://wwwmagicsoftwarecom/[EMAIL PROTECTED] strg2 = rreleasenotesforwildmagicversion02updatefromversion01toversion02ifyourcopyofthebookhasversion01andifyoudownloadedversion02fromthewebsitethenapplythefollowingdirectionsforinstallingtheupdateforalinuxinstallationseethesectionattheendofthisdocumentupdatedirectionsassumingthatthetopleveldirectoryiscalledmagicreplacebyyourtoplevelnameyoushouldhavetheversion01contentsinthislocation1deletethecontentsofmagic\include2deletethesubdirectorymagic\source\mgcapplication3deletetheobsoletefiles:amagic\source\mgc import sys print sys.version from difflib import SequenceMatcher as SM smo = SM(None, strg1, strg2) print len(strg1), len(strg2) print smo.find_longest_match(0, 500, 0, 500) print smo.find_longest_match(0, 499, 0, 499) print smo.find_longest_match(0, 100, 0, 100) print smo.find_longest_match(1, 101, 1, 101) print smo.find_longest_match(2, 102, 2, 102) 8--- The results on 4 python versions: C:\junkc:\python24\python koara.py 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] 500 500 (24, 43, 10) (24, 43, 10) (24, 43, 10) (24, 43, 10) (24, 43, 10) C:\junkc:\python23\python koara.py 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] 500 500 (24, 43, 10) (24, 43, 10) (24, 43, 10) (24, 43, 10) (24, 43, 10) C:\junkc:\python22\python koara.py 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] 500 500 (0, 0, 32) (0, 0, 32) (0, 0, 32) (1, 1, 31) (2, 2, 30) C:\junkc:\python21\python koara.py 2.1.3 (#35, Apr 8 2002, 17:47:50) [MSC 32 bit (Intel)] 500 500 (0, 0, 32) (0, 0, 32) (0, 0, 32) (1, 1, 31) (2, 2, 30) Looks to me like the problem has nothing at all to do with the length of the searched strings, but a bug appeared in 2.3. What version(s) were you using? Can you reproduce your results (500 499 giving different answers) with the same version? Anyway, as they say in the classics, Take a number; the timbot will be with you shortly. Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: easy questions from python newbie
In [EMAIL PROTECTED], walterbyrd wrote: This is the first real python program I have ever worked on. What I want to do is: 1) count identical records in a cvs file 2) create a new file with quantities instead duplicate records 3) open the new file in ms-excel For example, I will start with a file like: 1001 1012 1008 1012 1001 1001 and finish with a file like: 1001,3 1008,1 1012,2 What I need to know: 1) is there a function in python that will sort a file into another file. Something like: sort file1.txt file2.txt from the DOS command line. I know there is also a similar sort funtion in Unix. Lists have a sort method. No need to do this with temporary files. Just read in the first file into a list and sort it. 3) I will probably be working with 50 items, or less, would it be best for me to do this with a multi-diminsional array? For example: sort the file, read a rec into the array, if the next rec is the same then incr the count, otherwise add a new rec with a count of 1. Then write the array to a file? I would read the file into a list of list, that's what comes closest to a multidimensional array, via the `csv` module. Sort that (outer) list and then use `itertools.groupby()` to group the identical lists. You can write the rows with the `csv` module again. Short example: import csv from itertools import groupby in_file = open('test.csv', 'rb') data = list(csv.reader(in_file)) in_file.close() data.sort() out_file = open('test2.csv', 'wb') writer = csv.writer(out_file) for row, identical_rows in groupby(data): row.append(len(list(identical_rows))) writer.writerow(row) out_file.close() Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
[ python-Bugs-1525447 ] Build fails on OS X with case sensitive fs
Bugs item #1525447, was opened at 2006-07-19 19:24 Message generated for change (Comment added) made by ronaldoussoren You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1525447group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Build Group: Python 2.5 Status: Open Resolution: None Priority: 6 Submitted By: gideon may (gdm) Assigned to: Ronald Oussoren (ronaldoussoren) Summary: Build fails on OS X with case sensitive fs Initial Comment: When compiling python from svn on a Mac OS X with a case sensitive file system I get the following build error: running build running build_ext db.h: found (4, 3) in /opt/local/include/db4 db lib: using (4, 3) db-4.3 sqlite: found /usr/include/sqlite3.h /usr/include/sqlite3.h: version 3.1.3 Traceback (most recent call last): File ./setup.py, line 1507, in module main() . . File ./setup.py, line 1088, in addMacExtension raise RuntimeError(%s not found % name) RuntimeError: MacOS not found make: *** [sharedmods] Error 1 It can be fixed by either renaming the file: Mac/Modules/macosmodule.c to Mac/Modules/MacOSmodule.c or applying the following patch : Index: setup.py === --- setup.py(revision 50687) +++ setup.py(working copy) @@ -1101,7 +1101,7 @@ carbon_kwds = {'extra_compile_args': carbon_extra_compile_args, 'extra_link_args': ['-framework', 'Carbon'], } -CARBON_EXTS = ['ColorPicker', 'gestalt', 'MacOS', 'Nav', +CARBON_EXTS = ['ColorPicker', 'gestalt', 'macos', 'Nav', 'OSATerminology', 'icglue', # All these are in subdirs '_AE', '_AH', '_App', '_CarbonEvt', '_Cm', '_Ctl', Cheers, Gideon -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 11:31 Message: Logged In: YES user_id=580910 The patch is incorrect, as this would rename user visible name of the python extension ('import MacOS' would stop working). The rename would be correct. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1525447group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1517996 ] IDLE (macosx): Class and Path browsers show Tk menu
Bugs item #1517996, was opened at 2006-07-06 10:34 Message generated for change (Comment added) made by ronaldoussoren You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1517996group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: IDLE Group: Python 2.5 Status: Open Resolution: None Priority: 4 Submitted By: Ronald Oussoren (ronaldoussoren) Assigned to: Ronald Oussoren (ronaldoussoren) Summary: IDLE (macosx): Class and Path browsers show Tk menu Initial Comment: I've done some work on fixing the menus for IDLE before 2.5b1, but recently found more toplevel windows that don't have their own menu. Both the Path Browser and Class Browser don't have a menubar of their own. The Tk implementation on MacOSX won't accept an empty menubar, but will replace that by a generic Tk menubar. -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 11:35 Message: Logged In: YES user_id=580910 Changing the default root menu is indeed possible without changes outside macosxSupport.py. I'm going to apply the attached patch later today. Annoyingly enough AquaTk still adds an (empty) Help menu to our menubar. It would be nice to fill that menu, but that would IMHO add to much code. I'm happy enough to blame that glitch on AquaTk. -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 09:26 Message: Logged In: YES user_id=580910 Yes I mean AquaTk. The debugger has the same problem (as expected because it doesn't define its own menu) The behaviour of AquaTk is a feature, not a bug. Appearently the default menu is inheritted from the root (.). I'll see if I can override that menu in macosxSupport.setupApp. -- Comment By: Kurt B. Kaiser (kbk) Date: 2006-07-22 23:23 Message: Logged In: YES user_id=149084 Tk uses the per-window menu for the currently selected window I assume you mean AquaTk uses the per-window... Please put in a bug request on AquaTk to get it to conform to how Tk handles this menu on Linux and Windows. Then downgrade the priority of this bug or close it. Does the debugger have the same problem? -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-18 14:20 Message: Logged In: YES user_id=580910 These menu problems are at the very root caused by differences in the way menus work on OSX (one application wide menu at the top of the screen) and most other systems (optional menus for every window). Applications on OSX must have some kind of menu (or be full-screen, but we don't want that for IDLE), Tk uses the per-window menu for the currently selected window for that. If a window doesn't have a menu it makes up one and I haven't found a way yet to override that menu (which would solve that problem once and for all). I do believe that my other changes really are good, they make IDLE perform more like a true OSX application. This is important because IDLE is the default IDE, and hence probably the first thing naive OSX users will see of python. BTW. AquaTk really sucks, which makes it annoyingly hard to create a really good OSX citizen of IDLE and I have given up on getting there. If I understand the lazyweb correctly you're supposed to port your appliations to some other GUI library (Tile?) to get a native LF for lots of widgets and standard dialogs. -- Comment By: Kurt B. Kaiser (kbk) Date: 2006-07-15 21:51 Message: Logged In: YES user_id=149084 I suppose this is also true for the debugger? I'd consider this a bug in OSX Tk, it should be reported there. Without a specific need for a menubar, all it does is take up valuable vertical space. And continuing to add OSX special casing clutters up the IDLE code; IMO it should be limited to situations where functionality is compromised. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1517996group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1517996 ] IDLE (macosx): Class and Path browsers show Tk menu
Bugs item #1517996, was opened at 2006-07-06 10:34 Message generated for change (Comment added) made by ronaldoussoren You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1517996group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: IDLE Group: Python 2.5 Status: Closed Resolution: Fixed Priority: 4 Submitted By: Ronald Oussoren (ronaldoussoren) Assigned to: Ronald Oussoren (ronaldoussoren) Summary: IDLE (macosx): Class and Path browsers show Tk menu Initial Comment: I've done some work on fixing the menus for IDLE before 2.5b1, but recently found more toplevel windows that don't have their own menu. Both the Path Browser and Class Browser don't have a menubar of their own. The Tk implementation on MacOSX won't accept an empty menubar, but will replace that by a generic Tk menubar. -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 11:46 Message: Logged In: YES user_id=580910 Applied in revision 50785 -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 11:35 Message: Logged In: YES user_id=580910 Changing the default root menu is indeed possible without changes outside macosxSupport.py. I'm going to apply the attached patch later today. Annoyingly enough AquaTk still adds an (empty) Help menu to our menubar. It would be nice to fill that menu, but that would IMHO add to much code. I'm happy enough to blame that glitch on AquaTk. -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 09:26 Message: Logged In: YES user_id=580910 Yes I mean AquaTk. The debugger has the same problem (as expected because it doesn't define its own menu) The behaviour of AquaTk is a feature, not a bug. Appearently the default menu is inheritted from the root (.). I'll see if I can override that menu in macosxSupport.setupApp. -- Comment By: Kurt B. Kaiser (kbk) Date: 2006-07-22 23:23 Message: Logged In: YES user_id=149084 Tk uses the per-window menu for the currently selected window I assume you mean AquaTk uses the per-window... Please put in a bug request on AquaTk to get it to conform to how Tk handles this menu on Linux and Windows. Then downgrade the priority of this bug or close it. Does the debugger have the same problem? -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-18 14:20 Message: Logged In: YES user_id=580910 These menu problems are at the very root caused by differences in the way menus work on OSX (one application wide menu at the top of the screen) and most other systems (optional menus for every window). Applications on OSX must have some kind of menu (or be full-screen, but we don't want that for IDLE), Tk uses the per-window menu for the currently selected window for that. If a window doesn't have a menu it makes up one and I haven't found a way yet to override that menu (which would solve that problem once and for all). I do believe that my other changes really are good, they make IDLE perform more like a true OSX application. This is important because IDLE is the default IDE, and hence probably the first thing naive OSX users will see of python. BTW. AquaTk really sucks, which makes it annoyingly hard to create a really good OSX citizen of IDLE and I have given up on getting there. If I understand the lazyweb correctly you're supposed to port your appliations to some other GUI library (Tile?) to get a native LF for lots of widgets and standard dialogs. -- Comment By: Kurt B. Kaiser (kbk) Date: 2006-07-15 21:51 Message: Logged In: YES user_id=149084 I suppose this is also true for the debugger? I'd consider this a bug in OSX Tk, it should be reported there. Without a specific need for a menubar, all it does is take up valuable vertical space. And continuing to add OSX special casing clutters up the IDLE code; IMO it should be limited to situations where functionality is compromised. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1517996group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1527397 ] PythonLauncher uses incorrect working directory
Bugs item #1527397, was opened at 2006-07-23 13:29 Message generated for change (Tracker Item Submitted) made by Item Submitter You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1527397group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Macintosh Group: Python 2.5 Status: Open Resolution: None Priority: 7 Submitted By: Bob Ippolito (etrepum) Assigned to: Jack Jansen (jackjansen) Summary: PythonLauncher uses incorrect working directory Initial Comment: PythonLauncher, the application used to launch .py files by double-click, does not launch with the correct working directory. This means that any script that depends on data files will not work via PythonLauncher. Users expect the working directory to be that of the launched script, like on other platforms, but PythonLauncher launches with a working directory of $HOME. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1527397group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1525447 ] Build fails on OS X with case sensitive fs
Bugs item #1525447, was opened at 2006-07-19 19:24 Message generated for change (Comment added) made by ronaldoussoren You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1525447group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Build Group: Python 2.5 Status: Open Resolution: None Priority: 6 Submitted By: gideon may (gdm) Assigned to: Ronald Oussoren (ronaldoussoren) Summary: Build fails on OS X with case sensitive fs Initial Comment: When compiling python from svn on a Mac OS X with a case sensitive file system I get the following build error: running build running build_ext db.h: found (4, 3) in /opt/local/include/db4 db lib: using (4, 3) db-4.3 sqlite: found /usr/include/sqlite3.h /usr/include/sqlite3.h: version 3.1.3 Traceback (most recent call last): File ./setup.py, line 1507, in module main() . . File ./setup.py, line 1088, in addMacExtension raise RuntimeError(%s not found % name) RuntimeError: MacOS not found make: *** [sharedmods] Error 1 It can be fixed by either renaming the file: Mac/Modules/macosmodule.c to Mac/Modules/MacOSmodule.c or applying the following patch : Index: setup.py === --- setup.py(revision 50687) +++ setup.py(working copy) @@ -1101,7 +1101,7 @@ carbon_kwds = {'extra_compile_args': carbon_extra_compile_args, 'extra_link_args': ['-framework', 'Carbon'], } -CARBON_EXTS = ['ColorPicker', 'gestalt', 'MacOS', 'Nav', +CARBON_EXTS = ['ColorPicker', 'gestalt', 'macos', 'Nav', 'OSATerminology', 'icglue', # All these are in subdirs '_AE', '_AH', '_App', '_CarbonEvt', '_Cm', '_Ctl', Cheers, Gideon -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 20:11 Message: Logged In: YES user_id=580910 Could you please test if renaming Mac/Modules/macosmodule.c to Mac/ Modules/MacOS.c solves your problem? With that rename the build still works for me, but I don't have a case-sensitive filesystem. -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 11:31 Message: Logged In: YES user_id=580910 The patch is incorrect, as this would rename user visible name of the python extension ('import MacOS' would stop working). The rename would be correct. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1525447group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1527397 ] PythonLauncher uses incorrect working directory
Bugs item #1527397, was opened at 2006-07-23 19:29 Message generated for change (Comment added) made by ronaldoussoren You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1527397group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Macintosh Group: Python 2.5 Status: Open Resolution: None Priority: 7 Submitted By: Bob Ippolito (etrepum) Assigned to: Jack Jansen (jackjansen) Summary: PythonLauncher uses incorrect working directory Initial Comment: PythonLauncher, the application used to launch .py files by double-click, does not launch with the correct working directory. This means that any script that depends on data files will not work via PythonLauncher. Users expect the working directory to be that of the launched script, like on other platforms, but PythonLauncher launches with a working directory of $HOME. -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-07-23 20:50 Message: Logged In: YES user_id=580910 I agree that the current behaviour is not what users expect and should be considered a bug. I'd like to get the attached patch into 2.5, it changes the CWD to the directory containing the script and should be perfectly save. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1527397group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1436532 ] length of unicode string changes print behaviour
Bugs item #1436532, was opened at 2006-02-22 10:45 Message generated for change (Comment added) made by loewis You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1436532group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: IDLE Group: Python 2.4 Status: Closed Resolution: Invalid Priority: 5 Submitted By: James (hover_boy) Assigned to: Martin v. Löwis (loewis) Summary: length of unicode string changes print behaviour Initial Comment: Python 2.4.2 and IDLE (with Courier New font) on XP and the following code saved as a UTF-8 file if __name__ == __main__: print é¶ ä¸ äº ä¸ å äº å ä¸ å « print é¶ ä¸ äº ä¸ å äº å ä¸ å « ä¹ å results in... IDLE 1.1.2 RESTART éâºÂ¶ ä¸⬠亊ä¸Ⱐåâºâº äºâ Ã¥â¦Ã¤Â¸Æ Ã¥â¦Â« é¶ ä¸ äº ä¸ å äº å ä¸ å « ä¹ å -- Comment By: Martin v. Löwis (loewis) Date: 2006-07-23 21:42 Message: Logged In: YES user_id=21627 This is not a bug. The program should not attempt to print byte strings, since it cannot know what the encoding of the byte strings is. Instead, the program should use Unicode strings, such as print uå «å «å «å «å «å «å «å «å «å «å «å «å «å «å «å «å «å «å «å «å «å « If you attempt to print byte strings, they have to be in the encoding of stdout, or else the behaviour is unspecified. In my installation/locale, sys.stdout.encoding is cp1250. IDLE's OutputWindow.write has this code: # Tk assumes that byte strings are Latin-1; # we assume that they are in the locale's encoding if isinstance(s, str): try: s = unicode(s, IOBinding.encoding) except UnicodeError: # some other encoding; let Tcl deal with it pass Of the strings specified in the source file, only strings 2..5 decode properly as cp1250; the others don't. So these get passed directly to Tcl, which then assumes they are UTF-8, with some fallback also. The strings that look incorrectly are actually printed out as designed: using sys.stdout.encoding. -- Comment By: Kurt B. Kaiser (kbk) Date: 2006-07-23 07:33 Message: Logged In: YES user_id=149084 I don't have a font installed which will print those characters. When I load your sample file, I see print statements which include unicode characters like \u5341. The printed output contains the same unicode characters as the input program. Maybe Martin has an idea. -- Comment By: James (hover_boy) Date: 2006-03-22 16:21 Message: Logged In: YES user_id=1458491 I've attached an example file to demonstrate the problem better. it seems not to be the length but something else which I haven't figured out yet. I've also added the encoding comment and also tried changing the default encoding in sitecustomize.py from latin -1 to utf-8 but neither seem to work. thanks, James. XP professional, SP2, english -- Comment By: James (hover_boy) Date: 2006-03-22 16:12 Message: Logged In: YES user_id=1458491 -- Comment By: Terry J. Reedy (tjreedy) Date: 2006-03-06 02:44 Message: Logged In: YES user_id=593130 I am fairly ignorant of unicode and encodings, but I am surprised you got anything coherent without an encoding cookie comment at the top (see manual). Have you tried that? Other questions that might help someone answer: What specific XP version? SP2 installed? Country version? Your results for sys.getdefaultencoding() 'ascii' sys.getfilesystemencoding() 'mbcs' What happens if you reverse the order of the print statements? (Ie, is it really the shorter string that does not work or just the first?) I don't know enough to know if this is really a bug. If you don't get an answer here, you might try for more info on python-list/comp.lang.python -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1436532group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1525678 ] exec and eval allocate lots of memory and do not free it
Bugs item #1525678, was opened at 2006-07-20 05:57 Message generated for change (Comment added) made by loewis You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1525678group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Closed Resolution: Wont Fix Priority: 5 Submitted By: Connelly (connelly) Assigned to: Nobody/Anonymous (nobody) Summary: exec and eval allocate lots of memory and do not free it Initial Comment: I'm not sure if this is a bug. The bug is that if I start a new Python session, create a dict or list called d which takes around 2 MB of memory, and then I set d = eval(repr(d)), the Python process now is using ~38 MB of memory more than where it started at. The high memory usage continues even after d is deleted. Example 1: % python # Memory use: 3216 KB d = dict.fromkeys(range(5)) # Memory use: 5400 KB d = eval('%r' % d) # Memory use: 41620 KB del d # Memory use: 40080 KB I am using Python 2.4.1 (#65, Mar 30 2005) on Windows XP SP2 with 512 MB RAM. If we start with a larger initial dict -- say dict.fromkeys(range(1000**2)), then the line d = eval('%r' % d) can easily cause the process to start paging to disk, even though both the data structure and its string representation fit easily in memory. Perhaps this behavior is due Python caching bytecodes. One peculiarity about this bug is that if Example 1 is repeated with a second variable such as d2, which is set to the value dict.fromkeys(range(5,10)), then the memory usage ends up exactly at 40080 KB after the second del statement. If Python were caching the bytecodes, then one would expect the repetition of the example to put the memory usage at ~8 KB. -- Comment By: Martin v. Löwis (loewis) Date: 2006-07-23 21:53 Message: Logged In: YES user_id=21627 This bug falls into the won't fix category, and also in the already fixed category. Python does indeed free the memory; there is no caching going on. It just doesn't return the memory to the operating system. You can see that the memory is really freed by performing the same operating over and over again (say, a thousand times), and watch the memory consumption not grow. Python obtains the memory not from the system, but from malloc, which obtains it from the system. Whether or not malloc will return memory to the system depends on the malloc implementation; this is out of our control (it's in the Microsoft C library). However, Python does not return the memory to malloc, either. In the specific case, there are two allocators on top of malloc operating: the integer allocator, and the small objects allocator. The integer allocator allocates a chunk from malloc and then subdivides it into integer objects. This memory is never returned to malloc; you are using this allocator within the range() function. When the integers are released, the memory becomes available for other integer objects, but not for objects of another kind. The small objects allocator is likely used for the repr strings of the integers. Object sizes are rounded up to the next multiple of 8 (say, 24), and then a pool of 24-byte-sized blocks is maintained. When the string objects are released, they are released to the pool. In Python 2.4, pool memory is never returned to malloc. In Python 2.5, this aspect is fixed: under certain circumstances (which are too involved to describe here), pool memory is returned to malloc, which then hopefully returns it to the system. Closing as won't fix. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1525678group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1524938 ] MemoryError with a lot of available memory - gc not called
Bugs item #1524938, was opened at 2006-07-19 04:46 Message generated for change (Comment added) made by loewis You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Feature Request Status: Open Resolution: None Priority: 5 Submitted By: Mark Matusevich (markmat) Assigned to: Nobody/Anonymous (nobody) Summary: MemoryError with a lot of available memory - gc not called Initial Comment: Also the gc behavior is consistent with the documentation, I beleave it is wrong. I think, that Gc should be called automatically before any memory allocation is raised. Example 1: for i in range(700): a = [range(500)] a.append(a) print i This example will crash on any any PC with less then 20Gb RAM. On my PC (Windows 2000, 256Mb) it crashes at i==7. Also, this example can be fixed by addition of a call to gc.collect() in the loop, in real cases it may be unreasonable. -- Comment By: Martin v. Löwis (loewis) Date: 2006-07-23 22:00 Message: Logged In: YES user_id=21627 This is very difficult to implement. The best way might be to introduce yet another allocation function, one that invokes gc before failing, and call that function in all interesting places (of which there are many). Contributions are welcome and should probably start with a PEP first. -- Comment By: Rene Dudfield (illume) Date: 2006-07-20 01:20 Message: Logged In: YES user_id=2042 Perhaps better than checking before every memory allocation, would be to check once a memory error happens in an allocation. That way there is only the gc hit once there is low memory. So... res = malloc(...); if(!res) { gc.collect(); } res = malloc(...); if(!res) { raise memory error. } -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1524938 ] MemoryError with a lot of available memory - gc not called
Bugs item #1524938, was opened at 2006-07-19 05:46 Message generated for change (Comment added) made by markmat You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Feature Request Status: Open Resolution: None Priority: 5 Submitted By: Mark Matusevich (markmat) Assigned to: Nobody/Anonymous (nobody) Summary: MemoryError with a lot of available memory - gc not called Initial Comment: Also the gc behavior is consistent with the documentation, I beleave it is wrong. I think, that Gc should be called automatically before any memory allocation is raised. Example 1: for i in range(700): a = [range(500)] a.append(a) print i This example will crash on any any PC with less then 20Gb RAM. On my PC (Windows 2000, 256Mb) it crashes at i==7. Also, this example can be fixed by addition of a call to gc.collect() in the loop, in real cases it may be unreasonable. -- Comment By: Mark Matusevich (markmat) Date: 2006-07-23 23:11 Message: Logged In: YES user_id=1337765 This is exectly what I meant. For my recollection, this is the policy in Java GC. I never had to handle MemoryError in Java, because I knew, that I really do not have any more memory. -- Comment By: Martin v. Löwis (loewis) Date: 2006-07-23 23:00 Message: Logged In: YES user_id=21627 This is very difficult to implement. The best way might be to introduce yet another allocation function, one that invokes gc before failing, and call that function in all interesting places (of which there are many). Contributions are welcome and should probably start with a PEP first. -- Comment By: Rene Dudfield (illume) Date: 2006-07-20 02:20 Message: Logged In: YES user_id=2042 Perhaps better than checking before every memory allocation, would be to check once a memory error happens in an allocation. That way there is only the gc hit once there is low memory. So... res = malloc(...); if(!res) { gc.collect(); } res = malloc(...); if(!res) { raise memory error. } -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1524938 ] MemoryError with a lot of available memory - gc not called
Bugs item #1524938, was opened at 2006-07-19 05:46 Message generated for change (Comment added) made by markmat You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Feature Request Status: Open Resolution: None Priority: 5 Submitted By: Mark Matusevich (markmat) Assigned to: Nobody/Anonymous (nobody) Summary: MemoryError with a lot of available memory - gc not called Initial Comment: Also the gc behavior is consistent with the documentation, I beleave it is wrong. I think, that Gc should be called automatically before any memory allocation is raised. Example 1: for i in range(700): a = [range(500)] a.append(a) print i This example will crash on any any PC with less then 20Gb RAM. On my PC (Windows 2000, 256Mb) it crashes at i==7. Also, this example can be fixed by addition of a call to gc.collect() in the loop, in real cases it may be unreasonable. -- Comment By: Mark Matusevich (markmat) Date: 2006-07-23 23:19 Message: Logged In: YES user_id=1337765 Sorry, my last comment was to illume (I am slow typer :( ) -- Comment By: Mark Matusevich (markmat) Date: 2006-07-23 23:11 Message: Logged In: YES user_id=1337765 This is exectly what I meant. For my recollection, this is the policy in Java GC. I never had to handle MemoryError in Java, because I knew, that I really do not have any more memory. -- Comment By: Martin v. Löwis (loewis) Date: 2006-07-23 23:00 Message: Logged In: YES user_id=21627 This is very difficult to implement. The best way might be to introduce yet another allocation function, one that invokes gc before failing, and call that function in all interesting places (of which there are many). Contributions are welcome and should probably start with a PEP first. -- Comment By: Rene Dudfield (illume) Date: 2006-07-20 02:20 Message: Logged In: YES user_id=2042 Perhaps better than checking before every memory allocation, would be to check once a memory error happens in an allocation. That way there is only the gc hit once there is low memory. So... res = malloc(...); if(!res) { gc.collect(); } res = malloc(...); if(!res) { raise memory error. } -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1524938 ] PEP MemoryError with a lot of available memory gc not called
Bugs item #1524938, was opened at 2006-07-19 05:46 Message generated for change (Settings changed) made by markmat You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Feature Request Status: Open Resolution: None Priority: 5 Submitted By: Mark Matusevich (markmat) Assigned to: Nobody/Anonymous (nobody) Summary: PEP MemoryError with a lot of available memory gc not called Initial Comment: Also the gc behavior is consistent with the documentation, I beleave it is wrong. I think, that Gc should be called automatically before any memory allocation is raised. Example 1: for i in range(700): a = [range(500)] a.append(a) print i This example will crash on any any PC with less then 20Gb RAM. On my PC (Windows 2000, 256Mb) it crashes at i==7. Also, this example can be fixed by addition of a call to gc.collect() in the loop, in real cases it may be unreasonable. -- Comment By: Mark Matusevich (markmat) Date: 2006-07-23 23:19 Message: Logged In: YES user_id=1337765 Sorry, my last comment was to illume (I am slow typer :( ) -- Comment By: Mark Matusevich (markmat) Date: 2006-07-23 23:11 Message: Logged In: YES user_id=1337765 This is exectly what I meant. For my recollection, this is the policy in Java GC. I never had to handle MemoryError in Java, because I knew, that I really do not have any more memory. -- Comment By: Martin v. Löwis (loewis) Date: 2006-07-23 23:00 Message: Logged In: YES user_id=21627 This is very difficult to implement. The best way might be to introduce yet another allocation function, one that invokes gc before failing, and call that function in all interesting places (of which there are many). Contributions are welcome and should probably start with a PEP first. -- Comment By: Rene Dudfield (illume) Date: 2006-07-20 02:20 Message: Logged In: YES user_id=2042 Perhaps better than checking before every memory allocation, would be to check once a memory error happens in an allocation. That way there is only the gc hit once there is low memory. So... res = malloc(...); if(!res) { gc.collect(); } res = malloc(...); if(!res) { raise memory error. } -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1524938group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1012435 ] ctrl-left/-right works incorectly with diacritics
Bugs item #1012435, was opened at 2004-08-19 15:40 Message generated for change (Comment added) made by kbk You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1012435group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Tkinter Group: Python 2.3 Status: Open Resolution: None Priority: 5 Submitted By: Krzysztof Wilkosz (sophros) Assigned to: Nobody/Anonymous (nobody) Summary: ctrl-left/-right works incorectly with diacritics Initial Comment: When left- or right-going in line with ctrl-arrow (left/right) I have encountered problems: example word with polish (cp1250) diacritics: za#380;ó#322;#263; (last 4 letters might be unreadable) this keybord shortcut stops each time diacritic letter is encountered, not at the end of the word. It is frustrating as I have many strings and comments in Polish. I thing it might work bad also in the case of any other non-ASCII letters. -- Comment By: Kurt B. Kaiser (kbk) Date: 2006-07-23 16:45 Message: Logged In: YES user_id=149084 The control-{right,left} action appears to be implemented in Tkinter, probably in Tcl/Tk, it's not an IDLE issue. Someone should check if the problem exists in Tk. Passing this on to the Tkinter crew. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1012435group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Bugs-1512124 ] OSX: debugger hangs IDLE
Bugs item #1512124, was opened at 2006-06-25 09:45 Message generated for change (Comment added) made by kbk You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1512124group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: IDLE Group: Python 2.5 Status: Open Resolution: Accepted Priority: 7 Submitted By: Aahz (aahz) Assigned to: Nobody/Anonymous (nobody) Summary: OSX: debugger hangs IDLE Initial Comment: This has been previously found on 2.3/2.4 and verified again with 2.5b1 (was hoping that the 1.2 IDLE update would fix): When you bring up the debugger window in IDLE on OSX, you'll notice that it's vibrating, as if it's stuck in a resize loop. Unless you immediately resize the window manually, IDLE will hang the next time you try to do something that updates the debugger window. -- Comment By: Kurt B. Kaiser (kbk) Date: 2006-07-23 16:47 Message: Logged In: YES user_id=149084 Please try running IDLE w/o the subprocess by starting it with the -n switch. Does the issue still exist? -- Comment By: Ronald Oussoren (ronaldoussoren) Date: 2006-06-25 16:49 Message: Logged In: YES user_id=580910 And when you do resize the window layout sucks, automatic layout seems to be buggy here (although I must admit that I haven't seen IDLE on other platforms). I have no idea what is causing this bug. Upgrading the Tk version to the very latest instead of using the system version doesn't help. I've checked in a workaround in revision 47091. That patch avoids explicitly sizing the stackviewer widget on OSX, which seems to fix this problem. The debugger window now is rather less wide than it was before, which means users will likely have to resize the window if they want to use the debugger. I'm leaving this bug open and unassign it in the hope that someone who actually knows Tkinter proposed a better fix. -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=105470aid=1512124group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[ python-Feature Requests-1510853 ] Add Windows 9x/ME (lack of) support information to README.TX
Feature Requests item #1510853, was opened at 2006-06-22 20:36 Message generated for change (Settings changed) made by loewis You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=355470aid=1510853group_id=5470 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Documentation Group: None Status: Closed Resolution: Accepted Priority: 5 Submitted By: ajaksu (ajaksu2) Assigned to: A.M. Kuchling (akuchling) Summary: Add Windows 9x/ME (lack of) support information to README.TX Initial Comment: I suggest that following informative note should be added as a new sub-section in README.TXT's Unsupported systems (which really needs mentioning MacOS 9). The sub-section idea regards README.TXT only talking about already unsupported platforms. Motivation: As one of the few Windows 98 users that program in Python AND installed the 2.5 beta1, I was greeted by Tools/msi/msi.py's warning message. However, no further information was available in the downloaded release or in the online beta documents (What's New, release notes, etc.). Best regards, Daniel Diniz - Warning on install in Windows 98 and Windows Me Following Microsoft's closing of Extended Support for Windows 98/ME (July 11, 2006), Python 2.6 will stop supporting these platforms. Python development and maintainability becomes easier (and more reliable) when platform specific code targeting OSes with few users and no dedicated expert developers is taken out. The vendor also warns that the OS versions listed above can expose customers to security risks and recommends upgrade. For a more detailed discussion regarding no-longer-supported and resupporting platforms, as well as a list of platforms that became or will be unsupported, see PEP 11. Current behavior The Python 2.5 installer presents a warning message on those systems: Warning: Python 2.5.x is the last Python release for Windows 9x. Suggested readings PEP 11: Removing support for little used platforms (http://www.python.org/dev/peps/pep-0011) End of support for Windows 98, Windows Me, and Windows XP Service Pack 1 (http://www.microsoft.com/windows/support/endofsupport.mspx) Copyright This document has been placed in the public domain. -- -- Comment By: Martin v. Löwis (loewis) Date: 2006-07-24 07:06 Message: Logged In: YES user_id=21627 Thanks for the suggestion. I added the text (in some form) to README in r50794. -- Comment By: Georg Brandl (gbrandl) Date: 2006-06-22 20:59 Message: Logged In: YES user_id=849994 Andrew? -- You can respond by visiting: https://sourceforge.net/tracker/?func=detailatid=355470aid=1510853group_id=5470 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com