[Tutor] StringIO and dictionaries
I want to run an external python script inside my script. So here is what I came up with: Print codename for testing purposes. Define an empty dictionary. Read the file. Do StringIO assignments and run the code. Get the outputs and append them to the dictionary. Print the outputs for testing purposes. The code: codeOut= {} codeErr= {} Print codename for testing purposes: print codename Note: codename is a parameter that is passed to the function for the first parts (keys) of codeOut and codeErr dictionaries; and it prints the given name (deneme in this case) correctly. Read the file: localfile= open(filename,'r') code2run= localfile.readlines() localfile.close() Do StringIO assignments and run the code: codOut= StringIO.StringIO() codErr= StringIO.StringIO() sys.stdout= codOut sys.stderr= codErr exec code2run sys.stdout= sys.__stdout__ sys.stderr= sys.__stderr__ Get the outputs and append them to the dictionary. erroroutput= codErr.getvalue() normaloutput= codOut.getvalue() codeOut.append({codename:erroroutput}) codeErr.append({codename:normaloutput}) Print the outputs (just for testing): print codename print normaloutput print erroroutput print codeOut print codeErr And I get nothing. Is there something I am doing wrong? Because I have read the parts on dictionaries and stringio and googled, but I can't find the problem. Any comments, suggestions? Or could someone please tell me if there is something wrong with my code? -- Necmettin Begiter Blog: http://begiter.blogspot.com/feeds/posts/default ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] iterating
1) Unordered maps/collections like sets and dictionaries also seem to support iterating over set members or dictionary keys with "for x in y" syntax. As far as I can tell, the following three ways generally behave the same way, or is there a difference in behavior between: a) for key in dictionary: b) for key in dictionary.keys(): c) mykeys = dictionary.keys() for k in mykeys: 2) The following three ways for manipulating objects in a list with a function will generally do the same thing, right? (but the last two have a shorter syntax). a) newls = [] for entry in mylist: newls.append(function(entry)) b) newlist = map(function, mylist) c) newlist = [function(entry) for entry in mylist] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem with "Hello, World"
So I've written my first python program (the ubiquitous 'Hello, World'). Already, I'm having problems. First, the question I can't find the answer to. Where (exactly) am I supposed to save my files? When I wrote "hello.py" there was no clearly stated "Make sure you save it HERE or else Python won't know where to look for it." In case that won't solve the problem, here are the gory details. I wrote "hello.py" with TextWrangler. I can get it to run from TextWrangler by clicking on the "run in terminal" command. And it runs beautifully. A masterful demonstration of my ability to follow directions. But I can't get it to run directly from Python. If I go to the terminal and type "python hello.py" (which is what the instructions say I should be doing!) I get the following: hello.py Traceback (most recent call last): File "", line 1, in ? NameError: name 'hello' is not defined When I run debugger (in TextWrangler) I get the following: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/pdb.py", line 9, in ? import cmd File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/cmd.py", line 48, in ? import string File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/string.py", line 83, in ? import re as _re File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/re.py", line 5, in ? from sre import * File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/sre.py", line 97, in ? import sre_compile File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/sre_compile.py", line 17, in ? assert _sre.MAGIC == MAGIC, "SRE module mismatch" AssertionError: SRE module mismatch logout [Process completed] All help gratefully taken with both hands! Alex ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with "Hello, World"
On 4/28/07, Alexander Dering <[EMAIL PROTECTED]> wrote: > But I can't get it to run directly from Python. If I go to the terminal and > type "python hello.py" (which is what the instructions say I should be > doing!) I get the following: > > >>> hello.py > Traceback (most recent call last): > File "", line 1, in ? > NameError: name 'hello' is not defined > >>> What you are doing above is typing "python", then - IN python - typing "hello.py". This does not work, since you're in the interpreter and the function "hello.py" is unknown to Python. So try again: "python hello.py" in the command interpreter. Or if you are in the python interpreter, you might try "import hello" and see what happens. -- - Rikard - http://bos.hack.org/cv/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] iterating
Cecilia Alm wrote: > 1) Unordered maps/collections like sets and dictionaries also seem to > support iterating over set members or dictionary keys with "for x in > y" syntax. As far as I can tell, the following three ways generally > behave the same way, or is there a difference in behavior between: > > a) for key in dictionary: > b) for key in dictionary.keys(): > c) mykeys = dictionary.keys() > for k in mykeys: These are equivalent in most common usage but there is a difference which can be significant. Calling dict.keys() creates an actual list with all the keys in it which is not created when you iterate the dictionary directly. In most cases this doesn't matter but for a huge dictionary it might. Also if you are adding or deleting from the dict during the iteration then dict.keys() is safer because the list of keys is created before the add and delete. Using dict.iterkeys() is more equivalent to 'for key in dictionary' because it doesn't create the intermediate list. Of course using c) you will also have the variable mykeys available which is not the case in a) and b) > > 2) The following three ways for manipulating objects in a list with a > function will generally do the same thing, right? (but the last two > have a shorter syntax). > > a) newls = [] > for entry in mylist: > newls.append(function(entry)) > > b) newlist = map(function, mylist) > > c) newlist = [function(entry) for entry in mylist] Yes, pretty much. After a) and c) the entry variable will still be defined. For c) this is considered a bug and IIRC will change with Python 3. IMO modern usage tends toward c). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: iterating
2007/4/30, Kent Johnson <[EMAIL PROTECTED]>: . Also if you are adding or deleting from the dict > during the iteration then dict.keys() is safer because the list of keys > is created before the add and delete. Thanks for the response; by adding and deleting, I assume you refer to adding or deleting keys (rather than changing the value associated with the key). -- E. Cecilia Alm Graduate student, Dept. of Linguistics, UIUC Office: 2013 Beckman Institute -- E. Cecilia Alm Graduate student, Dept. of Linguistics, UIUC Office: 2013 Beckman Institute ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] StringIO and dictionaries
Necmettin Begiter wrote: > I want to run an external python script inside my script. So here is what I > came up with: > > Print codename for testing purposes. > Define an empty dictionary. > Read the file. > Do StringIO assignments and run the code. > Get the outputs and append them to the dictionary. > Print the outputs for testing purposes. > > > The code: > codeOut= {} > codeErr= {} > > Print codename for testing purposes: > print codename > Note: codename is a parameter that is passed to the function for the first > parts (keys) of codeOut and codeErr dictionaries; and it prints the given > name (deneme in this case) correctly. > > Read the file: > localfile= open(filename,'r') > code2run= localfile.readlines() > localfile.close() > > Do StringIO assignments and run the code: > codOut= StringIO.StringIO() > codErr= StringIO.StringIO() > sys.stdout= codOut > sys.stderr= codErr > exec code2run > sys.stdout= sys.__stdout__ > sys.stderr= sys.__stderr__ > > Get the outputs and append them to the dictionary. > erroroutput= codErr.getvalue() > normaloutput= codOut.getvalue() > codeOut.append({codename:erroroutput}) > I'm confused when you "assign" errorputput to codeOut and normaloutput to codeErr. Also unclear why you use dictionaries in the first place. When I run this program from a command prompt I get: File "begiter.py", line 21, in codeOut.append({codename:erroroutput}) AttributeError: 'dict' object has no attribute 'append' However when I run it from my IDE (PythonWin) the traceback does not show. In fact sys.stdout= sys.__stdout__ does not restore the print to the interactive window. So there are 2 problems. Fix the first by either codeOut.update({codename:erroroutput}) or (preferred): codeOut[codename]=normaloutput > codeErr.append({codename:normaloutput}) > > Print the outputs (just for testing): > print codename > print normaloutput > print erroroutput > print codeOut > print codeErr > > And I get nothing. > Is there something I am doing wrong? Because I have read > the parts on dictionaries and stringio and googled, but I can't find the > problem. Any comments, suggestions? Or could someone please tell me if there > is something wrong with my code? > > -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: iterating
Cecilia Alm wrote: > 2007/4/30, Kent Johnson <[EMAIL PROTECTED]>: > . Also if you are adding or deleting from the dict >> during the iteration then dict.keys() is safer because the list of keys >> is created before the add and delete. > > Thanks for the response; by adding and deleting, I assume you refer to > adding or deleting keys (rather than changing the value associated > with the key). Right, I think changing a value will be safe. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] best way to tell if i am connected to the internet
hello there all, i am wondering, what would be the simplest way to get a true/false am i connected to the internet ? right now i am using httplib to fetch a webpage every 20 minutes to see, but i am thinking that there is a better way, any suggestions would be encouraging thanks shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to tell if i am connected to the internet
ping a host on the net if you get an echo response back you are good. better yet ping the host page you are scraping. On 4/30/07, shawn bright <[EMAIL PROTECTED]> wrote: hello there all, i am wondering, what would be the simplest way to get a true/false am i connected to the internet ? right now i am using httplib to fetch a webpage every 20 minutes to see, but i am thinking that there is a better way, any suggestions would be encouraging thanks shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to tell if i am connected to the internet
ok, cool. thanks sk On 4/30/07, Jalil <[EMAIL PROTECTED]> wrote: ping a host on the net if you get an echo response back you are good. better yet ping the host page you are scraping. On 4/30/07, shawn bright < [EMAIL PROTECTED]> wrote: > > hello there all, > > i am wondering, what would be the simplest way to get a true/false > am i connected to the internet ? > > right now i am using httplib to fetch a webpage every 20 minutes to see, > but i am thinking that there is a better way, > > any suggestions would be encouraging > > thanks > shawn > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor