Re: Calling Bash Command From Python
Wildman via Python-list wrote: > Python 2.7.9 on Linux > > Here is a bash command that I want to run from a python > program: sudo grep "^user\:" /etc/shadow > > If I enter the command directly into a terminal it works > perfectly. If I run it from a python program it returns an > empty string. Below is the code I am using. Suggestions > appreciated. > > cmdlist = ["sudo", "grep", '"^$USER\:"', "/etc/shadow"] > p = subprocess.Popen(cmdlist, > stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > shadow, err = p.communicate() > print shadow What happens if you hardcode $USER? Compare: >>> subprocess.Popen(["sudo", "echo", "$USER"], stdout=subprocess.PIPE).communicate() ('$USER\n', None) That should explain the empty result. Possible fix: >>> subprocess.Popen(["sudo", "echo", os.environ["USER"]], stdout=subprocess.PIPE).communicate() ('user\n', None) While a shell should work, too, >>> subprocess.Popen("sudo echo $USER", stdout=subprocess.PIPE, shell=True).communicate() ('petto\n', None) I'd prefer the os.environ lookup. -- https://mail.python.org/mailman/listinfo/python-list
Re: Calling Bash Command From Python
On 2016-10-31, Wildman wrote: > Here is a bash command that I want to run from a python > program: sudo grep "^user\:" /etc/shadow > > If I enter the command directly into a terminal it works > perfectly. If I run it from a python program it returns an > empty string. Below is the code I am using. Suggestions > appreciated. > > cmdlist = ["sudo", "grep", '"^$USER\:"', "/etc/shadow"] > p = subprocess.Popen(cmdlist, > stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > shadow, err = p.communicate() > print shadow Slightly surprised that nobody's pointed out that in your bash invocation, the first argument to grep is: ^user\: and in the Python code it is: "$USER\:" Your cmdlist should read: ["sudo", "grep", r"^user\:", "/etc/shadow"] or if you really want it to do the same as the bash: ["sudo", "grep", "^" + os.environ["USER"] + r"\:", "/etc/shadow"] -- https://mail.python.org/mailman/listinfo/python-list
Re: pip3 : command not found
On 2016-10-31, Ben Finney wrote: > Instead, you should invoke the exact Python interpreter you want – and, > by extension, the Python environment into which you want packages > installed. > > $ /foo/bar/virtualenv/bin/python3 -m pip install LoremIpsum I'm slightly curious about that. /foo/bar/virtualenv/bin/python3 will just be a symbolic link to /usr/bin/python3, so how does invoking the intepreter that way make any difference? -- https://mail.python.org/mailman/listinfo/python-list
Re: pip3 : command not found
On Mon, 31 Oct 2016 07:21 pm, Jon Ribbens wrote: > On 2016-10-31, Ben Finney wrote: >> Instead, you should invoke the exact Python interpreter you want – and, >> by extension, the Python environment into which you want packages >> installed. >> >> $ /foo/bar/virtualenv/bin/python3 -m pip install LoremIpsum > > I'm slightly curious about that. /foo/bar/virtualenv/bin/python3 > will just be a symbolic link to /usr/bin/python3, so how does > invoking the intepreter that way make any difference? It doesn't. If you read the rest of Ben's post, or for that matter the subject line of this thread, you will see he is comparing: path/to/python3 -m pip install LoremIpsum against: pip3 install LoremIpsum not a direct path to the executable versus a symbolic link to the executable. The problem here is the "pip3" command, which does not exist. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Recursive generator in Python 3.5
I have some confuse about the recursive generator where the code mixing Yield and return keywork as below. I understand that "return" keywork just raise StopIteration exception but can not understanding in depth, so, could some one explain me about the actual function of two "return" keyworks in case it have other function and mechanism of for running this code segment below: def fg(args): if not args: yield "" return for i in args[0]: for tmp in fg(args[1:]): yield i + tmp return print(list(fg(['abc', 'xyz', '123']))) -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
On Tue, Nov 1, 2016 at 12:39 AM, wrote: > I have some confuse about the recursive generator where the code mixing Yield > and return keywork as below. I understand that "return" keywork just raise > StopIteration exception but can not understanding in depth, so, could some > one explain me about the actual function of two "return" keyworks in case it > have other function and mechanism of for running this code segment below: > > > def fg(args): > if not args: > yield "" > return > for i in args[0]: > for tmp in fg(args[1:]): > yield i + tmp > return > print(list(fg(['abc', 'xyz', '123']))) > When you call list() on something, the list constructor will step through the generator until it returns. The generator function will run, just like any other function, until it hits a 'return' or falls off the end. Imagine that every 'yield ...' is actually 'some_list.append(...)'. That's really what's happening here. The list you get at the end consists of all the things yielded. Don't worry about the mechanism of StopIteration. The point of a generator is that it produces values by yielding, and when it returns, it no longer produces any values. This is exactly the same as you'd get if the function printed those values, or called some other function, or appended them to a list, or something - it's a way of generalizing a function so its values can be used in any way you choose. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
Hi ChrisA, Thank you so much for sharing this point, I just concern to the point that: normally, I found that the generator (it may be recursive generator also) do not apply the "return" keyword, just the yield only. But when I delete one or both of "return" keyword above, the code do not action. What is the concern here, could you share more experience on that, -- https://mail.python.org/mailman/listinfo/python-list
Re: Calling Bash Command From Python
On Mon, 31 Oct 2016 15:31:27 +1100, Chris Angelico wrote: > On Mon, Oct 31, 2016 at 3:19 PM, Wildman via Python-list > wrote: >> Here is a bash command that I want to run from a python >> program: sudo grep "^user\:" /etc/shadow >> >> If I enter the command directly into a terminal it works >> perfectly. If I run it from a python program it returns an >> empty string. Below is the code I am using. Suggestions >> appreciated. >> >> cmdlist = ["sudo", "grep", '"^$USER\:"', "/etc/shadow"] >> p = subprocess.Popen(cmdlist, >> stdout=subprocess.PIPE, >> stderr=subprocess.PIPE) >> shadow, err = p.communicate() >> print shadow > > Are you able to run that command without a password? sudo might be > attempting to open the console, failing, and aborting the command. Is > anything being printed to stderr? > > ChrisA No. The program is run in a terminal and sudo asks for a password. -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
On Tue, Nov 1, 2016 at 1:03 AM, wrote: > Hi ChrisA, > > Thank you so much for sharing this point, I just concern to the point that: > normally, I found that the generator (it may be recursive generator also) do > not apply the "return" keyword, just the yield only. But when I delete one or > both of "return" keyword above, the code do not action. What is the concern > here, could you share more experience on that, > They do use return, though. Can you show me an example of a generator that doesn't? Recursive generators may be slightly tricky, as you can't simply call them. Most likely you want to 'yield from' the recursive call. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
Hi Chris, Please see the exampl I just got it below, this is the Tower of Hanoi with recursive generator but it do not have the 'return' here, do you have the advice for this term: def A001511(): yield 1 b=1 for x in A001511(): yield x+1 yield 1 trial=A001511() for i in range(10): a=next(trial) print(a) -- https://mail.python.org/mailman/listinfo/python-list
Re: Call a shell command from Python (was: Calling Bash Command From Python)
On Mon, 31 Oct 2016 15:44:13 +1100, Ben Finney wrote: > Wildman via Python-list writes: > >> Python 2.7.9 on Linux >> >> Here is a bash command that I want to run from a python >> program: sudo grep "^user\:" /etc/shadow > > Some points to note: > > * Those commands are not special to Bash, or any particular shell. They > invoke commands, without AFAIK any specific Bash features. So this is > asking rather to invoke a shell command. Yes, I know. I perhaps should have used the word "shell" instead of bash. However, bash is a shell. > Nothing wrong with that; but on that basis, I've changed the subject > field. > > * You're asking to invoke the ‘sudo’ command, which itself is designed > to switch to a separate user identity and run another program. Yes, I know. > * The above command is (I assume) typed into a shell, but your Python > program never invokes Bash or any other shell. The program is run in a shell so invocation is not needed. >> If I enter the command directly into a terminal it works perfectly. > > Note that ‘sudo’ is specifically designed to be invoked interactively, > seeking to verify that the current user has credentials to run the > command. > > Note further that ‘sudo’ will record when the *current user session* > last invoked ‘sudo’ and seek re-verification if that is too long in the > past. > > Both of these are security measures, and are designed to avoid > non-interactive use of ‘sudo’. Rather, it's meant to be used > interactively by a real, present human with credentials to run the > command. Yes, I know all that. >> If I run it from a python program it returns an empty string. > > You can also check the exit status of a command; ‘grep’ will give > different exit status for a match versus no match. > >> Below is the code I am using. Suggestions >> appreciated. >> >> cmdlist = ["sudo", "grep", '"^$USER\:"', "/etc/shadow"] > > One immediate difference I see is that you specify different arguments > to ‘grep’. You have a different pattern for each command. > > * The ‘^user\:’ pattern matches “user\:” at the start of a line. > > * The ‘^$USER\:’ pattern I think won't match anything, since “$” matches > end-of-line and then you expect further characters *past* the end of > the line. I think that will always fail to match any line. Yes, the '^' indicates the start of the line and the ':' indicates the character where to stop. The colon has a special meaning so it has to be escaped, '\:'. The dollar sign precedes a variable. In this case it is an environment variable. The Linux shadow file contains information about the users of the system. It has the user name, encrypted password, salt, encryption type and other information. The format is such that the user name is at the start of the line ending with a colon. Here is an example: wildman:$6$hODbsJJp$/NWGXZ3fMIVB4U.v/oLtAv.CnL0l0I39.IwsDx1ZAlKW3wUSjTfwJdnQvOMpYNbqNqqFfZ52vgYWBmnjsaX9R.:16177:0:9:7::: >> p = subprocess.Popen(cmdlist, >> stdout=subprocess.PIPE, >> stderr=subprocess.PIPE) >> shadow, err = p.communicate() > > Maybe you are expecting Bash to be involved somehow (and so “$USER” will > be substituted by Bash with some other value). That's not what happens. No, the shell is already running. And $USER will be substituted by the name of the user that invoked the shell. > Instead, the ‘subprocess.Popen.communicate’ method will invoke the > program directly, without involving a shell. See the documentation for > ‘subprocess.Popen’. I will look into that. Thanks for the reply. -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: Calling Bash Command From Python
On Mon, 31 Oct 2016 09:12:57 +0100, Peter Otten wrote: > Wildman via Python-list wrote: > >> Python 2.7.9 on Linux >> >> Here is a bash command that I want to run from a python >> program: sudo grep "^user\:" /etc/shadow >> >> If I enter the command directly into a terminal it works >> perfectly. If I run it from a python program it returns an >> empty string. Below is the code I am using. Suggestions >> appreciated. >> >> cmdlist = ["sudo", "grep", '"^$USER\:"', "/etc/shadow"] >> p = subprocess.Popen(cmdlist, >> stdout=subprocess.PIPE, >> stderr=subprocess.PIPE) >> shadow, err = p.communicate() >> print shadow > > What happens if you hardcode $USER? Compare: > subprocess.Popen(["sudo", "echo", "$USER"], > stdout=subprocess.PIPE).communicate() > ('$USER\n', None) > > That should explain the empty result. Possible fix: > subprocess.Popen(["sudo", "echo", os.environ["USER"]], > stdout=subprocess.PIPE).communicate() > ('user\n', None) > > While a shell should work, too, > subprocess.Popen("sudo echo $USER", stdout=subprocess.PIPE, > shell=True).communicate() > ('petto\n', None) > > I'd prefer the os.environ lookup. I have code using that approach but I am trying to save myself from having to parse the entire shadow file. Grep will do it for me if I can get code right. -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: Calling Bash Command From Python
On Mon, Oct 31, 2016, at 10:55, Wildman via Python-list wrote: > I have code using that approach but I am trying to save myself > from having to parse the entire shadow file. Grep will do it > for me if I can get code right. Python already has built-in functions to parse the shadow file. https://docs.python.org/3/library/spwd.html#module-spwd But you can't use sudo this way if you use that. But why do you want to use sudo from within the python script instead of just running the python script with sudo? -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
On Tue, Nov 1, 2016 at 1:50 AM, wrote: > > Please see the exampl I just got it below, this is the Tower of Hanoi with > recursive generator but it do not have the 'return' here, do you have the > advice for this term: > > def A001511(): > yield 1 > b=1 > for x in A001511(): > yield x+1 > yield 1 > > > trial=A001511() > for i in range(10): > a=next(trial) > print(a) As soon as you get to the end of the function, it returns. When you iterate over a generator, you run it to completion, taking all the values it yields. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Calling Bash Command From Python
On Mon, 31 Oct 2016 08:13:54 +, Jon Ribbens wrote: > On 2016-10-31, Wildman wrote: >> Here is a bash command that I want to run from a python >> program: sudo grep "^user\:" /etc/shadow >> >> If I enter the command directly into a terminal it works >> perfectly. If I run it from a python program it returns an >> empty string. Below is the code I am using. Suggestions >> appreciated. >> >> cmdlist = ["sudo", "grep", '"^$USER\:"', "/etc/shadow"] >> p = subprocess.Popen(cmdlist, >> stdout=subprocess.PIPE, >> stderr=subprocess.PIPE) >> shadow, err = p.communicate() >> print shadow > > Slightly surprised that nobody's pointed out that in your bash > invocation, the first argument to grep is: > > ^user\: > > and in the Python code it is: > > "$USER\:" > > Your cmdlist should read: > > ["sudo", "grep", r"^user\:", "/etc/shadow"] > > or if you really want it to do the same as the bash: > > ["sudo", "grep", "^" + os.environ["USER"] + r"\:", "/etc/shadow"] The above line works perfectly. I didn't occur to me to use the 'r' modifier. Thank you. Still one thing is odd. No matter what, if I use the environment variable $USER in the code, it won't work. Just returns an empty string. At this point that is a non-issue tho. Thanks again. -- GNU/Linux user #557453 -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
On Tue, 1 Nov 2016 12:39 am, tpqnn...@gmail.com wrote: > I have some confuse about the recursive generator where the code mixing > Yield and return keywork as below. I understand that "return" keywork just > raise StopIteration exception but can not understanding in depth, so, > could some one explain me about the actual function of two "return" > keyworks in case it have other function and mechanism of for running this > code segment below: What part confuses you? The recursion, or the generator? In a generator, "yield" outputs a value. "return" ends the generator. Return is optional: reaching the end of the generator is the same as return. So this generator: def gen(): yield 1 yield 2 return # optional will output 1, then 2, then stop. This generator: def gen(*args): if not args: yield "" return for a in args: yield a return # optional will either output "", then stop, or it will output each of the arguments given, then stop. Now let's look at your recursive call version: > def fg(args): > if not args: > yield "" > return > for i in args[0]: > for tmp in fg(args[1:]): > yield i + tmp > return Let's start with a simple call: list(fg([])) args is empty, so it will output "" and halt. list(fg(["ab"])) args[0] is "ab", so it will loop: for c in "ab": Inside this loop, it loops again, this time over the rest of the arguments. There are no more arguments, so the recursive call to fg() will yield "". So the generator outputs "a" + "" (which is "a"); then it outputs "b" + "" (which is "b"). Then it halts. Second example: list(fg(["xy", "ab"])) args[0] is "xy", so it will loop: for c in "xy": Inside this loop, it loops again, this time over the rest of the arguments. There is one more argument, "ab", so the recursive call to fg() is like: fg(["ab"]) But we have already seen what happens when you call fg(["ab"]): it outputs "a", then "b", then halts. So now we have: for c in "xy": for d in ("a", "b"): yield c + d so you will get "xa", "xb", "ya", "yb". Last example: list(fg(["PQR", "xy", "ab"])) will loop over "PQR", then as the inner loop it will loop over fg(["xy", "ab"]). But we already have seen that. So you will get: "Pxa", "Pxb", "Pya", "Pyb", "Qxa", "Qxb", "Qya", "Qyb", "Rxa", "Rxb", "Rya", "Ryb" and then halt. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Calling Bash Command From Python
On Mon, 31 Oct 2016 11:05:23 -0400, Random832 wrote: > On Mon, Oct 31, 2016, at 10:55, Wildman via Python-list wrote: >> I have code using that approach but I am trying to save myself >> from having to parse the entire shadow file. Grep will do it >> for me if I can get code right. > > Python already has built-in functions to parse the shadow file. > > https://docs.python.org/3/library/spwd.html#module-spwd I didn't know about that module. Thanks, this can simplify things for me. > But you can't use sudo this way if you use that. But why do you want to > use sudo from within the python script instead of just running the > python script with sudo? In view of the module I just learned about, that would be a better approach. -- GNU/Linux user #557453 -- https://mail.python.org/mailman/listinfo/python-list
Reading Fortran Ascii output using python
Hi all, I am trying to read an ascii file written in Fortran90 using python. I am reading this file by opening the input file and then reading using: inputfile.readline() On each line of the ascii file I have a few numbers like this: line 1: 1 line 2: 1000.834739 2000.38473 3000.349798 line 3: 1000 2000 5000.69394 99934.374638 54646.9784 The problem is when I have more than 3 numbers on the same line such as line 3, python seems to read this using two reads. This makes the above example will be read like this: line 1: 1 line 2: 1000.834739 2000.38473 3000.349798 line 3: 1000 2000 5000.69394 line 4: 99934.374638 54646.9784 How can I fix this for each fortran line to be read correctly using python? Thanks in Advance for your help, -- https://mail.python.org/mailman/listinfo/python-list
Re: Reading Fortran Ascii output using python
On 31-10-2016 18:20, Heli wrote: > Hi all, > > I am trying to read an ascii file written in Fortran90 using python. I am > reading this file by opening the input file and then reading using: > > inputfile.readline() > > On each line of the ascii file I have a few numbers like this: > > line 1: 1 > line 2: 1000.834739 2000.38473 3000.349798 > line 3: 1000 2000 5000.69394 99934.374638 54646.9784 > > The problem is when I have more than 3 numbers on the same line such as line > 3, python seems to read this using two reads. This makes the above example > will be read like this: > > line 1: 1 > line 2: 1000.834739 2000.38473 3000.349798 > line 3: 1000 2000 5000.69394 > line 4: 99934.374638 54646.9784 > > How can I fix this for each fortran line to be read correctly using python? > > Thanks in Advance for your help, > > You don't show any code so it's hard to say what is going on. My guess is that your file contains spurious newlines and/or CRLF combinations. Try opening the file in universal newline mode and see what happens? with open("fortranfile.txt", "rU") as f: for line in f: print("LINE:", line) Irmen -- https://mail.python.org/mailman/listinfo/python-list
Re: Reading Fortran Ascii output using python
On Monday, October 31, 2016 at 6:30:12 PM UTC+1, Irmen de Jong wrote: > On 31-10-2016 18:20, Heli wrote: > > Hi all, > > > > I am trying to read an ascii file written in Fortran90 using python. I am > > reading this file by opening the input file and then reading using: > > > > inputfile.readline() > > > > On each line of the ascii file I have a few numbers like this: > > > > line 1: 1 > > line 2: 1000.834739 2000.38473 3000.349798 > > line 3: 1000 2000 5000.69394 99934.374638 54646.9784 > > > > The problem is when I have more than 3 numbers on the same line such as > > line 3, python seems to read this using two reads. This makes the above > > example will be read like this: > > > > line 1: 1 > > line 2: 1000.834739 2000.38473 3000.349798 > > line 3: 1000 2000 5000.69394 > > line 4: 99934.374638 54646.9784 > > > > How can I fix this for each fortran line to be read correctly using python? > > > > Thanks in Advance for your help, > > > > > > You don't show any code so it's hard to say what is going on. > My guess is that your file contains spurious newlines and/or CRLF > combinations. > > Try opening the file in universal newline mode and see what happens? > > with open("fortranfile.txt", "rU") as f: > for line in f: > print("LINE:", line) > > > Irmen Thanks Irmen, I tried with "rU" but that did not make a difference. The problem is a line that with one single write statement in my fortran code : write(UNIT=9,FMT="(99g20.8)") value seems to be read in two python inputfile.readline(). Any ideas how I should be fixing this? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Call a shell command from Python (was: Calling Bash Command From Python)
On 2016-10-31, Wildman via Python-list wrote: > On Mon, 31 Oct 2016 15:44:13 +1100, Ben Finney wrote: >> Wildman via Python-list writes: >> >>> Python 2.7.9 on Linux >>> >>> Here is a bash command that I want to run from a python >>> program: sudo grep "^user\:" /etc/shadow >> >> Some points to note: >> >> * Those commands are not special to Bash, or any particular shell. They >> invoke commands, without AFAIK any specific Bash features. So this is >> asking rather to invoke a shell command. Actually it's not a shell command either. > Yes, I know. I perhaps should have used the word "shell" instead > of bash. However, bash is a shell. To most people, "bash command" or "shell command" refers to 1) a commands built in to bash (or other shell) or 2) a command line that must be interpreted by bash (or other shell) because it uses I/O redirection, variable substituion, globbing, etc. Your code does not use bash (or any other shell), nor does it need to. It's just running the sudo executable. There are no shells involved, so using the word shell isn't any better than using the word bash. -- Grant Edwards grant.b.edwardsYow! I have seen these EGG at EXTENDERS in my Supermarket gmail.com... I have read the INSTRUCTIONS ... -- https://mail.python.org/mailman/listinfo/python-list
Re: Calling Bash Command From Python
On Mon, 31 Oct 2016 11:55:26 -0500, Wildman wrote: > On Mon, 31 Oct 2016 11:05:23 -0400, Random832 wrote: > >> On Mon, Oct 31, 2016, at 10:55, Wildman via Python-list wrote: >>> I have code using that approach but I am trying to save myself >>> from having to parse the entire shadow file. Grep will do it >>> for me if I can get code right. >> >> Python already has built-in functions to parse the shadow file. >> >> https://docs.python.org/3/library/spwd.html#module-spwd > > I didn't know about that module. Thanks, this can simplify > things for me. > >> But you can't use sudo this way if you use that. But why do you want to >> use sudo from within the python script instead of just running the >> python script with sudo? > > In view of the module I just learned about, that would be > a better approach. I made a discovery that I thought I would share. When using sudo to run the script the environment variable $USER will always return 'root'. Not what I wanted. But this will work: user = os.environ["SUDO_USER"] shadow = spwd.getspnam(user) That will return the actual user name that invoked sudo. -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: Reading Fortran Ascii output using python
On 31-10-2016 18:46, Heli wrote: > Thanks Irmen, > > I tried with "rU" but that did not make a difference. The problem is a line > that with one single write statement in my fortran code : > > write(UNIT=9,FMT="(99g20.8)") value > > seems to be read in two python inputfile.readline(). > > Any ideas how I should be fixing this? > > Thanks, > We don't speak Fortran here (at least I don't). Please show your Python code. What is 'inputfile'? Also, in Python, the readline method is defined as: >>> help(io.TextIOBase.readline) Help on method_descriptor: readline(...) Read until newline or EOF. Returns an empty string if EOF is hit immediately. So there must be something in that line in your file that it considers an EOF. Have you tried opening it in a regular text editor and inspecting what the characters are on that particular line? Irmen -- https://mail.python.org/mailman/listinfo/python-list
Re: Reading Fortran Ascii output using python
On 31-10-2016 19:24, Irmen de Jong wrote: > So there must be something in that line in your file that it considers an EOF. I meant to type EOL there. (end-of-line/newline). Irmen -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
Thanks for your detail explanation, my problem may be the 'return' keyword. I confuse at the point that return come after yield keyword, I wonder what if in case this code do not have the 1st return. So, when i try to delete the 1st return, the code run with error (the list out of range). This is actual clear from your example above, when the 1st return is delected, the statement flow shall be from (yield '') to (yield i + tmp) when the Next() is called. This behaviour shall reach to a statement is: for i in [][0] that shall raise an error: list index out of range. -- https://mail.python.org/mailman/listinfo/python-list
Re: Reading Fortran Ascii output using python
On 2016-10-31 17:46, Heli wrote: On Monday, October 31, 2016 at 6:30:12 PM UTC+1, Irmen de Jong wrote: On 31-10-2016 18:20, Heli wrote: > Hi all, > > I am trying to read an ascii file written in Fortran90 using python. I am reading this file by opening the input file and then reading using: > > inputfile.readline() > > On each line of the ascii file I have a few numbers like this: > > line 1: 1 > line 2: 1000.834739 2000.38473 3000.349798 > line 3: 1000 2000 5000.69394 99934.374638 54646.9784 > > The problem is when I have more than 3 numbers on the same line such as line 3, python seems to read this using two reads. This makes the above example will be read like this: > > line 1: 1 > line 2: 1000.834739 2000.38473 3000.349798 > line 3: 1000 2000 5000.69394 > line 4: 99934.374638 54646.9784 > > How can I fix this for each fortran line to be read correctly using python? > > Thanks in Advance for your help, > > You don't show any code so it's hard to say what is going on. My guess is that your file contains spurious newlines and/or CRLF combinations. Try opening the file in universal newline mode and see what happens? with open("fortranfile.txt", "rU") as f: for line in f: print("LINE:", line) Irmen Thanks Irmen, I tried with "rU" but that did not make a difference. The problem is a line that with one single write statement in my fortran code : write(UNIT=9,FMT="(99g20.8)") value seems to be read in two python inputfile.readline(). Any ideas how I should be fixing this? Thanks, What is actually in the file? Try opening it in binary mode and print using the ascii function: with open("fortranfile.txt", "rb") as f: contents = f.read() print("CONTENTS:", ascii(contents)) -- https://mail.python.org/mailman/listinfo/python-list
Need help with coding a function in Python
http://imgur.com/a/rfGhK#iVLQKSW How do I code a function that returns a list of the first n elements of the sequence defined in the link? I have no idea! So far this is my best shot at it (the problem with it is that the n that i'm subtracting or adding in the if/else part does not represent the element's position, but just the n that I am plugging into the function): def reca(n): rlist=[0] while len(rlist) < n: if (rlist[-1]-n) > 0 and (rlist[-1]-n) not in rlist: rlist.append(rlist[-1]-n) else: rlist.append(rlist[-1]+n) return(rlist) -- https://mail.python.org/mailman/listinfo/python-list
Re: Recursive generator in Python 3.5
tpqnn...@gmail.com wrote: def fg(args): if not args: yield "" return for i in args[0]: for tmp in fg(args[1:]): yield i + tmp return The final return is redundant, since there is an implicit return at the end, just like an ordinary function. The other one is just performing an early exit from the iteration. You could write it without any returns like this: def fg(args): if not args: yield "" else: for i in args[0]: for tmp in fg(args[1:]): yield i + tmp this is the Tower of Hanoi with recursive generator but it do > not have the 'return' here -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Need help with coding a function in Python
On 2016-10-31 22:09, devers.meetthebadger.ja...@gmail.com wrote: http://imgur.com/a/rfGhK#iVLQKSW How do I code a function that returns a list of the first n elements of the sequence defined in the link? I have no idea! So far this is my best shot at it (the problem with it is that the n that i'm subtracting or adding in the if/else part does not represent the element's position, but just the n that I am plugging into the function): def reca(n): rlist=[0] while len(rlist) < n: if (rlist[-1]-n) > 0 and (rlist[-1]-n) not in rlist: rlist.append(rlist[-1]-n) else: rlist.append(rlist[-1]+n) return(rlist) When you're calculating a[n], what's n? Well: When you're calculating a[1], you already have a[0], or 1 element. When you're calculating a[2], you already have a[0] and a[1], or 2 elements. So, in general, when you're calculating a[n], you already have a[0] ... a[n - 1], or n elements. The value of n is the length of the list so far. -- https://mail.python.org/mailman/listinfo/python-list
Re: Need help with coding a function in Python
devers.meetthebadger.ja...@gmail.com writes: > http://imgur.com/a/rfGhK#iVLQKSW ... > So far this is my best shot at it (the problem with it is that the n > that i'm subtracting or adding in the if/else part does not represent > the element's position... Right, so can you figure out the element's position? If yes, calculate it and use it instead of n. -- https://mail.python.org/mailman/listinfo/python-list
Re: Need help with coding a function in Python
On Tue, 1 Nov 2016 09:09 am, devers.meetthebadger.ja...@gmail.com wrote: > http://imgur.com/a/rfGhK#iVLQKSW Why on earth are you posting a screen shot? Do you edit your code with Photoshop? As soon as you post an unnecessary screenshot, you cut the number of people willing and able to help you in half: - anyone who is blind or visually impaired and reading this with a screen reader cannot help you, even if they wanted to; - anyone reading this post somewhere where access to imgur is blocked (say, from work); - anyone who takes one look at the URL and says "F--k it, if they can't be bothered to copy and paste text, I can't be bothered to follow the link". We're volunteers. We're not paid to solve your problem, so if you make it hard for us, we simply won't bother. > How do I code a function that returns a list of the first n elements of > the sequence defined in the link? I have no idea! I have no idea either, because I cannot see the image. I'll leave you to guess why I can't. But the usual way to return a list of the first n elements of a sequence is with slicing: first_bunch = sequence[:n] # a slice from index 0 to just before index n If sequence is a list, then the slice sequence[:n] will also be a list. Otherwise you may want to convert it to a list: first_bunch = list(sequence[:n]) How does slicing work? You can think of it as a faster, more efficient way of something like this: def cut_slice(sequence, start=0, end=None): if end is None: end = len(sequence) result = [] for i in range(start, end): value = sequence[i] result.append(value) return result P.S. any time you think you want a while loop, 90% of the time you don't. While-loops have their uses, but they're unusual. Whenever you know ahead of time how many loops will be done, use a for-loop. While-loops are only for those unusual cases where you don't know how many times you need to loop. Use a while-loop for: - loop *until* something happens; - loop *while* something happens; Use a for-loop for: - loop over each element of a sequence; - loop a fixed number of times. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Call a shell command from Python
Wildman via Python-list writes: > On Mon, 31 Oct 2016 15:44:13 +1100, Ben Finney wrote: > > > One immediate difference I see is that you specify different > > arguments to ‘grep’. You have a different pattern for each command. > > > > * The ‘^user\:’ pattern matches “user\:” at the start of a line. > > > > * The ‘^$USER\:’ pattern I think won't match anything, since “$” matches > > end-of-line and then you expect further characters *past* the end of > > the line. I think that will always fail to match any line. > > Yes, the '^' indicates the start of the line and the ':' indicates > the character where to stop. The colon has a special meaning so it > has to be escaped, '\:'. The dollar sign precedes a variable. In > this case it is an environment variable. The ‘grep’ program you're invoking knows nothing of such variables, and the ‘$’ sign means to ‘grep’ what I said above. > > Maybe you are expecting Bash to be involved somehow (and so “$USER” > > will be substituted by Bash with some other value). That's not what > > happens. > > No, the shell is already running. I don't know what you mean by this. If you mean that some *other* instances of the shell ar running: that isn't relevant to how your Python program invokes a subprocess. The shell is not involved in the command as you invoke it directly as a subprocess, without asking for a shell. > And $USER will be substituted by the name of the user that invoked the > shell. It will not, because there is no shell involved: your Python program invokes ‘sudo’, which invokes ‘grep’. The shell is never involved in that chain, so its substitutions rules are irrelevant. -- \ “Try adding “as long as you don't breach the terms of service – | `\ according to our sole judgement” to the end of any cloud | _o__) computing pitch.” —Simon Phipps, 2010-12-11 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Need help with coding a function in Python
On 10/31/2016 03:09 PM, devers.meetthebadger.ja...@gmail.com wrote: http://imgur.com/a/rfGhK#iVLQKSW How do I code a function that returns a list of the first n elements of the sequence defined in the link? I have no idea! So far this is my best shot at it (the problem with it is that the n that i'm subtracting or adding in the if/else part does not represent the element's position, but just the n that I am plugging into the function): def reca(n): rlist=[0] while len(rlist) < n: if (rlist[-1]-n) > 0 and (rlist[-1]-n) not in rlist: rlist.append(rlist[-1]-n) else: rlist.append(rlist[-1]+n) return(rlist) I'm not looking at your link or your code, just your description of the problem. Paraphrased: "A list of the first n elements of a sequence" No function necessary, just use slicing... newlist = oldlist[:n] list:[1,2,3,4,5,6,7,8,9,10][:5] gives [1,2,3,4,5] tuple: (1,2,3,4,5,6,7,8,9,10)[:5] gives (1,2,3,4,5) string: "Hello there"[:5] gives "Hello" Works for sequences shorter than n as well, which gives the full (short) sequence. list:[1,2,3][:5] gives [1,2,3] etc... -- -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
[RELEASE] Python 3.6.0b3 is now available
On behalf of the Python development community and the Python 3.6 release team, I'm pleased to announce the availability of Python 3.6.0b3. 3.6.0b3 is the third of four planned beta releases of Python 3.6, the next major release of Python. Among the new major new features in Python 3.6 are: * PEP 468 - Preserving the order of **kwargs in a function * PEP 487 - Simpler customization of class creation * PEP 495 - Local Time Disambiguation * PEP 498 - Literal String Formatting * PEP 506 - Adding A Secrets Module To The Standard Library * PEP 509 - Add a private version to dict * PEP 515 - Underscores in Numeric Literals * PEP 519 - Adding a file system path protocol * PEP 520 - Preserving Class Attribute Definition Order * PEP 523 - Adding a frame evaluation API to CPython * PEP 524 - Make os.urandom() blocking on Linux (during system startup) * PEP 525 - Asynchronous Generators (provisional) * PEP 526 - Syntax for Variable Annotations (provisional) * PEP 528 - Change Windows console encoding to UTF-8 (provisional) * PEP 529 - Change Windows filesystem encoding to UTF-8 (provisional) * PEP 530 - Asynchronous Comprehensions Please see "What’s New In Python 3.6" for more information: https://docs.python.org/3.6/whatsnew/3.6.html You can find Python 3.6.0b3 here: https://www.python.org/downloads/release/python-360b3/ Beta releases are intended to give the wider community the opportunity to test new features and bug fixes and to prepare their projects to support the new feature release. We strongly encourage maintainers of third-party Python projects to test with 3.6 during the beta phase and report issues found to bugs.python.org as soon as possible. While the release is feature complete entering the beta phase, it is possible that features may be modified or, in rare cases, deleted up until the start of the release candidate phase (2016-12-05). Our goal is have no changes after rc1. To achieve that, it will be extremely important to get as much exposure for 3.6 as possible during the beta phase. Please keep in mind that this is a preview release and its use is not recommended for production environments The next pre-release of Python 3.6 will be 3.6.0b4, currently scheduled for 2016-11-21. More information about the release schedule can be found here: https://www.python.org/dev/peps/pep-0494/ -- Ned Deily n...@python.org -- [] -- https://mail.python.org/mailman/listinfo/python-list
Re: Call a shell command from Python
On Tue, 01 Nov 2016 12:08:52 +1100, Ben Finney wrote: > Wildman via Python-list writes: > >> On Mon, 31 Oct 2016 15:44:13 +1100, Ben Finney wrote: >> >> > One immediate difference I see is that you specify different >> > arguments to ‘grep’. You have a different pattern for each command. >> > >> > * The ‘^user\:’ pattern matches “user\:” at the start of a line. >> > >> > * The ‘^$USER\:’ pattern I think won't match anything, since “$” matches >> > end-of-line and then you expect further characters *past* the end of >> > the line. I think that will always fail to match any line. >> >> Yes, the '^' indicates the start of the line and the ':' indicates >> the character where to stop. The colon has a special meaning so it >> has to be escaped, '\:'. The dollar sign precedes a variable. In >> this case it is an environment variable. > > The ‘grep’ program you're invoking knows nothing of such variables, and > the ‘$’ sign means to ‘grep’ what I said above. You are correct about that but, in this case grep never "sees" the '$' sign. Bash expands $USER to the actual user name beforehand. If you are on a Linux system, enter this into a terminal to illustrate: sudo grep ^$USER\: /etc/shadow If the user name is ben then grep would see this: grep ben\: /etc/shadow >> > Maybe you are expecting Bash to be involved somehow (and so “$USER” >> > will be substituted by Bash with some other value). That's not what >> > happens. >> >> No, the shell is already running. > > I don't know what you mean by this. If you mean that some *other* > instances of the shell ar running: that isn't relevant to how your > Python program invokes a subprocess. I simply meant that the script is run from a terminal. > The shell is not involved in the command as you invoke it directly as a > subprocess, without asking for a shell. > >> And $USER will be substituted by the name of the user that invoked the >> shell. > > It will not, because there is no shell involved: your Python program > invokes ‘sudo’, which invokes ‘grep’. The shell is never involved in > that chain, so its substitutions rules are irrelevant. I think my terminology is causing confusion. I apologize for that. -- GNU/Linux user #557453 -- https://mail.python.org/mailman/listinfo/python-list
Re: Call a shell command from Python
Wildman via Python-list writes: > […] in this case grep never "sees" the '$' sign. Bash expands $USER to > the actual user name beforehand. I understand how Bash substitutes variables on the command line. What I need to repeat, though: In this case, no, Bash doesn't do that because Bash isn't getting involved. Grep does in fact see the “$” in the command argument, because nothing ever replaces it. > >> No, the shell is already running. > > > > I don't know what you mean by this. > > I simply meant that the script is run from a terminal. Which means that shell isn't involved in that command you're invoking from inside the script. The Bash instance which invoked your script is now *doing nothing*, and will never do anything again until control returns to it (by your script exiting, or suspending, or some other control interruption). Your script invoking further programs will *not* get the earlier Bash involved in that process (except in the special cases where those programs themselves manipulate running processes; ‘sudo’ and ‘grep’ are not special in this way). So the way your script was invoked has no bearing on whether Bash will get involved in what your script does. Your script is *directly* invoking programs, and if you don't ask for a shell to be involved you won't get it. -- \ “… a Microsoft Certified System Engineer is to information | `\ technology as a McDonalds Certified Food Specialist is to the | _o__) culinary arts.” —Michael Bacarella | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Call a shell command from Python
On Tue, 1 Nov 2016 04:00 pm, Wildman wrote: > You are correct about that but, in this case grep never "sees" the '$' > sign. Bash expands $USER to the actual user name beforehand. If you > are on a Linux system, enter this into a terminal to illustrate: > > sudo grep ^$USER\: /etc/shadow Bash is not involved here. Python is calling grep directly. You don't have to believe us, you can test this yourself. Create a simple text file with a single line containing your username, and a simple Python script that calls grep as you have been: [steve@ando ~]$ echo $USER steve [steve@ando ~]$ cat foo.txt blah blah steve blah blah [steve@ando ~]$ cat greptest.py import subprocess cmdlist = ['grep', '$USER', 'foo.txt'] p = subprocess.Popen(cmdlist, stdout=subprocess.PIPE,stderr=subprocess.PIPE) line, err = p.communicate() print err, line [steve@ando ~]$ python2.7 greptest.py [steve@ando ~]$ So there you have it: categorical proof that bash does not expand the string '$USER'. It cannot: bash is not involved in the subprocess call. Python calls grep directly. If you want to expand the '$USER' string from Python, do it yourself: py> import os py> os.environ['USER'] 'steve' > If the user name is ben then grep would see this: > > grep ben\: /etc/shadow If would, if you called grep from bash. But you didn't. >>> > Maybe you are expecting Bash to be involved somehow (and so “$USER” >>> > will be substituted by Bash with some other value). That's not what >>> > happens. >>> >>> No, the shell is already running. >> >> I don't know what you mean by this. If you mean that some *other* >> instances of the shell ar running: that isn't relevant to how your >> Python program invokes a subprocess. > > I simply meant that the script is run from a terminal. That's irrelevant. Just because the script is running from a terminal doesn't mean that the shell can peer deep inside each and every process and magically apply the shell's string expansion rules. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
how to compile this code
would like to change Add operator to custom function op2 in solve function and then this solve([x*y - 1, x + 2], x, y) during solve, the parameters also change Add to custom function op2 import ast from __future__ import division from sympy import * x, y, z, t = symbols('x y z t') k, m, n = symbols('k m n', integer=True) f, g, h = symbols('f g h', cls=Function) import inspect def op2(a,b): return a*b+a class ChangeAddToMultiply(ast.NodeTransformer): """Wraps all integers in a call to Integer()""" def visit_BinOp(self, node): print(dir(node)) print(dir(node.left)) if isinstance(node.op, ast.Add): node.op = op2(node.left, node.right) return node code = inspect.getsourcelines(solve) tree = ast.parse(code) tree = ChangeAddToMultiply().visit(tree) ast.fix_missing_locations(tree) co = compile(tree, '', "exec") exec(code) exec(co) -- https://mail.python.org/mailman/listinfo/python-list