Re: Code generator and visitor pattern
Karsten Wutzke kwutzke at web.de writes: So, what is the de facto method in Python to handle source code generation? Take a look at the NodeVisitor class in the ast module in python 2.6+. The visitor pattern is implemented in the python standard library. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Namespace problem?
Josh English joshua.r.english at gmail.com writes: I have a script that generates a report from a bunch of data I've been collecting for the past year. I ran the script, successfully, for several weeks on test runs and creating more detailed reports. Today (back from vacation) and the script doesn't work. It's giving me a name error. I'm running python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v. 1310 32 bit (Intel)]. (Upgrading is not a possibility right now. Eventually we'll be upgraded top Windows 7.) Here is the reduced code: fws_first_col = 6 for student in range(32): if True: idx = fws_first_col for _month in range(12): idx += 2 fws_last_col = idx print fws_last_col I get: NameError: name 'fws_last_col' is not defined This snippet of code works on it's own, but not in the main script. That doesn't give me enough information to help you with the issue. In general you need to provide enough code to reproduce the failure, not some modified version that doesn't fail. My guess is that the if True is actually something else, and it isn't being interpreted as True. As such, fws_last_col never gets assigned, and thus never gets created. You can fix that by assigning fws_last_col to an appropriate default value before the for loop. But what do I know, that is just a guess. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: hex question
Sneaky Wombat joe.hrbek at gmail.com writes: Why is python turning \x0a into a \n ? In [120]: h='\x0a\xa8\x19\x0b' In [121]: h Out[121]: '\n\xa8\x19\x0b' I don't want this to happen, can I prevent it? 'h' is an ascii string. The ascii encoding for '\n' is the number(byte) 0x0A. When you type '\x0a' you are entering the ascii code directly. hex(ord('\n')) '0xa' Python doesn't know that you entered the values using the '\xXX' syntax, it just knows that the string contains a byte with that value. When it prints it back out, it will print out the corresponding symbol. Any character that has a reasonable ascii representation will show up as that symbol when it (or its repr) is printed. \x61\x62\x63\x64\x65\x66 'abcdef' If you are interested in printing the hex values, you could so something like this: h = '\x0a\xa8\x19\x0b' for c in h: ... print 0x%02x % ord(c), ... 0x0a 0xa8 0x19 0x0b Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Traversing through variable-sized lists
I've tried the following workaround, but it often gives me inaccurate results (due to integer division), so I had to add a safety check: num_frames = 32 values = [0, 1, 2, 3, 4] offset_step = num_frames / len(values) for index in xrange(0, num_frames): offset = index / offset_step if offset offset_values[-1]: offset = offset_values[-1] frames[index].func(values[offset]) There has to be a better way to do this. I'd appreciate any help. Cheers! This is how I would do it, assuming you just want to call the remaining frames with the last value. from itertools import izip def stretch(seq, n): for val in seq: for i in xrange(n): yield val while True: yield val frames_per_value = num_frames // len(values) for frame, value in izip(frames, stretch(values, frames_per_value)): frame.func(value) Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list when the contents is a list
Wes James comptekki at gmail.com writes: I have been trying to create a list form a string. The string will be a list (this is the contents will look like a list). i.e. [] or ['a','b'] The [] is simple since I can just check if value == [] then return [] But with ['a','b'] I have tried and get: a=['a','b'] b=a[1:-1].split(',') returns [ 'a' , 'b' ] when I want it to return ['a','b']. How can I do this? eval will work, but has a safety issue. It also has the issue of evaluating any and everything that a user might pass in. If you are using python 2.6 check out ast.literal_eval. It uses python's built in ast parser to generate an AST and then traverses it to generate a python object. Unlike eval though, it will raise an exception if anything other than a literal is represented in the string. I have used the same function in python 2.5 (copied from 2.6) and it works just fine. Here is a version modified from the code in python 2.6 that should only parse lists of strings: from _ast import List, Str, PyCF_ONLY_AST def parse(expr, filename='unknown', mode='exec'): Parse an expression into an AST node. Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST). return compile(expr, filename, mode, PyCF_ONLY_AST) def list_eval(text): Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. node = parse(text, mode='eval').body if not isinstance(node, List): raise ValueError('malformed string') def _convert(node): if isinstance(node, Str): return node.s raise ValueError('malformed string') return list(map(_convert, node.elts)) Matt McCredie -- http://mail.python.org/mailman/listinfo/python-list
Re: switch
hong zhang henryzhang62 at yahoo.com writes: List, Python does not have switch statement. Any other option does similar work? Thanks for help. --henry I see a couple of people have mentioned using a dictionary. If the value that you are switching on is a string, or could be made into one, you can use a variant of the command dispatch pattern. class MyCommandDispatcher(object): def do_a(self): # do stuff def do_b(self): # do stuff def do_5(self): # do stuff def default(self): # do stuff def switch(self, option): getattr(self, 'do_' + str(option), self.default)() d = MyCommandDispatcher() d.switch('a') d.switch(5) This isn't _much_ more coding than using the dictionary method, and is pretty readable. This is also a common pattern in python. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: import from a string
iu2 israelu at elbit.co.il writes: Hi, Having a file called funcs.py, I would like to read it into a string, and then import from that string. That is instead of importing from the fie system, I wonder if it's possible to eval the text in the string and treat it as a module. For example with file('funcs.py') as f: txt = r.read() string_import(txt, 'funcs') # is string_import possible? to have now a module called funcs with the functions defined in funcs.py. You can do something like this: import types import sys mymodule = types.ModuleType(mymodule, Optional Doc-String) with file('funcs.py') as f: txt = f.read() exec txt in globals(), mymodule.__dict__ sys.modules['mymodule'] = mymodule Note that you shouldn't exec untrusted code. You might also look at the __import__ funciton, which can import by python path. You might also look at the imp module. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Read any function in runtime
Rhodri James rhodri at wildebst.demon.co.uk writes: On Fri, 23 Oct 2009 17:39:40 +0100, Matt McCredie mccredie at gmail.com wrote: joao abrantes senhor.abrantes at gmail.com writes: Hey. I want to make a program like this:print Complete the function f(x)=then the user would enter x+2 or 1/x or any other function that only uses the variable x. Then my python program would calculate f(x) in some points for example in f(2),f(4).. etc . How can I do this? check out 'eval' or 'exec'. Then check out all the reasons you shouldn't use them in an environment that you don't trust absolutely -- if someone wipes your hard disc, you won't get any sympathy from here. The safe answer is to write yourself a small parser. Given that you've got a very limited symbol set, that shouldn't be too hard. This should only be a concern if it is some sort of client/server app (like a web-app). If this is something that is going to be run on a local machine then the person running it could do just as much damage via the command line. While I agree that there is a danger if the input might come from untrusted users, and the original poster should be aware of that, writing your own parser only makes sense in those instances. If this application is run locally then users have access to the machine anyway. I don't want to give a (potentially) new user to python the impression that they need to be writing their own parser to solve this problem. It depends on where the input is coming from. Two things to note: 1. eval and exec are perfectly safe if the input is from a trusted source. 2. eval and exec are never safe if the input is not from a trusted source. Matt McCredie -- http://mail.python.org/mailman/listinfo/python-list
Re: Read any function in runtime
joao abrantes senhor.abrantes at gmail.com writes: Hey. I want to make a program like this:print Complete the function f(x)=then the user would enter x+2 or 1/x or any other function that only uses the variable x. Then my python program would calculate f(x) in some points for example in f(2),f(4).. etc . How can I do this? check out 'eval' or 'exec'. statement = raw_input(Complete the function f(x)=) print eval(statement, {'x':2}) print eval(statement, {'x':4}) print eval(statement, {'x':6}) or with 'exec': statement = raw_input(Complete the function f(x)=) exec f = lambda x:+statement in {} print f(2) print f(4) print f(6) Matt McCredie -- http://mail.python.org/mailman/listinfo/python-list
Re: Windows file paths, again
Dan Guido dguido at gmail.com writes: Hi Anthony, Thanks for your reply, but I don't think your tests have any control characters in them. Try again with a \v, a \n, or a \x in your input and I think you'll find it doesn't work as expected. -- Dan Guido Why don't you try it yourself? He gave you the code. I changed cfg.ini to contain the following: [foo] bar=C:\x\n\r\a\01\x32\foo.py Which produced the following output: C:\x\n\r\a\01\x32\foo.py 'C:\\x\\n\\r\\a\\01\\x32\\foo.py' Looks like config parser worked just fine to me. There is a difference between a python string literal written inside of a python script and a string read from a file. When reading from a file (or the registry) what you see is what you get. There is no need to do so much work. Matt McCredie -- http://mail.python.org/mailman/listinfo/python-list
[issue3370] importing with_statement causes exec to raise syntax error on block that doesn't end with a newline
New submission from Matt McCredie [EMAIL PROTECTED]: The following session demonstrates the issue: Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. exec def foo():\nreturn 0 # no ending newline works fine foo() 0 exec def foo():\nreturn 1\n # with an ending newline works fine foo() 1 from __future__ import with_statement exec def foo():\nreturn 2\n # with an ending newline works fine foo() 2 exec def foo():\nreturn 3 # without an ending new line... breaks Traceback (most recent call last): File stdin, line 1, in module File string, line 2 return 3 ^ Possibly related to http://bugs.python.org/issue1184112, and/or http://bugs.python.org/issue501622? -- components: Interpreter Core messages: 69723 nosy: mccredie severity: normal status: open title: importing with_statement causes exec to raise syntax error on block that doesn't end with a newline versions: Python 2.5 ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue3370 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Insert image to a List box
On Nov 15, 2007 2:15 PM, linda. s [EMAIL PROTECTED] wrote: On 11/15/07, Matimus [EMAIL PROTECTED] wrote: On Nov 15, 12:45 pm, linda.s [EMAIL PROTECTED] wrote: I run the following code and got the error (I put a .gif file on the desktop) Traceback (most recent call last): File 11.py, line 25, in module for gifname in os.listdir(dirpath): OSError: [Errno 2] No such file or directory: '.\\Desktop\\' import os import Tkinter root = Tkinter.Tk() L = Tkinter.Listbox(selectmode=Tkinter.SINGLE) gifsdict = {} dirpath = '.\\Desktop\\' for gifname in os.listdir(dirpath): if not gifname[0].isdigit(): continue gifpath = os.path.join(dirpath, gifname) gif = Tkinter.PhotoImage(file=gifpath) gifsdict[gifname] = gif L.insert(Tkinter.END, gifname) L.pack() img = Tkinter.Label() img.pack() def list_entry_clicked(*ignore): imgname = L.get(L.curselection()[0]) img.config(image=gifsdict[imgname]) L.bind('ButtonRelease-1', list_entry_clicked) root.mainloop() The exception points to this line as being the issue: for gifname in os.listdir(dirpath): and the error says `No such file or directory: '.\\Desktop\\'' So, there must be no directory named '.\\Desktop\\' in your current working directory. To find out your current working directory use `os.getcwd()'. Make sure that `os.getcwd()' returns the path to a directory with a Desktop folder in it. This is usually something like 'C:\\Documents and Settings\\username'. In my mac machine, I got something like: os.getcwd() '/Users/linda/Desktop' So I changed the code like: dirpath = '.\Users\linda\Desktop\\' But I still got the error: ValueError: invalid \x escape Two things. When you type out a path, if you include a '.' in the front, that means that the path is relative to your current working directory. So, if your current working directory is /Users/linda/Desktop, and dirpath is set to './Users/linda/desktop', then os.listdir(dirpath) is going to search '/Users/linda/desktop/Users/linda/Desktop', which probably doesn't exist. You want to use an absolute path just drop the '.'. If you want to get the files from the current directory, just use a single '.'. The following two things should work: dirpath = . # This always uses the current working directory dirpath = /Users/linda/Desktop # This will always use the same directory The second thing, which I think is the error you are currently seeing, is the escape error. In strings in python the backslash is used as an escape character, if you want to type a single backslash you have to type two of them '\\' or, for paths (especially on a mac) is is completely valid to use forward slashes instead. Also, there are raw strings which don't do escaping. Any of the following will work: dirpath = \\Users\\linda\\Desktop dirpath = /Users/linda/Desktop dirpath = r\Users\linda\Desktop # prepending an 'r' makes it a raw string (no escaping) HTH Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert image to a List box
Thanks a lot for your patience. I put the gif file to a folder called fig in my desktop. dirpath = './fig' still got error: Traceback (most recent call last): File 11.py, line 238, in module img.config(image=gifsdict[imgname]) NameError: name 'imgname' is not defined You should reply to the python list. That way everyone gets the benefit of your questions. The problem is here: [code] def list_entry_clicked(*ignore): imgname = L.get(L.curselection()[0]) img.config(image=gifsdict[imgname]) L.bind('ButtonRelease-1', list_entry_clicked) [/code] I think the main problem is that the line `img.config(image=gifsdict[imgname])' is supposed to be part of `list_entry_clicked' and it isn't. What you want to do (maybe? I made some assumptions): [code] def list_entry_clicked(*ignore): imgname = L.get(L.curselection()[0]) img.config(image=gifsdict[imgname]) L.bind('ButtonRelease-1', list_entry_clicked) [/code] That _should_ work, but it is untested. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Assertion for python scripts
On 11/2/07, matthias [EMAIL PROTECTED] wrote: Howdy ! I started using the assert() stmt and found it quite useful :-) I have only one problem: I don't know how to turn them off again. I know that -O turns off assertions in general. However, how do I pass thus parameter to python to an executable script ? I have tried the following: 1. !#/usr/bin/env python -O - Fails with this msg: /usr/bin/env: python -O: No such file or directory Also, putting it in quotes won't do it. 2. Passing the -O to the runnable script won't work either. Here is my question: How do I maintain debug / release builds that allow me to switch debug stmts, like assert, on / off ? Thanx, Matthias Use: python -O -mcompileall path That command will compile all of the files in the given path and produce .pyo files. If the .pyo file is present and up-to-date it will be used instead of the .py file. Alternatively you could do this: python -O -mpy_compile somefile.py which can be used to compile one file at a time. Many Python programs and modules include a compile step as part of their installation process. There is also a -OO option, which will strip doc-strings as well. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Going past the float size limits?
It would be great if I could make a number that can go beyond current size limitations. Is there any sort of external library that can have infinitely huge numbers? Way way way way beyond say 5x10^350 or whatever it is? I'm hitting that inf boundary rather fast and I can't seem to work around it. You have a couple of options. 1. Use long if that is appropriate for your data, they can be as large as you want (eventually you will reach memory constraints, but that isn't likely) 2. There is a decimal type, which is based on long (I think) and can have a decimal portion. to use longs: x = 5 * 10**350 to use decimal: import decimal x = decimal.Decimal(5e350) You will probably want to read up on the decimal module. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Control Log File Size
I've got an application running on linux which writes log files using the python logging module. I'm looking for some help and advice to cap the size which the file will grow too, something reasonably like 2Mb would be great. What is the best way to handle this kind of thing? Can this be done using a setting in the logging module? Or is it something I'll have to get the FS to handle for me? Check out the RotatingFileHandler: http://docs.python.org/lib/node413.html It is part of the logging module and should do what you want. Examples of using alternative handlers are here: http://docs.python.org/lib/multiple-destinations.html Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: __main__ : What is this?
En Fri, 19 Oct 2007 21:26:22 -0300, Matimus [EMAIL PROTECTED] escribió: The common pattern: if __name__ == __main__: # do stuff IMHO better written: if __main__ == __name__: # do stuff I'm intrigued why do you feel the second alternative is better. Which is your native language? In English (and Spanish, and many others but still not in the majority) the usual ordering is subject-verb-object or SVO, which matches the first alternative: If the name is __main__, do this... As all the languages I know (not so many!) are SVO, I can't think of any equivalent of the second form [that I could say it's better than the first] English is my native language (and only, I'm American :|). The reason I like the second version better is simply that: variable == literal can easily be mis-written as variable = literal I suppose that isn't a huge issue in Python, since most of the time comparisons happen within if and for statements. Even if it is within a functions parameters it will raise a SyntaxError exception. There is still the case where one might write something like this: a = b == 'c' or, as I prefer: a = 'c' == b It is just habit from writing so much C code that way. In C the reasoning is that if you have mistyped it, you will catch the issue at compile time instead of runtime (which is potentially much more difficult to debug). I'm used to seeing that pattern. In terms of natural language I do agree with you. It really is just my _humble_ opinion. I can't make a huge argument for it in Python. To be honest, I didn't give it much thought before I wrote it. I am simply used to doing it that way, and being irked whenever I see it written the other way in C or C++ (and perhaps unjustifiably Python). Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbi Q: What is a rational for strings not being lists in Python?
On 10/15/07, Dmitri O.Kondratiev [EMAIL PROTECTED] wrote: To clarify my point: reverse() is a lucky one - Python has variants of *this particular* function both for lists and strings. Yet what about other list functions? How in general, can I write a function that works both on list and string types? Both are sequences, right? Why string is not a subtype of a list then? The advantage of string being a list of elements, where element is a char is that all list functions will work *without any modifications* on strings as well as on other types of lists. So, I am trying to understand: what is a rational for strings not being lists in Python? Thanks, -- Dmitri O. Kondratiev [EMAIL PROTECTED] http://www.geocities.com/dkondr On 10/15/07, Dmitri O.Kondratiev [EMAIL PROTECTED] wrote: Gary, thanks for lots of info! Python strings are not lists! I got it now. That's a pity, I need two different functions: one to reverse a list and one to reverse a string: def reverseList(xs): if xs == []: return xs else: return (reverseList (xs[1:])) + [xs[0]] def reverseStr(str): if str == : return str else: return (reverseStr (str[1:])) + str[0] Ok. Now regarding in-place reversal of a list: l = [1,2,3] l [1, 2, 3] l.reverse() l [3, 2, 1] That was, as I expected. Good. Then why this ? : ls = [1,2,3].reverse() ls print [1,2,3].reverse() None I mean, why ls is empty after assignment? Also, I couldn't find in the Python docs what this form of slicing means: xs[::-1] ? It works for creating a reversed copy of either a string or a list, but what does '::-1' syntax means? Thanks, Dmitri O. Kondratiev [EMAIL PROTECTED] http://www.geocities.com/dkondr On 10/15/07, Gary Herron [EMAIL PROTECTED] wrote: Dmitri O.Kondratiev wrote: The function I wrote (below) reverses lists all right: def reverse(xs): if xs == []: return [] else: return (reverse (xs[1:])) + [xs[0]] reverse ([1,2,3]) [3, 2, 1] Yet when I try to reverse a string I get: reverse (abc) ... ... ... File C:\wks\python-wks\reverse.py, line 5, in reverse return (reverse (xs[1:])) + [xs[0]] File C:\wks\python-wks\reverse.py, line 5, in reverse return (reverse (xs[1:])) + [xs[0]] File C:\wks\python-wks\reverse.py, line 2, in reverse if xs == []: RuntimeError: maximum recursion depth exceeded in cmp What's wrong? Why recursion never stops? If you are doing this as an python-learning exercise, then read on. If you are doing this reversal for real code, then try: xs.reverse() for in-place reversal of a list (but not a string), or result = xs[::-1] for creating a reversed copy of either a string or a list Your recursion stops when xs == [], but when you're stripping characters off a string, like 'abc', the remaining portion will be 'bc', then 'c', than '', but never [] so you 'll never stop. Try: if xs == []: return [] elif xs == '': return '' else: ... Gary Herron Thanks, Dima The example you posted won't work with tuples either because they, like strings, are also immutable. So, the best way to get the posted code to work (which is a bad way to go about reversing a string, but I digress) is to cast the input parameter to a list first. The returned value will always be a list, but you will simply have to convert it back to the appropriate type when you are done. What is the purpose if immutability? It allows a value to be hashed. I don't want to get into a discussion about methods for hashing mutable types, if you are interested just do a search on the list archives. Hashing allows for quick comparisons of values, but more importantly it allows for values to be used as keys for the dict type. This is very important because, as you will soon find out if you keep learning the language, all namespaces in python are implemented as dicts. So... if you want a mutable string, just cast it to a list, do your operations and cast it back to a string. Incidentally, the proper method for converting a list of characters to a string is by using the join method on an empty string. s = I am a string x = list(s) x ['I', ' ', 'a', 'm', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g'] .join(x) 'I am a string' Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving objects in Tkinter
On 10/12/07, Evjen Halverson [EMAIL PROTECTED] wrote: I have tried to make a Tkinter program make a rectangle move down the window, but did not succeed. All it does is make a rectangle trail. What am I doing wrong? from Tkinter import* root = Tk() RectangleColor='orange' Background=tk_rgb = #%02x%02x%02x % (100, 255, 100) root.geometry('1000x800+0+0') root.title(Moving Object Test) w = Canvas (root,width=1000,height=800,bg=Background) w.grid() import time t1=time.time() t2=t1 ti=t2-t1 x=100 y=0 a=0 rect=Canvas.create_rectangle(w,x,y,200,100,fill=RectangleColor) def CLS(): cls=Canvas.create_rectangle(w,0,0,1000,800,fill=Background) while ti10: t2=time.time() ti=t2-t1 y=y+10 Canvas.create_rectangle(w,x,y,200,100,fill=RectangleColor) st1=time.time() st2=st1 subti=st2-st1 root.mainloop() time.sleep(100) while subti1: st2=time.time() subti=st2-st1 a=a+1 CLS() # rect=Canvas.create_rectangle(w,x,y,1000,800,fill=RectangleColor) root.mainloop() quit() Tkinter canvas works a little different than say, openGL or pygame. You don't have to draw the rectangle over and over for each frame. Every time you call create_rectangle you are creating another rectangle. Another thing to note is that Tkinter has a built in scheduler. You don't need to use time. The way you are calling Canvas looks a little funny to me also. Anyway, here is a simple example: [code] import Tkinter as tk rectanglecolor = 'orange' background = tk_rgb = #%02x%02x%02x % (100, 255, 100) disty = 6 root = tk.Tk() root.geometry('1000x800+0+0') root.title(Moving Object Test) can = tk.Canvas (root,width=1000,height=800,bg=background) can.grid() # note that rect is actually just an integer that is used to identify that shape in the # context of the canvas that it was created within. rect = can.create_rectangle(400,0,600,200,fill=rectanglecolor) for i in range(100): # move the rectangle 0 in the x direction and disty in the y direction. can.move(rect, 0, disty) root.update() # update the display root.after(30) # wait 30 ms root.mainloop() # this just keeps the window open [/code] Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: I could use some help making this Python code run faster using only Python code.
Yes, Digital Mars D is what I was referring to and yes I know D is not as efficient as C++. If I knew of a good C++ compiler that is not from Microsoft that works natively with Windows I would be happy to consider using it. I've had good luck with MinGW (gcc compiled for windows). http://www.mingw.org Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: I could use some help making this Python code run faster using only Python code.
Now I think I will code this little scrambler using nothing but the D Language just to see whether there is any benefit in using D over Python for this sort of problem. Isn't D compiled to machine code? I would expect it to win hands down. That is, unless it is horribly unoptimized. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: I could use some help making this Python code run faster using only Python code.
It would be nice if Python could be made to automatically detect the LC and string translation patterns used by the unoptimized Python code and make them into optimized Python code on the fly at runtime. I am more than a little amazed nobody has chosen to build a JIT (Just In- Time compiler) or cached-compiler into Python but maybe that sort of thing is just not needed given the fact that Python code can be easily optimized to run 30x faster. See PyPy http://codespeak.net/pypy/ for a JIT comiler for python. Although it is in the research phase, but worth taking a look at. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: re question
On 9/19/07, Dan Bar Dov [EMAIL PROTECTED] wrote: I'm trying to construct a regular expression to match valid IP address, without leading zeroes (i.e 1.2.3.4, 254.10.0.0, but not 324.1.1.1, nor 010.10.10.1) This is what I come up with, and it does not work. r'(^[12]?\d{0,2}\.){3,3}[12]?\d{0,2}' What am I doing wrong? I'm not sure what affect having the ^ inside of the parens will have, but it surely isn't what you want. This part: r[12]?\d{0,2} will match the following strings, which I'm sure you dont' want: - yes it will match an empty string (Is ... a valid IP?) 00 - It could start with a 0, as long as there are only two characters 299 - A little outside of the range you are interested in That {3,3} is better written as {3}. Any common knowledge IP matching RE? I don't know if there is any common knowledge RE, but I came up with the following: r((1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.){3}(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)) Let us break it down: This matches an octet: r(1\d{2}|2[0-5]\d|[1-9]\d|\d) Which will match any ONE of the following 1\d{2} - A 1 followed by any two digits 2[0-4]\d - A 2 followed by 0,1,2,3 or 4 followed by any digit 25[0-5] - A 25 followed by 0,1,2,3,4 or 5 [1-9]\d - Any digit but 0 followed by any digit \d - Any Digit I generally discourage people from using REs. I think the folowing is much easier to read: def isip(x): octs = x.split(.) if len(octs) != 4: return False for oct in octs: if len(oct) 1 and oct[0] == 0: return False try: if not 0 = int(oct) 256: return False except ValueError: return False return True Both solutions seem to work, though I used a small set of test cases. Others may have better suggestions. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: re question
On 9/20/07, Ricardo Aráoz [EMAIL PROTECTED] wrote: Dan Bar Dov wrote: I'm trying to construct a regular expression to match valid IP address, without leading zeroes (i.e 1.2.3.4 http://1.2.3.4, 254.10.0.0 http://254.10.0.0, but not 324.1.1.1, nor 010.10.10.1 http://010.10.10.1) This is what I come up with, and it does not work. r'(^[12]?\d{0,2}\.){3,3}[12]?\d{0,2}' What am I doing wrong? Any common knowledge IP matching RE? Thanks, Dan r'^[12]\d?\d?.\d{1,3}.\d{1,3}.\d{1,3}$' so 299.999.999.999 is a valid IP and 34.0.0.1 isn't? Also, the .s need to be escaped. Otherwise they match ANY character. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: I could use some help making this Python code run faster using only Python code.
On 9/20/07, Python Maniac [EMAIL PROTECTED] wrote: I am new to Python however I would like some feedback from those who know more about Python than I do at this time. Well, you could save some time by not applying the scramble one line at a time (that is if you don't mind losing the line endings in the scrambled version). For that to be effective though, you probably want to open in binary mode. Also, your scramble can be written using list comprehension. [code] def scramble(s, key=0x80): return ''.join([chr(ord(c) ^ key) for c in s]) output = scramble(f.read()) [/code] If you use xor (^) as above, you can use the same method for scramble as descramble (running it again with the same key will descramble) and you can use an arbitrary key. Though, with 255 combinations, it isn't very strong encryption. If you want stronger encryption you can use the following AESish algorithm: [code] import random def scramble(s, key): random.seed(key) return ''.join([chr(ord(c) ^ random.randint(0,255)) for c in s]) [/code] This allows you to use much larger keys, but with a similar effect. Still not strong enough to be unbreakable, but much better than the origional. It is strong enough that someone knowing how you scrambled it will have trouble unscrambling it even if they don't know the key. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Using python to create windows apps that everyone can use?
On 9/18/07, Thomas Harding [EMAIL PROTECTED] wrote: Hi guys, sorry to post another topic on this, as I am aware that it has already been posted a few times, but not with specifically what I am looking for. I want an app that makes a gui interface for python (similar to Microsoft visual studio or qt designer, not a code based one) and/or an app that can make this into a .exe that can be opened by any person on any computer without python installed. check out py2exe: http://py2exe.org matt -- http://mail.python.org/mailman/listinfo/python-list
Re: help - error when trying to call super class method
I am trying to extend list class to build a stack class -- see code below--- but I got an error when I try to call len method from list class here.. why? Thanks in advance! Jeff did a good job of answering your questions. I just wanted to note that your pop is broken, but that doesn't matter since list already has a pop method that will do what you want. Actually it has a push method that does what you want too, it is called `append'. If you really want a push method, you could just do this: [code] class Stack(list): ... push = list.append s = Stack() s.push(1) s.push(2) s.push(3) s.pop() 3 s.pop() 2 s.pop() 1 [/code] Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Registering a python function in C
Is Maya a different python build than what is contained at python.org? If so, I suggest you get your C program to work with the latest python build from python.org. Then see if you can get it to work with the Maya version. Ok, did that. If I write a normal C++ program and use the python installed in my system, everything works ok and I can call the python funtions. From within maya(where the C code is running as a plugin), nothing happens. I tried removing my python installation so that only the one that comes with maya is running, but then I have no python.h or libs to compile against!! I found no help at the maya/python newsgroup, is there anyone who has done this before??? I don't really know how maya works with python. Is it possible to just run arbitrary python code from maya? Can you get the version strings and stuff? import sys print sys.version That might give you a clue. It might just be that you need to compile against a different version of Python. You could always just download the different versions of Python and see if the included Python.h and Python.lib work. I would go in this order: 2.3, 2.4, 2.2, 2.5. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: So what exactly is a complex number?
So what exactly is a complex number? It is a math construct, and has almost nothing to do with Python, other than the fact that Python has a data type for them. So, here is a list of better ways to get information about complex numbers than asking python-list: 1. Google it: http://www.google.com/search?q=Complex+Numbers 2. Wikipedia: See the first result of the above 3. Ask a math teacher 4. Just stay in school and do your math homework. You will learn about it eventually. -- http://mail.python.org/mailman/listinfo/python-list
Re: the del function
For some odd reason the del array[ray] isn't actually deleting the array item in the list I get the following output: C:\Documents and Settings\program\Desktop\python\pygameremix.py [2, 2, 2, 4, 4, 4, 4, 3, 3, 3, 3] [2, 2, 2, 4, 4, 4, 4, 3, 3, 3, 3] [2, 2, 2, 2, 4, 4, 4, 3, 3, 3, 3] [2, 2, 2, 2, 4, 4, 4, 3, 3, 3, 3] [2, 2, 4, 4] The _red function is fine, but the del function isn't working. What did I do wrong? The code is doing what you told it to: [code] while x 4: array = single_players[4:17] # -- you are re-creating `array' in every loop length = len(array) - 1 ray = random.randint(0,length) _red[x] = array[ray] del array[ray] print array x = x + 1 print _red [/code] My guess is that you want this: [code] array = single_players[4:17] while x 4: length = len(array) - 1 ray = random.randint(0,length) _red[x] = array[ray] del array[ray] print array x = x + 1 print _red [/code] I'm sure you could do something with random.shuffle or random.choice though that would be much cleaner. It isn't entirely clear what you are trying to do though, so I'm stopping short of posting any code. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Image.open( C:\test.jpg) is this wrong ?
Image.open(C:\test.jpg) # this is what I have right now. And it can't find the file or directory. The file is there (it is everywhere on my computer now!!!) I found some code where they apply the file path to a variable in single quotes. Is that how it is done. Also I thought single quotes were for characters not strings. In python there is no difference between single and double quotes. You are close, but the C:\test.jpg, sees the \t as the escape sequence for `tab'. You can generally use three things: C:\\test.jpg where \\ is the escape sequence for a single backslash `\' You can use a raw string: rC:\test.jpg which disables escaping. You can use forward slashes C:/test.jpg, which might look funny but does work. Read more here: http://docs.python.org/tut/node5.html#SECTION00512 Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: an eval()-like exec()
A python interactive interpreter works by having the user type in some code, compiling and running that code, then printing the results. For printing, the results are turned into strings. I would like make an interpreter which does this, without the last part: i.e. where the results are returned as objects, instead of as strings. I.e. have I would like to see something that behaves like this: ip = MyInterpreter() # this started a new interpreter ip.run(import math) is None True ip.run(math.pi) is math.pi True Neither exec() or eval() is usable for this, as far as I see, because eval can't handle arbitrary python code (eval(import math) ), and exec() doesn't return the results. Subclassing an code.InteractiveInterpreter or code.InteractiveConsole seems like a usable idea, but I couldn't find out what to do to get the results before they are turned into strings. Using compile() and then eval() didn't seem usable either. Any ideas? Well, my first thought is that exec and eval serve two different purposes, and you should just have both of them and use the appropriate one based on the situation. However, I think it is possible to enable the behavior you want: [code] def myeval(statement, globals_=None, locals_=None): try: return eval(statement, globals_, locals_) except SyntaxError: if locals_ is None: import inspect locals_ = inspect.currentframe().f_back.f_locals exec statement in globals_, locals_ [/code] It seems to work for me. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: I can't get value of entry box, Tinker
What/should I, can I do? Fix your code? def login(): global e2,e1 print e2.get() print e1.get() That should work. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem w/ Tkinter
text=Action?,command=self.V(a,b,c,d)).pack(anchor=W) doesn't even do anything, what can I do to fix this problem? I see many mistakes. First: `command=self.V(a,b,c,d)' is actually calling self.V. You don't want to call self.V, which will assing `command' to the return value, you want to pass self.V itself. Also, here: [code] a = Radiobutton(choices, text=Add News, variable=v,value=1).pack(anchor=W) ... [/code] You are assigning a, b, c and d to the return value of `pack' which is None. You do the same thing with the Button. Also, V (well, callbacks in general) can't take any parameters. V is checking a,b,c and d which, if done correctly, would be instances of Radiobutton. Directly comparing them against an empty string won't work. How to fix your code? Read this: http://docs.python.org/tut/ Then this: http://www.pythonware.com/library/tkinter/introduction/ Also, this might help: http://www.python.org/dev/peps/pep-0008/ Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem w/ Tkinter
On 8/24/07, Lamonte Harris [EMAIL PROTECTED] wrote: How to fix my code, wth that doesn't help solve anything really.the Tkinter tutorial doesn't even explain radio buttons correctly, let alone, everything else. gah. can you give a answer that I can work from. You asked, what can I do to fix this problem?. You stated this in the singular, implying that there was one problem with your code to fix. In reality, to fix your code you have to totally re-write it. You need to slowly work your way through tutorials and go through all the pain everyone else who has learned Python and Tkinter has had to go through if you want to be a compitent coder. I know these may seem like harsh words, so let me give you an example of working Radiobutton code. [code] import Tkinter as tk choices = {1:One, 2:Two, 3:Three, 4:Four} def main(): root = tk.Tk() global v v = tk.IntVar() for i, name in choices.iteritems(): b = tk.Radiobutton(root, text=name, variable=v, value=i) b.pack(anchor='w') tk.Button(root, text=Action?, command=print_val).pack(anchor='w') root.mainloop() def print_val(): k = v.get() print k, choices[k] if __name__ == __main__: main() [/code] I'm curious, what do you feel is incorrect about the Radiobutton explanation? It seemed to work for me. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: porting vc++ project to python?
i have a very large project in visual studio2005-visual c++ in windowsxp. i'd like to port it, or my next project, over to python. is this possible without rewriting all my code? are there multiple options to do this? my project is so large, that entirely rewriting it is actually no option. You have a couple of options, but it really depends on the structure of your program. Option 1: You could encapsulate the important portions of your code in DLLs and access them from python using the ctypes module. Option 1b: You could encapsulate the important portions of your code as COM DLLs and access them from win32com. Option 2: You could turn your code into python extensions (DLL): http://docs.python.org/ext/ Option 3: You could rewrite portions of your code in python and access it from VC++: same as before - http://docs.python.org/ext/ It all depends on how your project is structured, and what you consider to be the most difficult part to convert. If your app has a complex user interface and that is the most difficult part to convert then option 3 is probably best. If the code is well encapsulated and the user interface will be easy to rewrite then options 1 or 2 will work best. It is probably best to stay away from 1b unless portions of your stuff already exist as COM DLLs. If you do have COM DLLs however, accessing them from Python is very easy. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: What Are These Import/From Statements about?
While that's an interesting link, I was thinking of the named items like: Numeric, (this one I know about.) Image ImageChops ImageTk time binascii tkMessageBox tkSimpleDialog The `image' ones are all part of PIL (Python Imaging Library) which is a third party module (http://www.pythonware.com/products/pil/). The others: time, binascii, os.path, tkMessageBox and tkSimpleDialog are all part of the standard library (http://docs.python.org/modindex.html). I don't see the documentation for the tk* ones in the global module index, but you can always look at the source. I'm not sure what MakeQTE is. If this is a program you are able to run, you should be able to do something like this: import MakeQTE print MakeQTE.__file__ That will show you where it lives, and you can then look at the source for more clues. Also, you should be able to do this: import MakeQTE help(MakeQTE) Which will also likely give you some good clues. -Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python using a book based on version 1.5
A friend of mine dropped off a copy of Sams Teach Yourself Python in 24 Hours published in 2000. I skimmed the first couple of chapters looking for the interpreter version and the book was based on version Python version 1.5. Is this book still relevant? Should I toss it and look for something newer? I'm sure it contains a fair amount of relevant information. The problem is, it will also contain irrelevant information and there is no way to tell the difference. There definitely some better resources out there. Here are some good free online ones: http://docs.python.org/tut/ http://www.diveintopython.org/ -Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: how to convert a c program to java program
I heard I need to port C program to JPython first and compile it to native JAVA. I don't know anything about JPython. Is there a tool to do the porting? If not, what is the quickest way to learn JPython? I'm assuming that you are refering to Jython. You probably want to start by learning Python, since Jython is essentially just an implementation of Python written in Java. I'm not sure why you need to involve Jython at all though. Syntax wise there are many more similarities between C and Java than there are between C and Python or Python and Java. Anyway, if you want to learn Python try one of these: http://python.org/doc/tut/ or http://www.diveintopython.org/. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: threads, mutual exclusion, and lists
Why do you think they are not? Because they aren't. You even mentioned that a few operations that aren't atomic. If operations are atomic it isn't necessarily because of the design of the list, but the design of CPython. More specifically the GIL. I don't mean to imply that you can't get a multi-threaded app to communicate using lists, but the Queue is explicitly built for it and better suited. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: threads, mutual exclusion, and lists
My question is -- are python list operations atomic? If they are not, then I assume I need to put some mutual exclusion around the append() and pop() calls ? They are not, but there is one included in the standard library: http://docs.python.org/dev/lib/module-Queue.html Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: try/finally exceptions dying
I thought finally always propagated the exception all the way up the stack until it was handled. Finally will propagate the exception up, unless another exception occurs within the finally block. Since there is not (yet: http://www.python.org/dev/peps/pep-3134/) exception chaining in Python, only the last exception to be raised is held onto. The finally block combined with the recursion makes it impossible to catch every exception that way. Something like this might work: [code] def dostuff(args): stuff return status def run_with_error_handling(): exceptions = [] done = False while 1: try: status = dostuff(args) except (exception1, exception2), data: exceptions.append(data) modify status finally: update args based on status break if done [/code] Note that in the above example, status could be just a tuple of args. Then you just need to figure out how to modify the args in the exception case. The above leaves a lot of holes though. If you want more help you will have to send a more complete example. Matt -- http://mail.python.org/mailman/listinfo/python-list
AttributeError: 'module' object has no attribute 'HTTPSHandler'
I am using Fedora Core 4 linux. Where should I look for _ssl.pyd ? I am trying to build and use Python-2.5.1 I don't have access to that type of system. I do know that you need OpenSSL to use ssl. It might be as simple as just finding and installing OpenSSL for fedora. You can also find the source in the python repository at: http://svn.python.org/projects/external/openssl-0.9.8a And build that yourself. I _think_ that if you have it installed, configure might find it for you and autmatically build _ssl.pyd. Matt -- http://mail.python.org/mailman/listinfo/python-list
Fwd: AttributeError: 'module' object has no attribute 'HTTPSHandler'
I built and installed python 2.5 from source and when I do this: opener = urllib2.build_opener(SmartRedirectHandler(), DefaultErrorHandler(), urllib2.HTTPSHandler()) I get this error. AttributeError: 'module' object has no attribute 'HTTPSHandler' What should I do? You need `_ssl.pyd' for HTTPSHandler to work. I guess, try to figure out why that wasn't built, then build it. I suppose I _might_ be able to give a little more help, but you haven't mentioned what platform you are using. Even then, I'm not an expert on building Python. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: pylint style convention
Which style convention is it referring to? Should these really be all caps? I think pylint is expecting that any variables declared outside of a function should be constants with special meanings (similar to #define or enum values in c). So, I guess to get rid of that message you should do something like this: code def main(args=None): if args is None: args = sys.argv parser = optparse.OptionParser(usage='usage: %prog [OPTIONS]') parser.add_option('-c', '--config', action='store', type='string', dest='configFilename', help='config file containing defaults') (options, args) = parser.parse_args(args) if __main__ == __name__: sys.exit(main()) /code Here is an article by GvR that goes over main functions in python: http://www.artima.com/weblogs/viewpost.jsp?thread=4829. You didn't really ask for it, but I think it is good reading. His examples all use getopt though, instead of optparse. I have come up with my own (probably overkill for 99% of python scripts) template that uses optparse. If you really want to see it let me know and I will send it to you. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Images in Tkinter
That code doesn't tell me anything. You are going to have to post a more complete example to get help. Like, enough code so that I can run it and see the same problem. Also, I tried creating the image object outside class but it gives a runtime error saying it is too early to create an image object. Please help! You need to call Tkinter.Tk() before you can create an instance of `PhotoImage' (I don't know why, but I know I got a similar error when I didn't). Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check if an item exist in a nested list
Is there any way to check if an item in specific location in a multiple dimension nested exist? For example something like: if M_list[line][row][d] exist: do_something_0 else: do_something_1 Certainly: code try: M_list[line][row][d] except IndexError: do_something_1 else: do_something_0 /code Assuming that you want to check because something in `do_someting_0' fails if the item isn't defined, it is very likely that this can be shortened to: code try: do_something_0 except IndexError: do_something_1 /code The try/except should be wrapped as tightly as possible around the specific code that actually throws the exception. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: class C: vs class C(object):
How about broke instead of deprecated: class Old: ... def __init__(self): ... self._value = 'broke' ... value = property(lambda self: self._value) ... How is this broken? Properties are not supported for old-style classes. They may not support features introduced in new-style classes, but that's hardly the same as broken. What does that give you that this does not: class Old: def __init__(self): self.value = 'broke' To further illustrate, what happens when you do this: class Old: def __init__(self): self._value = 'broke' def _set_value(self, val): print set called def _get_value(self): print get called return self._value value = property(_get_value, _set_value) x = Old() print x.value x.value = not broke print x.value print type(x.value) print x._value This is what happens: x = Old() print x.value get called broke x.value = not broke print x.value not broke print type(x.value) type 'str' print x._value broke Now, no exceptions were raised or anything, but with old-style classes I'm having difficulty thinking of a scenario where they might actually be useful. I suppose you could use it to do a calculation on instance variables and return the result. You are probably better of using a method for that anyway though. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficiently removing duplicate rows from a 2-dimensional Numeric array
Could you use a set of tuples? set([(1,2),(1,3),(1,2),(2,3)]) set([(1, 2), (1, 3), (2, 3)]) Matt On 7/19/07, Alex Mont [EMAIL PROTECTED] wrote: I have a 2-dimensional Numeric array with the shape (2,N) and I want to remove all duplicate rows from the array. For example if I start out with: [[1,2], [1,3], [1,2], [2,3]] I want to end up with [[1,2], [1,3], [2,3]]. (Order of the rows doesn't matter, although order of the two elements in each row does.) The problem is that I can't find any way of doing this that is efficient with large data sets (in the data set I am using, N 100) The normal method of removing duplicates by putting the elements into a dictionary and then reading off the keys doesn't work directly because the keys – rows of Python arrays – aren't hashable. The best I have been able to do so far is: def remove_duplicates(x): d = {} for (a,b) in x: d[(a,b)] = (a,b) return array(x.values()) According to the profiler the loop takes about 7 seconds and the call to array() 10 seconds with N=1,700,000. Is there a faster way to do this using Numeric? -Alex Mont -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Break up list into groups
That certainly is fast, unfortunately it doesn't pass all of the tests. I came up with those tests so I don't know how important they are to the original poster. I modified it and came up with a generator and a non-generator version based (roughly) on your algorithm, that are almost as quick, and pass all of the tests. Some of the modifications were done just to make it quicker, so it would be fair when comparing against the other methods. I hard-coded the comparison instead of using a function and created a function that directly generates and returns a list instead of a generator. I would probably use the generator version in my code, but wrapping `list' around a generator adds about 4us (on my machine). Anyway, getgroups7 passes all of the tests I mentioned and it was timed at 10.37usec/pass. The down side: the code doesn't seem nearly as elegant. Matt code def gengroups7(seq): iseq = iter(xrange(len(seq))) start = 0 for i in iseq: if seq[i]0x80: start = i break else: return for i in iseq: if seq[i]0x80: yield seq[start:i] start = i yield seq[start:] def getgroups7(seq): groups = [] iseq = iter(xrange(len(seq))) start = 0 for i in iseq: if seq[i]0x80: start = i break else: return groups for i in iseq: if seq[i]0x80: groups.append(seq[start:i]) start = i groups.append(seq[start:]) return groups /code -- http://mail.python.org/mailman/listinfo/python-list