Data transfer from Python CGI to javascript
I have written the following program #!c:\Python27\Python.exe import cgi, cgitb; import sys, serial cgitb.enable() ser = serial.Serial('COM27', 9600) myvar = ser.readline() print "Content-type:text/html\n\n" print """ Real Time Temperature window.onload = startInterval; function startInterval() { setInterval("startTime();",1000); } function startTime(myvar) { document.getElementById('mine').innerHTML ="Temperature" +parseInt(myvar); } Real Time Temperature: """ It is giving an output of 'NaN' in the output. If the python program is run alone it is giving the correct output. Can anyone please help. Is it also possible to separate the python and the javascript to two different files. If so how will I get/transfer the data in/from HTML . Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
Le jeudi 8 août 2013 22:29:00 UTC+2, Terry Reedy a écrit : > On 8/8/2013 7:41 AM, Chris Angelico wrote: > > > On Thu, Aug 8, 2013 at 7:20 AM, wrote: > > > def z2(): > > >> ... letters = 'abc' > > >> ... while True: > > >> ... c = input('letter: ') > > >> ... if c not in letters: > > >> ... print('end, fin, Schluss') > > >> ... break > > >> ... else: > > >> ... print('do stuff') > > > > > > > > > Minor quibble: I don't like having a hard exit followed by an "else". > > > > Whereas I tend to prefer to have the two alternatives cleanly marked as > > alternatives by both being indented the same. > > > > Many alternatives are not so trivial as the above. I remember reading > > one snippet in the CPython codebase where the 'else' was omitted and the > > if clause subdivided into about three paths. It took at least a minute > > to determine that all paths terminated in such a way that there really > > was an inplied else. How much easier it would have been to read the code > > if the author had explicitly types the 'else'. > > > > > If the "if" branch will unconditionally quit the loop (with a break, > > > here, but could also be a return, a thrown exception, etc etc), I > > > would prefer to see the "else" removed and its code unindented one > > > level. Maybe this is just personal preference, though, learned from > > > assembly language programming where a "block if" looks something like > > > this: > > > > > > ; if x == y: > > > CMP x,y > > > JNZ .else > > > ; Code for "x == y" > > > JMP .endif > > > .else: > > > ; Code for "else" > > > .endif > > > > > > Putting an unconditional departure in the "x == y" branch makes the > > > "JMP .endif" redundant. > > > > Python is not assembly ;-). 3.3 effectively ignores the extraneous > > 'else:'. Either way, if the condition is false, control jumps to the > > second print. For what little it matters, the bytecode is the same length. > > > > def f(): > >while True: > > if a: > >b = 1 > >break > > else: > >b = 2 > > > > >>> dis(f) > >2 0 SETUP_LOOP 25 (to 28) > > > >3 >>3 LOAD_GLOBAL 0 (a) > >6 POP_JUMP_IF_FALSE 19 > > > >4 9 LOAD_CONST 1 (1) > > 12 STORE_FAST 0 (b) > > > >5 15 BREAK_LOOP > > 16 JUMP_ABSOLUTE3 > > > >7 >> 19 LOAD_CONST 2 (2) > > 22 STORE_FAST 0 (b) > > 25 JUMP_ABSOLUTE3 > > >> 28 LOAD_CONST 0 (None) > > 31 RETURN_VALUE > > > > def f(): > >while True: > > if a: > >b = 1 > >break > > b = 2 > > > > >>> dis(f) > >2 0 SETUP_LOOP 25 (to 28) > > > >3 >>3 LOAD_GLOBAL 0 (a) > >6 POP_JUMP_IF_FALSE 19 > > > >4 9 LOAD_CONST 1 (1) > > 12 STORE_FAST 0 (b) > > > >5 15 BREAK_LOOP > > 16 JUMP_FORWARD 0 (to 19) > > > >6 >> 19 LOAD_CONST 2 (2) > > 22 STORE_FAST 0 (b) > > 25 JUMP_ABSOLUTE3 > > >> 28 LOAD_CONST 0 (None) > > 31 RETURN_VALUE > > > > -- > > Terry Jan Reedy - The problem of this guy is not at this level. His problem is more simply, he most probably does not understand how to build a correct, proper loop. "What I wanted to happen is when the user typed something ... ... would cause the program to stop ... " jmf -- http://mail.python.org/mailman/listinfo/python-list
connection change
Hi, I have a compiled code, which currently uses SQLITE connection (datastore). I was wondering if it is possible to change the connection string to use SQL Server database. I would like to export the datastore to SQL server database, as we are having issues with the sqlite limit. Thank you. Inna This email is intended solely for the named addressee. If you are not the addressee indicated please delete it immediately. -- http://mail.python.org/mailman/listinfo/python-list
"Nested" virtual environments
Dear all, is there a way to "nest" virtual environments? I work on several different projects that involve Python programming. For a lot of this projects I have to use the same packages (e.g. numpy, scipy, matplotlib and so on), while having to install packages that are specific for each project. For each of this project I created a virtual environment (using virtualenv --no-site-packages) and I had to reinstall the shared packages in each of them. I was wondering if there is a way to nest a virtual environment into another, so that I can create a "common" virtual environment that contains all the shared packages and then "specialize" the virtual environments installing the packages specific for each project. In a way this is not conceptually different to using virtualenv --system-site-packages, just instead of getting access to the system packages a virtual environment should be able to access the packages of an other one. Thanks a lot in advance for the help, Luca -- http://mail.python.org/mailman/listinfo/python-list
Re: right adjusted strings containing umlauts
Le jeudi 8 août 2013 18:27:06 UTC+2, Kurt Mueller a écrit : > Now I have this small example: > > -- > > #!/usr/bin/env python > > # vim: set fileencoding=utf-8 : > > > > from __future__ import print_function > > import sys, shlex > > > > print( repr( sys.stdin.encoding ) ) > > > > strg_form = u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}' > > for inpt_line in sys.stdin: > > proc_line = shlex.split( inpt_line, False, True, ) > > encoding = "utf-8" > > proc_line = [ strg.decode( encoding ) for strg in proc_line ] > > print( strg_form.format( *proc_line ) ) > > -- > > > > $ echo -e "a b c d e\na ö u 1 2" | file - > > /dev/stdin: UTF-8 Unicode text > > $ echo -e "a b c d e\na ö u 1 2" | ./align_compact.py > > None > > a b c d e > > a ö u 1 2 > > $ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | file - > > /dev/stdin: ISO-8859 text > > $ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | ./align_compact.py > > None > > a b c d e > > Traceback (most recent call last): > > File "./align_compact.py", line 13, in > > proc_line = [ strg.decode( encoding ) for strg in proc_line ] > > File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode > > return codecs.utf_8_decode(input, errors, True) > > UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 0: > invalid start byte > > muk@mcp20:/sw/prog/scripts/text_manip> > > > > How do I handle this two inputs? > > > > > > TIA > > -- > > Kurt Mueller It's very easy. The error msg indicates, you cann't decode your series of bytes with the utf-8 codec, simply because your string is encoded in iso-8859-* (you did it explicitly!). Your problem is not Python, your problem is the coding of the characters. You should be aware about the coding of the strings you are manipulating (creating) and if necessary decode and/or encode correctly accordingly to what you wish, eg. a suitable coding for the display. That's on this level that Python (or any language) matters. The sys.std*.encoding is a different problem. iso-8859-* ? iso-8859-1 == latin-1 and latin9 == iso-8859-15. If one excepts "das grosse Eszett", both codings are able to handle German (it seems to be your case) and there are no problems when working directly with these codings. jmf -- http://mail.python.org/mailman/listinfo/python-list
Re: passing Python data to a javascript function
On Thursday, October 27, 2011 9:25:28 AM UTC+5:30, Chris Angelico wrote: > On Thu, Oct 27, 2011 at 2:51 PM, Bill Allen wrote: > > > Chris, > > > > > > Wow, that seems so simple now that I see it. I was dancing around that all > > > day, but just not landing on it. Thanks so very much for the assist. > > >This code is giving an error 500 when run. Although the python code is > >independently running OK > > > --Bill > > > > > > Final code that works perfectly, passes the value from the Python script to > > > the javascript correctly: > > > > > > > > > > Congratulations! You've just written code that writes code. It takes a > > bit to get your head around it (especially when you start worrying > > about literal strings that might contain quotes, for instance), but > > it's really cool and seriously powerful stuff. Your server-side Python > > code can generate client-side Javascript code any way it likes... > > unlimited possibilities. > > > > ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
Am 09.08.2013 05:47, schrieb David: On 9 August 2013 14:11, Adam Mercer wrote: I'm trying to write a script that writes some content to a file root through sudo, but it's not working at all. I am using: [...] At a quick glance, I have a couple of suggestions. command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] sudo doesn't work like this. It doesn't read from standard input. You need to supply the command as an argument to sudo. Get the sudo syntax correct by learning to use it in a shell (eg terminal running bash ) before trying to use it from python code. Also, I think that passing the pipe character '|' as an argument to Popen is not the correct way to use pipes. So, if you figure out how to use sudo without '|' you will solve both these issues. Or try to change the permissions os.system("chmod 666 config_file) do what you want with the file and set the permissions back to their original state. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On Thu, Aug 8, 2013 at 11:47 PM, David wrote: > At a quick glance, I have a couple of suggestions. > >> command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] > > sudo doesn't work like this. It doesn't read from standard input. You > need to supply the command as an argument to sudo. Get the sudo syntax > correct by learning to use it in a shell (eg terminal running bash ) > before trying to use it from python code. The above does works in the terminal: [ram@cizin ~]$ ls -l /opt/ldg/etc/ total 20 -rw-rw-r-- 1 ram admin 5246 Jun 14 15:15 globus-user-env.csh -rw-rw-r-- 1 ram admin 3388 Jun 14 15:15 globus-user-env.sh -rw-rw-r-- 1 ram admin 91 Jun 24 11:33 ldg.conf -rw-r--r-- 1 ram admin5 Jun 14 15:24 os.conf $ echo -n stable | sudo tee /opt/ldg/etc/channel.conf > /dev/null Password: $ ls -l /opt/ldg/etc/ total 24 -rw-r--r-- 1 root admin6 Aug 9 07:59 channel.conf -rw-rw-r-- 1 ram admin 5246 Jun 14 15:15 globus-user-env.csh -rw-rw-r-- 1 ram admin 3388 Jun 14 15:15 globus-user-env.sh -rw-rw-r-- 1 ram admin 91 Jun 24 11:33 ldg.conf -rw-r--r-- 1 ram admin5 Jun 14 15:24 os.conf $ Thats why I'm trying to use it. > Also, I think that passing the pipe character '|' as an argument to > Popen is not the correct way to use pipes. I believe subprocess uses pipes as follows import subprocess with open("/opt/ldg/etc/channel.conf","w") as out: subprocess.Popen(command, stdout=out) Then the file wouldn't be opened as root? And if the file already exists then I wouldn't be able to open it due to incorrect permissions. > So, if you figure out how to use sudo without '|' you will solve both > these issues. I'm open to suggestions as everything I can find involved pipes or redirections and neither seems to work with subprocess. I can use subprocess.call() as in: import subprocess import sys channel='stable' config_file='/opt/ldg/etc/channel.conf' command="echo -n %s | sudo tee %s > /dev/null" % (channel, config_file) try: retcode = subprocess.call(command, shell=True) if retcode < 0: sys.exit('Error: Failed to set channel.conf') except OSError as e: sys.exit('Error: Execution failed "%s"' % e) But I was under the impression that Popen was the preferred approach to running external processes? Cheers Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On Fri, Aug 9, 2013 at 2:21 PM, Adam Mercer wrote: > command="echo -n %s | sudo tee %s > /dev/null" % (channel, config_file) > You shouldn't need to use 'echo' here. Just provide tee with the text on its standard input, and don't bother with the pipe at all. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
Op 09-08-13 15:29, Chris Angelico schreef: > On Fri, Aug 9, 2013 at 2:21 PM, Adam Mercer wrote: >> command="echo -n %s | sudo tee %s > /dev/null" % (channel, config_file) >> > > You shouldn't need to use 'echo' here. Just provide tee with the text > on its standard input, and don't bother with the pipe at all. That is probably beside the point. I suspect Adam is just giving a minimal example to show the kind of thing he is trying to do. Nit picking the specific example instead of advising on the problem is likely to be less than helpful. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On Fri, Aug 9, 2013 at 8:29 AM, Chris Angelico wrote: > You shouldn't need to use 'echo' here. Just provide tee with the text > on its standard input, and don't bother with the pipe at all. Thanks, that's much better! Cheers Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On Fri, Aug 9, 2013 at 8:42 AM, Antoon Pardon wrote: > That is probably beside the point. I suspect Adam is just giving a > minimal example to show the kind of thing he is trying to do. > > Nit picking the specific example instead of advising on the problem > is likely to be less than helpful. It is a simplified example, but in this case the nitpicking was very helpful. Caused me to think about the problem differently, and therefore come up with a neater solution. Cheers Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
Op 09-08-13 06:11, Adam Mercer schreef: > Hi > > I'm trying to write a script that writes some content to a file root > through sudo, but it's not working at all. I am using: > > channel = 'stable' > config_file = '/opt/ldg/etc/channel.conf' > command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] > p = subprocess.Popen(command, stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > out, _ = p.communicate() > > But it seems as if this isn't doing anything. > > I just want to write the contents of the variable channel to the file > /opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can > anyone offer any pointers? > > Cheers > > Adam subprocess.Popen by default doesn't use a shell. That could mean that the equivallent shell command you are trying to execute is: echo -n stable '|' sudo tee /opt/ldg/etc/channel.conf instead of echo -n stable | sudo tee /opt/ldg/etc/channel.conf which you probably want. You should also take care with the echo command. Echo is usually a shell builtin that can behave differently than the echo command. You should make sure you are using the one you actually want to use. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On Fri, Aug 9, 2013 at 2:50 PM, Adam Mercer wrote: > On Fri, Aug 9, 2013 at 8:42 AM, Antoon Pardon > wrote: > >> That is probably beside the point. I suspect Adam is just giving a >> minimal example to show the kind of thing he is trying to do. >> >> Nit picking the specific example instead of advising on the problem >> is likely to be less than helpful. > > It is a simplified example, but in this case the nitpicking was very > helpful. Caused me to think about the problem differently, and > therefore come up with a neater solution. It wasn't nitpicking so much as suggesting a more Pythonic way to do things. It's entirely plausible that the response would have been "Actually, the real command is more complicated than 'echo' so I really do need a pipe", but it's *very* common in shell scripts to call on an external process to do something that in other languages is a builtin. For instance, how do you get the name of the parent of your current directory in bash? Something along the lines of: parent=$(dirname $(pwd$)$) which executes two external commands. In Python, it would be: os.path.dirname(os.getcwd()) which is two internal function calls. It's just one of the things to consider when porting code from one language to another - I wouldn't often use a list comprehension in bash, and I wouldn't often want a pipe between two external processes in Python. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Data transfer from Python CGI to javascript
On 09/08/2013 07:53, e...@cleantechsolution.in wrote: I have written the following program #!c:\Python27\Python.exe import cgi, cgitb; import sys, serial cgitb.enable() ser = serial.Serial('COM27', 9600) myvar = ser.readline() print "Content-type:text/html\n\n" print """ Real Time Temperature window.onload = startInterval; function startInterval() { setInterval("startTime();",1000); } function startTime(myvar) { document.getElementById('mine').innerHTML ="Temperature" +parseInt(myvar); } Real Time Temperature: """ It is giving an output of 'NaN' in the output. If the python program is run alone it is giving the correct output. Can anyone please help. Is it also possible to separate the python and the javascript to two different files. If so how will I get/transfer the data in/from HTML . Thanks At a guess I'd say that it's because you have: setInterval("startTime();",1000); which will call 'startTime' with no arguments, but: function startTime(myvar) which means that it's expecting an argument. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On 9 August 2013 23:21, Adam Mercer wrote: > On Thu, Aug 8, 2013 at 11:47 PM, David wrote: > >> At a quick glance, I have a couple of suggestions. >> >>> command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] >> >> sudo doesn't work like this. It doesn't read from standard input. You >> need to supply the command as an argument to sudo. Get the sudo syntax >> correct by learning to use it in a shell (eg terminal running bash ) >> before trying to use it from python code. > > The above does works in the terminal: Ah, sorry, I didn't pay close attention to what you are doing (with tee). >> So, if you figure out how to use sudo without '|' you will solve both >> these issues. At least I wasn't 100% wrong :) Anyway I'm glad some smarter people helped you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 3 and SSH Tunnel
D. Xenakis schrieb: > I've played with putty to achieve this but to be honest i'd like > something more efficient. Opening putty everytime and making all the > connection settings etc, and then running the programm, is kinda messy. > Id like this to be done in an automatic way from the program so that > things roll easy. I thought maybe i should find a way how to call and > run a batch file from inside my python program or a powershell command, > but i do not know even if that could work for the ssh tunneling. > > any ideas? Both popular frameworks for python SSH - twisted and paramiko - are still being ported to python3. If you need to run your code on Windows, take a look at plink, a command line tool for PuTTY: http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter7.html#plink You can wrap plink and your python script in a batch-file or call plink from inside your script using subprocess. -- http://mail.python.org/mailman/listinfo/python-list
Re: connection change
On Thu, Aug 8, 2013 at 10:23 PM, Inna Belakhova wrote: > Hi, > > > > I have a compiled code, which currently uses SQLITE connection (datastore). I don't understand 'compiled code'. It would be nice if you show the code, give the version of python you are using, and the operating system Have you tried to change your program to use mysql instead? If so, show the changes you made and what the results were. > I was wondering if it is possible to change the connection string to use SQL > Server database. yes it is possible > > > > I would like to export the datastore to SQL server database, as we are > having issues with the sqlite limit. > > > > Thank you. > > Inna > > > > This email is intended solely for the named addressee. > If you are not the addressee indicated please delete it immediately. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com -- http://mail.python.org/mailman/listinfo/python-list
Python3 Multiprocessing
Aloha! I need a command that will make threads created by "multiprocessing.Process()" wait for each other to complete. For instance, I want to do something like this: job1 = multiprocessing.Process(CMD1()) job2 = multiprocessing.Process(CMD2()) jobs1.start(); jobs2.start() PY_FUNC() The command "PY_FUNC()" depends on the end result of the actions of CMD1() and CMD2(). I need some kind of wait command for the two threads that will not let the script continue until job1 and job2 are complete. Is this possible in Python3? Mahalo, devyncjohn...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python3 Multiprocessing
On 09/08/2013 20:30, Devyn Collier Johnson wrote: Aloha! I need a command that will make threads created by "multiprocessing.Process()" wait for each other to complete. For instance, I want to do something like this: job1 = multiprocessing.Process(CMD1()) job2 = multiprocessing.Process(CMD2()) jobs1.start(); jobs2.start() PY_FUNC() The command "PY_FUNC()" depends on the end result of the actions of CMD1() and CMD2(). I need some kind of wait command for the two threads that will not let the script continue until job1 and job2 are complete. Is this possible in Python3? Possibly you mean .join: jobs1.start() jobs2.start() jobs1.join() jobs2.join() -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On Thu, 08 Aug 2013 23:11:09 -0500, Adam Mercer wrote: > I'm trying to write a script that writes some content to a file root > through sudo, but it's not working at all. I am using: > command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] You can't create a pipeline like this. All of the list elements after the first will be passed as arguments to "echo". Try: command = ['sudo', 'tee', config_file] p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, _ = p.communicate('channel') -- http://mail.python.org/mailman/listinfo/python-list
Re: Using sudo to write to a file as root from a script
On Fri, 09 Aug 2013 21:12:20 +0100, Nobody wrote: > Try: > > command = ['sudo', 'tee', config_file] > p = subprocess.Popen(command, stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > out, _ = p.communicate('channel') Oops; you also need stdin=subprocess.PIPE. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python3 Multiprocessing
On 08/09/2013 03:44 PM, MRAB wrote: On 09/08/2013 20:30, Devyn Collier Johnson wrote: Aloha! I need a command that will make threads created by "multiprocessing.Process()" wait for each other to complete. For instance, I want to do something like this: job1 = multiprocessing.Process(CMD1()) job2 = multiprocessing.Process(CMD2()) jobs1.start(); jobs2.start() PY_FUNC() The command "PY_FUNC()" depends on the end result of the actions of CMD1() and CMD2(). I need some kind of wait command for the two threads that will not let the script continue until job1 and job2 are complete. Is this possible in Python3? Possibly you mean .join: jobs1.start() jobs2.start() jobs1.join() jobs2.join() Thanks MRAB! That is easy. I always (incorrectly) thought the join() command got two threads and made them one. I did not know it made the script wait for the threads. Mahalo, devyncjohn...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python3 Multiprocessing
On Fri, Aug 9, 2013, at 16:43, Devyn Collier Johnson wrote: > Thanks MRAB! That is easy. I always (incorrectly) thought the join() > command got two threads and made them one. I did not know it made the > script wait for the threads. What you're missing is the fact that the main thread [i.e. the one running "the script", and that waits for the thread you call the method on] is, well, a thread. So, you start with two threads [the main thread and the jobs1 thread, for example], and end up with one [the main thread]. This is why it's called join. -- http://mail.python.org/mailman/listinfo/python-list
RE: Python3 Multiprocessing
Devyn Collier Johnson > On 08/09/2013 03:44 PM, MRAB wrote: > > On 09/08/2013 20:30, Devyn Collier Johnson wrote: [snip] > >> > > jobs1.join() > > jobs2.join() > > > > Thanks MRAB! That is easy. I always (incorrectly) thought the join() > command got two threads and made them one. I did not know it made the > script wait for the threads. > It does join two threads, just not the threads you think! It "joins" a child thread with the parent thread and not two children thread. > Mahalo, > > devyncjohn...@gmail.com > -- ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
This is what I ended up with btw. Does this insult anyone's more well attuned Python sensibilities? letters='abcdefghijkl' def repeat(): print('wanna go again?') batman=input() if batman in ('y','yes'): main() else: return def main(): print('guess a letter') batman=input() if batman in letters: print('ok that letter was in letters') repeat() else: print('asdasdasd') repeat() main() print('how ya doin') -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
This is what I ended up with btw. Does this insult anyone's more well-attuned Pythonic sensibilities? letters='abcdefghijkl' def repeat(): print('wanna go again?') batman=input() if batman in ('y','yes'): main() else: return def main(): print('guess a letter') batman=input() if batman in letters: print('ok that letter was in letters') repeat() else: print('asdasdasd') repeat() main() print('how ya doin') -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
On 9 August 2013 23:27, wrote: > This is what I ended up with btw. Does this insult anyone's more well attuned > Python sensibilities? ... Yes. You didn't listen to any of the advice we've been giving you. You've had *much* better answers given than this. Start from the top. We need letters, so define that: letters = "abcdefghijkl" We then want to loop, possibly forever. A good choice is a "while" loop. # True is always True, so will loop forever while True: We then want to ask for a letter. We want to use "input". Write "help(input)" in the Python Shell and you get >>> help(input) Help on built-in function input in module builtins: input(...) input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading. So our line should be: letter = input("Type a letter from 'a' to 'n' in the alphabet: ") Then we want to test if it's on of our letters: if letter in letters: And if so we want to say something positive: print("That's right.") If not we want to say something negative: else: print("That's wrong.") And then we want to ask if we should go again: go_again = input("Do you want to do this again? ") If the response is "y" or "yes", we want to continue looping. The while loop will do that automatically, so we can do nothing in this circumstance. If the response in *not* "y" or "yes", we want to stop: if go_again not in ("y", "yes"): break That's it. No need to complicate things. Just take it one step at a time. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
I don't understand any of the advice any of you have given. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
Thanks, though me not utilizing any of the other advice wasn't from lack of trying; I couldn't understand any of it. I get it now that I have a corrrect example code in front of me. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
On 10 August 2013 00:14, wrote: > I don't understand any of the advice any of you have given. What about it don't you understand? Pick a sentence you don't understand and throw it back at us. If you understand all the sentences but not how they come together, say so. If there's a leap that you don't understand, say that you don't get it. We've tried rephrasing things a few ways but without any interaction we can't really help. --- You have said "I figured that ... the batman==False part of the repeat() function would cause the 'while batman==True' part to become False and end." We have said this is untrue. The "batman = False" inside the function does not affect "batman" outside of the function. You need to put "global batman" in the function for it to change "batman" on a global scope. You've not once explained what part of this explanation confuses you. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
What does global mean? -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
(I forgot to post this with my last post.) Also, I don't understand any part of the following example, so there's no specific line that's confusing me. Thanks for the help btw. var = 42 def myfunc(): var = 90 print "before:", var myfunc() print "after:", var def myfunc(): global var var = 90 -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
(I forgot to post this with my last post.) Also, I don't understand any part of the following example, so there's no specific line that's confusing me. Thanks for the help btw. var = 42 def myfunc(): var = 90 print "before:", var myfunc() print "after:", var def myfunc(): global var var = 90 -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
On 10/08/2013 00:40, eschneide...@comcast.net wrote: (I forgot to post this with my last post.) Also, I don't understand any part of the following example, so there's no specific line that's confusing me. Thanks for the help btw. You don't understand _any_ of it? > var = 42 Here you're assigning to 'var'. You're not in a function, so 'var' is a global variable. def myfunc(): > var = 90 Here you're assigning to 'var'. If you assign to a variable anywhere in a function, and you don't say that that variable is global, then it's treated as being local to that function, and completely unrelated to any other variable outside that function. print "before:", var myfunc() print "after:", var def myfunc(): global var var = 90 Here you're assigning to 'var', but this time you've declared that it's global, so you're assigning to the global variable called 'var'. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simulate `bash` behaviour using Python and named pipes.
Luca Cerone wrote: Thanks! I managed to make it work using the threading library :) If at least one of the external programs can accept the source or destination as a filename argument instead of redirecting its stdin or stdout, you can also do something like this: import subprocess p2 = subprocess.Popen(["cat", "named_pipe"]) pipe = open("named_pipe", "w") p1 = subprocess.Popen(["ls", "-lah"], stdout = pipe) pipe.close() p1.wait() p2.wait() That works because opening the reading end of the pipe is done by the subprocess executing cat, leaving the main process free to open the other end. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
I'm sorry, but I still don't understand how it applies to my problem. Thanks for everyone's patience. -- http://mail.python.org/mailman/listinfo/python-list
PEP 450 Adding a statistics module to Python
I am seeking comments on PEP 450, Adding a statistics module to Python's standard library: http://www.python.org/dev/peps/pep-0450/ Please read the FAQs before asking anything :-) Also relevant: http://bugs.python.org/issue18606 -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
On 10 August 2013 00:34, wrote: > What does global mean? Python has "scopes" for its variables. Most programming languages do. A "scope" is a restriction on where variables exist -- they exist only within the scope. This can be seen in this example: def function(): # A new "scope" is made when you enter a function variable = 100 function() print(variable) # Error, as variable doesn't exist outside of "function"'s scope There are lots of different "scopes" in code. Every function has one, and there are some more too. One of the scopes is the "global" scope. This is the scope *outside* of all the functions and other scopes. Everything in the file is within this sope: # Make in global scope variable = 100 def function(): # Works because we're inside the global scope print(variable) # Prints "100" function() So "a = b" inside the function applies to the function's scope, but when accessing variables (such as "print(variable)") it will look in all of the outer scopes too. If you want to write "a = b" inside the function and change the global scope, you need to say that "a" refers to the "a" in the global scope. You do that like this: def function(): # "variable" is in the global scope, not the functions' global variable variable = 100 function() # Prints "100" print(variable) Does that help you understand what "global" means? -- http://mail.python.org/mailman/listinfo/python-list
Re: right adjusted strings containing umlauts
On Thu, 08 Aug 2013 17:24:49 +0200, Kurt Mueller wrote: > What do I do, when input_strings/output_list has other codings like > iso-8859-1? When reading from a text file, honour some sort of encoding cookie at the top (or bottom) of the file, like Emacs and Vim use, or a BOM. If there is no encoding cookie, assume UTF-8. When reading from stdin, assume UTF-8. Otherwise, make it the caller's responsibility to specify the encoding if they wish to use something else. Pseudo-code: encoding = None if command line arguments include '--encoding': encoding = --encoding argument if encoding is None: if input file is stdin: encoding = 'utf-8' else: open file as binary if first 2-4 bytes look like a BOM: encoding = one of UTF-8 or UTF-16 or UTF-32 else: read first two lines if either looks like an encoding cookie: encoding = cookie # optionally check the end of the file as well close file if encoding is None: encoding = 'utf-8' read from file using encoding -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner question (True False help)
On Fri, 09 Aug 2013 16:34:48 -0700, eschneider92 wrote: > What does global mean? Hi eschneider92, A few bits of advice: - You may like to actually sign your emails with a real name, or at least an alias that you want to be called, otherwise we'll just call you by your email address, and apart from sounding silly, many people don't like that. - You might also find that the tutor mailing list is a better match for your status as a complete beginner to Python. You can subscribe to it here: http://mail.python.org/mailman/listinfo/tutor - If you do, please take my advice and use individual emails, not daily digests. It is MUCH easier to carry on a back-and-forth conversation with individual emails. - Please try to quote enough of the message you are replying to to establish context, but without filling the email with page after page of irrelevant history. (Notice the line at the top of this message, starting with ">"? That's what you previously wrote.) If you need help configuring your email program to quote the previous message, just ask. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 450 Adding a statistics module to Python
On Fri, Aug 9, 2013 at 8:10 PM, Steven D'Aprano wrote: > I am seeking comments on PEP 450, Adding a statistics module to Python's > standard library: > > http://www.python.org/dev/peps/pep-0450/ > > Please read the FAQs before asking anything :-) Given that installing numpy or scipy is generally no more difficult that executing "pip install (scipy|numpy)" I'm not really feeling the need for a battery here... (Of course, I use this stuff at work from time-to-time, so maybe I'm more in the "nuclear reactor of batteries" camp anyway.) Skip -- http://mail.python.org/mailman/listinfo/python-list
Re: connection change
On Fri, Aug 9, 2013 at 7:31 PM, Dennis Lee Bieber wrote: > On Fri, 9 Aug 2013 14:36:54 -0400, Joel Goldstick > declaimed the following: > > >> >>Have you tried to change your program to use mysql instead? If so, >>show the changes you made and what the results were. >> > > Pardon? "mssql" is not the same as "mysql" oops.. bad reading Dennis.. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 450 Adding a statistics module to Python
Skip Montanaro writes: > Given that installing numpy or scipy is generally no more difficult > that executing "pip install (scipy|numpy)" I'm not really feeling the > need for a battery here... NumPy and SciPy are not available for many Python users, including those using a Python implementation for which there is no Numpy support http://new.scipy.org/faq.html#python-version-support> and those for whom large, dependency-heavy third-party packages are too much burden. See the Rationale of PEP 450 for more reasons why “install NumPy” is not a feasible solution for many use cases, and why having ‘statistics’ as a pure-Python, standard-library package is desirable. -- \ “Dad always thought laughter was the best medicine, which I | `\guess is why several of us died of tuberculosis.” —Jack Handey | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list