Re: [Tutor] Newbie to Python

2007-12-09 Thread Alan Gauld
Hi Fred,

welcome to the list

> I would like to provide the script the path and filename of a file.

OK, Look at sys.argv for command line argument passing
Look at raw_input() for interactive prompting

> Then the script would create a series of required directories
> then copy the contents of the provided folder into the destination
> location + series of required directories

Look at the os module for lots of stuff around handling files.
In particular look at the os.path module and the shutil module.

> It doesn't seem to terribly difficult, and I think i could get this
> same functionality done in something other than python, but I would
> love to try and start off with something like this.

It's a perfectly valid starting point. Have a go, and ask here
if/when you hit problems.

You might find the  "Using the OS" topic in my tutorial useful
as a starter.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] logic for a tree like structure

2007-12-09 Thread Tiago Saboga
On Sat, Dec 08, 2007 at 04:13:19PM -0800, Jeff Younker wrote:
> Pipes and IO channels are buffered.   The buffers are much larger than
> the amount of information you are writing to them, so they're never getting
> flushed while the program is running.   The child program completes, the
> IO channel closes, and it flushes out the output.

Yes! I've just noticed that if I replaced the python writer.py with an
equivalent bash script it worked as expected (my original example).
And then, I could make it work with writer.py if I flush sys.stdout
after each print statement. It is a little frustrating to see
that I was not able to see something that already bit me other times,
but... c'est la vie...

> My advice is to forget about all the file descriptor and pipes stuff.  
> Getting
> incremental IO from them via the subprocess module (or any standard IO
> module) is painful.   You're probably better off getting the nonstandard
> pexpect module and using that instead.

Thanks, it sounds like a useful module (and I don't mind it being
nonstandard, as long as it is in debian - and it is ;) ) but I still
can't see how to do something like that in my case. As I mentioned in
another mail, I have two independent classes, one for the gui and the
other for the converter. The converter has to spawn several processes
depending on the return code of each of them and then do some clean
up. In this mean time (it can take hours), the gui must be alive and
updated with the stdout of the processes.

Tiago.

> Here's your program using pexpect.
>
> #/usr/bin/python2.5
>
> import pexpect
>
> def main():
>cmd = pexpect.spawn('./writer.py')
>try:
>while True:
># wait for the end of the line or ten seconds
>cmd.expect('\r\n', timeout=10)
># get output preceding the EOF matched above
>line = cmd.before
>print '[main]', line
>except pexpect.TIMEOUT:
>  print "No output received for ten seconds"
>except pexpect.EOF:
>  pass
>finally:
> cmd.close()
>
> if __name__ == '__main__':
>   main()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timed While Loops and Threads

2007-12-09 Thread bhaaluu
On Dec 8, 2007 11:11 PM, Michael H. Goldwasser <[EMAIL PROTECTED]> wrote:
>
> Hi everyone,
>
> I'm having some fun combining two recent topics: the "Timed While
> Loops" game and that of communication between threads.  Here is an
> example that allows a person to gather points in a while loop, but
> only for a fixed period of time.  It relies on a few shared variables
> to coordinate the activity of the main thread and the secondary
> thread.
>
>
> import threading
>
> def playGame():
> global score, inPlay # shared with main thread
> while inPlay:
> raw_input('Press return to score a point! ')
> if inPlay:
> score += 1
> print 'Score =', score
>
> score = 0
> inPlay = True
> numSeconds = 5   # I didn't have patience for the 30 second version
> T = threading.Thread(target=playGame)
> T.start()
> T.join(numSeconds)
> inPlay = False   # signal to secondary thread that game is over
> print
> print 'After', numSeconds, 'seconds, you scored', score, 'points.'
>

WOW! That is awesome! 8^D

Here's my best score:
After 5 seconds, you scored 106 points.
Heheheh.

It shouldn't take much to convert that into any kind of timed gaming event.
It is easy to read and understand.
It is elegant!
Thank you!

Although I'm not the OP, I'm interested in all things related to gaming in
Python. This code is trully excellent! 8^D

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m

>
>
>
>
>+---
>| Michael Goldwasser
>| Associate Professor
>| Dept. Mathematics and Computer Science
>| Saint Louis University
>| 220 North Grand Blvd.
>| St. Louis, MO 63103-2007
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Noob question

2007-12-09 Thread quantrum75
Hi there,
I am a newbie trying to actively learn python.
My question is,
Suppose I have a list
a=["apple","orange","banana"]

How do I convert this list into a string which is

b="appleorangebanana"
Sorry for my ignorance, but I have been trying a "for" loop and trying the 
inbuilt str.join() method without success.
Thanks a lot.
Regards
Quant


   
-
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Noob question

2007-12-09 Thread Eric Brunson
quantrum75 wrote:
> Hi there,
> I am a newbie trying to actively learn python.
> My question is,
> Suppose I have a list
> a=["apple","orange","banana"]
>
> How do I convert this list into a string which is
>
> b="appleorangebanana"
> Sorry for my ignorance, 

No worries, every new language has its idioms that only come from 
experience and exposure.

Try this:

In [1]: a=["apple","orange","banana"]

In [2]: print "".join( a )
appleorangebanana

And just for clarity:

In [3]: print "|".join( a )
apple|orange|banana

Hope that helps, good luck learning python.

Sincerely,
e.

> but I have been trying a "for" loop and trying the inbuilt str.join() 
> method without success.
> Thanks a lot.
> Regards
> Quant
>
> 
> Looking for last minute shopping deals? Find them fast with Yahoo! 
> Search. 
> 
>  
>
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

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


Re: [Tutor] Noob question

2007-12-09 Thread Eric Walstad
Eric Brunson wrote:
> quantrum75 wrote:
>> Hi there,
>> I am a newbie trying to actively learn python.
>> My question is,
>> Suppose I have a list
>> a=["apple","orange","banana"]
>>
>> How do I convert this list into a string which is
>>
>> b="appleorangebanana"
>> Sorry for my ignorance, 
> 
> No worries, every new language has its idioms that only come from 
> experience and exposure.
> 
> Try this:
> 
> In [1]: a=["apple","orange","banana"]
> 
> In [2]: print "".join( a )
> appleorangebanana
> 
> And just for clarity:
> 
> In [3]: print "|".join( a )
> apple|orange|banana

And another good reference for you to know about is the built-in help
system that comes with Python.  For example, to learn a bit about why
Eric's code works the way it does:
>>> help("".join)

Help on built-in function join:

join(...)
S.join(sequence) -> string

Return a string which is the concatenation of the strings in the
sequence.  The separator between elements is S.


In Eric's example, the variable 'a' was a type of Python sequence,
specifically, a list.


You could also achieve the same result of concatenating a list of
strings by looping over the list items like so:

b = ''
for fruit in a:
b += fruit

print b

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


Re: [Tutor] Noob question

2007-12-09 Thread Alan Gauld
"Eric Walstad" <[EMAIL PROTECTED]> wrote

> You could also achieve the same result of concatenating a list of
> strings by looping over the list items like so:
> 
> b = ''
> for fruit in a:
>b += fruit
> 
> print b

And to add to the options you could use the formatting operator 
provided you know there are only 3 items, 

b = "%s%s%s" % tuple(a)

Or for an indefinite number of strings:

b = "%s" * len(a)
b = b % tuple(a)

So many options. However, to the OP, if you get stuck in 
future its best if you post the erroneous code that you have tried, 
then we can better see where you have gone wrong and thus 
provide clearer guidance on how to fix it. Its better to learn to 
do it your own way correctly than just to see other folks 
attempts .

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


[Tutor] A couple of modules

2007-12-09 Thread Ricardo Aráoz
Hi, somebody did ask about dates. Found this package, might be usefull
http://labix.org/python-dateutil
"""
The dateutil module provides powerful extensions to the standard
datetime module, available in Python 2.3+.

Features

* Computing of relative deltas (next month, next year, next monday,
last week of month, etc);
* Computing of relative deltas between two given date and/or
datetime objects;
* Computing of dates based on very flexible recurrence rules, using
a superset of the

  iCalendar specification. Parsing of RFC strings is supported as well.
* Generic parsing of dates in almost any string format;
* Timezone (tzinfo) implementations for tzfile(5) format files
(/etc/localtime, /usr/share/zoneinfo, etc), TZ environment string (in
all known formats), iCalendar format files, given ranges (with help from
relative deltas), local machine timezone, fixed offset timezone, UTC
timezone, and Windows registry-based time zones.
* Internal up-to-date world timezone information based on Olson's
database.
* Computing of Easter Sunday dates for any given year, using
Western, Orthodox or Julian algorithms;
* More than 400 test cases.
"""

Somebody was also asking about taking accents out of a string. Check
this out : http://packages.debian.org/sid/python/python-unac
"""
Unac is a programmer's library that removes accents from a string.

This package contains Python bindings for the original C library.
"""
and also :
http://groups.google.com/group/linux.debian.bugs.dist/browse_thread/thread/adb5cc05b1b7b247
"""
python-unac -- code is of poor quality and can be done as easily in
native Python
"""


HTH

Ricardo

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