Re: Read any function in runtime
Matt McCredie wrote: 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 I'd like to add that there are several lisp apps out there that give you a REPL (for example stumpwm). A REPL could be seen as a sophisticated `eval' loop. Case in point, it is common in the lisp world. You could, in theory, hose your system from inside emacs (and you may not even know it...hahaha). -Jack -- http://mail.python.org/mailman/listinfo/python-list
Re: The rap against while True: loops
kj wrote: I'm coaching a group of biologists on basic Python scripting. One of my charges mentioned that he had come across the advice never to use loops beginning with while True. Of course, that's one way to start an infinite loop, but this seems hardly a sufficient reason to avoid the construct altogether, as long as one includes an exit that is always reached. (Actually, come to think of it, there are many situations in which a bona fide infinite loops (typically within a try: block) is the required construct, e.g. when implementing an event loop.) I use while True-loops often, and intend to continue doing this while True, but I'm curious to know: how widespread is the injunction against such loops? Has it reached the status of best practice? TIA! kynn This thread has gotten a lot of posts concerning programming practices and dogma alike. I'd like to add a personal use of `while True:` that has nothing to do with either best practices or dogma. I use python a *lot* to do day-to-day tasks in an engineering lab. I use it to control, log, or otherwise converse with rs232 based gear, as well as use it to back up or organize documents, etc... (lo and behold, I use this scripting language to write little scripts here and there). Don't get me wrong, I also write full blown control/logging apps with python, but that is only 10% of my usage. Whenever I need to quickly log something (serial output of a device) quickly, I find myself writing this in the python REPL: import serial comport = serial.Serial('COMx', timeout=1) while True: get = comport.readline() f.open(blah, 'a') f.write(get) f.close() It is short enough that I don't see the need to write my own module. Sometimes I even add a little regexp based filtering -- which adds 2 lines total. When I am done logging I just give 'er a CTRL-C and be done with it. It is also a hell of a lot less buggy and error prone than hyperterminal, which my boss uses to do the same thing. I think this is a perfect example of `while True:` that works damn well, and there isn't anything that can replace its simplicity. Programming practices be damned, it is invaluable, and I would recommend doing it in my situation to any person, regardless of programming experience. Food for thought. -Jack -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading hex to int from a binary string
Luc wrote: Hi all, I read data from a binary stream, so I get hex values as characters (in a string) with escaped x, like \x05\x88, instead of 0x05. I am looking for a clean way to add these two values and turn them into an integer, knowing that calling int() with base 16 throws an invalid literal exception. Any help appreciated, thanks. Hi, Check out the ord() function. Example: x = '\x34' print ord(x) output: 52 Also, if you, lets say read(4), and end up with `x = '\x05\x41\x24\x00' you can use x[i] to address each character. So if you print x[1] output: 'A' That should be enough to get you started in the right direction. -Jack -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a better way to code variable number of return arguments?
Dr. Phillip M. Feldman wrote: I currently have a function that uses a list internally but then returns the list items as separate return values as follows: if len(result)==1: return result[0] if len(result)==2: return result[0], result[1] (and so on). Is there a cleaner way to accomplish the same thing? How about using yield and then iterate over the answer: def some_fun(): \tfor result in some_loopable_stuff: \t\t yield result Then call it thusly: for i in some_fun() result = i -Jack (PS, sorry to the OP, you will get two of these -- I forgot to CC the list) -- http://mail.python.org/mailman/listinfo/python-list
Retracing your steps in an interactive python env
Hello all, I am playing around in a python shell (IPython on win32 right now actually). I am writing some code on the fly to interface to a rotary encoder (not important in this scope). Anyway, I have created a function using def, and well, I like the way it is working, however... I have already filled the command line history buffer (the com.exe buffer?) so _what_ I actually filled this def with is lost. Now, it isn't that complicated, and I can easily re-write the function off the top of my head, however it would be really nice to be able to _ask_ python what makes up a def. Something like this (remember I am using IPython interactive interpreter session): In [0]: def func(input): .:print im in this function! + str(input) .:print doing some stuff .:sleep(10) Then later on while still in this interactive shell session I could do something like: In [1]: what_is_in(func) The def for func(input) is: print im in this function! + str(input) print doing some stuff sleep(10) and therefore be able to recount what I just did. Cheers, Jack -- http://mail.python.org/mailman/listinfo/python-list