Re: [Tutor] Read from large text file, parse, find string, print string + line number to second text file.
On 02/02/13 04:57, Scurvy Scott wrote: It may just be an email thing but... def main(mystring, infile, outfile): with open('infile', 'r') as inF: for index, line in enumerate(inF): if myString in line: newfile.write(string %s found on line #%d (line, index)) print complete. The print should be inside the function not outside. And main is probably not the best name. You could call it printFoundString or somesuch... 'main' is usually used to collect all the program driver code that you currently have under the if name... test. The stuff you wouldn't ever use if importing as a module. Your way works fine too, a main is not obligatory. :-) if __name__ == '__main__': import sys newfile = open('outfile', 'w') help_text = usage: python scanfile.py STRINGTOSEARCH IMPORTFILENAME OUTPUTFILENAME if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) == 0: The last test should be == 1 since the program name will always be there. But in fact you need the string and file args too so it should really be: len(sys.argv) 4 anything less than 4 args and your code breaks... myString = sys.argv[1] infile = sys.argv[2] outfile = sys.argv[3] HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On 02/02/13 01:47, Jack Little wrote: def simpstart(): global ammo1 global ammo2 global ammo3 global health global tech_parts global exp global radio_parts ammo1=10 ammo2=0 ammo3=0 health=100 tech_parts=0 exp=0 radio_parts=0 This function is completely pointless, you might as well just define the variables at the top level. print You awake in a haze. A crate,a door and a radio. g1 = raw_input(Which do you choose ) if g1 == CRATE or g1 == Crate or g1 == crate: ... elif g1 ==DOOR or g1 == Door or g1 == door: print You are outside elif g1 == RADIO or g1 == Radio or g1 == radio: ... g2 = raw_input(So this is NYC.Ten years after.There are a few streets.Go west or north ) if g2 == WEST or g2 == West or g2 == west: path2_pt1() elif g2 == NORTH or g2 == North or g2 == north: path1pt1() The block above is at top level so Python will execute it as it reads the file. And at this stage pathpt1 does not exist so it fails. You need to move this block into a function (maybe it was intended to be part of the one above but you messed up the indent?). Alternatively you need to move the definition of pathpt1 above this block. def path1pt1(): print This is where it all started. Freedom Tower. A biotech firm called Aesthos Biotechnology. Based here. print I worked there. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
* Simon Yan simon...@fedoraproject.org [2013-02-02 01:11:12 +0800]: I would recommend start off from a simple text editor that has basic syntax highlighting features. There are a number of options out there. TextMate is a good choice, a little pricy. VIM, if you are a terminal guy Even Python IDLE is a good choice you wanted to edit just a few simple .py files. I would suggest give it a look in the Mac App Store and you will find a few other good ones too. I believe the editor of choice on Mac OS X these days is BBEdit - again, a bit expensive. TextMate used to be good but there are better ones out there now. Personally, I prefer the commandline editors, vi or vim which is already installed on Mac OS X. I haven't used the Xcode editor before but if it's what you're comfortable with then it's probably best to stick with it. Primary Key: 4096R/1D31DC38 2011-12-03 Key Fingerprint: A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
hmm ... you should use GNU Emacs, it's Free in price and license! Extensible Text Editor with a cool Python-mode ;-) Jamie Griffin ja...@kode5.net writes: * Simon Yan simon...@fedoraproject.org [2013-02-02 01:11:12 +0800]: I would recommend start off from a simple text editor that has basic syntax highlighting features. There are a number of options out there. TextMate is a good choice, a little pricy. VIM, if you are a terminal guy Even Python IDLE is a good choice you wanted to edit just a few simple .py files. I would suggest give it a look in the Mac App Store and you will find a few other good ones too. I believe the editor of choice on Mac OS X these days is BBEdit - again, a bit expensive. TextMate used to be good but there are better ones out there now. Personally, I prefer the commandline editors, vi or vim which is already installed on Mac OS X. I haven't used the Xcode editor before but if it's what you're comfortable with then it's probably best to stick with it. Primary Key: 4096R/1D31DC38 2011-12-03 Key Fingerprint: A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Aurélien DESBRIÈRES Ride free! Ride GNU.ORG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
Two free good text editors for the MAC are; 1. Komodo 2. Text Wrangler. hth Sydney On 01/02/2013 17:11, Simon Yan wrote: On Fri, Feb 1, 2013 at 9:18 PM, Dustin Guerri dustingue...@gmail.com mailto:dustingue...@gmail.com wrote: Thanks for replying, Simon. I have no particular reason to use Xcode - what would you recommend instead ? I would recommend start off from a simple text editor that has basic syntax highlighting features. There are a number of options out there. TextMate is a good choice, a little pricy. VIM, if you are a terminal guy Even Python IDLE is a good choice you wanted to edit just a few simple .py files. I would suggest give it a look in the Mac App Store and you will find a few other good ones too. *Dustin Guerri* Mobile : (+ 34) 625 857 967 tel:%28%2B%2034%29%20625%20857%20967 dustingue...@gmail.com mailto:dustingue...@gmail.com LinkedIn http://www.linkedin.com/profile/view?id=16395850trk=tab_pro Contact me: Google Talk dustinguerri Skype dustinguerri Get a signature like this. http://r1.wisestamp.com/r/landing?promo=20dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_20 CLICK HERE. http://r1.wisestamp.com/r/landing?promo=20dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_20 On 1 February 2013 13:05, Simon Yan simon...@fedoraproject.org mailto:simon...@fedoraproject.org wrote: On Thu, Jan 17, 2013 at 6:47 AM, Dustin Guerri dustingue...@gmail.com mailto:dustingue...@gmail.com wrote: Hi there, I'm trying to create a plain text file called hello.py with the following text : print('hello world') raw_input('Press any key to continue') I'd then like to run this program with Python Launcher on a Mac. I'd lke to use Xcode as my text editor. Once I have Xcode open, which template should I use to input the text ? I don't think there is a file type of Python that you can create from Xcode. Just curious, why would you want to use XCode as a Python editor? Thanks, *Dustin Guerri* Mobile : (+ 34) 625 857 967 tel:%28%2B%2034%29%20625%20857%20967 dustingue...@gmail.com mailto:dustingue...@gmail.com | www.vimeo.com/dustinguerri/pop http://www.vimeo.com/dustinguerri/pop LinkedIn http://www.linkedin.com/profile/view?id=16395850trk=tab_pro Contact me: Google Talk dustinguerri Skype dustinguerri Get a signature like this. http://r1.wisestamp.com/r/landing?promo=20dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_20 CLICK HERE. http://r1.wisestamp.com/r/landing?promo=20dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_20 ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Regards, YeeYaa (Simon Yan) http://simonyan.fedorapeople.org/ -- Regards, YeeYaa (Simon Yan) http://simonyan.fedorapeople.org/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
Text Wrangler is a free, cut-down version of BB-Edit, I think. Sydney On 02/02/2013 09:31, Jamie Griffin wrote: * Simon Yan simon...@fedoraproject.org [2013-02-02 01:11:12 +0800]: I would recommend start off from a simple text editor that has basic syntax highlighting features. There are a number of options out there. TextMate is a good choice, a little pricy. VIM, if you are a terminal guy Even Python IDLE is a good choice you wanted to edit just a few simple .py files. I would suggest give it a look in the Mac App Store and you will find a few other good ones too. I believe the editor of choice on Mac OS X these days is BBEdit - again, a bit expensive. TextMate used to be good but there are better ones out there now. Personally, I prefer the commandline editors, vi or vim which is already installed on Mac OS X. I haven't used the Xcode editor before but if it's what you're comfortable with then it's probably best to stick with it. Primary Key: 4096R/1D31DC38 2011-12-03 Key Fingerprint: A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
Dear Aurelien, Would you please explain how one installs GNU Emacs on a MAC using OS X v10.6. I cannot find a binary package. The GNU site seems to me to have only source code packages. Mille fois merci. Sydney On 02/02/2013 09:54, Aurélien DESBRIÈRES wrote: hmm ... you should use GNU Emacs, it's Free in price and license! Extensible Text Editor with a cool Python-mode ;-) Jamie Griffin ja...@kode5.net writes: * Simon Yan simon...@fedoraproject.org [2013-02-02 01:11:12 +0800]: I would recommend start off from a simple text editor that has basic syntax highlighting features. There are a number of options out there. TextMate is a good choice, a little pricy. VIM, if you are a terminal guy Even Python IDLE is a good choice you wanted to edit just a few simple .py files. I would suggest give it a look in the Mac App Store and you will find a few other good ones too. I believe the editor of choice on Mac OS X these days is BBEdit - again, a bit expensive. TextMate used to be good but there are better ones out there now. Personally, I prefer the commandline editors, vi or vim which is already installed on Mac OS X. I haven't used the Xcode editor before but if it's what you're comfortable with then it's probably best to stick with it. Primary Key: 4096R/1D31DC38 2011-12-03 Key Fingerprint: A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
Join #emacs on irc.freenode.net and ask them. I do not bring any support for non free Operating System nor BrainWashing ones ;-) Shall, Sydney sydney.sh...@kcl.ac.uk writes: Dear Aurelien, Would you please explain how one installs GNU Emacs on a MAC using OS X v10.6. I cannot find a binary package. The GNU site seems to me to have only source code packages. Mille fois merci. Sydney On 02/02/2013 09:54, Aurélien DESBRIÈRES wrote: hmm ... you should use GNU Emacs, it's Free in price and license! Extensible Text Editor with a cool Python-mode ;-) Jamie Griffin ja...@kode5.net writes: * Simon Yan simon...@fedoraproject.org [2013-02-02 01:11:12 +0800]: I would recommend start off from a simple text editor that has basic syntax highlighting features. There are a number of options out there. TextMate is a good choice, a little pricy. VIM, if you are a terminal guy Even Python IDLE is a good choice you wanted to edit just a few simple .py files. I would suggest give it a look in the Mac App Store and you will find a few other good ones too. I believe the editor of choice on Mac OS X these days is BBEdit - again, a bit expensive. TextMate used to be good but there are better ones out there now. Personally, I prefer the commandline editors, vi or vim which is already installed on Mac OS X. I haven't used the Xcode editor before but if it's what you're comfortable with then it's probably best to stick with it. Primary Key: 4096R/1D31DC38 2011-12-03 Key Fingerprint: A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Aurélien DESBRIÈRES Ride free! Ride GNU.ORG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read from large text file, parse, find string, print string + line number to second text file.
I'd suggest having the newfile open after outfile is defined also a close statement on newfile - or use it with 'with' such as: unchanged code except removing newfile = open('outfile', 'w') line ... and replace the last line like so: with open(outfile, 'w') as newfile: main(mystring, infile, newfile) (and looking muchly improved, well done) Nick On Fri, Feb 1, 2013 at 8:57 PM, Scurvy Scott etanes...@gmail.com wrote: And just for the records sake, this is what I've gotten and you guys should see obviously that you helped a lot and I learned a thing or two so I won't have to ask the same silly questions next time: def main(mystring, infile, outfile): with open('infile', 'r') as inF: for index, line in enumerate(inF): if myString in line: newfile.write(string %s found on line #%d (line, index)) print complete. if __name__ == '__main__': import sys newfile = open('outfile', 'w') help_text = usage: python scanfile.py STRINGTOSEARCH IMPORTFILENAME OUTPUTFILENAME if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) == 0: print (help_text) sys.exit() myString = sys.argv[1] infile = sys.argv[2] outfile = sys.argv[3] main(mystring, infile, outfile) Look right to you? Looks okay to me, except maybe the three ORs in the information line, is there a more pythonic way to accomplish that task? Scott On Fri, Feb 1, 2013 at 8:31 PM, Scurvy Scott etanes...@gmail.com wrote: Best practice is to check if your program is being run as a script before doing anything. That way you can still import the module for testing or similar: def main(mystring, infile, outfile): # do stuff here if __name__ == '__main__': # Running as a script. import sys mystring = sys.argv[1] infile = sys.argv[2] outfile = sys.argv[3] main(mystring, infile, outfile) Best practice for scripts (not just Python scripts, but *any* script) is to provide help when asked. Insert this after the import sys line, before you start processing: if '-h' in sys.argv or '--help' in sys.argv: print(help_text) sys.exit() If your argument processing is more complicated that above, you should use one of the three argument parsing modules that Python provides: http://docs.python.org/2/library/getopt.html http://docs.python.org/2/library/optparse.html (deprecated -- do not use this for new code) http://docs.python.org/2/library/argparse.html getopt is (in my opinion) the simplest to get started, but the weakest. There are also third-party argument parsers that you could use. Here's one which I have never used but am intrigued by: http://docopt.org/ -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Steve- thanks a lot for showing me the if __name__ = main part I've often wondered how it was used and it didn't make sense until I saw it in my own code if that makes any sense. Also appreciate the help on the instructional side of things. One question related to the instruction aspect- does this make sense to you? If len(sys.argv) == 0: print usage: etc etc etc Nick, Dave, and Steve, again, you guys are awesome. Thanks for all your help. Scott ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
You are correct, of course. OK. So Bluefish is the Free open-source GNU editor and it is suitable for Python. We will have a complete menagerie soon. Thanks. Sydney On 02/02/2013 13:23, Aurélien DESBRIÈRES wrote: Join #emacs on irc.freenode.net and ask them. I do not bring any support for non free Operating System nor BrainWashing ones ;-) Shall, Sydney sydney.sh...@kcl.ac.uk writes: Dear Aurelien, Would you please explain how one installs GNU Emacs on a MAC using OS X v10.6. I cannot find a binary package. The GNU site seems to me to have only source code packages. Mille fois merci. Sydney On 02/02/2013 09:54, Aurélien DESBRIÈRES wrote: hmm ... you should use GNU Emacs, it's Free in price and license! Extensible Text Editor with a cool Python-mode ;-) Jamie Griffin ja...@kode5.net writes: * Simon Yan simon...@fedoraproject.org [2013-02-02 01:11:12 +0800]: I would recommend start off from a simple text editor that has basic syntax highlighting features. There are a number of options out there. TextMate is a good choice, a little pricy. VIM, if you are a terminal guy Even Python IDLE is a good choice you wanted to edit just a few simple .py files. I would suggest give it a look in the Mac App Store and you will find a few other good ones too. I believe the editor of choice on Mac OS X these days is BBEdit - again, a bit expensive. TextMate used to be good but there are better ones out there now. Personally, I prefer the commandline editors, vi or vim which is already installed on Mac OS X. I haven't used the Xcode editor before but if it's what you're comfortable with then it's probably best to stick with it. Primary Key: 4096R/1D31DC38 2011-12-03 Key Fingerprint: A4B9 E875 A18C 6E11 F46D B788 BEE6 1251 1D31 DC38 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
On 02/02/13 12:57, Shall, Sydney wrote: Dear Aurelien, Would you please explain how one installs GNU Emacs on a MAC using OS X v10.6. Last time I looked it was already installed. Just type emacs at a Terminal prompt. You can also get a Cocoa version that run in a separate Window, try Google for Cocoa emacs... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
On 02/02/13 15:27, Shall, Sydney wrote: OK. So Bluefish is the Free open-source GNU editor and it is suitable for Python. No thats emacs. Bluefish is an open source web editor(HTML, CSS etc). It may support Python but its not an ideal IDE. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
Thanks for both your comments, Alan. I am wiser now. Sydney On 02/02/2013 17:50, Alan Gauld wrote: On 02/02/13 15:27, Shall, Sydney wrote: OK. So Bluefish is the Free open-source GNU editor and it is suitable for Python. No thats emacs. Bluefish is an open source web editor(HTML, CSS etc). It may support Python but its not an ideal IDE. -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Python Test
* Alan Gauld alan.ga...@btinternet.com [2013-02-02 17:49]: On 02/02/13 12:57, Shall, Sydney wrote: Dear Aurelien, Would you please explain how one installs GNU Emacs on a MAC using OS X v10.6. Last time I looked it was already installed. Just type emacs at a Terminal prompt. Verified on 10.6.8 -- David Rock da...@graniteweb.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor