Re: [Tutor] Python N00bi - Tough Cookie

2007-12-07 Thread Kent Johnson
Guess?!? wrote:
> Hello All,
>  
> I want trying to write a program that searches all the files ( 
> recursively) under a given directory in the filesystem

> There are following requirments
> 1> Search for a pattern in all files in a directory
> 2> Outputing the result with a unique format ( (626) 674-5901 -> 626-674 )
> 3> if the result has new office code (which I am guessing first 3 digits 
> -- 626) -- add to new line
>if the result has office code already in the list then append
>  
>  
> 
>  
> I have generated the regular expression for the pattern   and have 
> tested it also
>  
> \([0-9]{3}\)\s[0-9]{3}-[0-9]{4}
>  
>> >> import re
>> >> p = re.compile('\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}')
>> >> p = re.compile('\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}')
>> >> p
> <_sre.SRE_Pattern object at 0x00C400B8>
>> >> p
> <_sre.SRE_Pattern object at 0x00C400B8>
>> >> print p.match("")
> None
>> >> print p.match('(619) 223-1212')
> <_sre.SRE_Match object at 0x00A3F678>
>  
> I need options to proceed after finding the match in the files.

You can replace text using p.sub() (re.sub)

> 
> I was thinking to find all filenames in the directory using something 
> like 
>  
> import os
> path="C:\\somedirectory"  # insert the path to the directory of interest
> here
> dirList=os.listdir(path)
> for fname in dirList:
> print fname
>  
> Am I thinking correct ???

Yes, did you try this code? It looks good to me.

To open the file you have to construct the full path, e.g.
fpath = os.path.join(path, fname)
f = open(fpath)
data = f.read()
f.close()

Then you can process the data with your regex and write it back out.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Still Can't Find Timed While Loops

2007-12-07 Thread bob gailer
earlylight publishing wrote:
> Hello all,
>  
> I now have my bit of code in while loop form and it works!  It's great 
> but not exactly what I wanted to do.  I've been googling my heart out 
> and I find lots of info on while loops and lots of info on timers that 
> will execute an action AFTER a given interval but nothing on a timer 
> that will execute an action DURING a given interval.  What I'd really 
> like to do at this point in my game is have the player execute the 
> loop for 30 seconds then have it print the final score and break.  
> Does anyone out there have any code that'll do that?
import time
start = time.time()
raw_input("Wait a few seconds, then press enter to continue.")
print time.time() - start

Does that give you a hint?

You don't need a timer, unless you want to interrupt the user if he is 
not entering anything.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python N00bi - Tough Cookie

2007-12-07 Thread Guess?!?
Hello All,

I want trying to write a program that searches all the files (recursively)
under a given directory in the filesystem for phone
 numbers in the form of (626) 674-5901 and  and then outputting all phone
numbers found in a unique format 626-674


(If several numbers have the same office code, there should be only one line
of output for that office code.)

There are following requirments
1> Search for a pattern in all files in a directory
2> Outputing the result with a unique format ( (626) 674-5901 -> 626-674 )
3> if the result has new office code (which I am guessing first 3 digits --
626) -- add to new line
   if the result has office code already in the list then append




I have generated the regular expression for the pattern   and have
tested it also

\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}

>>> import re
>>> p = re.compile('\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}')
>>> p = re.compile('\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}')
>>> p
<_sre.SRE_Pattern object at 0x00C400B8>
>>> p
<_sre.SRE_Pattern object at 0x00C400B8>
>>> print p.match("")
None
>>> print p.match('(619) 223-1212')
<_sre.SRE_Match object at 0x00A3F678>

I need options to proceed after finding the match in the files.


I was thinking to find all filenames in the directory using something like


import os
path="C:\\somedirectory"  # insert the path to the directory of interest
here
dirList=os.listdir(path)
for fname in dirList:
print fname

Am I thinking correct ???

~Thanks
Geo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Still Can't Find Timed While Loops

2007-12-07 Thread bob gailer
bhaaluu wrote:
> On Dec 7, 2007 10:41 AM, bob gailer <[EMAIL PROTECTED]> wrote:
>   
>> earlylight publishing wrote:
>> 
>>> Hello all,
>>>
>>> I now have my bit of code in while loop form and it works!  It's great
>>> but not exactly what I wanted to do.  I've been googling my heart out
>>> and I find lots of info on while loops and lots of info on timers that
>>> will execute an action AFTER a given interval but nothing on a timer
>>> that will execute an action DURING a given interval.  What I'd really
>>> like to do at this point in my game is have the player execute the
>>> loop for 30 seconds then have it print the final score and break.
>>> Does anyone out there have any code that'll do that?
>>>   
>
>   
>> import time
>> start = time.time()
>> raw_input("Wait a few seconds, then press enter to continue.")
>> print time.time() - start
>>
>> Does that give you a hint?
>>
>> You don't need a timer, unless you want to interrupt the user if he is
>> not entering anything.
>> 
>
> Cool snippet! So if the player is entering numbers to an adding game,
> and he has to continue entering numbers for 30 seconds, then it could
> be accomplished with something similar to this, right?
>
> import time
> start = time.time()
> while 1:
> raw_input("Press  Now!")
> if time.time() - start < 30.0:
> print time.time() - start
> else: break
>   
Even simpler?

import time
start = time.time()
while time.time() - start < 30.0:
raw_input("Press  Now!")
print time.time() - start



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Still Can't Find Timed While Loops

2007-12-07 Thread Ricardo Aráoz
earlylight publishing wrote:
> Hello all,
>  
> I now have my bit of code in while loop form and it works!  It's great
> but not exactly what I wanted to do.  I've been googling my heart out
> and I find lots of info on while loops and lots of info on timers that
> will execute an action AFTER a given interval but nothing on a timer
> that will execute an action DURING a given interval.  What I'd really
> like to do at this point in my game is have the player execute the loop
> for 30 seconds then have it print the final score and break.  Does
> anyone out there have any code that'll do that?
>  
> Here's what I've got.  I'm sure it ain't pretty and I'm happy to hear
> suggestions on cleaning it up as well.  I know it needs some def's in
> there and possibly a class too.
>  
> import random

import time

> startNum = random.choice(range(1, 9))
> print 'Start with the number ', startNum,'.  Then continuously add 7 to
> that number until the timer runs out.  You have 30 seconds.'
> score = 0

  start_time = time.clock()

> answer = int(raw_input('Enter your answer: '))
> while (score < 50):
> startNum = startNum + 7
> if startNum == answer:
> print 'That is correct!'
> score = score + 5
> print 'Your score is ', score

  if (time.clock() - start_time) >= 30
  print 'Time's out. Game over. Your final score is ', score
  break

>
> answer = int(raw_input('Enter your answer: '))
> else:
> print 'That is incorrect.  Game over. Your final score is ', score
> break
> else:
> print 'You win! Your final score is ', score
>  
>  

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Still Can't Find Timed While Loops

2007-12-07 Thread bhaaluu
On Dec 7, 2007 2:36 PM, Scottie Hotchkiss <[EMAIL PROTECTED]> wrote:
> Disclaimer: I can't test this while I'm at work, but using
>
> "while 1:"
>
> instead of
>
> "while time.time() - start < 30.0"
>
> would be better.
>
> In the former case if you press enter after time has run out, it won't
> print the time, in the latter you could potentially make a correct
> answer after time has  run out.
>
> Example: At 29.0 you answer
> raw_input awaits and you wait 5 minutes, then answer.
> It will still run the print statement. The loop started and it doesn't
> matter how long you wait, because it won't evaluate that until the loop
> starts again.
>
> But it may alright depending on what you are trying to accomplish.
>
> Just my 2 cents,
> S Hotchkiss
>
> Tutor Lurker
>

Okay! Here's a sample 30-second Adding Game using time.time()
and a while True: loop. The timing granularity isn't all that great.
Sometimes it times out 2 or 3 seconds past 30 seconds, but in
this case, it probably isn't a big deal. This has been tested with
Python 2.4.3 running on Linux kernel 2.6.15 from the vim editor!

#!/usr/bin/python
import random
import time

startNum = random.choice(range(1, 9))
score = 0

print "#"*65
print " "*14,"Welcome to the 30-second Adding Game!"
print "#"*65
print " "* 5,"Instructions:"
print " "*10,"You'll choose a number."
print " "*10,"You'll be given a starting number."
print " "*10,"Add the number you chose to the starting number,"
print " "*10,"then keep adding your number to the sum until"
print " "*10,"the timer runs out."
print " "*10,"You'll have 30 seconds."
print " "* 5,
num2add = raw_input(" Choose a number to add: ")
print raw_input(" Press  when you're ready to begin.")
print "#"*65
num2add = int(num2add) # interpolation
newNum = startNum + num2add
print " Start with the number ", startNum
start = time.time()
while True:
if time.time() - start < 30.0:
answer = int(raw_input(" Enter your answer: "))
if answer == newNum:
 print " That is correct!  Keep going."
 score = score + 5
 newNum = newNum + num2add
 print " Your score is ", score
else:
 print " That is incorrect.  Please try again."
 print " Hurry! You only have %2.1f seconds left!"\
 % (30 - (time.time() - start))
else:
break

print " Buzzz!!! Time's up!"
print time.time() - start
#print "\n"
print " Your total score was: ", score


Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/python/index.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Still Can't Find Timed While Loops

2007-12-07 Thread Scottie Hotchkiss
Disclaimer: I can't test this while I'm at work, but using

"while 1:"

instead of

"while time.time() - start < 30.0"

would be better.

In the former case if you press enter after time has run out, it won't 
print the time, in the latter you could potentially make a correct 
answer after time has  run out.

Example: At 29.0 you answer
raw_input awaits and you wait 5 minutes, then answer.
It will still run the print statement. The loop started and it doesn't 
matter how long you wait, because it won't evaluate that until the loop 
starts again.

But it may alright depending on what you are trying to accomplish.

Just my 2 cents,
S Hotchkiss

Tutor Lurker
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Still Can't Find Timed While Loops

2007-12-07 Thread bhaaluu
On Dec 7, 2007 10:41 AM, bob gailer <[EMAIL PROTECTED]> wrote:
> earlylight publishing wrote:
> > Hello all,
> >
> > I now have my bit of code in while loop form and it works!  It's great
> > but not exactly what I wanted to do.  I've been googling my heart out
> > and I find lots of info on while loops and lots of info on timers that
> > will execute an action AFTER a given interval but nothing on a timer
> > that will execute an action DURING a given interval.  What I'd really
> > like to do at this point in my game is have the player execute the
> > loop for 30 seconds then have it print the final score and break.
> > Does anyone out there have any code that'll do that?

> import time
> start = time.time()
> raw_input("Wait a few seconds, then press enter to continue.")
> print time.time() - start
>
> Does that give you a hint?
>
> You don't need a timer, unless you want to interrupt the user if he is
> not entering anything.

Cool snippet! So if the player is entering numbers to an adding game,
and he has to continue entering numbers for 30 seconds, then it could
be accomplished with something similar to this, right?

import time
start = time.time()
while 1:
raw_input("Press  Now!")
if time.time() - start < 30.0:
print time.time() - start
else: break

Thanks! 8^D
Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Still Can't Find Timed While Loops

2007-12-07 Thread Luis N
I can't recall what your initial project was. I think it was a text
adventure. I imagine that you have bigger ideas regarding timers then
your current code suggests. Maybe you should investigate a time aware
long-running process. Twisted Python is such a beast.

While I can't recommend it's use as it's probably over your head and
certainly over mine, there is some code out there for a text adventure
using Twisted Python. I think the codebase is called Twisted Reality
now.

Luis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor