Re: Error in following code
[EMAIL PROTECTED] wrote: > Im working with python2.2 on red hat linux. > The following program is supposed to print decreasing numbers from an > entered number till 1, each decrement being = 1 : > > #! usr/bin/env/python > > def f(n=int(raw_input("enter number: "))): > print 'n=',n > if n>1: > return n*f(n-1) > else: > print 'end' >return 1 > > > > > Though it works fine on the python interpretor, i dont get the > required output when i write this code in gedit (text editor). The > output that i get is (where t4.py is the name of the file): > > [EMAIL PROTECTED] root]# python t4.py > enter number: 6 > [EMAIL PROTECTED] root]# > > i.e it takes the input but doesn't print anything. If anybody can > help... Thanx! > Hello, When you run it through the interpreter, then the interpreter "looks" at your definition of f, "understands" it, and continues on. What follows your definition of f? Nothing. In particular, nothing instructs the interpreter to *execute* f. So your problem is not that f is being executed but is not printing anything, but rather that f is not being executed. To do what you want it to do, maybe try the following: #! usr/bin/env/python def f(n): print 'n=',n if n>1: return n*f(n-1) else: print 'end' return 1 if __name__ == '__main__': n = int(raw_input("enter number: ")) f(n) The line (if __name__...) means that if the interpreter is running your module the way you mean here, then it should get the raw input for n, then call f. HTH, Efrat P.S. Note that I changed your f so that it doesn't do input itself. Coupling calculation code with user-interaction code is maybe not so good (IMHO). -- http://mail.python.org/mailman/listinfo/python-list
Re: Fastest Way To Iterate Over A Probability Simplex
[EMAIL PROTECTED] wrote: > On May 22, 11:19 am, Efrat Regev: >> I want to iterate over all >> such vectors under the constraint that the granularity of >> each component is at most some delta. > > You can think of this like your sum is an integer>=1 and the single > "probabilities" are integers>=1 So given the sum, like 6, you can find > all the parts of it, and then find all the permutations of such parts. > Eppstein has given code for the parts of an integer, and you can can > find the iterable permutations code on the cookbook. But the number of > such possible vectors grows very quickly... > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/218332 > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474124 > > Bye, > bearophile > Many thanks. I modified the recipes you attached some, and it works much better. Nice and informative answer! -- http://mail.python.org/mailman/listinfo/python-list
Fastest Way To Iterate Over A Probability Simplex
Hello, Let's say a probability vector of dimension d is x_1, ..., x_d, where each one is a non-negative term, and they all sum up to 1. Now I'd like to iterate over all probability vectors, but this is impossible, since they're uncountable. So instead, let's say I want to iterate over all such vectors under the constraint that the granularity of each component is at most some delta. To make things pythonesque, here's the interface: class simplex: def __init__(self, dim, delta): ... def __iter__(self): ... def next(self): ... The problem is, what's a fast implementation? I tried something simple, and it is slow. If anyone can think of something clever, I'd love to hear it. Many Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Re: Grabbing the output of a long-winded shell call (in GNU/Linux)
Diez B. Roggisch wrote: > Efrat Regev schrieb: >> [EMAIL PROTECTED] wrote: >>> On May 1, 2:23 pm, Efrat Regev <[EMAIL PROTECTED]> wrote: >>> >>>> So my question is if there's a way to "grab" the output as it's being >>>> generated. It doesn't matter if the solution is blocking (as opposed to >>>> callback based), since threads can handle this. I just don't know >>>> how to >>>> "grab" the output. I appreciate your time in reading (and answering >>>> this), as I've been googling several hours for this. >>> >>> There may be more pythonic solution than what I suggest here but this >>> is what I have done when I needed similar functionality. Basically run >>> your command in the background and redirect its stdout/err to a temp >>> file. You may run the command either in the background or in a >>> separate thread. You can then run the command "tail --retry -- >>> pid= -n+0 -F " and grab the output. The tail command >>> exits once the real command is done. > > Or instead use the python subprocess module and read the commands > stdin/out/err from the Popen-object. > > Diez Excellent, thanks! BTW: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554 (found this after seeing responses on list) -- http://mail.python.org/mailman/listinfo/python-list
Re: Grabbing the output of a long-winded shell call (in GNU/Linux)
[EMAIL PROTECTED] wrote: > On May 1, 2:23 pm, Efrat Regev <[EMAIL PROTECTED]> wrote: > >> So my question is if there's a way to "grab" the output as it's being >> generated. It doesn't matter if the solution is blocking (as opposed to >> callback based), since threads can handle this. I just don't know how to >> "grab" the output. I appreciate your time in reading (and answering >> this), as I've been googling several hours for this. > > There may be more pythonic solution than what I suggest here but this > is what I have done when I needed similar functionality. Basically run > your command in the background and redirect its stdout/err to a temp > file. You may run the command either in the background or in a > separate thread. You can then run the command "tail --retry -- > pid= -n+0 -F " and grab the output. The tail command > exits once the real command is done. > > Raghu. > > > > Many Thanks! I'll try this -- http://mail.python.org/mailman/listinfo/python-list
Grabbing the output of a long-winded shell call (in GNU/Linux)
Hello, Suppose I want to run from within a Python GUI app some long-output shell call. For example, from within Python I might want to call g++ foo.cpp I already know there are many ways to do this, e.g., commands.getstatusoutput('g++ foo.cpp') to name one. The problem is that this might generate a ton of output (e.g., because of compilation errors), and might take a while to do so. In my GUI, I'd like to print out the output as it's being generated, not wait until all is done (as commands.getstatusoutput will do) and dump it at once. So my question is if there's a way to "grab" the output as it's being generated. It doesn't matter if the solution is blocking (as opposed to callback based), since threads can handle this. I just don't know how to "grab" the output. I appreciate your time in reading (and answering this), as I've been googling several hours for this. Many Thanks, E -- http://mail.python.org/mailman/listinfo/python-list
Compiler-AST-Walk-Visitor: Any Examples or Documentation?
Hello, I'm trying to write something that will translate Python code to pseudo-code (for teaching purposes). Googling around indicated that the compiler module is pertinent, especially creating a visitor to walk the generated AST: http://docs.python.org/lib/module-compiler.html I can build the AST, but I can't figure out how to write the visitor. The package documentation didn't help me out that much, and I couldn't find any examples. In fact, google only came up with this unanswered related question: http://mail.python.org/pipermail/python-list/2006-July/392716.html Any help is appreciated. Thanks and Bye, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Calling GNU/make from a Python Script
Hello, I need to call GNU/make from within a Python script. This raised some problems: 1. The script is not in the directory of the makefile, and changing the locations of either is not an option. Consequently, the makefile fails, since it can't find the targets/dependencies. 2. After searching around, it seems that os.system(..) should be avoided if there's an alternative. Is there one in this case? Many Thanks! Efrat -- http://mail.python.org/mailman/listinfo/python-list
Re: Python and location of .so files?
Carsten Haese wrote: > On Tue, 2006-01-10 at 09:42, Efrat Regev wrote: > >> Hello, >> >> On FC4, I've generated an .so file from C++ which I want to use from >>python. It works when I copy it into /usr/lib/python2.4/site-packages. >>(I.e., say I have hello.so in that directory, then from the python >>prompt I can 'import hello', and the code works fine). The problem is >>that the said directory requires su - so I'd rather python load my .so >>from a different user-privilege directory (when I type 'import hello'). >>Is there some way to tell python to use a different directory? > > > Yes. See > http://docs.python.org/tut/node8.html#SECTION00811 for > information on Python's module search path. > > Hope this helps, > > Carsten. > > Perfect. Thank you very much! -- http://mail.python.org/mailman/listinfo/python-list
Python and location of .so files?
Hello, On FC4, I've generated an .so file from C++ which I want to use from python. It works when I copy it into /usr/lib/python2.4/site-packages. (I.e., say I have hello.so in that directory, then from the python prompt I can 'import hello', and the code works fine). The problem is that the said directory requires su - so I'd rather python load my .so from a different user-privilege directory (when I type 'import hello'). Is there some way to tell python to use a different directory? Many thanks for considering this question. Also, it's possible that this question belongs in a different forum, and if so, I'd appreciate if you'd tell me where. Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Re: One-step multiples list generation?
Damien Wyart wrote: > * Efrat Regev <[EMAIL PROTECTED]> in comp.lang.python: > >>Suppose I have some non-numerical Foo and would like to create a list >>of 20 Foo-s. Is there a one-step method (not a loop) of doing so? > > > Maybe : > > [ Foo ] * 20 > > or, more verbose, > > [ Foo for _ in range(20) ] > > ? > Great. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
One-step multiples list generation?
Hello, Suppose I have some non-numerical Foo and would like to create a list of 20 Foo-s. Is there a one-step method (not a loop) of doing so? E.g., something like [Foo * 20] (which is obviously not the right way) that would create [Foo, Foo, Foo, ...,Foo]. I tried looking through the docs, FAQs and help, but couldn't find anything (the closest is range, but it seems it's only for numerics) - I very much appreciate your time in answering. Also, please excuse me if I used some wrong terminology. Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Python CGI Script
Hello, I'm a data-structures course TA trying to write a python CGI script for automatically compiling and testing students' projects. Unfortunately, I've run into some questions while writing this, which I couldn't solve with the various (and helpful) python-CGI documentation. (It's possible that I'm posting to the wrong group; if so, I'd appreciate suggestions for the appropriate group.) 1. In my HTML page, I have the following: ... In the above, submission_processor.py is the python CGI script. I didn't write a URL in the action field, since I'm first testing everyting on a local machine (running FC4). The first line of submission_processor.py is #!/usr/bin/python and I've done chmod +x submission_processor.py When I hit the "submit" button, my browser (Firefox on FC4) doesn't run the script; it asks me whether it should open submission_processor.py or save it to disk. I couldn't figure out why. 2. My HTML page has the option for an instructor to list the various submissions and scores. Obviously, this should be inaccessible to students. The instructor has a password for doing this, therefore. Suppose I place the password inside a python script, and give this script only +x permission for others. Is this adequate as far as security? Thanks in advance for answering these questions. Efrat -- http://mail.python.org/mailman/listinfo/python-list
Newbie Question on ctypes
Hello, (I apologize in advance if I'm posting to the wrong group. If so, could you please instruct me which is appropriate?) I'm trying to use uTidyLib, HTML-tidy's python binding. When I import tidy Python says it can't import ctypes. Since I'm using FC4, I looked for a FC4 ctypes rpm. All I could find, however, were buildlog errors for ctypes on FC4. My question is, therefore, if I can build ctypes locally. I tried rpm -i python-ctypes-0.9.1-1.rf.src.rpm but that didn't seem to work (python still couldn't import ctypes). Thanks in advance for all answers. I should point out that I'm not an expert in python or linux (in the combination, alas, even less). Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Re: HC Library and *attributes parameter
"Efrat Regev" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hello, > ... Many thanks for the useful replies!! -- http://mail.python.org/mailman/listinfo/python-list
HC Library and *attributes parameter
Hello, I'm a really new (and quite bad) Python programmer. While trying to use the HC HTML-Generating library, I couldn't figure out how to set a table's width to some given width. Moreover, the constructors interface is def __init__(self, object = None, align = None, border = None, cellspacing = None, cellpaddding = None, *attributes) So, what does *attribute stand for (being a C++ programmer, it looks like a pointer, probably not the case). Is it like the C++ ellipsis? If so, how can I use it? Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Modules for Various Internet Protocols?
"Kartic" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Efrat Regev wrote: > > Hello, > > > > I was wondering whether there are any Python > Erfat...yes...batteries included! > > http://docs.python.org/lib/internet.html > > Thanks, > -Kartic > Excellent! more like generator included :-) Many thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Python Modules for Various Internet Protocols?
Hello, I was wondering whether there are any Python modules for various Internet protocols, e.g., is there something similar to import ftp client = ftpopen(...) and so on. Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Re: Pythonic poker client
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi all, > > My PC finally went belly up last week and I'm looking forward to > playing with my new Mac. However, I play a bit of online poker, and > there is no Mac client for my poker room. > > Ideally, instead of running Virtual PC, I'd much rather build a custom > poker client with Python. It's an idea I've been kicking around for a > while, as it would let me create logs and better analyze my playing. > I'm hoping it would also help me improve my Python coding skills. > > I've mostly done web development, so I'm a little out of my element > here, but I would certainly appreciate any help someone could offer. > How would one go about porting a client like this? > It's already built into Python :-) import poker import poker.holdem import poker.omaha_eight import poker.seven_stud ... But, seriously, any Poker site (at least the commercial ones) go the opposite direction from using an open protocol. Moreover, they somewhat compete with themselves on using encryption techniques (Poker site reviews take encryption into consideration). Finally, I think any Poker site administrators would do anything possible to block your client, since once you have a Python Poker client, the door is open to writing an automated client - bringing on the doom of Poker websites. -- http://mail.python.org/mailman/listinfo/python-list
Q: Portable Way to Make Files Read Only
Hello, I would like to recurse through a directory and make files (which match a specific criteria) read only. From searching the Internet, I found http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303343 which shows how to change file attributes to read only using the win32api module. That's great, but I was hoping there's a more portable way to do so. Is there a more portable API call to make files read only? Many Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list
Re: Python in Makefile Question
"Efrat Regev" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hello, > > I'd like to ask a question concerning a python script in a makefile. > ... Many thanks for the very useful (and very quick) answers! Efrat -- http://mail.python.org/mailman/listinfo/python-list
Python in Makefile Question
Hello, I'd like to ask a question concerning a python script in a makefile. Suppose I have a C++ project (sorry for raising this in a Python newsgroup), with some makefile for it. Before compiling the code, I'd like to check that there are no C++ convention violations (e.g., identifiers beginning with an underscore). So my makefile looks something like this: target: ... verify.py $(CC) ... verify.py is a python script that checks for convention violations, i.e., its first line is #! /usr/bin/env python I can't figure out the following: 1. How can I get the python script to return a value to make, so that if it decides that there are convention violations make will fail? 2. How can I pass information from the makefile to the python script, e.g., the base directory to check? Thanks, Efrat -- http://mail.python.org/mailman/listinfo/python-list