Re: [Tutor] Need to be able to accept Page Down or CTRL-E

2013-02-01 Thread Alan Gauld

On 01/02/13 03:19, Dave Wilder wrote:


So how are you taking this input in?


I am using a terminal application and an application called pexpect


OK, We need to get the terminology a lot more exact.

I'm guessing that what you mean is that you have an application that 
somebody else wrote running in a terminal session and that you are 
writing a program to communicate with that application using the pexpect 
module. Is that correct?


> (instead of raw_input) where  I send something to the console

and then look for a result,


When you say you send "something" to the console I'm guessing you mean 
you send a command string to the external application via pexpect
and then read the response from the application  coming back via 
pexpect? Is that correct?


> such as  or UP/DOWN arrows.

If I got the previous guesses right then this is, I suspect, impossible.
Pagedown and page up are terminal control characters that are not part 
of the output that would be sent to pexpect. pexpect reads the monitored 
session's stdin/stdout. There are some exceptional cases where you might 
see those characters but in most cases they will be swallowed by the app 
before pexpect ever sees them.


OTOH if you mean you want to send the page up/down/arrows as the 
"something" then its a different game and you can do it. In that case 
you need todo what you did with ESC. The character set your external app 
uses will determine the codes you send. Assuming its the same set as 
your local setup you can use the following program(untested!) to read 
the codes from your keyboard (assuming you are on Windows):


import msvcrt

print "Hit space to end..."
print

while True:
   ky = msvcrt.getch()
   length = len(ky)
   if length != 0:
  if ky == " ": # check for quit event
 print ord(ky)
 raise SystemExit
  else:
 if ky == '\x00' or ky == '\xe0': # non ASCII
 ky = msvcrt.getch() # fetch second character
 print ord(ky),

There is also a curses version for Linux/MacOS on my tutor
topic page for event driven programming...



I'm not sure if pexpect is standard Python application.


It's not an application it's a module. If you are using an app called 
pexpect then its not part of Python (although it may still be written in 
Python!)


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] Need to be able to accept Page Down or CTRL-E

2013-02-01 Thread Dave Angel

On 02/01/2013 03:34 AM, Alan Gauld wrote:


  

OTOH if you mean you want to send the page up/down/arrows as the
"something" then its a different game and you can do it. In that case
you need todo what you did with ESC. The character set your external app
uses will determine the codes you send. Assuming its the same set as
your local setup you can use the following program(untested!) to read
the codes from your keyboard (assuming you are on Windows):

import msvcrt

print "Hit space to end..."
print

while True:
ky = msvcrt.getch()


The OP is running Python 2.73 on Linux.

--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need to be able to accept Page Down or CTRL-E

2013-02-01 Thread Dave Wilder


-Original Message-
From: Tutor [mailto:tutor-bounces+d.wilder=f5@python.org] On Behalf Of Dave 
Angel
Sent: Friday, February 01, 2013 7:38 AM
To: tutor@python.org
Subject: Re: [Tutor] Need to be able to accept Page Down or CTRL-E

On 02/01/2013 03:34 AM, Alan Gauld wrote:
>>
>>   
>>
>> OTOH if you mean you want to send the page up/down/arrows as the 
>> "something" then its a different game and you can do it. In that case 
>> you need todo what you did with ESC. The character set your external 
>> app uses will determine the codes you send. Assuming its the same set 
>> as your local setup you can use the following program(untested!) to 
>> read the codes from your keyboard (assuming you are on Windows):
>>
>> import msvcrt
>>
>> print "Hit space to end..."
>> print
>>
>> while True:
>> ky = msvcrt.getch()

> The OP is running Python 2.73 on Linux.

Okay, thanks Alan and Dave.  I will look into this a little more and if need 
be, email the list again but taking Dave's advice and start over w/ a clearer 
problem statement.

Dave

--
DaveA
___
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

2013-02-01 Thread Simon Yan
On Fri, Feb 1, 2013 at 9:18 PM, Dustin Guerri 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
> dustingue...@gmail.com
> [image: 
> LinkedIn]
>
> Contact me: [image: Google Talk] dustinguerri [image: Skype] dustinguerri
>   Get a signature like this.
> 
>  CLICK
> HERE.
>
>
>
> On 1 February 2013 13:05, Simon Yan  wrote:
>
>>
>>
>>
>> On Thu, Jan 17, 2013 at 6:47 AM, Dustin Guerri 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
>>> dustingue...@gmail.com | www.vimeo.com/dustinguerri/pop
>>> [image: 
>>> LinkedIn]
>>> Contact me: [image: Google Talk] dustinguerri [image: Skype]dustinguerri
>>>   Get a signature like this.
>>> 
>>>  CLICK
>>> HERE.
>>>
>>>
>>> ___
>>> Tutor maillist  -  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


[Tutor] Read from large text file, parse, find string, print string + line number to second text file.

2013-02-01 Thread Scurvy Scott
Hey all how're things?

I'm hoping for some guidance on a problem I'm trying to work through.
I know this has been previously covered on this list but I'm hoping it
won't bother you guys to run through it again.

My basic program I'm attempting to create is like this..

I want to read from a large, very large file.
I want to find a certain string
if it finds the string I would like to select the first 15-20
characters pre and proceeding the string and then output that new
string to a new file along with the line the string was located on
within the file.

It seems fairly straight forward but I'm wondering if y'all can point
me to a direction that would help me accomplish this..

Firstly I know I can read a file and search for the string with (a
portion of this code was found on stackoverflow and is not mine and
some of it is my own)

with open('largeFile', 'r') as inF:
for line in inF:
myString = "The String"
if 'myString' in line:
f = open(thenewfile', 'w')
f.write(myString)
f.close()

I guess what I'm looking for then is tips on A)My stated goal of also
writing the 15-20 characters before and after myString to the new file
and
B)finding the line number and writing that to the file as well.

Any information you can give me or pointers would be awesome, thanks in advance.

I'm on Ubuntu 12.10 running LXDE and working with Python 2.7

Scott
___
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.

2013-02-01 Thread Nick W
quite easy to do; just use enumerate - as so:
myString = "The String"
with open('largeFile', 'r') as inF:
for index, line in enumerate(inF):
#myString = "The String" ##Not here because otherwise this gets run
for every single line of the large file (which is nasty waste of resources)
if 'myString' in line:
with open(thenewfile', 'w') as f:
f.write("Line #%d has string: %s"  (index, line))
That will print the whole line into the new file, If you only want the
characters before that use find and take a slice of the line instead.
HTH
Nick


On Fri, Feb 1, 2013 at 12:09 PM, Scurvy Scott  wrote:

> Hey all how're things?
>
> I'm hoping for some guidance on a problem I'm trying to work through.
> I know this has been previously covered on this list but I'm hoping it
> won't bother you guys to run through it again.
>
> My basic program I'm attempting to create is like this..
>
> I want to read from a large, very large file.
> I want to find a certain string
> if it finds the string I would like to select the first 15-20
> characters pre and proceeding the string and then output that new
> string to a new file along with the line the string was located on
> within the file.
>
> It seems fairly straight forward but I'm wondering if y'all can point
> me to a direction that would help me accomplish this..
>
> Firstly I know I can read a file and search for the string with (a
> portion of this code was found on stackoverflow and is not mine and
> some of it is my own)
>
> with open('largeFile', 'r') as inF:
> for line in inF:
> myString = "The String"
> if 'myString' in line:
> f = open(thenewfile', 'w')
> f.write(myString)
> f.close()
>
> I guess what I'm looking for then is tips on A)My stated goal of also
> writing the 15-20 characters before and after myString to the new file
> and
> B)finding the line number and writing that to the file as well.
>
> Any information you can give me or pointers would be awesome, thanks in
> advance.
>
> I'm on Ubuntu 12.10 running LXDE and working with Python 2.7
>
> 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] Read from large text file, parse, find string, print string + line number to second text file.

2013-02-01 Thread Dave Angel

On 02/01/2013 03:09 PM, Scurvy Scott wrote:

Hey all how're things?

I'm hoping for some guidance on a problem I'm trying to work through.
I know this has been previously covered on this list but I'm hoping it
won't bother you guys to run through it again.

My basic program I'm attempting to create is like this..

I want to read from a large, very large file.
I want to find a certain string
if it finds the string I would like to select the first 15-20
characters pre and proceeding the string and then output that new
string to a new file along with the line the string was located on
within the file.


Why not just use grep ?



It seems fairly straight forward but I'm wondering if y'all can point
me to a direction that would help me accomplish this..

Firstly I know I can read a file and search for the string with (a
portion of this code was found on stackoverflow and is not mine and
some of it is my own)



First, you probably want to do something to quit when you get your first 
match.  If you do want to continue finding matches, then you'd have to 
change the location of that open() on the newfile.  Currently, it'll 
throw out any earlier contents, and just write the match.


The linenum is easy, using enumerate.


with open('largeFile', 'r') as inF:
 for line in inF:


   for linenum, line in enumerate(inF):



 myString = "The String"


This should be moved to a location before the loop;  it's a waste 
reassigning it every time through the loop.



 if 'myString' in line:
 f = open(thenewfile', 'w')
 f.write(myString)
 f.close()


   break #quit upon first match



I guess what I'm looking for then is tips on A)My stated goal of also
writing the 15-20 characters before and after myString to the new file
and
B)finding the line number and writing that to the file as well.

Any information you can give me or pointers would be awesome, thanks in advance.

I'm on Ubuntu 12.10 running LXDE and working with Python 2.7



About giving the 15 characters before and after the match:

Is it sufficient to truncate that spec at the line boundaries?  What I 
mean is that if the match occurs at column 10, do you really need the 
last 5 characters of the previous line?   Likewise, if it occurs near 
the end of the line, do you need some from the next line(s) ?



If you never need to show more than the current line, then you can parse 
the line (write a separate function).  If you have to go 15 characters 
earlier in the file, then consider using file.seek


http://docs.python.org/2/library/stdtypes.html?highlight=seek#file.seek

The catch to that is that it messes up the position in the file, so if 
you do want multiple matches, you'll need to use file.tell to save and 
restore the location to continue reading lines.


Lots of other options, but it all depends on what you REALLY want.



--
DaveA
___
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.

2013-02-01 Thread Scurvy Scott
>
> Why not just use grep ?
>
Honestly this seemed like a good excuse to write some code and learn
some stuff, secondly, it honestly just didn't dawn on me.

Also, the code y'all both showed me was exactly what I was looking for
and I'm planning on using Nicks code as a template to improve upon.

As always, thank you guys a lot, it usually only takes a couple of
emails from y'all to get my brain working correctly.

Be safe,
Scott
___
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.

2013-02-01 Thread Scurvy Scott
One last question on this topic..

I'd like to call the files and the string form the command line like

Python whatever.py STRINGTOSEARCH NEWFILE FILETOOPEN

My understanding is that it would be accomplished as such

import sys

myString = sys.argv[1]
filetoopen = sys.argv[2]
newfile = sys.argv[3]


ETC ETC CODE HERE

Is this correct/pythonic? Is there a more recommended way? Am I retarded?

Thanks again in advance,
Scott
___
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.

2013-02-01 Thread Dave Angel

On 02/01/2013 04:24 PM, Scurvy Scott wrote:

One last question on this topic..

I'd like to call the files and the string form the command line like

Python whatever.py STRINGTOSEARCH NEWFILE FILETOOPEN

My understanding is that it would be accomplished as such

import sys

myString = sys.argv[1]
filetoopen = sys.argv[2]
newfile = sys.argv[3]


Notice that you have 2 and 3 reversed from your description above. 
Neither one is wrong, but you have to pick one of them.


If it were my utility, I'd not specify any outfile on the commandline, 
but use redirection as is normal in Linux/Unix/Windows.  In other words, 
just use print as your output, and don't have an argument for the output 
file.






ETC ETC CODE HERE



If it were my code, I'd be first checking the number of arguments ( 
len(sys.argv) ).  If the number isn't exactly right, print a usage 
string and exit the program.


If your commandline gets any more complex, consider using the argparse 
module.


http://docs.python.org/2/library/argparse.html?highlight=argparse#argparse

--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] First Python Test

2013-02-01 Thread Simon Yan
On Thu, Jan 17, 2013 at 6:47 AM, Dustin Guerri 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
> dustingue...@gmail.com | www.vimeo.com/dustinguerri/pop
> [image: 
> LinkedIn]
> Contact me: [image: Google Talk] dustinguerri [image: Skype] dustinguerri
>   Get a signature like this.
> 
>  CLICK
> HERE.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
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


Re: [Tutor] Need to be able to accept Page Down or CTRL-E

2013-02-01 Thread Alan Gauld

On 01/02/13 12:38, Dave Angel wrote:


your local setup you can use the following program(untested!) to read
the codes from your keyboard (assuming you are on Windows):

import msvcrt



The OP is running Python 2.73 on Linux.


OK, I didn't notice that. In that case the Linux variant on my tutor 
page will work for him.



--
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] Need to be able to accept Page Down or CTRL-E

2013-02-01 Thread Steven D'Aprano

On 01/02/13 23:38, Dave Angel wrote:

On 02/01/2013 03:34 AM, Alan Gauld wrote:




OTOH if you mean you want to send the page up/down/arrows as the
"something" then its a different game and you can do it. In that case
you need todo what you did with ESC. The character set your external app
uses will determine the codes you send. Assuming its the same set as
your local setup you can use the following program(untested!) to read
the codes from your keyboard (assuming you are on Windows):

import msvcrt

print "Hit space to end..."
print

while True:
ky = msvcrt.getch()


The OP is running Python 2.73 on Linux.


You can use getch under Linux.

http://code.activestate.com/recipes/577977/


Using that recipe, I see that PageUp sends the following four bytes:

'\x1b[5~'


PageDown also sends four bytes:

'\x1b[6~'


and Ctrl-E sends a single byte:

'\x05'



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] First Python Test

2013-02-01 Thread Alan Gauld

On 01/02/13 12:05, Simon Yan wrote:


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 know about templates but there are instructions on the MacPython 
pages that tell you how to set up XCode. I used it for some experiments 
in Cocoa programming on my Mac. But it was a while ago.



Just curious, why would you want to use XCode as a Python editor?


Lots of possible reasons:
1) He is already using XCode for other Mac projects and
   wants a familiar tool
2) XCode is a better editor than IDLE
3) He wants to do Cocoa (Mac GUI) programming and XCode
   provides integration with the MacOS GUI creator
4) He wants to have a mixed language program using
   Objective C and Python in the same project.

Any and all are perfectly reasonable and valid.

--
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] Read from large text file, parse, find string, print string + line number to second text file.

2013-02-01 Thread Steven D'Aprano

On 02/02/13 08:24, Scurvy Scott wrote:

One last question on this topic..

I'd like to call the files and the string form the command line like

Python whatever.py STRINGTOSEARCH NEWFILE FILETOOPEN

My understanding is that it would be accomplished as such

import sys

myString = sys.argv[1]
filetoopen = sys.argv[2]
newfile = sys.argv[3]


ETC ETC CODE HERE

Is this correct/pythonic? Is there a more recommended way? Am I retarded?




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


[Tutor] Help

2013-02-01 Thread Jack Little
I get this error

Traceback (most recent call last):
  File "C:\Users\Jack\Desktop\python\g.py", line 56, in 
    path1pt1()
NameError: name 'path1pt1' is not defined

With this amount of code:


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
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":
        print "There is a pack of ammo,some food and an odd microchip"
        ammo1=ammo1 + 6
        health=health + 10
        tech_parts=tech_parts + 1
elif g1 =="DOOR" or g1 == "Door" or g1 == "door":
      print "You are outside"
elif g1 == "RADIO" or g1 == "Radio" or g1 == "radio":
      print "It's only a few parts"
      radio_parts=radio_parts+3
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()

      
def path1pt1():
    print "This is where it all started. Freedom Tower. A biotech firm called 
Aesthos Biotechnology. Based here."
    print "I worked there."___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help

2013-02-01 Thread Brandon
You get the error because you call path1pt1() before it is defined.  Define
your path1pt1() method at the top of your code before simpstart().

Brandon

On Fri, Feb 1, 2013 at 5:47 PM, Jack Little  wrote:

> I get this error
>
> Traceback (most recent call last):
>   File "C:\Users\Jack\Desktop\python\g.py", line 56, in 
> path1pt1()
> NameError: name 'path1pt1' is not defined
>
> With this amount of code:
>
>
> 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
> 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":
> print "There is a pack of ammo,some food and an odd microchip"
> ammo1=ammo1 + 6
> health=health + 10
> tech_parts=tech_parts + 1
> elif g1 =="DOOR" or g1 == "Door" or g1 == "door":
>   print "You are outside"
> elif g1 == "RADIO" or g1 == "Radio" or g1 == "radio":
>   print "It's only a few parts"
>   radio_parts=radio_parts+3
> 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()
>
>
> def path1pt1():
> print "This is where it all started. Freedom Tower. A biotech firm
> called Aesthos Biotechnology. Based here."
> print "I worked there."
>
> ___
> 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] Help

2013-02-01 Thread Dave Angel

On 02/01/2013 08:47 PM, Jack Little wrote:

I get this error

Traceback (most recent call last):
   File "C:\Users\Jack\Desktop\python\g.py", line 56, in 
 path1pt1()
NameError: name 'path1pt1' is not defined

With this amount of code:


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


You've stopped defining that function, and now are doing top-level code

This following stuff belongs in a function, probably called main()


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":
 print "There is a pack of ammo,some food and an odd microchip"
 ammo1=ammo1 + 6
 health=health + 10
 tech_parts=tech_parts + 1
elif g1 =="DOOR" or g1 == "Door" or g1 == "door":
   print "You are outside"
elif g1 == "RADIO" or g1 == "Radio" or g1 == "radio":
   print "It's only a few parts"
   radio_parts=radio_parts+3
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()


Now, in top-level code, you're trying to call a function that hasn't 
been defined yet.  If you want to have complete freedom of order when 
you define your functions, don't put any top-level code except a tiny 
block at the end of the script.  No function may be called before it has 
been defined.  But inside a function, the call won't be made till the 
function is called.  So if the only calls happen at the end of the 
script, there'll never be such a problem.






def path1pt1():
 print "This is where it all started. Freedom Tower. A biotech firm called 
Aesthos Biotechnology. Based here."
 print "I worked there."



This should be the only non-trivial top-level code, and it should be at 
the end of the script.


if __name__ == "__main__":
simpstart()
main()


--
DaveA
___
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.

2013-02-01 Thread Scurvy Scott
> 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


Re: [Tutor] Read from large text file, parse, find string, print string + line number to second text file.

2013-02-01 Thread Mitya Sirenef

On 02/01/2013 11:31 PM, Scurvy Scott wrote:



> 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"


If should not be capitalized; sys.argv always has the name
of the script as the first item.

So you need to do something like:

if len(sys.argv) != 3:
print "usage: ..."

HTH!  -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

Emotion is primarily about nothing and much of it remains about nothing to
the end.  George Santayana

___
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.

2013-02-01 Thread Scurvy Scott
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  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


Re: [Tutor] Read from large text file, parse, find string, print string + line number to second text file.

2013-02-01 Thread Steven D'Aprano

On 02/02/13 15:31, Scurvy Scott wrote:

One question related to the instruction aspect- does this make sense to you?

If len(sys.argv) == 0:
 print "usage: etc etc etc"



Right idea, but not quite correct.

sys.argv always includes at least one item, the name of the script being 
called. This is why you will often see code like this, to extract the actual 
arguments:


argv = sys.argv[1:]  # slice excluding the first item
if len(argv) == 0:
usage()
else:
main(argv)


or similar.


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor