Re: [Tutor] remove not-empty directory

2006-03-30 Thread Tim Golden
[kakada]

| Just a quick question again, how can I remove not-empty 
| directory in python?
| 
| assumed I have the following directory:
| 
| ( temp/a/b/
|   temp/a/c/d.odt
|   temp/e.xml)
| 
| I want to remove the whole temp/ directory.

Look at the shutil module, and in particular at the rmtree function

TJG


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk

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


[Tutor] remove not-empty directory

2006-03-30 Thread kakada
Hi all,

Just a quick question again, how can I remove not-empty directory in python?

assumed I have the following directory:

( temp/a/b/
  temp/a/c/d.odt
  temp/e.xml)

I want to remove the whole temp/ directory.


Thanks :)

da

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


[Tutor] Apple Remote "Mouse"

2006-03-30 Thread Johnston Jiaa
I recently bought a Macbook Pro from Apple.  As it comes with a remote, I thought it would be great to use it as a mouse when not in Front Row.  The fast forward button would move the cursor to the left, the volume increase would move it up the screen, etc and the play button would serve as a "click."Is there any way to manipulate the cursor position on the screen using Python?  It is greatly appreciated if someone can point in the direction of where to look or what to search as no immediately useful hits came up from google.I am absolutely clueless about how to implement any of this.  It would be awesome if someone could briefly explain, or maybe point me to a tutorial of any sort, how to write a program that could detect the button pressed on the remote and accordingly move the cursor up, down, side-to-side.  Knowing how to disable the volume control of the "up and down" function of the anticipated remote would also be great, if
 anyone has time to elaborate how this can be done.Thank you for reading my message, and I hope you do not regard me as being a waste of your time.Johnston Jiaa ([EMAIL PROTECTED])
		Blab-away for as little as 1¢/min. Make  PC-to-Phone Calls using Yahoo! Messenger with Voice.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ASCII

2006-03-30 Thread andrew clarke
On Wed, Mar 29, 2006 at 01:51:30PM +0530, Kaushal Shriyan wrote:

> > How do i use this ASCII values in my day to day activities, I am going
> > through learning python,
> 
> Its a very general question not related to python at all, I have a
> minimum knowledge in ASCII just wanted to know how it is used and how
> it helps out

http://en.wikipedia.org/wiki/Ascii may help.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] XML node name and property

2006-03-30 Thread Keo Sophon
Hi all,

How can I get a name of an XML node and and its property name and its property 
value?

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


Re: [Tutor] Searching across .Py files for a particular string/ character

2006-03-30 Thread Kent Johnson
Janesh Ramakrishnan wrote:
> Hi Folks,
> 
> I was wondering what would be the best way to look up a string across
> 
different files in the Python interpreter (PythonWin 2.4). The find
function only finds files within currently open files. If I have a
folder of .py scripts and need to look up a specific keyword or string
among all these files within the project folder, is there any method
that you'd recommend?

I would do that in my editor, probably. But Python can get a list of 
files in a directory with os.listdir(). Loop over the files, use 
os.path.join() to make a full path, open the file, read the data, look 
for the string in the data.

Kent

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


Re: [Tutor] Searching across .Py files for a particular string/character

2006-03-30 Thread David Heiser

Here's a simple Python script that will do it. It's not very
sophisticated, but it's easy to modify for special cases.

import os, string

def Find(TargetString, DIR, Names):
for Name in Names:
if Name != "Search.py":
try:
TargetFile = DIR + "/" + Name
Blob = open(TargetFile, "r").read()
if Blob.find(TargetString) > -1:
print TargetFile
except IOError:
pass
return

TargetString = 'telnetlib'

print "\nFinding " + TargetString + "\n---\n"
os.path.walk(".", Find, TargetString)
==



-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Janesh Ramakrishnan
Sent: Thursday, March 30, 2006 5:57 PM
To: tutor@python.org
Subject: [Tutor] Searching across .Py files for a particular
string/character



Hi Folks,

I was wondering what would be the best way to look up a string across
different files in the Python interpreter (PythonWin 2.4). The find
function only finds files within currently open files. If I have a
folder of .py scripts and need to look up a specific keyword or string
among all these files within the project folder, is there any method
that you'd recommend?

For eg: Visual Studio 6.0 can look for a string across numerous files
indexed in a project and returns a list of files containing that
specific string. 

Any help would be greatly appreciated.

Thanks.
Janesh



___
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] Searching across .Py files for a particular string/ character

2006-03-30 Thread Janesh Ramakrishnan

Hi Folks,

I was wondering what would be the best way to look up a string across different 
files in the Python interpreter (PythonWin 2.4). The find function only finds 
files within currently open files. If I have a folder of .py scripts and need 
to look up a specific keyword or string among all these files within the 
project folder, is there any method that you'd recommend?

For eg: Visual Studio 6.0 can look for a string across numerous files indexed 
in a project and returns a list of files containing that specific string. 

Any help would be greatly appreciated.

Thanks.
Janesh



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


Re: [Tutor] number of nodes

2006-03-30 Thread Kent Johnson
kakada wrote:
> Hello everyone,
> 
> textp = xmldoc.getElementsByTagName('text:p')
> 
> from the example above, How can I know how many  node are there?

len(textp), maybe?

Kent

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


Re: [Tutor] Program for outputing the letter backward

2006-03-30 Thread Hoffmann
--- Ed Singleton <[EMAIL PROTECTED]> wrote:

> On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote:
> > --- John Fouhy <[EMAIL PROTECTED]> wrote:
> >
> > > On 29/03/06, Hoffmann <[EMAIL PROTECTED]>
> wrote:
> > > > vehicle='car'
> > > > index = vehicle[-1]   #the last letter
> > > > index_zero = vehicle[0]   #the first letter
> > > >
> > > > while index >= index_zero:
> > > >letter=vehicle[index]
> > > >print letter
> > > >index -= 1
> > > >
> > > > The problem is that I get no output here.
> Could I
> > > hear
> > > > from you?
> > >
> > > I can print the letters backwards like this:
> > >
> > > vehicle = 'car'
> > > print vehicle[2]
> > > print vehicle[1]
> > > print vehicle[0]
> > >
> > > Output:
> > >
> > > r
> > > a
> > > c
> > >
> > > -
> > >
> > > This is not very useful, though, because it will
> > > only work for strings
> > > that are exactly three letters long.  Can you
> see
> > > how to write a loop
> > > to produe this output?
> > >
> > > Hint: the len() function will tell you how long
> a
> > > string is.
> > >
> > > eg: if vehicle == 'car' then len(vehicle) == 3.
> > >
> > > --
> > > John.
> > > ___
> >
> > Hi John,
> >
> > I am still 'blind' here.
> >
> > Please, see below what I did, and what I got:
> >
> > >>> vehicle='car'
> > >>> index = 0
> > >>> lenght =len(vehicle)
> > >>> last = vehicle[lenght -1]
> > >>> while last >= vehicle[0]:
> > ... letter = vehicle[index]
> > ... print letter
> > ... last -= 1
> > ...
> > c
> > Traceback (most recent call last):
> >   File "", line 4, in ?
> > TypeError: unsupported operand type(s) for -=:
> 'str'
> > and 'int'
> >
> > As you can see, I am still a newbie...
> > Could anyone, please, guide me on this exercise?
> > Thanks!
> > Hoffmann
> 
> A technique I used to find useful when I was very
> first learning (and
> struggling) was to calculate the variables for each
> pass of the loop
> (basically remove all the variable names, just like
> doing algebra).
> 
> So:
> 
> >>> vehicle='car'
> >>> index = 0
> >>> lenght = len(vehicle) # therefore:
> >>> lenght = 3
> >>> last = vehicle[lenght -1] # therefore:
> >>> last = vehicle[2] # therefore:
> >>> last = "r"
> >>> while "r" >= "c": # first pass
> ... letter = vehicle[index] # therefore:
> ... letter = vehicle[0] # therefore:
> ... letter = "c"
> ... print letter
> ... last -= 1 # therefore:
> ... "r" -= 1 # therefore:
> ... "r" = "r" - 1 # therefore:
> ... ERROR
> 
> You'll find that that can make it much clearer what
> is actually
> happening.  An alternative is to use lots and lots
> of print
> statements:
> 
> >>> vehicle='car'
> >>> print vehicle
> >>> index = 0
> >>> print index
> >>> lenght = len(vehicle)
> >>> print lenght
> 
> and so on...
> 
> It would be really good if there was a way to have a
> "verbose"
> interpreter that showed you each of the steps in the
> process, but I
> don't know of any.
> 
> For example:
> 
> >>> last = vehicle[lenght -1]
> last = vehicle[2]
> last = "r"
> >>>
> 
> Ed
> 

Hi Ed,

Many thanks for the hints!

Hoffmann

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Turnkey Python on a USB stick

2006-03-30 Thread Steve Slevinski
Hey Michael Sparks,

You said you were interested in my Turnkey Python on a USB stick so I 
thought I'd tell you about it.  It's going very well.  I'm just putting 
the polish on it now.  I have 2 main USB sticks: 1 for development and 1 
for distribution to end users.

My development USB can be plugged into any Windows computer and I have 
my entire development environment ready to go.  I can work at home, on 
location, or even at the library.  Movable Python (less than $10 and the 
only purchase) make this possible.  My main editor is Leo (open source 
python).  It takes a while to grok, but there isn't anything else like it.

My distribution USB runs the same code as my server.  I'm using the 
Uniform Server project.  All my users have to do is plug in the USB, 
start the server and point the browser to localhost.  It should look 
really nice once I get my logo printed on the USB sticks.

Right now my server code is staying PHP, but I'm on the path to Python.  
The Uniform Server hasn't completed the mod_python plug-in so I have 
some time.  I'm also going to try py2exe once I get the hang of the GTK 
and Twisted.  Again, I have some time.

Three additional software packages that help are...
PStart - Shortcuts for USB
Allway Sync - PC to USB sync.
Disc Image XML - take a distribution image and writes a USB stick in 
under 2 minutes.

Regards,
-Steve



Michael Sparks wrote:
> On Monday 27 March 2006 22:21, Steve Slevinski wrote:
>   
>> Does this sound reasonable?  Are their any alternatives to Movable
>> Python? (http://www.voidspace.org.uk/python/movpy/)
>> 
>
> Hi Steve,
>
> I've got no experience of setting up such a beast or using movable python, 
> but 
> I just wanted to say it sounds like an EXCELLENT idea, and I'd be interested 
> in hearing how you get on!
>
> :)
>
> Michael.
>
>
>   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OT: Tutor Digest and Outlook 2003

2006-03-30 Thread Mike Hansen
The company I work for is moving to Outlook 2003 and Exchange server. I used
to use Thunderbird. Anyway, when digest messages show up in Outlook 2003,
all the posts are attachments. Does anyone know how to get them to display
in-line?

Thanks,

Mike

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


Re: [Tutor] for loops and exceptions

2006-03-30 Thread Kent Johnson
Matthew White wrote:
> Hello,
> 
>>From a general style and/or programmatic perspective, which is a "better"
> way to write this bit of code?

Hmm, neither?
> 
> try:
> (dn, attrs) = conn.search_s(search_base, search_scope, search_filter, 
> search_attrs):
> except Exception, e:
> warn_the_user(e)
> do_something_useful()
> 
> for (name, data) in (dn, attrs):
> print '%s' % (name)
> for key, value in data.iteritems():
> print '%s => %s' % (key, value)

This will run the for loop even if you get an exception. I think the for 
loop is buggy, too, it is different from the one below.
> 
> OR
> 
> try:
> for dn, attrs in conn.search_s(search_base, search_scope, search_filter, 
> search_attrs):
> print dn
> for key, value in attrs.iteritems():
> print '\t%s => %s' % (key, value)
> print
> except Exception, e:
>  warn_the_user(e)
>  do_something_useful

This might be OK. It will catch exceptions in the for loop, which you 
may or may not want.

If this code is at a low level of your program, it's best to only catch 
expected exceptions, and let unexpected exceptions propagate to a higher 
level. At the higher level, you can have a generic except block that 
reports the error and moves on. This is often in a top-level loop or 
event handler. Your code has elements of both - code that does real work 
with a generic except block that reports errors.

Anyway here is a construction that is very useful for catching just the 
exceptions you expect to see from a bit of code, while running the rest 
of the code outside of the try block.

try:
 (dn, attrs) = conn.search_s(search_base, search_scope, 
search_filter, search_attrs):
except ConnectionError, e:
 warn_the_user(e)
 do_something_useful()
else:
 print dn
 for key, value in attrs.iteritems():
 print '\t%s => %s' % (key, value)
 print

The idea is to put as little code as possible in the scope of the try, 
and to catch as few exceptions as possible. If no exception is raised, 
the else: block will run. If there is an exception in that code, it will 
propagate to a higher level.

Kent

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


Re: [Tutor] Multi-thread environments

2006-03-30 Thread Kent Johnson
Liam Clarke wrote:
> Hi all,
> 
> I'm working in my first multi-threaded environments, and I think I
> might have just been bitten by that.
> 
> class Parser:
> def __init__(self, Q):
> self.Q = Q
> self.players = {}
> self.teams = {}
> 
> def sendData(self):
> if not self.players or not self.teams: return
> self.Q.put((self.players, self.teams))
> self.resetStats()
> 
> def resetStats():
> for key in self.players:
> self.players[key] = 0
> for key in self.teams:
> self.teams[key] = 0
> 

> What I'm finding is that if a lot more sets of zeroed data are being
> sent to the DAO than should occur.
> 
> If the resetStats() call is commented out, data is sent correctly. I
> need to reset the variables after each send so as to not try and
> co-ordinate state with a database, otherwise I'd be away laughing.
> 
> My speculation is that because the Queue is shared between two
> threads, one of which is looping on it, that a data write to the Queue
> may actually occur after the next method call, the resetStats()
> method, has occurred.
> 
> So, the call to Queue.put() is made, but the actual data is accessedin
> memory by the Queue after resetStats has changed it.

You're close. The call to Queue.put() is synchronous - it will finish 
before the call to resetStats() is made - but the *data* is still shared.

What is in the Queue is references to the dicts that is also referenced 
by self.players and self.teams. The actual dict is not copied! This is 
normal Python function call and assignment semantics, but in this case 
it's not what you want. You have a race condition - if the data in the 
Queue is processed before the call to resetStats() is made, it will work 
fine; if resetStats() is called first, it will be a problem. Actually 
there are many possible failures since resetStats() loops over the 
dicts, the consumer could be interleaving its reads with the writes in 
resetStats().

What you need to do is copy the data, either before you put it in the 
queue or as part of the reset. I suggest rewriting resetStats() to 
create new dicts because dict.fromkeys() will do just what you want:
   def resetStats():
 self.players = dict.fromkeys(self.players.keys(), 0)
 self.teams = dict.teams(self.players.keys(), 0)

This way you won't change the data seen by the consumer thread.

> I've spent about eight hours so far trying to debug this; I've never
> been this frustrated in a Python project before to be honest... I've
> reached my next skill level bump, so to speak.

Yes, threads can be mind-bending until you learn to spot the gotchas 
like this.

By the way you also have a race condition here:
> if self.dump:
> self.parser.sendDat()
> self.dump = False

Possibly the thread that sets self.dump will set it again between the 
time you test it and when you reset it. If the setting thread is on a 
timer and the time is long enough, it won't be a problem, but it is a 
potential bug.

Kent

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


[Tutor] Multi-thread environments

2006-03-30 Thread Liam Clarke
Hi all,

I'm working in my first multi-threaded environments, and I think I
might have just been bitten by that.

I having issue with one method in a parser of mine. Only the code I
think is causing the issue is included below, I can do a full post to
RAFB if requested, but there's a bit of it.

The parser:

class Parser:
def __init__(self, Q):
self.Q = Q
self.players = {}
self.teams = {}


def parsePack(self, packet):
#Do stuff to packet
#Few conditionals removed, below is example actions.
self.players["A_value"] += self.players["A_value"]
self.teams["As_above"] += self.players["As_above"]

def sendData(self):
if not self.players or not self.teams: return
self.Q.put((self.players, self.teams))
self.resetStats()

def resetStats():
for key in self.players:
self.players[key] = 0
for key in self.teams:
self.teams[key] = 0

The thread in question:

class ParseThread(threading.Thread):

def __init__(self, Q, daoQ, cfg):
self.Q = Q
self.parser = parserx.WatcherInTheDark(daoQ,
cfg["clan_regular_expressions"])
self.shutdown = False
self.dump = False
threading.Thread.__init__(self)

def run(self):
print "Parser starting."
while True:

if self.dump:
self.parser.sendDat()
self.dump = False

try:
data = self.Q.get(False)
self.parser.check(data)

except Empty:

if self.shutdown:
return

continue

The variable Q being passed in is a Queue.Queue, which is used to send
data into another thread, which holds the DAO.


The sendData() method is called by a timer thread setting
Parsethread.dump to True. I was looking to avoid any asynchrous
problems via these convoluted method.

What I'm finding is that if a lot more sets of zeroed data are being
sent to the DAO than should occur.

If the resetStats() call is commented out, data is sent correctly. I
need to reset the variables after each send so as to not try and
co-ordinate state with a database, otherwise I'd be away laughing.

My speculation is that because the Queue is shared between two
threads, one of which is looping on it, that a data write to the Queue
may actually occur after the next method call, the resetStats()
method, has occurred.

So, the call to Queue.put() is made, but the actual data is accessedin
memory by the Queue after resetStats has changed it.

Am I on a right path here? I may have to do a bit of a rewrite to get
around this, as I've made some some assumptions early in the
architecture, so before I devote an hour to finding out I'm making
another wrong assumption, any hints on multi-threading in general are
welcomed and requested, and any confirmation or refutation of my
hypothesis also gladly welcomed.

I've spent about eight hours so far trying to debug this; I've never
been this frustrated in a Python project before to be honest... I've
reached my next skill level bump, so to speak.

Much thanks,

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


Re: [Tutor] Program for outputing the letter backward

2006-03-30 Thread Ed Singleton
On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote:
> --- John Fouhy <[EMAIL PROTECTED]> wrote:
>
> > On 29/03/06, Hoffmann <[EMAIL PROTECTED]> wrote:
> > > vehicle='car'
> > > index = vehicle[-1]   #the last letter
> > > index_zero = vehicle[0]   #the first letter
> > >
> > > while index >= index_zero:
> > >letter=vehicle[index]
> > >print letter
> > >index -= 1
> > >
> > > The problem is that I get no output here. Could I
> > hear
> > > from you?
> >
> > I can print the letters backwards like this:
> >
> > vehicle = 'car'
> > print vehicle[2]
> > print vehicle[1]
> > print vehicle[0]
> >
> > Output:
> >
> > r
> > a
> > c
> >
> > -
> >
> > This is not very useful, though, because it will
> > only work for strings
> > that are exactly three letters long.  Can you see
> > how to write a loop
> > to produe this output?
> >
> > Hint: the len() function will tell you how long a
> > string is.
> >
> > eg: if vehicle == 'car' then len(vehicle) == 3.
> >
> > --
> > John.
> > ___
>
> Hi John,
>
> I am still 'blind' here.
>
> Please, see below what I did, and what I got:
>
> >>> vehicle='car'
> >>> index = 0
> >>> lenght =len(vehicle)
> >>> last = vehicle[lenght -1]
> >>> while last >= vehicle[0]:
> ... letter = vehicle[index]
> ... print letter
> ... last -= 1
> ...
> c
> Traceback (most recent call last):
>   File "", line 4, in ?
> TypeError: unsupported operand type(s) for -=: 'str'
> and 'int'
>
> As you can see, I am still a newbie...
> Could anyone, please, guide me on this exercise?
> Thanks!
> Hoffmann

A technique I used to find useful when I was very first learning (and
struggling) was to calculate the variables for each pass of the loop
(basically remove all the variable names, just like doing algebra).

So:

>>> vehicle='car'
>>> index = 0
>>> lenght = len(vehicle) # therefore:
>>> lenght = 3
>>> last = vehicle[lenght -1] # therefore:
>>> last = vehicle[2] # therefore:
>>> last = "r"
>>> while "r" >= "c": # first pass
... letter = vehicle[index] # therefore:
... letter = vehicle[0] # therefore:
... letter = "c"
... print letter
... last -= 1 # therefore:
... "r" -= 1 # therefore:
... "r" = "r" - 1 # therefore:
... ERROR

You'll find that that can make it much clearer what is actually
happening.  An alternative is to use lots and lots of print
statements:

>>> vehicle='car'
>>> print vehicle
>>> index = 0
>>> print index
>>> lenght = len(vehicle)
>>> print lenght

and so on...

It would be really good if there was a way to have a "verbose"
interpreter that showed you each of the steps in the process, but I
don't know of any.

For example:

>>> last = vehicle[lenght -1]
last = vehicle[2]
last = "r"
>>>

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


Re: [Tutor] How does it function

2006-03-30 Thread Steve Nelson
On 3/30/06, Terry Carroll <[EMAIL PROTECTED]> wrote:

> On Wed, 29 Mar 2006, Steve Nelson wrote:
>
> > Simple answer - any python program you write is effectively a
> > 'module'.  Modules have an attribute __name__.  If you've imported the
> > module from elsewhere, the __name__ is set to the name of the module,
> > otherwise it is __name__.
>
> I don't mean to nitpick, but I see Steve had a small but crucial slip
> here.  I think he means, " If you've imported the module from elsewhere,
> the __name__ is set to the name of the module, otherwise it is "__main__".

Yes absolutely - well spotted, and sorry for the typo.

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