Re: [Tutor] 'open' is not defined
On Thu, Jul 30, 2015 at 11:41:56PM +, ltc.hots...@gmail.com wrote: > Hi Everyone: > > > Why is open not defined in the following code:NameError: name 'open' is not > defined Are you still running your code on the PythonTutor website? http://pythontutor.com/visualize.html says in the fine-print at the bottom of the page: "Online Python Tutor currently supports five languages (despite its name!): 1. Python 2.7 and 3.3 with limited module imports and no file I/O." So open() is not defined because the Online Python Tutor website has removed it. By the way, I tried working with that website for a little while. It is *amazingly* slow and clunky and painful to use on my computer. I don't know how you can bear to use it. Have you considered installing Python on your computer and working directly on that? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
> > So main.py contains: > > def get_field(value, start_bit, end_bit): > > > and I see: > > >>> import main > >>> help(get_field) > Traceback (most recent call last): >File "", line 1, in > NameError: name 'get_field' is not defined > > > help(main) works ok but is rather verbose. Try: import main help(main.get_field) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'open' is not defined
On 7/30/2015 4:41 PM, ltc.hots...@gmail.com wrote: Hi Everyone: Why is open not defined in the following code:NameError: name 'open' is not defined Because of something you did previously. We don't have enough information to answer. open exists as a built-in function in python: Python 2.5 (r25:51908, Dec 18 2009, 14:22:21) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> open We'd need to see a full cut-n-paste example to help diagnose what's actually happening. I can get a NameError with: >>> del open Traceback (most recent call last): File "", line 1, in NameError: name 'open' is not defined >>> So, don't try to delete built-in identifiers. For example, when I paste your code from below into a python shell, I get: Python 2.5 (r25:51908, Dec 18 2009, 14:22:21) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> fname = raw_input("Enter file name: ") Enter file name: if len(fname) < 1 : fname = "mbox-short.txt" >>> fh = open(fname) Traceback (most recent call last): File "", line 1, in IOError: [Errno 2] No such file or directory: 'if len(fname) < 1 : fname = "mbox-short.txt"' >>> count = 0 >>> for line in fh: Please paste in the contents from your screen where the error appears. Note that in my example the open line complains about fname not existing so I'm getting and IOError, and if the issue were with open, as per the example with xxopen below, you then get the NameError. >>> fh = xxopen('yyy') Traceback (most recent call last): File "", line 1, in NameError: name 'xxopen' is not defined Please post the equivalent from your system and we can help better. Emile Code reads as follows: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" Regards, Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
On 30/07/2015 23:34, ltc.hots...@gmail.com wrote: sure Sent from Surface From: Mark Lawrence Sent: Thursday, July 30, 2015 3:25 PM To: Tutor@python.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Could have fooled me :( -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] 'open' is not defined
Hi Everyone: Why is open not defined in the following code:NameError: name 'open' is not defined Code reads as follows: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" Regards, Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
On 30/07/15 23:51, ltc.hots...@gmail.com wrote: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[2] Note that you are overwriting sender each time through the loop. Also [2] isa the third element, I think you want the second [1] BTW Its probably clearer to write that last line as: if line.startswith('From') and '@' in line: sender = line.split()[1] Better still may be to split the line first: sections = line.split() if 'FROM' in sections[0].upper() and '@' in sections: sender = sections[1] print sender And this is outside the loop so will only print the last item. To print all of them you need to move the print inside the loop. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
On 30/07/15 22:17, ltc.hots...@gmail.com wrote: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] fn.seek(0) print sender Questions: Why is the loop not repeating, What makes you think so? If you get an error(as I suspect) please post the entire error message. I would expect a name error on the last line of the loop since there is no variable fn defined. I don't know what you think the seek() is doing, but (assuming you meant fh) it will reset the file to the first line each time so you never finish the loop. and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' From stephen.marqu...@uct.ac.za Sat Jan 5 09:14:16 2008 ← Mismatch Splitting on whitespace will ensure the bit you want is in the second element -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Hi Mark, I’m still confused because line 4 reads: fh=open(fname,'r') # Open a new file handle, not fn = open(fname) Therefore, can you write down line by line from error to correction? Here is the revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[2] print sender Regards, Hal Sent from Surface ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
sure Sent from Surface From: Mark Lawrence Sent: Thursday, July 30, 2015 3:25 PM To: Tutor@python.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Hi everyone, Revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] fn.seek(0) print sender Questions: Why is the loop not repeating, and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' From stephen.marqu...@uct.ac.za Sat Jan 5 09:14:16 2008 ← Mismatch Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 12:07 PM To: Tutor@python.org Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 11:47 AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.split()[0]: count += 1 print "There are %d lines starting with From" % count print len(line) fn = open(fname) print "There were", count, "lines in the file with From as the first word" Syntax message produced by iPython interperter: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in () 6 print "There are %d lines starting with From" % count 7 print len(line) > 8 fn = open(fname) 9 print "There were", count, "lines in the file with From as the first wor d" NameError: name 'fname' is not defined In [16]: Question: Why is fname = "mbox-short.txt" not loading the sample data? Sample data file is located at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface From: Steven D'Aprano Sent: Wednesday, July 29, 2015 7:42 AM To: ltc.hots...@gmail.com Cc: Tutor@python.org On Tue, Jul 28, 2015 at 11:33:53PM +, ltc.hots...@gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and > sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
On 30/07/2015 20:07, ltc.hots...@gmail.com wrote: When you post here can you please find a mechanism that gives us more text than whitespace, thank you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Attribute
Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 11:47 AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.split()[0]: count += 1 print "There are %d lines starting with From" % count print len(line) fn = open(fname) print "There were", count, "lines in the file with From as the first word" Syntax message produced by iPython interperter: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in () 6 print "There are %d lines starting with From" % count 7 print len(line) > 8 fn = open(fname) 9 print "There were", count, "lines in the file with From as the first wor d" NameError: name 'fname' is not defined In [16]: Question: Why is fname = "mbox-short.txt" not loading the sample data? Sample data file is located at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface From: Steven D'Aprano Sent: Wednesday, July 29, 2015 7:42 AM To: ltc.hots...@gmail.com Cc: Tutor@python.org On Tue, Jul 28, 2015 at 11:33:53PM +, ltc.hots...@gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and > sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
If I have a script called main.py and document a function in it: def get_value(x): """ Some text ... :param x: Some value :returns: Something useful """ What is the most basic way of showing those docstrings at the Python prompt? >>> >>> Try: >>> >>>help(get_value) >>> >>> At the Python prompt. This uses the built in help facility: >>> >>> https://docs.python.org/2/library/functions.html#help >>> >> >> I understand that 'help' works with modules that I have imported. But if >> I've just written a script called main.py (which contains get_value()) I >> don't think I can 'import' that. So how would I see the docstrings in >> main.py? >> > Try it! you'll find it works fine So main.py contains: def get_field(value, start_bit, end_bit): and I see: >>> import main >>> help(get_field) Traceback (most recent call last): File "", line 1, in NameError: name 'get_field' is not defined help(main) works ok but is rather verbose. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
On Thu, Jul 30, 2015 at 04:28:33PM +, David Aldrich wrote: > So main.py contains: > > def get_field(value, start_bit, end_bit): > > > and I see: > > >>> import main > >>> help(get_field) > Traceback (most recent call last): >File "", line 1, in > NameError: name 'get_field' is not defined Naturally. Name resolution works the same for help() as for anything else. If you say: import math then you need to refer to math.sin and math.cos, not sin or cos on their own. The same applies to your main.get_field, whether you are using help() or not. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
On Thu, Jul 30, 2015 at 08:24:27AM +, David Aldrich wrote: > I understand that 'help' works with modules that I have imported. But > if I've just written a script called main.py (which contains > get_value()) I don't think I can 'import' that. So how would I see the > docstrings in main.py? import main help(main) Of course, you have to write main.py so it is intended to be imported. If you write main.py as a regular script, then importing it the first time will run the script, which you don't want. The trick is to put the script functionality in a function, say: # File script.py def do_this(): """This is a helper function""" ... def main(): """Main function.""" print("Processing...") do_this() do_that() do_something_else() print("Done!") if __name__ == "__main__": # Run the main script. main() By using that pattern, the file "script.py" can be imported at the interactive interpreter: import script help(script.main) help(script.do_this) # etc. but of you run it directly, outside the interactive intepreter, the main() function will run and it will behave as a script as expected. The reason this works is that when you run a script, Python sets the magic variable __name__ to "__main__". But when you import it, the variable is set to the actual name of the file (minus the .py extension). -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
On Thu, Jul 30, 2015 at 4:24 AM, David Aldrich wrote: >>> If I have a script called main.py and document a function in it: >>> >>> def get_value(x): >>> """ >>> Some text ... >>> :param x: Some value >>> :returns: Something useful >>> """ >>> >>> What is the most basic way of showing those docstrings at the Python >>> prompt? >> >> Try: >> >>help(get_value) >> >> At the Python prompt. This uses the built in help facility: >> >> https://docs.python.org/2/library/functions.html#help >> > > I understand that 'help' works with modules that I have imported. But if I've > just written a script called main.py (which contains get_value()) I don't > think I can 'import' that. So how would I see the docstrings in main.py? > Try it! you'll find it works fine -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic question about docstrings
>> If I have a script called main.py and document a function in it: >> >> def get_value(x): >> """ >> Some text ... >> :param x: Some value >> :returns: Something useful >> """ >> >> What is the most basic way of showing those docstrings at the Python >> prompt? > > Try: > >help(get_value) > > At the Python prompt. This uses the built in help facility: > > https://docs.python.org/2/library/functions.html#help > I understand that 'help' works with modules that I have imported. But if I've just written a script called main.py (which contains get_value()) I don't think I can 'import' that. So how would I see the docstrings in main.py? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Socket Module
Writing to share an update on my previous request. So, after reviewing my code over, it seems like my last print statement "print(ResolvedAddresses)" was not properly indented inside the for loop - and for that reason the output was coming our as empty. After I aligned that with the rest of the loop, it worked fine. I still have couple parts to add to this project but at least this hurdle is behind. Thank you. On Tuesday, July 28, 2015 4:49 AM, Nym City via Tutor wrote: Hi Martin, Thank you for taking the time to write such a detailed response. Very much appreciate it. I tried the code again with modifications that you suggested and even though none of the public addresses resolved; I did get little more details. I am still working on finding a solution for this and I will share it here when I have it. Thank you. On Sunday, July 26, 2015 6:59 PM, Martin A. Brown wrote: Hello Nym, > Here is the updated code: https://bpaste.net/show/358583e1a0bd It's short. I have included inline here: import socket ListOfIPAddresses = [] with open('top500ips.csv', 'r') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address) except: print('No Resolution Available') print(ResolvedAddresses) > The issue that I am running into now is that for some reason, the > script is not resolving known-public IP addresses that I am > passing through. For example, some of the IPs, that I have used > are for sites like facebook (173.252.120.6) github > (207.97.227.239), however the script is not able to resolve them. > > But its interesting that I am able to resolve them using nslookup > on windows command prompt. Which means my laptop's DNS setting is > fine. The apparent (?) DNS lookup failure --- At time X, you run your Python program and something (perhaps in the DNS resolution process) fails and you see "No Resolution Available", but you do not know what has failed, nor for which address lookup. At time Y, you run 'nslookup' at the shell prompt, receive an answer and conclude that your script is operating properly. While this is may appear logical, it is an incorrect conclusion. One coding error (a dangerous habit to perpetuate) -- When performing the DNS lookup, you are using something called a 'bare except'. This will catch any and all errors, even if it's something unrelated like, for example, a signal. This is a bad and dangerous habit. In general, you should catch only the exceptions that you can do something about. In addition, this will offer you more information about the problem. Here's a simple example, where I'm only changing two lines: for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address) except socket.herror as e: print("No resolution available for %s: %s" % (address, e)) This will give you a little more information about what, specifically, the failure is in your call to socket.gethostbyaddr() Comment on NXdomain responses - I picked at random an address that had no PTR record and tried to call socket.gethostbyaddr('8.97.227.2'). What do you think I got for my trouble? When running through the code block above, I saw the following output to my terminal: No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error) In short, there is no guarantee that anybody has properly set up reverse DNS entries (DNS PTR records) for the addresses you are looking up. Although the vast majority of lookups occur successfully and smoothly, there are many things that can go wrong in the network and on an end host which can cause transient errors during DNS lookups, and it is possible that you have already encountered some of these problems (even though I would not expect to hit very many errors looking up PTR records for only 500 IPs). May I wish you good luck resolving not just your addresses, but also your problem! -Martin -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Root and power
Thank you all,\. Programming is the hardest thing i have ever tried to learn. So thank you for your help. Job On Tue, Jul 28, 2015 at 8:29 PM, Job Hernandez wrote: > How is it going tutors? > > The following problem seems impossible to me: > > "*Write a program that asks the user to enter an integer and prints two > integers, root and pwr, such that 0 < pwr < 6 and root^pwr (root**pwr) is > equal to the integer entered by the user. If no such pair of integers > exists, it should print a message to that effect*." > > I would like to solve this problem myself so please don't give me the > solution. > > I need to learn how in the world do find the root and power of an integer > that x user entered? I haven been looking on the python website for an > appropriate function but I have not. > > If you have the time can you please tell me about the functions and other > facts I need to know in order to solve this problem? > > Is there a book you guys recommend for total beginners who have no ideal > of what computer science and programming is? > > Thank you, > > Job > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mailbox
On 29/07/15 22:55, ltc.hots...@gmail.com wrote: #top of code, initialize variable output_list = ["default"] #rest of code print output_list ['default'] Raw Data File: Note, this is the code not the data... count = 0 fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" for line in fname: You are still looping over the filename not the file contents. thus line will take the values: m, b, ,o, x -, s, h, line = line.strip() if not line.startswith('From '): continue And this will always be true so the loop will stop here. line = line.split() count = count + 1 this is outside the loop so will always be 1 print len(line) fh = open(fname) this opens the file but you do nothing with it. print "There were", count, "lines in the file with From as the first word" You do not seem to have changed anything from your original post. All the same errors are still there. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor