Re: regex (?!..) problem
Wolfgang Rohdewald wrote: > I want to match a string only if a word (C1 in this example) appears > at most once in it. def match(s): if s.count("C1") > 1: return None return s If this doesn't fit your requirements, you may want to provide some more details. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: regex (?!..) problem
On Monday 05 October 2009, Carl Banks wrote: > What you're not realizing is that if a regexp search comes to a > dead end, it won't simply return "no match". Instead it'll throw > away part of the match, and backtrack to a previously-matched > variable-length subexpression, such as ".*?", and try again with a > different length. well, that explains it. This is contrary to what the documentation says, though. Should I fill a bug report? http://docs.python.org/library/re.html Now back to my original problem: Would you have any idea how to solve it? count() is no solution in my case, I need re.search to either return None or a match. -- Wolfgang -- http://mail.python.org/mailman/listinfo/python-list
Re: regex (?!..) problem
On Oct 4, 9:34 pm, Wolfgang Rohdewald wrote: > Hi, > > I want to match a string only if a word (C1 in this example) appears > at most once in it. This is what I tried: > > >>> re.match(r'(.*?C1)((?!.*C1))','C1b1b1b1 b3b3b3b3 C1C2C3').groups() > > ('C1b1b1b1 b3b3b3b3 C1', '')>>> re.match(r'(.*?C1)','C1b1b1b1 b3b3b3b3 > C1C2C3').groups() > > ('C1',) > > but this should not have matched. Why is the .*? behaving greedy > if followed by (?!.*C1)? It's not. > I would have expected that re first > evaluates (.*?C1) before proceeding at all. It does. What you're not realizing is that if a regexp search comes to a dead end, it won't simply return "no match". Instead it'll throw away part of the match, and backtrack to a previously-matched variable-length subexpression, such as ".*?", and try again with a different length. That's what happened above. At first the group "(.*?C1)" non-greedily matched the substring "C1", but it couldn't find a match under those circumstances, so it backtracked to the ".*?". and looked a longer match, which it found. Here's something to keep in mind: except for a few corner cases, greedy versus non-greedy will not affect the substring matched, it'll only affect the groups. > I also tried: > > >>> re.search(r'(.*?C1(?!.*C1))','C1b1b1b1 b3b3b3b3 > > C1C2C3C4').groups() > ('C1b1b1b1 b3b3b3b3 C1',) > > with the same problem. > > How could this be done? Can't be done with regexps. How you would do this kind of depends on your overall goals, but your first look should be toward the string methods. If you share details with us we can help you choose a better strategy. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular expression to structure HTML
504cr...@gmail.com wrote: > No -- sorry -- I don't want to use BeautifulSoup (though I have for > other projects). Humor me, please -- I'd really like to see if this > can be done with just regular expressions. I think the reason why people are giving funny comments here is that you failed to provide a reason for the above requirement. That makes it sound like a typical "How can I use X to do Y?" question. http://www.catb.org/~esr/faqs/smart-questions.html#id383188 Stefan -- http://mail.python.org/mailman/listinfo/python-list
blog
hey friends just made a new blog will you comment it http://makeing-money.blogspot.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: regex (?!..) problem
Why not check it simply by "count()"? >>> s = '1234C156789' >>> s.count('C1') 1 >>> -- http://mail.python.org/mailman/listinfo/python-list
regex (?!..) problem
Hi, I want to match a string only if a word (C1 in this example) appears at most once in it. This is what I tried: >>> re.match(r'(.*?C1)((?!.*C1))','C1b1b1b1 b3b3b3b3 C1C2C3').groups() ('C1b1b1b1 b3b3b3b3 C1', '') >>> re.match(r'(.*?C1)','C1b1b1b1 b3b3b3b3 C1C2C3').groups() ('C1',) but this should not have matched. Why is the .*? behaving greedy if followed by (?!.*C1)? I would have expected that re first evaluates (.*?C1) before proceeding at all. I also tried: >>> re.search(r'(.*?C1(?!.*C1))','C1b1b1b1 b3b3b3b3 C1C2C3C4').groups() ('C1b1b1b1 b3b3b3b3 C1',) with the same problem. How could this be done? -- Wolfgang -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all list entries of length unknown
"Chris Rebert" wrote in message news:50697b2c0910042047i1cf2c1a3mc388bc74bab95...@mail.gmail.com... Tuples are immutable (i.e. they cannot be modified after creation) and are created using parentheses. Slight correction: tuples are created using commas. Parentheses are only needed to disambiguate from other uses of comma: Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. a=1, a (1,) a=1,2,3 a (1, 2, 3) -Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: Skeletal animation
On Oct 4, 5:05 pm, Manowar wrote: > Here is my question sekeltal animation ( bone animation) is it > possible with python? >>> "For God's sakes man!"[:-1]+'owar, use Blender!' -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all list entries of length unknown
On Oct 4, 10:09 pm, flebber wrote: > Hi > > Can someone clear up how I can remove all entries of a list when I am > unsure how many entries there will be. Sure...! >>> a = range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> del a[0] >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> del a[-1] >>> a [1, 2, 3, 4, 5, 6, 7, 8] >>> del a[:] >>> a [] or you could simple say >>> a = [] ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all list entries of length unknown
On Sun, Oct 4, 2009 at 8:09 PM, flebber wrote: > Hi > > Can someone clear up how I can remove all entries of a list when I am > unsure how many entries there will be. I have been using sandbox to > play essentially I am creating two lists a and b I then want to add a > to b and remove all b entries. This will loop and b will receive new > entries add it to a and delete again. > > I am going wrong with slice and list deletion, I assign x = len(b) and > then attempting to delete based on this. Here is my sandbox session. > What part am I getting wrong? > > #>>> > a = (1, 2, 3, 4) > b = (5, 6, 7, 8) > x = len(b) > #>>> > del b[0:x] > Traceback (most recent call last): > Error: File "", line 1, in > Error: TypeError: 'tuple' object does not support item deletion As the error message says, you're using *tuples*, not lists. Tuples are immutable (i.e. they cannot be modified after creation) and are created using parentheses. Lists on the other hand are made using brackets: [1, 2, 3, 4] # like this Additionally, the idiomatic way to clear a list (besides the more obvious approach of just assigning a new, empty list to the variable) is: del a[:] Leaving out the endpoints in the slice causes it to default to the entire contents of the list. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Delete all list entries of length unknown
Hi Can someone clear up how I can remove all entries of a list when I am unsure how many entries there will be. I have been using sandbox to play essentially I am creating two lists a and b I then want to add a to b and remove all b entries. This will loop and b will receive new entries add it to a and delete again. I am going wrong with slice and list deletion, I assign x = len(b) and then attempting to delete based on this. Here is my sandbox session. What part am I getting wrong? #>>> a = (1, 2, 3, 4) b = (5, 6, 7, 8) #>>> a #--- (1, 2, 3, 4) #>>> b #--- (5, 6, 7, 8) #>>> a + b #--- (1, 2, 3, 4, 5, 6, 7, 8) #>>> b #--- (5, 6, 7, 8) #>>> len(b) #--- 4 #>>> x = len(b) #>>> del b[0:x] Traceback (most recent call last): Error: File "", line 1, in Error: TypeError: 'tuple' object does not support item deletion #>>> b[0:x] = d Traceback (most recent call last): Error: File "", line 1, in Error: NameError: name 'd' is not defined #>>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: sort's key and cmp parameters
[Paul Rubin] > Example of list of trees (nested dicts). In practice you could get > such a list from the simplejson module: > > list_of_trees = [{'value':1, 'left':{'value':3,'left':None,'right':None}, >'right':{'value':7,'left':{'value':5, ...}}}, >{'value':19, 'left':{'value':23', ...}}, >... > ] So, it looks like the relevant comparison values can be stored in nested lists: list_of_lists = \ [[1, [3, [], []], [7, [5, [], []], []]], [19, [23, [], []], []], ] Here I've used a transformation where a tree is stored as: [tree['value'], tree['left'], tree['right']] and the None value indicating an empty tree transformed to: [] > > Example comparison function: > > def compare(tree1, tree2): > c = cmp(tree1['value'], tree2['value']) > if c != 0: return c > c = compare(tree1['left'], tree2['left']) > if c != 0: return c > return compare(tree1['right'], tree2['right]) Hmm, that recursive comparison order looks like how Python already recursively compares lists. So, perhaps the lists can be compared directly: if list_of_lists[0] < list_of_lists[1]: ... >> Are the trees user defined classes? > > Not in general. They might be nested tuples, lists, or dictionaries. > Or they could come from a non-extensible library-defined class, like > from cElementTree Are there any of these trees that cannot be transformed (by a key function) into an equivalent nested list of lists and values so that the trees can be directly compared to one another using Python's normal built-in recursive comparison order for lists? Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: Need feedback on subprocess-using function
On Sat, 03 Oct 2009 13:21:00 +, gb345 wrote: > I'm relatively new to Python, and I'm trying to get the hang of > using Python's subprocess module. As an exercise, I wrote the Tac > class below, which can prints output to a file "in reverse order", > by piping it through the Unix tac utility. (The idea is to delegate > the problem of managing the memory for an arbitrarily large task > to tac.) > self.pipe = subprocess.Popen(['tac'], stdout=out, > stdin=subprocess.PIPE, > stderr=subprocess.PIPE) > This works OK, as far as I can tell, but I'm not sure that I've > dotted all the i's and crossed all the t's... E.g., I had to add > the line "p.stdin.close()" to the close method when I when I ran > into sporadic deadlock at the p.stderr.read() statement. Are there > other similar problems lurking in this code? Yep. If the process writes more than a buffer-full of data to stderr, it will deadlock. tac will block trying to write to stderr, and won't be reading its stdin, so your program will block trying to write to tac. This is why the POSIX popen() call only lets you attach a pipe to stdin or stdout but not both. If you want a "double-ended" slave process, you need to use polling or non-blocking I/O or asynchronous I/O or multiple threads. I'm not aware of any single solution which works on all platforms. The easiest way around this problem is to redirect stderr to a temporary file and read in the file's contents in the close() method. -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular expression to structure HTML
On Thu, 01 Oct 2009 22:10:55 -0700, 504cr...@gmail.com wrote: > I'm kind of new to regular expressions The most important thing to learn about regular expressions is to learn what they can do, what they can't do, and what they can do in theory but can't do in practice (usually because of exponential or combinatorial growth). One thing they can't do is to match any kind of construct which has arbitrary nesting. E.g. you can't match any class of HTML element which can self-nest or whose children can self-nest. In practice, this means you can only match a handful of elements which are either empty (e.g. ) or which can only contain CDATA (e.g.
Re: Skeletal animation
Building on what others have said and giving a +1 to Carl: I work daily in Maya doing character setup and rigging. As far as doing it straight in Python, again, like others, take a look at PyGame or Blender. I think the main question is: Do you want skeletal animation, or do you want skeletal animation specifically in Python? While Python is heavily used in Blender, I'm not sure how much of the foundation it actually makes. Take a look ta this PyGame: http://www.pygame.org/project-StickyPy-1248-2254.html Of course PyGame is just a wrapper for SDL Joints and skeletons are abstract concepts that sure, you could do with Python. The question is how you want to render that to the screen, and that's where the other apps come in. -- http://mail.python.org/mailman/listinfo/python-list
Re: Skeletal animation
On Oct 4, 5:16 pm, Manowar wrote: > On Oct 4, 6:38 pm, TerryP wrote: > > > On Oct 4, 10:05 pm, Manowar wrote: > > > > I am new to pyton and have asked this question several times the > > > answer is always not sure. > > > Here is my question sekeltal animation ( bone animation) is it > > > possible with python? What i want to develop is an aquarium in > > > realtime, skeletal animation, all the movements will be from > > > programming., no keyframes let me know if it is possible with python > > > > Manowar > > > Depending on how much code you want to write, you'll probably want to > > check out something like pygame or python-ogre > > Yeah, i know about those but i just really need to know if it is > possible to do in python > if it is what about some tutorials on bone animation? It's possible. I can't really recommend a tutorial because I don't know where you are coming from, and it is a very complex subject. What do know about 3D animation so far? Have you done 3D animation in other languages? Have you done 3D mesh modeling with a tool like 3DS Max or Blender? What is your mathematical background (especially in linear algrebra)? Please help us help you by providing us details on what you know already and what you want to do. You won't get much help by asking simple yes/no questions. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Skeletal animation
On Oct 5, 8:05 am, Manowar wrote: > I am new to pyton and have asked this question several times the > answer is always not sure. > Here is my question sekeltal animation ( bone animation) is it > possible with python? What i want to develop is an aquarium in > realtime, skeletal animation, all the movements will be from > programming., no keyframes let me know if it is possible with python I found this googling "python skeletal model animation", it sounds like it would make a great point to start from: http://croquetconsortium.org/index.php/Skeletal_Animation_Example "The point of this project is to build a complete working example of the UMN Skeletal Animation package which can be freely distributed under the Croquet license. It appears that the biggest part of this project is to create one or more model meshes, create one or more animation sequences for the meshes, and export the meshes, all related textures and materials, and all animations in the Ogre XML format. "Blender3D version 2.44 will be used for this example. The Ogre Meshes Exporter was chosen as it appears to be comprehensive and current. This exporter requires Python 2.5 to be installed, not just the cut- down version of Python included with Blender3D. While I was at it, I also grabbed a download of Python-Ogre because it has lots of interesting examples, although it will not be directly used by this project. At least that was the original plan - actually the binary<- >XML translator program was used to translate a couple of free models from Psionic. So now there are three rigged, skinned, and animated models available; the Star model built during this project, and two models made available by Psionic and translated to Ogre XML for this project, with his permission." This page also lists a number of packages with Python-bindings that list "skeletal animation" amongst their feature sets: http://www.worldforge.org/media/documents/docs3d/tools_3d_compare -- http://mail.python.org/mailman/listinfo/python-list
Re: Skeletal animation
On Oct 4, 6:38 pm, TerryP wrote: > On Oct 4, 10:05 pm, Manowar wrote: > > > I am new to pyton and have asked this question several times the > > answer is always not sure. > > Here is my question sekeltal animation ( bone animation) is it > > possible with python? What i want to develop is an aquarium in > > realtime, skeletal animation, all the movements will be from > > programming., no keyframes let me know if it is possible with python > > > Manowar > > Depending on how much code you want to write, you'll probably want to > check out something like pygame or python-ogre Yeah, i know about those but i just really need to know if it is possible to do in python if it is what about some tutorials on bone animation? if you kno of any Manowar -- http://mail.python.org/mailman/listinfo/python-list
Re: Python shared lib
thats because the standard way to build python packaged is to use distutils, and not make files. Blame Yafaray for not supplying a setup.py... ..M, Aahz wrote: > In article , > namekuseijin wrote: >> >>and then I realize that, for whatever reason, the super popular and >>trendy python DOESN'T FRIGGIN BUILD SHARED LIBS BY DEFAULT! > > I've got a dim memory that there's a reason for this -- you might try > searching the python-dev archives and/or bugs.python.org. > -- > Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ > > "Normal is what cuts off your sixth finger and your tail..." --Siobhan > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Skeletal animation
On Oct 4, 10:05 pm, Manowar wrote: > I am new to pyton and have asked this question several times the > answer is always not sure. > Here is my question sekeltal animation ( bone animation) is it > possible with python? What i want to develop is an aquarium in > realtime, skeletal animation, all the movements will be from > programming., no keyframes let me know if it is possible with python > > Manowar Depending on how much code you want to write, you'll probably want to check out something like pygame or python-ogre -- http://mail.python.org/mailman/listinfo/python-list
Re: Is pythonic version of scanf() or sscanf() planned?
On Sun, 4 Oct 2009 13:18:22 -0400, Simon Forman wrote: > On Sun, Oct 4, 2009 at 5:29 AM, Martien Verbruggen > wrote: >> On Sun, 4 Oct 2009 01:17:18 + (UTC), >> Grant Edwards wrote: >>> On 2009-10-03, ryniek90 wrote: >>> So, whether it is or has been planned the core Python implementation of *scanf()* ? >>> Given the bad behavior and general fragility of scanf(), I >>> doubt there's much demand for something equally broken for >>> Python. >> >> scanf() is not broken. It's just hard to use correctly for unpredictable >> input. >> >> Having something equivalent in Python would be nice where most or all of >> your input is numerical, i.e. floats or integers. Using the re module, >> or splitting and converting everything with int() or float() slows down >> your program rather spectacularly. If those conversions could be done >> internally, and before storing the input as Python strings, the speed >> improvements could be significant. > I haven't tried it but couldn't you use scanf from ctypes? I have just tried it. I wasn't aware of ctypes, being relatively new to Python. :) However, using ctypes makes the simple test program I wrote actually slower, rather than faster. Probably the extra conversions needed between ctypes internal types and Python's eat op more time. Built in scanf()-like functionality would not need to convert the same information two or three times. it would parse the bytes coming in from the input stream directly, and set the values of the appropriate Python variable directly. Contrive an example: Assume an input file with two integers, and three floats per line, separated by a space. output should be the same two integers, followed by the average of the three floats. In pure python, now, there is string manipulation (file.readline(), and split()) and conversion of floats going on: from sys import * for line in stdin: a, b, u, v, w = line.split() print a, " ", b, " ", (float(u) + float(v) + float(w))/3.0 (17.57s user 0.07s system 99% cpu 17.728 total) With ctypes, it becomes something like: from sys import * from ctypes import * from ctypes.util import find_library libc = cdll.LoadLibrary(find_library('c')) a = c_int() b = c_int() u = c_float() v = c_float() w = c_float() for line in stdin: libc.sscanf(line, "%d%d%f%f%f", byref(a), byref(b), byref(u), byref(v), byref(w)) print "{0} {1} {2}".format(a.value, b.value, (u.value + v.value + w.value)/3.0) (22.21s user 0.10s system 98% cpu 22.628) We no longer need split(), and the three conversions from string to float(), but now we have the 5 c_types(), and the .value dereferences at the end. And that makes it slower, unfortunately. (Maybe I'm still doing things a bit clumsily and it could be faster) It's not really a big deal: As I said before, if I really need the speed, I'll write C: #include int main(void) { int a, b; float u, v, w; while (scanf("%d%d%f%f%f", &a, &b, &u, &v, &w) == 5) printf("%d %d %f\n", a, b, (u + v + w)/3.0); return 0; } (5.96s user 0.06s system 99% cpu 6.042 total) Martien -- | Martien Verbruggen | There is no reason anyone would want a first.l...@heliotrope.com.au | computer in their home. -- Ken Olson, | president DEC, 1977 -- http://mail.python.org/mailman/listinfo/python-list
Skeletal animation
I am new to pyton and have asked this question several times the answer is always not sure. Here is my question sekeltal animation ( bone animation) is it possible with python? What i want to develop is an aquarium in realtime, skeletal animation, all the movements will be from programming., no keyframes let me know if it is possible with python Manowar -- http://mail.python.org/mailman/listinfo/python-list
Re: Client-server PDF creation with xtopdf, XML-RPC, ReportLab and Python
On Oct 4, 7:38 pm, vasudevram wrote: > Hi group, > > I'll update the README.txt file to correct that error soon.) Done. Corrected README.txt uploaded (as part of updated zip file). I forgot to mention, in the original post above, that both the client and the server programs have top-level classes that encapsulate all of their functionality, and those classes are callable / programmable. Which means that you can call those classes (or rather their methods) in your own larger applications, to get that functionality of client- server creation of PDF from text, in your apps. Take a look at the main () functions of the client and the server (in files PDFXMLRPCClient.py and PDFXMLRPCServer.py) - the code is simple and self-explanatory, since those two main functions themselves are instances of code that calls the methods of the classes mentioned above. - Vasudev -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
horos11 wrote: Anyways, maybe I got off to a bad start, Blaming programming errors on non-existent bugs in the interpreter is not a way to endear yourself. And perhaps Python truly is not your style. Maybe PyChecker or PyLint will help, I don't know. I do not use them, but others swear by them. ps - an aside, but what was the rationale behind only displaying one error at a time on trying to run a script? I typically like to run a compilation phase inside my editor (vim), get a list of errors, and then go to each one and fix them. It would complicate the compiler. It is consistent with the rest of Python's error reporting system (one runtime error only also). Error reports after the first may be bogus. Python aware editors, like the one with IDLE, put the cursor at the reported location of a syntax error. And they only have one cursor ;-). And how do you just check a script's syntax without running it anyways? The checker programs parse the code, so I would expect they report syntex errors. Or: put $, a syntax error, on a line at the bottom of your script to abort execution. If the interpreter stops there, everything before is syntacally OK. If using a Python-aware editor, you will pop back into the edit window at the bottom of the file. But of course, not running means not running the test code, so the program is not necessarily OK at all. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing email attachments: get_payload() produces unsaveable data
On Sun, 2009-10-04 at 09:17 -0700, dpapathanasiou wrote: > > Which is *really* difficult (for me) to read. Any chance of providing a > > "normal" traceback? > > File "/opt/server/smtp/smtps.py", line 213, in handle > email_replier.post_reply(recipient_mbox, ''.join(data)) > File "/opt/server/smtp/email_replier.py", line 108, in post_reply > save_attachments(result[2], msg_text) > File "/opt/server/smtp/email_replier.py", line 79, in > save_attachments > data_manager.upload_file(item_id, filename, filedata) > File "../db/data_manager.py", line 697, in upload_file > if docs_db.save_file(item_id, file_name, file_data): > File "../db/docs_db.py", line 102, in save_file > result = file_utils.write_file(saved_file_path, saved_file_name + > saved_file_ext, file_data) > > AttributeError Are you sure this is the complete traceback? Usually an AttributeError returns a text message such as: AttributeError: foo has no such attribute bar Also, the traceback says the exception happened in "save_file", but the code you posted was a function called "save_attachments" and the function call is different. Would be nice if we could get the full traceback with the exact matching code. Otherwise we have to make guesses. But I've given up. Perhaps someone else is better off helping you. -a -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
> > Anyways, maybe I got off to a bad start, but I'm a bit leery of the > language. In my estimation it's trying to be 'too clever by half', and > this coming from a veteran bash/perl programmer. I mean, free form is > one thing, but too much of a good thing can be harmful to your > programming health. Maybe PyChecker or PyLint will help, I don't know. > There's nothing at all clever going on here, though. There just seems to be a lack of understanding of how assignment and variables operate in Python. Nothing is being changed "at a distance"; the lookup and binding semantics of Python are actually really, really simple. Everytime you reference a name, it is first looked up in the local scope: if it's not found there, it's looked up in the global scope. That's it. Everytime you assign a name, it is always assigned to the local scope. Those are the basic rules, and that's really all you need to know for all the basic doings. Anytime you try to get the value of "state", it looks in two places -- local, then global. Anytime you try to assign the value of "state", it assigns it to the local scope. The global scope remains unchanged in the assignment; you're not altering anything at a distance or going and messing with the class definition that lives in the global scope of the module. You've just made a local variable that shadows it. Okay, things get slightly more complicated in two places-- if you use the 'global name' statement within a function, subsequent assignments to name will assign to the global namespace instead of the local namespace. Also, if you nest functions within each-other, there is a limited searching up that nesting of local scopes to find names. There's no other nested / lexical scoping in any other context in Python. But, overall, things are simple. Just different. There's only two scopes or namespaces: the current function body, and the global scope. Nothing's clever going on. All assignment is local, lookups search first the local and then the global namespace. In my experience, once you grasp that it's trivial to never run into this sort of error ever again. I don't just mean finding it when you do it-- but never even writing it anymore. Just don't shadow global names in the local namespace. It's really not a big deal. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL : How to write array to image ???
On Oct 4, 9:47 am, Martin wrote: > On Oct 3, 11:56 pm, Peter Otten <__pete...@web.de> wrote: > > > > > > > Martin wrote: > > > Dear group > > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to > > > an image. > > > Peace of cake, but it comes out looking strange. > > > > I use the below mini code, that I wrote for the purpose. The print of > > > a looks like expected: > > > > [[ 200. 200. 200. ..., 0. 0. 0.] > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > ..., > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > [ 0. 0. 0. ..., 200. 200. 200.]] > > > > But the image looks nothing like that. > > > > Please see the images on: > > >http://hvidberg.net/Martin/temp/quat_col.png > > >http://hvidberg.net/Martin/temp/quat_bw.png > > > > or run the code to see them locally. > > > > Please – what do I do wrong in the PIL part ??? > > > > :-? Martin > > > > import numpy as np > > > from PIL import Image > > > from PIL import ImageOps > > > > maxcol = 100 > > > maxrow = 100 > > > > a = np.zeros((maxcol,maxrow),float) > > > > for i in range(maxcol): > > > for j in range(maxrow): > > > if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>= > > > (maxrow/2)): > > > a[i,j] = 200 > > > else: > > > a[i,j] = 0 > > > > print a > > > > pilImage = Image.fromarray(a,'RGB') > > > pilImage.save('quat_col.png') > > > pilImage = ImageOps.grayscale(pilImage) > > > pilImage.save('quat_bw.png') > > > The PIL seems to copy the array contents directly from memory without any > > conversions or sanity check. In your example The float values determine the > > gray value of 8 consecutive pixels. > > > If you want a[i,j] to become the color of the pixel (i, j) you have to use > > an array with a memory layout that is compatible to the Image. > > Here are a few examples: > > > >>> import numpy > > >>> from PIL import Image > > >>> a = numpy.zeros((100, 100), numpy.uint8) > > >>> a[:50, :50] = a[50:, 50:] = 255 > > >>> Image.fromarray(a).save("tmp1.png") > > >>> b = numpy.zeros((100, 100, 3), numpy.uint8) > > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0] > > >>> Image.fromarray(b).save("tmp2.png") > > >>> c = numpy.zeros((100, 100), numpy.uint32) > > >>> c[:50, :50] = c[50:, 50:] = 0xff808000 > > >>> Image.fromarray(c, "RGBA").save("tmp3.png") > > > Peter > > Thanks All - That helped a lot... > > The working code ended with: > > imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint8) > for ro in range(imgL.shape[1]): > for co in range(imgL.shape[0]): > imga[ro,co] = imgL[ro,co] > Image.fromarray(imga).save('_a'+str(lev)+'.png') Without knowing how big your image is (can't remember if you said!). Perhaps rather than looping in the way you might in C for example, the numpy where might be quicker if you have a big image. Just a thought... -- http://mail.python.org/mailman/listinfo/python-list
Re: organizing your scripts, with plenty of re-use
On 2009-10-04 10:48 AM, Stef Mientki wrote: Steven D'Aprano wrote: On Sat, 03 Oct 2009 10:24:13 +0200, Stef Mientki wrote: I still don't use (because I don't fully understand them) packages, but by trial and error I found a reasonable good working solution, with the following specifications I find that fascinating. You haven't used packages because you don't understand them, but you've used another technique that you *also* don't understand well enough to generate a solution, and had to rely on trial and error. Packages are quite well documented. Since the alternative was trial-and- error on something you also don't fully understand, why did you avoid packages? I want to have the possibility to import any file from any other file: parrot/ +-- __init__.py +-- feeding/ +-- __init__.py +-- eating.py +-- drinking.py +-- fighting.py +-- flying.py +-- sleeping.py +-- talking.py import parrot # loads parrot/__init__.py import parrot.talking # loads parrot/talking.py from parrot import sleeping import parrot.feeding from parrot.feeding.eating import eat_cracker Instead of the above: from sleeping import sleeping_in_a_bed from eating import eat_cracker anything wrong with that (knowing I've no redundancy) ? With the package layout, you would just do: from parrot.sleeping import sleeping_in_a_bed from parrot.feeding.eating import eat_cracker This is really much more straightforward than you are making it out to be. -- 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: creating class objects inside methods
Benjamin Kaplan wrote in news:mailman.838.1254682604.2807.python- l...@python.org in comp.lang.python: >> And how do you just check a script's syntax without running it >> anyways? >> ) > > Because these aren't compile-time errors. Python has no compilation > phase- Sure it does, compilation happens for every script that is executed. And for every import, if the pre-compiled byte code can't be found it is compiled (and the byte code saved as a .pyc or .pyo file). Its only when a the interpreter has the complete compiled byte code for a script or imported module that it executes anything. Python could, if it was wanted, detect multiple syntax and other compilation errors, but AIUI the (CPython) developers choose not to, as it significantly simplifies (and thus speeds up) the compilation process, which can be significant for an interpreted language. For example I just ran a script with the one line: print "hello world" through IronPython (2.0 (2.0.0.0) on .NET 2.0.50727.3082) I counted "1 and 2 and ... 12" before I seeing "hello world" (Aside I think this is something that the current IronPython beta (2.6) "fixes", but I havent tried it myself yet.) > every statement (including def and class) is an executable Yes but for example the execution of the statement: def example() : pass just assignes (binds) the compiled function to the name "example". > statement and it gets turned into byte code at execution time. Just > like any other language, when Python hits a runtime error, it stops. > Rob. -- http://www.victim-prime.dsl.pipex.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Sun, Oct 4, 2009 at 2:44 PM, horos11 wrote: > >> >> > Thanks for the info, but a couple of points: >> >> > 1. it wasn't meant to be production code, simply a way to teach >> > python. >> >> Speaking as someone who does teach Python, "Ew, no!" If you start by >> teaching people bad habits, every educator who comes along afterwards >> will curse your name. That includes teaching yourself. >> >> -- >> Rhodri James *-* Wildebeest Herder to the Masses > > No offense, but I disagree. By programming without regards to pre- > existing style or convention I learned far more than I otherwise would > have if I had simply mimicked someone else. Don't teach newbies bad idiom. > And I still think that unbridled assignment - especially assignment > that can change the operational semantics of surrounding terms, at a > distance no less - is a horrid thing. That's opinion. Python allows you to shoot yourself in the foot. It's on us as programmers to be able to grok our code well enough to prevent nasty errors. "With great power comes great responsibility." and all that. > It gets even worse because the way python handles assignment. To go > back to my original program: why isn't the state variable that I > defined local to that 'if' loop? There's no such thing as an "if loop". The only answer to your question is "that's the way it is"... or, equivalently, "that's the way Guido made it." Check out "Python Scopes and Name Spaces" http://www.python.org/doc/2.2/tut/node11.html#SECTION001120 > while len(dq): > > ... > if curstate.is_answer(): > ... > else: > for state in ... > > > The answer? Because you can't explicitly declare it. It therefore > looks globally, finds the 'class state:' statement, and runs with it. > I should be able to say: > > for my state in curstate.next_states(): > > to show explicitly what I'm doing. You can, you just have to be careful not to "shadow" some other name in your namespace(s). Again, python makes a trade off between "babysitting" the programmer on the one hand vs. raw flexibility on the other. > Anyways, maybe I got off to a bad start, but I'm a bit leery of the > language. In my estimation it's trying to be 'too clever by half', and > this coming from a veteran bash/perl programmer. I mean, free form is > one thing, but too much of a good thing can be harmful to your > programming health. Maybe PyChecker or PyLint will help, I don't know. I think if you let python seep into your thinking you'll eventually come to love it. It /IS/ very (too) clever, but the cleverness is (IMHO) spent on getting out of your way, rather than trying to insure you're doing things right. Eric Raymond has a great article where he talks about learning python: http://www.linuxjournal.com/article/3882 "My second [surprise] came a couple of hours into the project, when I noticed (allowing for pauses needed to look up new features in Programming Python) I was generating working code nearly as fast as I could type. When I realized this, I was quite startled. An important measure of effort in coding is the frequency with which you write something that doesn't actually match your mental representation of the problem, and have to backtrack on realizing that what you just typed won't actually tell the language to do what you're thinking. An important measure of good language design is how rapidly the percentage of missteps of this kind falls as you gain experience with the language." I can vouch for this: I can routinely write 200~300 lines of python code and have it run flawlessly the first time. This doesn't happen every time, but it's the norm rather than the exception. Give it a chance. It's (IMHO) /beautiful/. Happy hacking, ~Simon > Ed > > ( > ps - an aside, but what was the rationale behind only displaying one > error at a time on trying to run a script? I typically like to run a > compilation phase inside my editor (vim), get a list of errors, and > then go to each one and fix them. I dunno the rationale, but the mechanism is uncaught exceptions propagate up and halt the interpreter. Since nothing keeps going after an uncaught exception, there's nothing there to report the next error. > And how do you just check a script's syntax without running it > anyways? Just run it. ;] FWIW, use the interactive interpreter (or IPython variant) to play with the language until you learn the syntax well enough not to write bad syntax. For me anyway, that was the best/fastest way to learn it. -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Oct 4, 3:12 am, Albert Hopkins wrote: > * You define a to_string() method. To have a string representation > of a class, one usually defines a __str__ method. This gives > the advantage whereby "print myobject" or '%s' % myjobject just > work. In fairness, a lot of types define a to_string() method (especially third-party types like numpy), but it's usually to produce a raw binary output. Presumably in Python 3 these methods will be renamed to_bytes. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Oct 4, 11:56 am, Benjamin Kaplan wrote: > On Sun, Oct 4, 2009 at 2:44 PM, horos11 wrote: > > > ( > > ps - an aside, but what was the rationale behind only displaying one > > error at a time on trying to run a script? I typically like to run a > > compilation phase inside my editor (vim), get a list of errors, and > > then go to each one and fix them. > > > And how do you just check a script's syntax without running it > > anyways? > > ) > > Because these aren't compile-time errors. Python has no compilation > phase- every statement (including def and class) is an executable > statement and it gets turned into byte code at execution time. Just > like any other language, when Python hits a runtime error, it stops. No, there is a compile phase, but the only error that is raised at compile-time is SyntaxError. Because of Python's dynamicism the compiler knows hardly anything about the objects at compile-time (except in a few cases involving constants, which Python takes advantage of to do some compile-time constant folding). Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Sun, Oct 4, 2009 at 2:44 PM, horos11 wrote: > >> >> > Thanks for the info, but a couple of points: >> >> > 1. it wasn't meant to be production code, simply a way to teach >> > python. >> >> Speaking as someone who does teach Python, "Ew, no!" If you start by >> teaching people bad habits, every educator who comes along afterwards >> will curse your name. That includes teaching yourself. >> >> -- >> Rhodri James *-* Wildebeest Herder to the Masses > > No offense, but I disagree. By programming without regards to pre- > existing style or convention I learned far more than I otherwise would > have if I had simply mimicked someone else. > > And I still think that unbridled assignment - especially assignment > that can change the operational semantics of surrounding terms, at a > distance no less - is a horrid thing. > > It gets even worse because the way python handles assignment. To go > back to my original program: why isn't the state variable that I > defined local to that 'if' loop? > > while len(dq): > > ... > if curstate.is_answer(): > ... > else: > for state in ... > > > The answer? Because you can't explicitly declare it. It therefore > looks globally, finds the 'class state:' statement, and runs with it. > I should be able to say: > > for my state in curstate.next_states(): > > to show explicitly what I'm doing. > > > Anyways, maybe I got off to a bad start, but I'm a bit leery of the > language. In my estimation it's trying to be 'too clever by half', and > this coming from a veteran bash/perl programmer. I mean, free form is > one thing, but too much of a good thing can be harmful to your > programming health. Maybe PyChecker or PyLint will help, I don't know. > > Ed > > ( > ps - an aside, but what was the rationale behind only displaying one > error at a time on trying to run a script? I typically like to run a > compilation phase inside my editor (vim), get a list of errors, and > then go to each one and fix them. > > And how do you just check a script's syntax without running it > anyways? > ) Because these aren't compile-time errors. Python has no compilation phase- every statement (including def and class) is an executable statement and it gets turned into byte code at execution time. Just like any other language, when Python hits a runtime error, it stops. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
> > > Thanks for the info, but a couple of points: > > > 1. it wasn't meant to be production code, simply a way to teach > > python. > > Speaking as someone who does teach Python, "Ew, no!" If you start by > teaching people bad habits, every educator who comes along afterwards > will curse your name. That includes teaching yourself. > > -- > Rhodri James *-* Wildebeest Herder to the Masses No offense, but I disagree. By programming without regards to pre- existing style or convention I learned far more than I otherwise would have if I had simply mimicked someone else. And I still think that unbridled assignment - especially assignment that can change the operational semantics of surrounding terms, at a distance no less - is a horrid thing. It gets even worse because the way python handles assignment. To go back to my original program: why isn't the state variable that I defined local to that 'if' loop? while len(dq): ... if curstate.is_answer(): ... else: for state in ... The answer? Because you can't explicitly declare it. It therefore looks globally, finds the 'class state:' statement, and runs with it. I should be able to say: for my state in curstate.next_states(): to show explicitly what I'm doing. Anyways, maybe I got off to a bad start, but I'm a bit leery of the language. In my estimation it's trying to be 'too clever by half', and this coming from a veteran bash/perl programmer. I mean, free form is one thing, but too much of a good thing can be harmful to your programming health. Maybe PyChecker or PyLint will help, I don't know. Ed ( ps - an aside, but what was the rationale behind only displaying one error at a time on trying to run a script? I typically like to run a compilation phase inside my editor (vim), get a list of errors, and then go to each one and fix them. And how do you just check a script's syntax without running it anyways? ) -- http://mail.python.org/mailman/listinfo/python-list
Re: Windows GCC Support (Mingw & Mingw-w64)
> Is there any chance of getting some of the devs or anyone familiar > enough with the source code to make this possibility become reality? Please take a look at http://bugs.python.org/issue4709 Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python: Rag October issue available
On Sun, 04 Oct 2009 07:37:35 -0500, Bernie wrote: > On Sat, 03 Oct 2009 20:09:18 -0700, TerryP wrote: > >> On Oct 3, 4:29 pm, Bernie wrote: >>> Hi, no -its just put on the website. Unless there's a method you can >>> suggest? >> >> Not to butt in, but off the top of my head, you could probably set up a >> mailing list and post the link to the file every cycle - simple but >> effective. > > Yes, good suggestion - I've had a look at google groups and they seem to > provide comprehensive facilities. I'll set one up. And here it is: http://groups.google.co.uk/group/pythonrag -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Sun, Oct 4, 2009 at 1:12 AM, horos11 wrote: > >> >>> a >> >> <__main__.Myclass instance at 0x95cd3ec b >> >> <__main__.Myclass instance at 0x95cd5ac> >> >> What's the problem? > > Like I said, the code was a sample of what I was trying to do, not the > entire thing.. I just wanted to see if the metaphor was kosher. Right, but it doesn't help if you post code that doesn't actually display the unexpected behaviour you're asking about. > It sounds to me from your answer that this is unexpected behavior, so > I'll go ahead and post the whole thing. My guess is that it is a > python bug.. /What/ is unexpected behaviour? I ran the code you posted and worked exactly like I expected. Posting the whole thing is better than posting code that doesn't display the issue, but this is too long. I'd ask you to repost a minimal version that actually displays the problem you're asking about but I see from the rest of this thread that you've already figured it out. Related to that, two points. Learn python AS python, don't get caught up in what it does differently than other languages. IMHO it's hands-down the most useful. productive language out there for a tremendous number of problem domains. Treat yourself to it. ;] And second, please don't /teach/ python until you've learned it... Regards, ~simon > Run it (a simple puzzle game solved by breadth first search), and the > first time state() is called inside the method, it calls __init__. > Second time, and therafter, it calls __call__. I've highlighted where > the code fails by putting a pdb.set_trace(). > > Anyways, I've got a workaround (simply pass in any new objects needed > from the caller), but it is truly annoying that python is either > misleading or broken in this way. > > Attached find code, does not work vs. 2.6.. > > > Ed > > > > from collections import deque > import copy > import pdb > > class state: > > def default_board(): > > return [ > [ 1, 'x', 'x', 0 ], > [ 2, 2, 3, 4 ], > [ 5, 6, 6, 7 ], > [ 5, 6, 6, 7 ], > [ 8, 9, 10, 10 ], > [ 0, 'x', 'x', 0 ] > ] > > def default_types(): > > return { > 1 : [ 0, 0 ], > 2 : [ 0, 0, 0, 1 ], > 3 : [ 0, 0 ], > 4 : [ 0, 0 ], > 5 : [ 0, 0, 1, 0 ], > 6 : [ 0, 0, 1, 0, 0, 1, 1, 1 ], > 7 : [ 0, 0, 1, 0 ], > 8 : [ 0, 0 ], > 9 : [ 0, 0 ], > 10 : [ 0, 0, 0, 1 ] > } > > def default_moves(): > > return [] > > def print_move(self, moveno, move): > print str(moveno) + ": " + str(move) + "\n" > > > def __init__(self, _board=default_board(), _moves=default_moves(), > _types=default_types()): > > self.board = _board > self.moves = _moves > self.types = _types > > def possible_moves(self): > > moves_so_far = set() > moves_so_far.add('x') > moves_so_far.add(0) > ret = [] > for y_idx in range(0, len(self.board)): > for x_idx in range(0, len(self.board[y_idx])): > > piece = self.board[y_idx][x_idx] > > if not piece in moves_so_far: > > moves = self.legal_moves(y_idx, x_idx) > moves_so_far.add(piece) > > if moves: > ret.extend(moves) > > return ret > > def is_answer(self): > > if self.board[5][3] == 1: > return True > else: > return False > > def legal_moves(self, ycoord, xcoord): > > ret = [] > for dir in [ [ 0, 1 ], [ 0, -1 ], [ 1, 0 ], [ -1, 0 ] ]: > ret.extend(self.addmove(dir[0], dir[1], ycoord, xcoord)) > > return ret > > def empty(self, type, ycoord, xcoord, pieceno): > > for itr in range(0, len(type), 2): > > yy = type[itr] > xx = type[itr+1] > > if not (len(self.board) > (yy+ycoord) >= 0) or not (len > (self.board[yy+ycoord]) > xx+xcoord >= 0): > return False > > if not self.board[yy+ycoord][xx+xcoord] in [ 0, pieceno ]: > return False > > return True > > def addmove(self, ymult, xmult, ycoord, xcoord): > > ret = [] > pieceno = self.board[ycoord][xcoord] > type = self.types[pieceno] > > if xmult != 0: > for xx in range(xcoord + xmult, -1 if xmult < 0 else 4, -1 > if xmult < 0 else 1): > # if xx == 0: > # continue > if self.empty(type, ycoord, xx, pieceno): > ret.append(self.newmove(ycoord, xcoord, ycoord, > xx )) > else: > break > > if ymult != 0: > for yy in range(ycoord + ymult, -1 if ymult < 0 else 6, -1 > if ymult < 0 else 1): > # if yy == 0: > # continue >
Re: Is pythonic version of scanf() or sscanf() planned?
On Sun, Oct 4, 2009 at 5:29 AM, Martien Verbruggen wrote: > On Sun, 4 Oct 2009 01:17:18 + (UTC), > Grant Edwards wrote: >> On 2009-10-03, ryniek90 wrote: >> >>> So, whether it is or has been planned the core Python >>> implementation of *scanf()* ? >> >> One of the fist things I remember being taught as a C progrmmer >> was to never use scanf. Programs that use scanf tend to fail >> in rather spectacular ways when presented with simple typos and >> other forms of unexpected input. > > That's right. One shouldn't use scanf() if the input is unpredictable > osr comes from people, because the pitfalls are many and hard to avoid. > However, for input that is formatted, scanf() is perfectly fine, and > nice and fast. > > fgets() with sscanf() is better to control if your input is not as > guaranteed. > >> Given the bad behavior and general fragility of scanf(), I >> doubt there's much demand for something equally broken for >> Python. > > scanf() is not broken. It's just hard to use correctly for unpredictable > input. > > Having something equivalent in Python would be nice where most or all of > your input is numerical, i.e. floats or integers. Using the re module, > or splitting and converting everything with int() or float() slows down > your program rather spectacularly. If those conversions could be done > internally, and before storing the input as Python strings, the speed > improvements could be significant. > > There is too much storing, splitting, regex matching and converting > going on if you need to read numerical data from columns in a file. > scanf() and friends make this sort of task rather quick and easy. > > For example, if your data is the output of a numerical analysis program > or data coming from a set of measuring probes, it often takes the form > of one or more columns of numbers, and there can be many of them. If you > want to take one of these output files, and transform the data, Python > can be terribly slow. > > It doesn't have to be scanf(), but something that would allow the direct > reading of text input as numerical data would be nice. > > On the other hand, if something really needs to be fast, I generally > write it in C anyway :) > > Martien I haven't tried it but couldn't you use scanf from ctypes? -- http://mail.python.org/mailman/listinfo/python-list
Re: defaults for function arguments bound only once(??)
On Sun, Oct 4, 2009 at 2:29 AM, horos11 wrote: > All, > > Another one, this time a bit shorter. > > It looks like defaults for arguments are only bound once, and every > subsequent call reuses the first reference created. Hence the > following will print '[10,2]' instead of the expected '[1,2]'. > > Now my question - exactly why is 'default_me()' only called once, on > the construction of the first object? And what's the best way to get > around this if you want to have a default for an argument which so > happens to be a reference or a new object? This is a FAQ: http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects > > code begins here --- > > import copy > class A: > > def default_me(): > return [1,2] > > def __init__(self, _arg=default_me()): > self.arg = _a > > > a = A() > a.arg[0] = 10 > b = A() > > print b.arg # prints [10,2] Your code is weird: you import copy module but don't use it; you define default_me() as a "sort of" static method (http://docs.python.org/library/functions.html#staticmethod) but then only use it to generate a default argument that has nothing to do with the class you just defined; and in __init__() you use "_a" which isn't defined anywhere in this code snippet. What are you actually trying to accomplish? -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing email attachments: get_payload() produces unsaveable data
> Which is *really* difficult (for me) to read. Any chance of providing a > "normal" traceback? File "/opt/server/smtp/smtps.py", line 213, in handle email_replier.post_reply(recipient_mbox, ''.join(data)) File "/opt/server/smtp/email_replier.py", line 108, in post_reply save_attachments(result[2], msg_text) File "/opt/server/smtp/email_replier.py", line 79, in save_attachments data_manager.upload_file(item_id, filename, filedata) File "../db/data_manager.py", line 697, in upload_file if docs_db.save_file(item_id, file_name, file_data): File "../db/docs_db.py", line 102, in save_file result = file_utils.write_file(saved_file_path, saved_file_name + saved_file_ext, file_data) AttributeError -- http://mail.python.org/mailman/listinfo/python-list
Re: "from logging import *" causes an error under Ubuntu Karmic
On Oct 4, 4:47 pm, Benjamin Kaplan wrote: > Looks like it's a Python problem, not Ubuntu's. That's correct, it's a Python problem. A fix has been checked in on the release26-maint branch (which means that it should be available in 2.6.4) and a unit test added to catch this case in the future. It was my goof, and I'm sorry for all the inconvenience caused. The fix was to remove "captureWarnings" and "NullHandler" from __all__ in logging/__init__.py, so you can patch your local installation if you need to. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing email attachments: get_payload() produces unsaveable data
On Sun, 2009-10-04 at 08:16 -0700, dpapathanasiou wrote: > > And where might we be able to see that stack trace? > > This is it: > > Exception: ('AttributeError', '', [' File "/opt/server/smtp/ > smtps.py", line 213, in handle\ne > mail_replier.post_reply(recipient_mbox, \'\'.join(data))\n', ' File "/ > opt/server/smtp/email_replier.py", l > ine 108, in post_reply\nsave_attachments(result[2], msg_text)\n', > ' File "/opt/server/smtp/email_repli > er.py", line 79, in save_attachments\ndata_manager.upload_file > (item_id, filename, filedata)\n', ' File > "../db/data_manager.py", line 697, in upload_file\nif > docs_db.save_file(item_id, file_name, file_data) > :\n', ' File "../db/docs_db.py", line 102, in save_file\nresult = > file_utils.write_file(saved_file_pat > h, saved_file_name + saved_file_ext, file_data)\n']) > > If you're wondering, I'm using this to capture the exception: > > def formatExceptionInfo(maxTBlevel=5): > """For displaying exception information""" > cla, exc, trbk = sys.exc_info() > excName = cla.__name__ > try: > excArgs = exc.__dict__["args"] > except KeyError: > excArgs = "" > excTb = traceback.format_tb(trbk, maxTBlevel) > return (excName, excArgs, excTb) > Which is *really* difficult (for me) to read. Any chance of providing a "normal" traceback? -- http://mail.python.org/mailman/listinfo/python-list
Re: organizing your scripts, with plenty of re-use
Steven D'Aprano wrote: On Sat, 03 Oct 2009 10:24:13 +0200, Stef Mientki wrote: I still don't use (because I don't fully understand them) packages, but by trial and error I found a reasonable good working solution, with the following specifications I find that fascinating. You haven't used packages because you don't understand them, but you've used another technique that you *also* don't understand well enough to generate a solution, and had to rely on trial and error. Packages are quite well documented. Since the alternative was trial-and- error on something you also don't fully understand, why did you avoid packages? I want to have the possibility to import any file from any other file: parrot/ +-- __init__.py +-- feeding/ +-- __init__.py +-- eating.py +-- drinking.py +-- fighting.py +-- flying.py +-- sleeping.py +-- talking.py import parrot # loads parrot/__init__.py import parrot.talking # loads parrot/talking.py from parrot import sleeping import parrot.feeding from parrot.feeding.eating import eat_cracker Instead of the above: from sleeping import sleeping_in_a_bed from eating import eat_cracker anything wrong with that (knowing I've no redundancy) ? cheers, Stef -- http://mail.python.org/mailman/listinfo/python-list
Re: "from logging import *" causes an error under Ubuntu Karmic
I can confirm this in Python 2.6.3 for Windows and Mac while it doesn't appear in Python 2.6.2 on Windows or the system Python 2.6.1 in Snow Leopard. Looks like it's a Python problem, not Ubuntu's. On Sun, Oct 4, 2009 at 3:31 AM, Valery wrote: > OK, I've filed a bug. Because Python2.5 works fine here. > -- > Valery > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing email attachments: get_payload() produces unsaveable data
> And where might we be able to see that stack trace? This is it: Exception: ('AttributeError', '', [' File "/opt/server/smtp/ smtps.py", line 213, in handle\ne mail_replier.post_reply(recipient_mbox, \'\'.join(data))\n', ' File "/ opt/server/smtp/email_replier.py", l ine 108, in post_reply\nsave_attachments(result[2], msg_text)\n', ' File "/opt/server/smtp/email_repli er.py", line 79, in save_attachments\ndata_manager.upload_file (item_id, filename, filedata)\n', ' File "../db/data_manager.py", line 697, in upload_file\nif docs_db.save_file(item_id, file_name, file_data) :\n', ' File "../db/docs_db.py", line 102, in save_file\nresult = file_utils.write_file(saved_file_pat h, saved_file_name + saved_file_ext, file_data)\n']) If you're wondering, I'm using this to capture the exception: def formatExceptionInfo(maxTBlevel=5): """For displaying exception information""" cla, exc, trbk = sys.exc_info() excName = cla.__name__ try: excArgs = exc.__dict__["args"] except KeyError: excArgs = "" excTb = traceback.format_tb(trbk, maxTBlevel) return (excName, excArgs, excTb) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python shared lib
In article , namekuseijin wrote: > >and then I realize that, for whatever reason, the super popular and >trendy python DOESN'T FRIGGIN BUILD SHARED LIBS BY DEFAULT! I've got a dim memory that there's a reason for this -- you might try searching the python-dev archives and/or bugs.python.org. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "Normal is what cuts off your sixth finger and your tail..." --Siobhan -- http://mail.python.org/mailman/listinfo/python-list
Re: execfile in python3 breaks emacs mode
Just answering my own question A little googling tells me to use (cmd (format "exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n" filename filename))) instead of (cmd (format "exec(open(r'%s').read()) # PYTHON-MODE\n" filename))) sheesh! On Sun, Oct 4, 2009 at 6:57 PM, Rustom Mody wrote: > > Removing execfile from python3 has broken the good-ol python-mode of emacs. > > Changing the line > > In python-mode.el in function py-execute-file changing the line > (cmd (format "execfile(r'%s') # PYTHON-MODE\n" filename))) > > to > (cmd (format "exec(open(r'%s').read()) # PYTHON-MODE\n" filename))) > > seems to solve the problem > > Since I am not quite upto the subtleties of what all has changed vis-a-vis > exec/execfile, please inform me if this is ok > -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing email attachments: get_payload() produces unsaveable data
On Sun, 2009-10-04 at 07:27 -0700, dpapathanasiou wrote: > When I try to write the filedata to a file system folder, though, I > get an AttributeError in the stack trace. And where might we be able to see that stack trace? -a -- http://mail.python.org/mailman/listinfo/python-list
Client-server PDF creation with xtopdf, XML-RPC, ReportLab and Python
Hi group, I've released a software package named PDFXMLRPC. It consists of a server and a client. Using them, you can do client-server PDF creation from text, over the Internet or your intranet. It runs over XML-RPC and uses HTTP as the transport. It can work with any available port, including the standard HTTP port 80, or any other allowed and available one. The server is written in Python and uses xtopdf (also written by me), XML-RPC (from the standard Python library), ReportLab (the open source version) and Python. The client is written in Python and uses XML-RPC. PDFXMLRPC can be downloaded from here: http://www.dancingbison.com/PDFXMLRPC.zip That zip file contains the server, the client, a README.txt file and the license. (NOTE: The README.txt file contains a line saying to download the PDFXMLRPC package from http://www.dancingbison.com/PDFXMLRPCClientAndServer.zip - that line is a mistake, left over from an earlier unreleased version. If you have the README.txt file, it means that you have already downloaded and extracted the correct file - which is PDFXMLRPC.zip, so just ignore that wrong line. I'll update the README.txt file to correct that error soon.) The README.txt file gives details on how to install the software needed, and how to run it. PDFXMLRPC is released under the BSD license. I welcome any feedback on it. I would like to thank the Python and ReportLab guys for creating great software. Thanks, Vasudev Ram Dancing Bison Enterprises http://www.dancingbison.com -- http://mail.python.org/mailman/listinfo/python-list
Parsing email attachments: get_payload() produces unsaveable data
I'm using python to access an email account via POP, then for each incoming message, save any attachments. This is the function which scans the message for attachments: def save_attachments (local_folder, msg_text): """Scan the email message text and save the attachments (if any) in the local_folder""" if msg_text: for part in email.message_from_string(msg_text).walk(): if part.is_multipart() or part.get_content_maintype() == 'text': continue filename = part.get_filename(None) if filename: filedata = part.get_payload(decode=True) if filedata: write_file(local_folder, filename, filedata) All the way up to write_file(), it's working correctly. The filename variable matches the name of the attached file, and the filedata variable contains binary data corresponding to the file's contents. When I try to write the filedata to a file system folder, though, I get an AttributeError in the stack trace. Here is my write_file() function: def write_file (folder, filename, f, chunk_size=4096): """Write the the file data f to the folder and filename combination""" result = False if confirm_folder(folder): try: file_obj = open(os.path.join(folder, file_base_name (filename)), 'wb', chunk_size) for file_chunk in read_buffer(f, chunk_size): file_obj.write(file_chunk) file_obj.close() result = True except (IOError): print "file_utils.write_file: could not write '%s' to '%s'" % (file_base_name(filename), folder) return result I also tried applying this regex: filedata = re.sub(r'\r(?!=\n)', '\r\n', filedata) # Bare \r becomes \r \n after reading this post (http://stackoverflow.com/questions/787739/ python-email-getpayload-decode-fails-when-hitting-equal-sign), but it hasn't resolved the problem. Is there any way of correcting the output of get_payload() so I can save it to a file? -- http://mail.python.org/mailman/listinfo/python-list
execfile in python3 breaks emacs mode
Removing execfile from python3 has broken the good-ol python-mode of emacs. Changing the line In python-mode.el in function py-execute-file changing the line (cmd (format "execfile(r'%s') # PYTHON-MODE\n" filename))) to (cmd (format "exec(open(r'%s').read()) # PYTHON-MODE\n" filename))) seems to solve the problem Since I am not quite upto the subtleties of what all has changed vis-a-vis exec/execfile, please inform me if this is ok -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
Jon Clements wrote: > On Oct 4, 12:08 pm, n00m wrote: >> Duncan Booth, >> >> alas... still TLE: >> >> 2800839 >> 2009-10-04 13:03:59 >> Q >> Enormous Input and Output Test >> time limit exceeded >> - >> 88M >> PYTH > > Just to throw into the mix... > > What about buffering? Does anyone know what the effective stdin buffer > is for Python? I mean, it really can't be the multiplying that's a > bottleneck. Not sure if it's possible, but can a new stdin be created > (possibly using os.fdopen) with a hefty buffer size? > > I'm probably way off, but something to share. > I did try a version where I just read the data in, split it up, and then wrote it out again. On my test file that took about 2 seconds compared with the 8 seconds it took the full code I posted, so while there may be scope for faster I/O (e.g. using mmap), any real speedup would have to be in the convert to int, multiply, convert back to str pipeline. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python: Rag October issue available
On Sat, 03 Oct 2009 20:09:18 -0700, TerryP wrote: > On Oct 3, 4:29 pm, Bernie wrote: >> Hi, no -its just put on the website. Unless there's a method you can >> suggest? > > Not to butt in, but off the top of my head, you could probably set up a > mailing list and post the link to the file every cycle - simple but > effective. Yes, good suggestion - I've had a look at google groups and they seem to provide comprehensive facilities. I'll set one up. -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Sun, 04 Oct 2009 07:14:08 +0100, horos11 wrote: Carl, Thanks for the info, but a couple of points: 1. it wasn't meant to be production code, simply a way to teach python. Speaking as someone who does teach Python, "Ew, no!" If you start by teaching people bad habits, every educator who comes along afterwards will curse your name. That includes teaching yourself. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
On Oct 4, 12:08 pm, n00m wrote: > Duncan Booth, > > alas... still TLE: > > 2800839 > 2009-10-04 13:03:59 > Q > Enormous Input and Output Test > time limit exceeded > - > 88M > PYTH Just to throw into the mix... What about buffering? Does anyone know what the effective stdin buffer is for Python? I mean, it really can't be the multiplying that's a bottleneck. Not sure if it's possible, but can a new stdin be created (possibly using os.fdopen) with a hefty buffer size? I'm probably way off, but something to share. Cheers, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
n00m wrote: > > I've given up :-) Here's my attempt, which is about 30% faster than your original but I've no idea if it would be fast enough for you. import sys, time, os, itertools import gc gc.set_threshold() D = [] def foo(): ##sys.stdin = open('D:/1583.txt', 'rt') count = int(sys.stdin.readline()) data = sys.stdin.read().split() D.append(data) data = map(int, data) D.append(data) nextval = iter(data).next data = map(str, (nextval()*nextval() for a in xrange(count))) D.append(data) sys.stdout.write('\n'.join(data)) sys.stdout.write('\n') start = time.time() foo() print >>sys.stderr, time.time() - start os._exit(0) Storing the large lists in a global prevent them deallocating when the function returns, so speeds up the time as I recorded it. If they are timing the whole process then I think calling _exit() should avoid the final cleanup time. Playing with the garbage collector makes it ever so slightly faster although disabling it entirely makes it much slower. I feel there ought to be a faster alternative to xrange but if so I couldn't find it. As Terry said, it's all hack tricks. -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
Just by a brief look at your code snippet there are a few things that I would point out, stylistically, that you may consider changing in your code as they are generally not considered "pythonic": * As already mentioned the "state" class is best if given a name that is capitalized. * As already mentioned the name "state" is (unintentionally) re-used within the same scope. This could have been avoided with the above naming convention change and also if the main body of the code were in a function, say "main()" * The "methods" default_board, default_types, and default_moves appear to be static methods but are not defined as such. * Also, since the above "methods" have no logic and simply return a value they are probably best defined as static attributes rather than methods. But if doing so you would need to alter your constructor. * You used a number of built-in names as attribute names (dir, type). While this is legal Python it is generally not recommended. * You use range(0, n). 0 is the default start value for range() so it is generally left out. * You define a to_string() method. To have a string representation of a class, one usually defines a __str__ method. This gives the advantage whereby "print myobject" or '%s' % myjobject just work. Probably others can make more suggestions. You will find a lot of these suggestions are borrowed from PEP8 [1] but you can peruse other people's Python code to "learn from the masters". Hope this helps, -a -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
Terry Reedy: > Don't waste your time with problem sites that judge raw-clock time over > (and before) accuracy, thereby greatly favoring low-level languages and > hack tricks over clear high-level code. I usually don't like to solve the kind of problems shown by those sites because those problems are too much artificial (and often too much difficult). But sometimes I have written some solutions. But those sites never judge "raw" running time over accuracy: in most or all such sites the programs are tested with tons of possible inputs, and if even one output is a bit wrong, the program is totally refused. This is a hard rule that encourages programmers to take a very good care of program correctness. Some sites add a little more "noise" in the inputs, simulating a bit more real-world inputs, while most of those online contests give clean inputs (the input bounds are well specified in the problem statement). >From what I've seen from some of the best solutions submitted to those sites (some sites allow people to see the source of the contest entries), the programs usually don't (need to) use "hack tricks" as you say (even if probably some program uses them). Using hacks is often unsafe so people usually prefer safer ways to code, because just a little bug may fully compromise the score of the program. I agree that the timing scores in such sites often encourage low level languages, like C (and sometimes C++, that's a multilevel language), but on the other hand such languages exist, C is used in many real- world places, so designing sites where people compete with such languages is legit. C allows people to understand better what's going on inside the computer, this is valuable and positive. Bashing low- level languages is silly. Even CPython is written in C. A good programmer has to know both higher and lower level languages. And in real-life sometimes you need performance. This thread shows that a normal Python program is not up to those timings for the enormous input problem (even if there are ways to write a Python program to solve this problem). People at Google are trying to create a 5 times faster Python (Unladen Swallow project) because they use lot of real-world Python code and they think Python is slow. I've found plenty of situations where CPython code is not fast enough for my purposes. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: Is pythonic version of scanf() or sscanf() planned?
On Sun, 4 Oct 2009 01:17:18 + (UTC), Grant Edwards wrote: > On 2009-10-03, ryniek90 wrote: > >> So, whether it is or has been planned the core Python >> implementation of *scanf()* ? > > One of the fist things I remember being taught as a C progrmmer > was to never use scanf. Programs that use scanf tend to fail > in rather spectacular ways when presented with simple typos and > other forms of unexpected input. That's right. One shouldn't use scanf() if the input is unpredictable osr comes from people, because the pitfalls are many and hard to avoid. However, for input that is formatted, scanf() is perfectly fine, and nice and fast. fgets() with sscanf() is better to control if your input is not as guaranteed. > Given the bad behavior and general fragility of scanf(), I > doubt there's much demand for something equally broken for > Python. scanf() is not broken. It's just hard to use correctly for unpredictable input. Having something equivalent in Python would be nice where most or all of your input is numerical, i.e. floats or integers. Using the re module, or splitting and converting everything with int() or float() slows down your program rather spectacularly. If those conversions could be done internally, and before storing the input as Python strings, the speed improvements could be significant. There is too much storing, splitting, regex matching and converting going on if you need to read numerical data from columns in a file. scanf() and friends make this sort of task rather quick and easy. For example, if your data is the output of a numerical analysis program or data coming from a set of measuring probes, it often takes the form of one or more columns of numbers, and there can be many of them. If you want to take one of these output files, and transform the data, Python can be terribly slow. It doesn't have to be scanf(), but something that would allow the direct reading of text input as numerical data would be nice. On the other hand, if something really needs to be fast, I generally write it in C anyway :) Martien -- | Martien Verbruggen | Unix is user friendly. It's just first.l...@heliotrope.com.au | selective about its friends. | -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary with Lists
John Nagle schrieb: > Shaun wrote: >> I'm trying to create a dictionary with lists as the value for each >> key. > >Try using a tuple, instead of a list, for each key. Tuples > are immutable, so there's no issue about a key changing while > being used in a dictionary. Only if Shaun wanted to use lists as keys (which of course doesn't work). Luckily he just wants them as values. Mick. -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
On Oct 4, 4:20 am, n00m wrote: > I've given up :-) Well, that numerix user (who already had the top Python solution) just submitted a ton of new ones to that problem, apparently trying to get a faster time. I don't think he can squeeze much more out of that stone, but unlike us, he's routinely under 11s. Crazy. I wish they had an option to let you keep running your program past the limit (at least two or three times the limit) to give you more feedback, even if they still consider your solution unacceptable. Especially in the tutorial section, which doesn't seem to contribute to your score anyway. John -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL : How to write array to image ???
On Oct 3, 11:56 pm, Peter Otten <__pete...@web.de> wrote: > Martin wrote: > > Dear group > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to > > an image. > > Peace of cake, but it comes out looking strange. > > > I use the below mini code, that I wrote for the purpose. The print of > > a looks like expected: > > > [[ 200. 200. 200. ..., 0. 0. 0.] > > [ 200. 200. 200. ..., 0. 0. 0.] > > [ 200. 200. 200. ..., 0. 0. 0.] > > ..., > > [ 0. 0. 0. ..., 200. 200. 200.] > > [ 0. 0. 0. ..., 200. 200. 200.] > > [ 0. 0. 0. ..., 200. 200. 200.]] > > > But the image looks nothing like that. > > > Please see the images on: > >http://hvidberg.net/Martin/temp/quat_col.png > >http://hvidberg.net/Martin/temp/quat_bw.png > > > or run the code to see them locally. > > > Please – what do I do wrong in the PIL part ??? > > > :-? Martin > > > import numpy as np > > from PIL import Image > > from PIL import ImageOps > > > maxcol = 100 > > maxrow = 100 > > > a = np.zeros((maxcol,maxrow),float) > > > for i in range(maxcol): > > for j in range(maxrow): > > if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>= > > (maxrow/2)): > > a[i,j] = 200 > > else: > > a[i,j] = 0 > > > print a > > > pilImage = Image.fromarray(a,'RGB') > > pilImage.save('quat_col.png') > > pilImage = ImageOps.grayscale(pilImage) > > pilImage.save('quat_bw.png') > > The PIL seems to copy the array contents directly from memory without any > conversions or sanity check. In your example The float values determine the > gray value of 8 consecutive pixels. > > If you want a[i,j] to become the color of the pixel (i, j) you have to use > an array with a memory layout that is compatible to the Image. > Here are a few examples: > > >>> import numpy > >>> from PIL import Image > >>> a = numpy.zeros((100, 100), numpy.uint8) > >>> a[:50, :50] = a[50:, 50:] = 255 > >>> Image.fromarray(a).save("tmp1.png") > >>> b = numpy.zeros((100, 100, 3), numpy.uint8) > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0] > >>> Image.fromarray(b).save("tmp2.png") > >>> c = numpy.zeros((100, 100), numpy.uint32) > >>> c[:50, :50] = c[50:, 50:] = 0xff808000 > >>> Image.fromarray(c, "RGBA").save("tmp3.png") > > Peter Thanks All - That helped a lot... The working code ended with: imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint8) for ro in range(imgL.shape[1]): for co in range(imgL.shape[0]): imga[ro,co] = imgL[ro,co] Image.fromarray(imga).save('_a'+str(lev)+'.png') -- http://mail.python.org/mailman/listinfo/python-list
Re: weak reference to bound method
On Oct 2, 4:54 am, Ole Streicher wrote: > Hi group, > > I am trying to use a weak reference to a bound method: > > class MyClass(object): > def myfunc(self): > pass > > o = MyClass() > print o.myfunc > > > > > import weakref > r = weakref.ref(o.myfunc) > print r() > > None > > This is what I do not understand. The object "o" is still alive, and > therefore the bound method "o.myfunc" shall exists. > > Why does the weak reference claim that it is removed? And how can I hold > the reference to the method until the object is removed? > > Is this a bug or a feature? (Python 2.6) > > Best regards > > Ole Have a look at: http://mindtrove.info/articles/python-weak-references -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
It can be not so "simple". There can be multiple input files, with *total* size ~30-50-80 MB. -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
This time limits too: = import psyco psyco.full() import sys def foo(): ##sys.stdin = open('D:/1583.txt', 'rt') a = sys.stdin.readlines() a = a[1:int(a[0]) + 1] for ai in a: x, y = ai.split() sys.stdout.write(str(int(x) * int(y)) + '\n') foo() = -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
PS Yes, they support psyco since long time ago (otherwise I'd get Compilitation Error verdict). I used Psyco there many many times. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary with Lists
Okay that makes sense. I was assuming that list.append returned the new list. thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
On Oct 4, 1:50 am, n00m wrote: > It can be not so "simple". > There can be multiple input files, > with *total* size ~30-50-80 MB. According to one of the global moderators, the 20s time limit is for each input file: https://www.spoj.pl/forum/viewtopic.php?f=6&t=4667 John -- http://mail.python.org/mailman/listinfo/python-list
Re: Enormous Input and Output Test
I've given up :-) -- http://mail.python.org/mailman/listinfo/python-list
Windows GCC Support (Mingw & Mingw-w64)
I work with the Mingw-w64 Project, and am the project owner of WPG System64 (you can find out about both at http://www.cadforte.com), which contains Python.org Python 2.6.2. I haven't used Python 3.1.1 because I have seen errors with "print" occur in several places, but that is another topic (I am just now adding Python to my portfolio.) Python support is becoming critical. For numerous reasons, I want to be able to build Python via the standard Posix method (configure, make, make install), but for Windows, which means using gcc and in my case MSys. The barriers to success are quite small, it is simply a matter of getting the right configuration, which the provided configure (and configure.ac) are unable to produce. It is missing some key elements that, while it builds just fine, produces a broken python (no module os, another missing module, site_import failure.) Mingw-w64 has created a branch that is to be labeled stable with the release of gcc 4.4.2. It is an excellent import crt for msvc using gcc. It is almost too good, making it all the way through the build process except for a few small changes here and there (mainly to the posixmodule, as it has no configuration there. I have configured it.) I am also unclear on what the MS_WINDOWS directive is for vs the standard WIN32, I don't know whether to use it or not. I have gotten through the build without it. Is there any chance of getting some of the devs or anyone familiar enough with the source code to make this possibility become reality? MinGW(32) seems to have given up after 2.5. We are quite active and have complete VC support back to pre-2005 even. Thanks Xenofears (Peter) -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Oct 3, 11:45 pm, horos11 wrote: > > It's not a bug. In Python classes and global variables share the same > > namespace. > > > Don't you think you should learn a bit more about how Python manages > > objects and namespaces before going around calling things bugs? > > > Carl Banks > > No, I don't think so.. > > Say you went to another country, where the people wore lead shoes, > hence not only going slower, but getting lead poisoning from time to > time. > > Pointing out that shoes made of fabric might just be better should not > be heresy. This analogy is wrong on two counts: 1. Pointing out that the "fabric might be better" is not analogous to what you did. You claimed that Python's behavior was a bug, which would be analogous to me saying, "lead shoes should be illegal". 2. I know enough about the properties of fabric and lead to make an informed complaint over the use of lead for shoes, even if I am new to the country. However you very clearly have a poor understanding of Python's namespace and object models, and so you have little standing to claim that Python is buggy in how it treats its namespaces. I'd say the strongest claim you have grounds to make, given the level of understanding you've shown, is "this is very confusing to noobs". BTW, the Python maintainers are very particular to define "bug" as "Python behaves differently than it is documentated to", and Python is most definitely documented as acting the way it does. > In this case, I think the overall goal was syntax simplicity, but it > sure as hell makes things confusing. No warning, or anything. The sane > behavior IMO would be to disallow the assignment unless put through a > special function, something like: > > class(state) = ... > After all, python does have a precedent when you try to join, when: > > ":".join([1,2]) > > does not work because [1,2] is an array of ints, whereas > > ":" . join( str(x) for x in [1,2]) > > does. No, it really isn't a precedent. Yes, Python does type-checking quite a bit, however there is no precedent at all for customizing assignment to a regular variable. When you write "a = b", a gets bound to the same object as b is bound to, end of story. You can't customize it, you can't prevent it, you can hook into it, and neither object is notified that an assignment is happening. This is true even if a or b happen to be a class. And if you say, "Well you should be able to customize assigment", my answer is, "You were saying there was a precedent, I said there was not". If you want to argue that it should be possible to customize assignment, be my guest, but you'll have to do it without the benefit of a precedent. (You can customize assignment to an attribute or item, but that is not what we're talking about, especially since classes are hardly ever attributes or items.) Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Sunday, 4 October 2009 08:14:08 horos11 wrote: > Saying that 'whoa, this coding error should be handled by naming > convention' may be the only practical way of getting around this > limitation, but it is a limitation nonetheless, and a pretty big one. You misunderstand the dynamic nature of python - YOU rebound the name to something else - how on earth can the interpreter know that this is not what you meant? Could you formulate a rule that the interpreter should follow to be able to flag what you have done as an error? - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: "from logging import *" causes an error under Ubuntu Karmic
OK, I've filed a bug. Because Python2.5 works fine here. -- Valery -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
horos11 wrote: Carl, Thanks for the info, but a couple of points: 1. it wasn't meant to be production code, simply a way to teach python. 2. this should either be a compile time or a runtime error. 'Actions at a distance' like this are deadly both to productivity and to correctness - not only is this a runtime error, it is a *silent* runtime error. Any other language I know would catch this, whether it be lua, java, or perl. Saying that 'whoa, this coding error should be handled by naming convention' may be the only practical way of getting around this limitation, but it is a limitation nonetheless, and a pretty big one. Ed The bug in your code that we're discussing is caused because you rebound a global attribute to a new value, and lost the original object.. Your complaint is that when the global name happens to reference a class, and the new value doesn't, you think the compiler should give an error. If you would call this a limitation of Python, and think it should be "fixed," then you clearly do not understand the language or its philosophy. I can't compare to either lua or perl, but Java is a vastly different language from Python, and this type of thing is at the heart of the difference. In Java you must declare the types of everything at compile time (except they had to make a big hole for collections), and the compiler is therefore told much about the possible behavior of each variable. In Python you declare nothing, and much of the power of the language comes from the fact that every construct is a first-class object. Function definitions are fancy literal values for function objects, class declarations are just one of the ways of making a factory function, and classes themselves are instances of metaclasses. You can declare new objects that mimic existing ones without having to define a new class that derives from the old class (duck-typing). And you can frequently reuse the runtime semantics intended for one purpose to accomplish something very different. Python also deliberately doesn't have a notion of "constant" declaration, nor "private." It doesn't have the notion of a library (or class) building a "sealed" implementation which cannot be customized. What Python offers for many of these is some conventions. Names starting with leading underscores are "private" by convention. Names all uppercase are "constant." Names starting with an uppercase letter are classes. But rather than trying to make a language definition of each of these terms, it's just a convention, observed among the consenting adults (?) cooperating in the codebase. I'm not saying (here) whether Python is better or worse than the languages which are strongly typed at compile time, but just that it's a deliberate set of design decisions. And special-casing one or two of the more obscure danger spots is not in keeping with the Python philosophy. Further, I don't see that you did anything that the runtime should disallow. You assigned a new value to the 'state' attribute. And when you tried to call it, it called the __str__() method of that object. DaveA -- http://mail.python.org/mailman/listinfo/python-list
"from logging import *" causes an error under Ubuntu Karmic
Hi all is it a pure Ubuntu Karmic (beta) issue?.. $ python Python 2.6.3 (r263:75183, Oct 3 2009, 11:20:50) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from logging import * Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'NullHandler' $ uname -a Linux vaktop 2.6.31-11-generic #38-Ubuntu SMP Fri Oct 2 11:55:55 UTC 2009 i686 GNU/Linux -- regards Valery -- http://mail.python.org/mailman/listinfo/python-list
Re: creating class objects inside methods
On Oct 3, 11:14 pm, horos11 wrote: > Carl, > > Thanks for the info, but a couple of points: > > 1. it wasn't meant to be production code, simply a way to teach > python. I understand, and if you think it's overkill for your pedagogical application then feel free not to follow the suggestions I gave you. > 2. this should either be a compile time or a runtime error. > > 'Actions at a distance' like this are deadly both to productivity and > to correctness - not only is this a runtime error, it is a *silent* > runtime error. Any other language I know would catch this, whether it > be lua, java, or perl. I'm afraid that's just not reasonable given the nature of classes in Python. Python made a deliberate design trade-off by treating classes as first-class objects. This decision carries benefits and drawbacks. You've seen one of the drawbacks. Benefits include simpler data model and increased opportunities for dynamicism. Perl, Java, and Lua made the opposite choice, thus they are able to catch the above mistake. You could argue that Python made the wrong choice, and that classes ought not to be first-class objects. Fine. But it didn't, and the protection you seek is sacrificed. I suggest looking at tools like PyChecker and PyLint. I don't know if they'd have found your error in this case, but they can find a lot of problems Python itself misses. > Saying that 'whoa, this coding error should be handled by naming > convention' may be the only practical way of getting around this > limitation, but it is a limitation nonetheless, and a pretty big one. I believe it's a miniscule limitation in practice. It seems like a big deal but doesn't actually happen much. YMMV. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list