Re: [Tutor] Re: Could I have used time or datetime modules here?

2004-12-09 Thread Liam Clarke
Hi Brian and Dick, 

I for one have learnt a lot from this combined poke around the
workings of datetime. The datetime.year thing never occurred to me,
yet it was so obvious when I saw it being used.

I give you, my python alarm clock timing mechanism, final version!

http://www.rafb.net/paste/results/qur2Pw95.html

It works on the cmd line - 

python alarm.py 17:30 

Sets the alarm for the next occurrence of 17:30, using nothing  but
datetime.datetime objects, and one timedelta (maybe).

'Twas a good discussion. : )

Liam Clarke

On Thu, 09 Dec 2004 00:12:04 -0500, Brian van den Broek
<[EMAIL PROTECTED]> wrote:
> Brian van den Broek said unto the world upon 2004-12-07 23:57:
> > Dick Moores said unto the world upon 2004-12-07 12:04:
> >
> 
> 
> 
> 
> 
> > The note you reference:
> >
> > date2 is moved forward in time if timedelta.days > 0, or backward if
> > timedelta.days < 0. Afterward date2 - date1 == timedelta.days.
> > timedelta.seconds and timedelta.microseconds are ignored. OverflowError
> > is raised if date2.year would be smaller than MINYEAR or larger than
> > MAXYEAR.
> >
> > presupposes you are adding a timedelta to a datetime as in Tim's
> > suggestion. It makes no promises if you were doing something goofy like
> > I was. (If that is what you were trying to get me to see, sorry, but I
> > missed it.)
> 
> Hey Dick and all,
> 
> I've taken a look at your code and finally seen what you meant, Dick.
> You were trusting datetime (along the same lines that Tim suggested to
> me) and thus weren't facing the problems I'd caused for myself. Sorry
> for missing your point before. Thanks,
> 
> Brian vdB
> 
> >
> > I hope this clarifies things some :-)
> 
> Ah, irony!
> 
> 
> 
> 
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2004-12-09 Thread Liam Clarke
Well, I figured out the memory error relatively easily once I poked
some stuff, I was using codeSt.find instead of codeSt.index, so it was
returning no matches as -1, index raises an error for me, and I wasn't
catering for that -1. The MemoryError occurred when Python wanted to
slice from 160,000 to -1 or 0.

Was quite funny watching 1 Gb of paging file disappear in seconds.

But, I give up. Now, I've got several more bugs pop up. I've got an
index of integers that has the occasional index stored as a string. I
have indexes that start pointing at the right place, but when it comes
time to slice, the right place has moved. Unsure why, I'm specifically
moving from highest to lowest to avoid messing with the index values.
Even if nothing changes, my stored indexes go off course. This is
yuck.

So, I'm going to throw caution to the wind, and try an re approach. It
can't be any more unwieldy and ugly than what I've got going at the
moment.

Thanks for all the help, I'm off to get myself another problem.

Regards,

Liam Clarke

On Wed, 08 Dec 2004 15:25:30 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote:
> Liam Clarke wrote:
> 
> >>I'm not sure why you're getting the MemoryError, but it'd be easier to
> >>figure out if you posted the entire text of the traceback.
> >
> > Traceback: 
> >
> > Line 39: seg=codeSt[element:endInd+len(endStr]
> > MemoryError
> >
> > Hehe. Helpful, no?
> 
> Actually, it was the "usual bit about in module" that might *be*
> helpful. ;)  Often, something can be determined by the sequence of
> function calls listed there.  Though I suppose, now that I think of
> it, that since your code is all module-level, there wouldn't be any
> function stack in this case...   Still, in general, when asking for
> help with something that throws an exception, it's always best to copy
> & paste the entire text of the exception.
> 
> One thing that I've noticed, which I thought was just a typo in your
> original email but which is duplicated again here (it's not present on
> the web page you linked to) -- you have a mismatched parenthesis in
> your len() call.  It's "[ ... len(endstr]" -- there's no ')' to close
> the function call.  Given that this typo isn't on the web page, I'm
> not sure whether it's actually there in the code you're running or
> not.  I'd have *thought*, however, that if it *is* present, you'd get
> a syntax error rather than a memory error, so it's probably not there
> but you should check it anyhow.  :)
> 
> 
> 
> Jeff Shannon
> Technician/Programmer
> Credit International
> 
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help matching elements from two lists and printingthem

2004-12-09 Thread Alan Gauld
> >>> out = open('sa_int_2.txt','w')
> >>> for ele1 in range(len(spot_cor)):
> x = spot_cor[ele1]

replace this with

for x in spot_cor:

> for ele2 in range(len(spot_int)):
> cols = split(spot_int[ele2],'\t')

and this with

for item in spot_int:
   cols = split(item,'\t')

> y = (cols[0]+'\t'+cols[1])
> if x == y:
> for ele3 in spot_int:
> if y in ele3:
> out.write(ele3)
> out.write('\n')

But that doesn't fix the problem, it just simplifies the code!

> On top of this this process is VERY SLOW on high end
> server too. I think its just the way it is to deal
> with string processing. 

It looks like you have 3 (4?!) levels of nested loops 
(altho' I can't really tell because the indentation got lost) , 
and that is usually going to be slow!

> As you asked I am all parsing out the pieces for a
> tab-delimitted text. I can get the values as CSV
> instead of tab delimitted. But what is the way using
> CSV to deal with this situation. 

There is a CSV module which means you have standard, tested 
code to start with... Sounds like a good place to start from.

Alan G.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to get the 2 complex cube roots of 1?

2004-12-09 Thread Dick Moores
My trusty $10 Casio calculator tells me that the 3 cube roots of 1 are:
1, (-.5 +0.866025403j), and (-.5 -0.866025403j), or thereabouts. Is there 
a way to do this in Python?

Checking the Casio results with Python:
>>> 1**3
1
>>> (-.5 + .866025403j)**3
(0.796196859+1.1766579932626087e-009j)
>>> (-.5 - .866025403j)**3
(0.796196859-1.1766579932626087e-009j)
Thanks,
Dick Moores
[EMAIL PROTECTED]
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re: Could I have used time or datetime modules here?

2004-12-09 Thread Kent Johnson
Liam Clarke wrote:
Hi Brian and Dick, 

I for one have learnt a lot from this combined poke around the
workings of datetime. The datetime.year thing never occurred to me,
yet it was so obvious when I saw it being used.
I give you, my python alarm clock timing mechanism, final version!
http://www.rafb.net/paste/results/qur2Pw95.html
Excuse me for butting in at the last minute, but you can use datetime for the date comparison as 
well. If you create desiredTime first, you can directly compare it with nowTime and increment it if 
necessary. Also note you can give better names to the hour and minute strings by using tuple assignment:

ja=sys.argv[1]
hh, mm=ja.split(':')
nowTime=datetime.datetime.now()
desiredTime=nowTime.replace(hour=int(hh), minute=int(mm),second=0)
if desiredTime <= nowTime:
desiredTime += datetime.timedelta(days=1)
Kent
It works on the cmd line - 

python alarm.py 17:30 

Sets the alarm for the next occurrence of 17:30, using nothing  but
datetime.datetime objects, and one timedelta (maybe).
'Twas a good discussion. : )
Liam Clarke
On Thu, 09 Dec 2004 00:12:04 -0500, Brian van den Broek
<[EMAIL PROTECTED]> wrote:
Brian van den Broek said unto the world upon 2004-12-07 23:57:
Dick Moores said unto the world upon 2004-12-07 12:04:


The note you reference:
date2 is moved forward in time if timedelta.days > 0, or backward if
timedelta.days < 0. Afterward date2 - date1 == timedelta.days.
timedelta.seconds and timedelta.microseconds are ignored. OverflowError
is raised if date2.year would be smaller than MINYEAR or larger than
MAXYEAR.
presupposes you are adding a timedelta to a datetime as in Tim's
suggestion. It makes no promises if you were doing something goofy like
I was. (If that is what you were trying to get me to see, sorry, but I
missed it.)
Hey Dick and all,
I've taken a look at your code and finally seen what you meant, Dick.
You were trusting datetime (along the same lines that Tim suggested to
me) and thus weren't facing the problems I'd caused for myself. Sorry
for missing your point before. Thanks,
Brian vdB

I hope this clarifies things some :-)
Ah, irony!

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String matching?

2004-12-09 Thread Liam Clarke
Hi Kent, 

Just wanted to say thanks for the advice on the regular expressions.
I've just created a regular expression which finds all 267 applets, my
.index iterations were producing some cockeyed results, so I tried re
in desperation, and it works, so simply. Looks pretty menacing, but
it's good.

Got to say, that re is very powerful, but that redemo.py you
recommended is brilliant for a beginner. I just wrote my re pattern in
there, and used it to troubleshoot.

So yeah, thanks again.

Regards,

Liam Clarke


On Tue, 07 Dec 2004 21:06:34 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Liam Clarke wrote:
> > And thanks for the re, hopefully I won't have to use it, but it gives
> > me a starting point to poke the re module from.
> 
> BTW Python comes with a nice tool for experimenting with regular expressions. 
> Try running
> Python23\Tools\Scripts\redemo.py
> 
> Kent
> 
> 
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to find complex roots

2004-12-09 Thread Matt Williams
Dick Mores wrote:

My trusty $10 Casio calculator tells me that the 3 cube roots of 1 are:
1, (-.5 +0.866025403j), and (-.5 -0.866025403j), or thereabouts. Is
there 
a way to do this in Python?



I think the neatest approach might be to consider that the n-complex
roots form at equal angle around the origin on an Argand diagram
(basically a cartesian plane) - here the points (in "standard")
cartesian notation are at (1,0), (-0.5, 0.86) and (-0.5, -0.86). The
whole effect looks a bit like a Mercedes-Benz symbol rotated clockwise
by 90 degrees. The points, in this case, lie on the unit circle.

As a result, I think you could just divide 360 by n (for the n-roots),
set the 1st root at (1,0) and then position the others around the
circle, incrementing by the required number of degrees.

If I've remembered correctly, for c where |c| <>1, the argument is the
same, but you need to take the nth root of the absolute value of c (this
can be ignored when we're doing it for 1, as of course the nth root of 1
is 1).

Obviously I haven't included any codebut I hope this helps a bit.

Matt

P.S. Thrilled to be able to answer something on the tutor list, instead
of just asking dumb questions!
-- 
Dr. M. Williams MRCP(UK)
Clinical Research Fellow,Cancer Research UK
+44 (0)207 269 2953
+44 (0)7834 899570

The views, opinions and judgements expressed in this message are solely those 
of the author.
The message may not have been reviewed or approved by Cancer Research UK

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the 2 complex cube roots of 1?

2004-12-09 Thread Pierre Barbier de Reuille
Do you want : the roots of 1 ? or something more general ?
Because the nth-roots of 1 are defined by :
r = exp(2*i*pi*k/n) with k = {0,1,...n-1}
If it's more complex ... then I don't know :)
Pierre
Dick Moores a écrit :
My trusty $10 Casio calculator tells me that the 3 cube roots of 1 are:
1, (-.5 +0.866025403j), and (-.5 -0.866025403j), or thereabouts. Is 
there a way to do this in Python?

Checking the Casio results with Python:
 >>> 1**3
1
 >>> (-.5 + .866025403j)**3
(0.796196859+1.1766579932626087e-009j)
 >>> (-.5 - .866025403j)**3
(0.796196859-1.1766579932626087e-009j)
Thanks,
Dick Moores
[EMAIL PROTECTED]
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel   : (33) 4 67 61 65 77fax   : (33) 4 67 61 56 68
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2004-12-09 Thread Kent Johnson
Liam,
I'm sorry to hear you so discouraged.
It sourds like your program has gotten too big for you to clearly understand what it is doing. An 
earlier poster suggested that you break your program up into functions. This is a very good idea, 
especially if you do it from the start.

A very powerful technique for developing a complex program is to start with small pieces and build 
up from them. The pieces can be coded *and tested* independently. Because they are known to work, 
you can confidently build larger pieces by hooking together the small ones.

In your case, a very simple place to start would be a function to get rid of the '=\n' and '=3D' 
strings:

def cleanup(codeSt):
codeSt=codeSt.replace("=\n",'')
codeSt=codeSt.replace('=3D','=')
return codeSt
Using the unittest module you could write a test like this:
import unittest
class CodeFixTest(unittest.TestCase):
def test_cleanup(self):
self.assertEquals('', cleanup(''))

if __name__ == '__main__':
unittest.main()
That's pretty simple. Now move on to a function that, given an applet tag, figures out the 
corresponding link tag. Write tests for that function using examples from your actual data.

You could have another function that finds the applet tags. Finally a function that puts it all 
together. Still you can have unit tests that make sure it is working correctly.

When you are done you have a working, well-tested program and a suite of unit tests for it. Keep the 
tests - they will be handy if you ever want to make a change.

HTH
Kent
Liam Clarke wrote:
Well, I figured out the memory error relatively easily once I poked
some stuff, I was using codeSt.find instead of codeSt.index, so it was
returning no matches as -1, index raises an error for me, and I wasn't
catering for that -1. The MemoryError occurred when Python wanted to
slice from 160,000 to -1 or 0.
Was quite funny watching 1 Gb of paging file disappear in seconds.
But, I give up. Now, I've got several more bugs pop up. I've got an
index of integers that has the occasional index stored as a string. I
have indexes that start pointing at the right place, but when it comes
time to slice, the right place has moved. Unsure why, I'm specifically
moving from highest to lowest to avoid messing with the index values.
Even if nothing changes, my stored indexes go off course. This is
yuck.
So, I'm going to throw caution to the wind, and try an re approach. It
can't be any more unwieldy and ugly than what I've got going at the
moment.
Thanks for all the help, I'm off to get myself another problem.
Regards,
Liam Clarke
On Wed, 08 Dec 2004 15:25:30 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote:
Liam Clarke wrote:

I'm not sure why you're getting the MemoryError, but it'd be easier to
figure out if you posted the entire text of the traceback.
Traceback: 
Line 39: seg=codeSt[element:endInd+len(endStr]
MemoryError
Hehe. Helpful, no?
Actually, it was the "usual bit about in module" that might *be*
helpful. ;)  Often, something can be determined by the sequence of
function calls listed there.  Though I suppose, now that I think of
it, that since your code is all module-level, there wouldn't be any
function stack in this case...   Still, in general, when asking for
help with something that throws an exception, it's always best to copy
& paste the entire text of the exception.
One thing that I've noticed, which I thought was just a typo in your
original email but which is duplicated again here (it's not present on
the web page you linked to) -- you have a mismatched parenthesis in
your len() call.  It's "[ ... len(endstr]" -- there's no ')' to close
the function call.  Given that this typo isn't on the web page, I'm
not sure whether it's actually there in the code you're running or
not.  I'd have *thought*, however, that if it *is* present, you'd get
a syntax error rather than a memory error, so it's probably not there
but you should check it anyhow.  :)

Jeff Shannon
Technician/Programmer
Credit International
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Complex roots

2004-12-09 Thread Matt Williams
Further to my previous post, please find some code below:

Hope this helps,
Matt

#Complex number Roots
#Matt Williams 9.12.04

import math

number=complex(0.5,0)
n=float(3)

size=math.sqrt((number.real**2)+(number.imag**2))


arg=math.radians(360/n)

root=1/n

modulus=size**root

theta=float(0)
roots={}
i=0
while ihttp://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the 2 complex cube roots of 1?

2004-12-09 Thread Alan Gauld

> My trusty $10 Casio calculator tells me that the 3 cube roots of 1
are:
> 1, (-.5 +0.866025403j), and (-.5 -0.866025403j), or thereabouts. Is
there
> a way to do this in Python?

Sorry the power operation in Python will only return 1+0j
You need to dig out the math books and write a function
to return all the roots. Or hunt google to see if somebody
else has already done one...

Alan G

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI Video collection application File I/O troubles

2004-12-09 Thread Guybrush Threepwood
Quoting Chad Crabtree <[EMAIL PROTECTED]>:

> Olli Rajala wrote:
>
> >
> >
> >
> >>Ok. I tried running the script on my Apache server on Windows NT
> and IT
> >>WORKS The script saves the values of videodb keys correctly.
> DARN!!!
> >>I don't get it. Why does the exact same script work on Win and not
> on Linux.
> >>
> >>Oh, did I mention I am developing the application on Linux. And now
> I tried
> >>it on Win XP with Apache and it works. On Linux I have httpd too.
> >>
> >>
> >
> >Have you triplechecked that you really can write to the file. I
> don't
> >know how python would react, if you can't write to the file (raise
> >IOError perhaps) but it doesn't cost you anything to check that...
> :)
> >
> >Yours,

Sorry for the delay but I was busy with soemthing else.
The script prints the values of the variables fine in the
browser so there must be a problem with the file writing part.
When I run the script from my bash shell it creates the videodb
database file, but when I run it from the browser it doesn't
create no file whatsoever.
At first I ran the script as a regular user and then I tried
as root, I don't understand how the scripts writes the file
from the shell when I am root but produces no output when
I run it from the browser(as root also).
> >
> >
> You know.  I that is what happened to me once.  I could not for the
> life
> of me figure it out.  I just chmod 777 it.
>



--
The lady on the call box in Monkey Island 2
Guybrush: I'm lost in the Inky Island Jungle in Monkey 2
Lady: Just walk off the edge of the screen
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Complex roots

2004-12-09 Thread Dick Moores
VERY helpful, Matt. Thanks.

One question: This seems to not compute accurately at all when the
imaginary part of number=complex() is other than 0. For example,
number=complex(5,5):

With 
number=complex(5,5)
n=float(4)

the code produces
{0: (1.631, 0.0), 1: (0.0, 1.631), 2: (-1.631, 0.0), 3: (0.0, -1.631)}

And testing:
>>> (0.0 + 1.631j)**4
(7.0764565459210003+0j)

If that's the case, why the line
size=math.sqrt((number.real**2)+(number.imag**2)) ?

Dick


On Thu, 09 Dec 2004 11:52:15 +, Matt Williams
<[EMAIL PROTECTED]> wrote:
> Further to my previous post, please find some code below:
> 
> Hope this helps,
> Matt
> 
> #Complex number Roots
> #Matt Williams 9.12.04
> 
> import math
> 
> number=complex(0.5,0)
> n=float(3)
> 
> size=math.sqrt((number.real**2)+(number.imag**2))
> 
> arg=math.radians(360/n)
> 
> root=1/n
> 
> modulus=size**root
> 
> theta=float(0)
> roots={}
> i=0
> while i y=round(modulus*(math.sin(theta)),3)
> x=round(modulus*(math.cos(theta)),3)
> roots[i]=(x,y)
> theta=theta+arg
> i=i+1
> 
> print roots
>
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Graphics coordinates

2004-12-09 Thread Ron Phillips



I work for a Civil Engineering organization. I need to give our users a 
means to generate geographic coordinates for our projects and structures. 

 
I plan to do this by making a viewer which will accept a graphic file and 
two  points (pixels) in that image for which geographic coordinates are 
given. Users can then click on the image where appropriate to generate a list of 
geographic points of interest for their work. The list will be saveable as an 
xml file or a shp file (a widely used geographic binary format.) In addition, 
other programs that I write will use the viewer as a dialog box:  will 
call the viewer directly and get the points from it, just like a color picker 
dialog.
 
What I am looking for: thoughts on which Python modules are most 
appropriate and generally applicable for this. PIL? Piddle? PyGIS? some of 
Hobu's modules? I believe I can write the glue-code, but I don't want to 
reinvent the wheel if there are existing modules that do 
almost (or even better, exactly) what I need.
 
I work on WinXp, Linux, and WindowsCE. WindowsCE has a fairly 
plain-vanilla Python build, so it's better if I stick to the core modules as far 
as possible.
 
Ron Phillips
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Complex roots

2004-12-09 Thread Tim Peters
[Dick Moores]
> VERY helpful, Matt. Thanks.
> 
> One question: This seems to not compute accurately at all
> when the imaginary part of number=complex() is other than 0.

That's just because the math was wrong .  Starting with theta =
0.0 *assumed* the imaginary part is 0, although I can't guess whether
that was by accident or design.

Try this instead:

def croots(c, n):
"""Return list of the n n'th roots of complex c."""
from math import sin, cos, atan2, pi

arg = abs(c)**(1.0/n)
theta = atan2(c.imag, c.real)
result = []
for i in range(n):
theta2 = (theta + 2*pi*i)/n
x = arg * cos(theta2)
y = arg * sin(theta2)
result.append(complex(x, y))
return result
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI Video collection application File I/O troubles

2004-12-09 Thread Patric Michael
<>
 
> Sorry for the delay but I was busy with soemthing else.
> The script prints the values of the variables fine in the
> browser so there must be a problem with the file writing part.
> When I run the script from my bash shell it creates the videodb
> database file, but when I run it from the browser it doesn't
> create no file whatsoever.
> At first I ran the script as a regular user and then I tried
> as root, I don't understand how the scripts writes the file
> from the shell when I am root but produces no output when
> I run it from the browser(as root also).

Just out of curiosity, does the directory where the file is to be written 
have write permission for others?

Since the server (hopefully!) isnt running as root, not only the 
destination file, but the directory where it is stored must have 
permissions to allow everyone (or at least the user the server is running 
as) to write there in the first place.

Also, did you check your webservers error log?

Patric


> > >
> > >
> > You know.  I that is what happened to me once.  I could not for the
> > life of me figure it out.  I just chmod 777 it.
> >
> 
> 
> 
> --
> The lady on the call box in Monkey Island 2
> Guybrush: I'm lost in the Inky Island Jungle in Monkey 2
> Lady: Just walk off the edge of the screen
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
> 


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI Video collection application File I/O troubles

2004-12-09 Thread Alan Gauld
> When I run the script from my bash shell it creates the videodb
> database file, but when I run it from the browser it doesn't
> create no file whatsoever.

This is usually due to wrong file permissions.
The script runs under the web server and this usually 
has restricted access for security reasons. You need to 
get your sys admin (who may be you! :-) to set things 
up so your script can write OK.

Alan G.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Difference between for i in range(len(object)) and for i in object

2004-12-09 Thread kumar s
Dear group, 
  

My Tab delimited text looks like this:

HG-U95Av2   32972_at432 117
HG-U95Av2   32972_at499 631
HG-U95Av2   32972_at12  185
HG-U95Av2   32972_at326 83
HG-U95Av2   32972_at62  197


I want to capture: columns 2 and 3 as tab delim. text:


Here is my code:
>>> spot_cor=[]
>>> for m in cor:
... cols = split(cor,'\t')
... spot_cor.append(cols[2]+'\t'+cols[3])
...
...
Traceback (most recent call last):
  File "", line 2, in ?
  File "/usr/local/lib/python2.3/string.py", line 121,
in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'

Here is 2nd way:


>>> test_cor=[]
>>> for m in cor:
... cols = split(cor,'\t')
... x = (cols[2]+'\t'+cols[3])
... test_cor.append(x)
...
Traceback (most recent call last):
  File "", line 2, in ?
  File "/usr/local/lib/python2.3/string.py", line 121,
in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'



Here is my 3rd way of doing this thing:
>>> for m in range(len(cor)):
... cols = split(cor[m],'\t')
... spot_cor.append(cols[2]+'\t'+cols[3])
...
>>>
>>> len(spot_cor)
2252
>>>



My question:
 Many people suggested me to avoid  iteration over  a
object using (range(len)) its index and use instead
'Python's power' by using for i in object, instead. 

However, when I tried that using some data, as
demonstrated above, I get error because append method
does not work on list.  In method 2, i tried to append
an object instead of string elements. In both ways the
execution failed because  'List object has no
attribute split'.


Can you help me making me clear about his dogma. 


Thank you. 

Kumar.



--- Guillermo Fernandez Castellanos
<[EMAIL PROTECTED]> wrote:

> Cheers,
> 
> I think your mistake is here:
> if x == y:
>for ele3 in spot_int:
>if y in ele3:
>   
> out.write(ele3)
>   
> out.write('\n')
> Each time you find an element that is the same
> (x==y) you don't write
> only y, you write *all* the elements that are in
> spot_init instead
> only the matching one! And it's not what you are
> looking for! :-)
> 
> I'll also change a bit your code to make it look
> more "pythonic" :-)
> 
> > for ele1 in spot_cor:
> > for ele2 in spot_int:
> > cols = split(ele2,'\t')
> > y = (cols[0]+'\t'+cols[1])
> > if ele1 == y:
> > for ele3 in spot_int:
> > if y in ele3:
> >
> out.write(ele3)
> >
> out.write('\n')
> 
> What changes I did:
> 
> for ele1 in range(len(spot_cor)):
>x = spot_cor[ele1]
> 
> can be writen like:
> for ele1 in spot_cor:
> x = ele1
> 
> Furthermore, as you only use x once, I changed:
>  if x == y:
> 
> with
> if ele1 == y:
> 
> and deleted the line:
> x = ele1
> 
> I also don't understand why you do this:
> cols = split(ele2,'\t')
> y = (cols[0]+'\t'+cols[1])
> 
> It seems to me that you are separating something to
> put it again
> together. I don't really see why...
> 
> Enjoy,
> 
> Guille
> 




__ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Difference between for i in range(len(object)) and for i in object

2004-12-09 Thread Robert, Andrew
 Good morning Kumar,

I believe you need to do something like:

   #
   # Read from command line supplied input file
   #
   for line in open(sys.argv[1]).xreadlines():

#
# Remove \n from end of line
#
line=line[:-1]

#
# Break line by tab delimiter and feed to list
#
split_line_by_tab=line.split('\t')
  

You should then be able to print out list items relatively easily.


Thank you,
Andrew Robert
Systems Architect
Information Technology - OpenVMS
Massachusetts Financial Services
Phone:  617-954-5882
Pager:   781-764-7321
E-mail:  [EMAIL PROTECTED]
Linux User Number: #201204

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of kumar s
Sent: Thursday, December 09, 2004 11:51 AM
To: [EMAIL PROTECTED]
Subject: [Tutor] Difference between for i in range(len(object)) and for
i in object

Dear group, 
  

My Tab delimited text looks like this:

HG-U95Av2   32972_at432 117
HG-U95Av2   32972_at499 631
HG-U95Av2   32972_at12  185
HG-U95Av2   32972_at326 83
HG-U95Av2   32972_at62  197


I want to capture: columns 2 and 3 as tab delim. text:


Here is my code:
>>> spot_cor=[]
>>> for m in cor:
... cols = split(cor,'\t')
... spot_cor.append(cols[2]+'\t'+cols[3])
...
...
Traceback (most recent call last):
  File "", line 2, in ?
  File "/usr/local/lib/python2.3/string.py", line 121,
in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'

Here is 2nd way:


>>> test_cor=[]
>>> for m in cor:
... cols = split(cor,'\t')
... x = (cols[2]+'\t'+cols[3])
... test_cor.append(x)
...
Traceback (most recent call last):
  File "", line 2, in ?
  File "/usr/local/lib/python2.3/string.py", line 121,
in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'



Here is my 3rd way of doing this thing:
>>> for m in range(len(cor)):
... cols = split(cor[m],'\t')
... spot_cor.append(cols[2]+'\t'+cols[3])
...
>>>
>>> len(spot_cor)
2252
>>>



My question:
 Many people suggested me to avoid  iteration over  a
object using (range(len)) its index and use instead
'Python's power' by using for i in object, instead. 

However, when I tried that using some data, as
demonstrated above, I get error because append method
does not work on list.  In method 2, i tried to append
an object instead of string elements. In both ways the
execution failed because  'List object has no
attribute split'.


Can you help me making me clear about his dogma. 


Thank you. 

Kumar.



--- Guillermo Fernandez Castellanos
<[EMAIL PROTECTED]> wrote:

> Cheers,
> 
> I think your mistake is here:
> if x == y:
>for ele3 in spot_int:
>if y in ele3:
>   
> out.write(ele3)
>   
> out.write('\n')
> Each time you find an element that is the same
> (x==y) you don't write
> only y, you write *all* the elements that are in
> spot_init instead
> only the matching one! And it's not what you are
> looking for! :-)
> 
> I'll also change a bit your code to make it look
> more "pythonic" :-)
> 
> > for ele1 in spot_cor:
> > for ele2 in spot_int:
> > cols = split(ele2,'\t')
> > y = (cols[0]+'\t'+cols[1])
> > if ele1 == y:
> > for ele3 in spot_int:
> > if y in ele3:
> >
> out.write(ele3)
> >
> out.write('\n')
> 
> What changes I did:
> 
> for ele1 in range(len(spot_cor)):
>x = spot_cor[ele1]
> 
> can be writen like:
> for ele1 in spot_cor:
> x = ele1
> 
> Furthermore, as you only use x once, I changed:
>  if x == y:
> 
> with
> if ele1 == y:
> 
> and deleted the line:
> x = ele1
> 
> I also don't understand why you do this:
> cols = split(ele2,'\t')
> y = (cols[0]+'\t'+cols[1])
> 
> It seems to me that you are separating something to
> put it again
> together. I don't really see why...
> 
> Enjoy,
> 
> Guille
> 




__ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


"MFS Relay Service" made the following
 annotations on 12/09/2004 12:05:50 PM
--
This email communication and any attachments may contain proprietary, 
confidential, or privileged information.  If you are not the intended 
recipient, you are hereby notified that you have received this email in error 
and that any review, disclosure, dissemination, distribution or copying of it 
or its co

Re: [Tutor] Difference between for i in range(len(object)) and for i in object

2004-12-09 Thread Bob Gailer
At 09:50 AM 12/9/2004, kumar s wrote:
[snip]
Personally I am getting weary of a lot of requests that to me seem to come 
from a lack of understanding of Python.. Would you be willing to take a 
good tutorial so you understand basic Python concepts and apply them to 
your code.

I also despair that you don't seem to benefit from some of our suggestions.
Bob Gailer
[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell 

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Capturing Logfile data in Windows

2004-12-09 Thread Philip Kilner
Hi List,
I have a closed-source application which creates log files. I'd like to 
capture this logfile data as it is crated, and do clever things with it!

Is this possible? I guess it must be, because there are "tail" type 
utilities for Windows...

Is it possible in Python? I'd be grateful for any pointers!
BTW, it doesn't matter either way whether I can somehow pipe the log 
output to my program /without/ the file actually being created, or if I 
can capture it /as/ it is created - I just need to get my mitts on the 
data as it is generated...it would be better if I had the file as well, 
but it is not essential...

Thanks in Anticipation!
--
Regards,
PhilK
Email: [EMAIL PROTECTED] / Voicemail & Facsimile: 07092 070518
"Work as if you lived in the early days of a better nation." - Alasdair Gray
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sorting a list of dictionaries

2004-12-09 Thread Larry Holish
Hello,

I have a list of dictionaries, each representing info about a file,
something like:

[{'name':'foo.txt','size':35}, {'name':'bar.txt','size':35}, ...]

I want to present a sorted list of all the files' data, sorting on the
keys 'name' or 'size'. The file 'name' s should be unique (I'm hoping)
across all the dictionaries. Can someone point me towards an efficient
solution for accomplishing the sort? (The list has 1000s of files).

Thanks in advance,

-- 
Larry Holish
<[EMAIL PROTECTED]>
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Lock down windows with python+usb

2004-12-09 Thread Asif Iqbal
Hi All

Has anyone done any script like this? Use a python script for Windows XP
that will continuosly check if my USB is plugged in. So if I unplug my
USB flashdrive it will fork a screensaver with password lock.

Thanks for any idea/suggestion

-- 
Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
"...it said: Install Windows XP or better...so I installed Solaris..."
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Kent Johnson
If you can use Python 2.4 it is very simple using the new key= parameter to sort and 
operator.itemgetter:

>>> import operator
>>> ds = [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':36}]
>>> ds.sort(key=operator.itemgetter('name'))
>>> ds
[{'name': 'bar.txt', 'size': 36}, {'name': 'foo.txt', 'size': 35}]
>>> ds.sort(key=operator.itemgetter('size'))
>>> ds
[{'name': 'foo.txt', 'size': 35}, {'name': 'bar.txt', 'size': 36}]
Otherwise you should use decorate - sort - undecorate. The idea here is to make a new list whose 
values are pairs of (key, item) for each data item in the original list. The new list is sorted, 
then a final list is extracted containing only the items in the desired order:

>>> d2 = [ (d['name'], d) for d in ds ]
>>> d2.sort()
>>> ds = [ d for (name, d) in d2 ]
>>> ds
[{'name': 'bar.txt', 'size': 36}, {'name': 'foo.txt', 'size': 35}]
Kent
Larry Holish wrote:
Hello,
I have a list of dictionaries, each representing info about a file,
something like:
[{'name':'foo.txt','size':35}, {'name':'bar.txt','size':35}, ...]
I want to present a sorted list of all the files' data, sorting on the
keys 'name' or 'size'. The file 'name' s should be unique (I'm hoping)
across all the dictionaries. Can someone point me towards an efficient
solution for accomplishing the sort? (The list has 1000s of files).
Thanks in advance,
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Karl Pflästerer
On  9 Dez 2004, [EMAIL PROTECTED] wrote:

> I have a list of dictionaries, each representing info about a file,
> something like:
>
> [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':35}, ...]
>
> I want to present a sorted list of all the files' data, sorting on the
> keys 'name' or 'size'. The file 'name' s should be unique (I'm hoping)
> across all the dictionaries. Can someone point me towards an efficient
> solution for accomplishing the sort? (The list has 1000s of files).

That's easy to achieve, since sort takes a custom sort function as
optional argument.  Now you need only a function which takes the values
of the fileds and compares them.

E.g.

lst.sort(lambda m, n: cmp(m.get(field), n.get(field)))
where field is either 'name' or 'size'.

As a function:

def sort_it (lst, field):
lst.sort(lambda m, n: cmp(m.get(field), n.get(field)))


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help matching elements from two lists and printing them

2004-12-09 Thread Jeff Shannon
kumar s wrote:
On top of this this process is VERY SLOW on high end
server too. 
That's because, for each line in spot_cor, you're examining every item 
in spot_int, and if there's a match, you examine every element in 
spot_int again!  If I'm remembering my big-O notation correctly, that 
makes this O(n*m + m) -- even if it simplifies to O(n*m) (which I 
think it may), that's still quadratic time -- as the length of your 
lists grows, your performance will degrade *very* rapidly.

Here's a simplified version of your code.  I've changed the for loops 
so that they iterate over the lists directly, instead of iterating 
over a range and then using that number to index into the list -- the 
result is the same, but this way is less extra work and easier to read.

out = open('sa_int_2.txt','w')
for cor_ele in spot_cor:
for int_ele in spot_int:
cols = split(int_ele, '\t')
y = cols[0] + '\t' + cols[1]
if x == y:
for int_ele2 in spot_int:
if y in int_ele2:
out.write(int_ele2 + '\n')
Remember, every time you use 'in', it means traversing the entire 
list.  Multiple levels of nesting statements that use 'in' causes a 
polynomial expansion of work.

Now, here's how I'd rewrite your code.
out = open('sa_int_2.txt','w')
element_dict = {}
for line in spot_int:
cols = split(line,'\t')
key = '\t'.join(cols[:2])
element_dict[key] = line
for cor in spot_cor:
try:
value = element_dict[cor]
out.write(value + '\n')
except KeyError:
pass
out.close()
Note that I iterate through each list only once, so my timing should 
be O(n + m).  First, I preprocess spot_int by putting it into a 
dictionary.  I create keys for the dictionary that are identical to 
what will be in spot_cor.  Then I iterate through spot_cor, and match 
each element with what's in the dictionary I've made.  If I find 
something that matches, I write it out; if I don't, then I simply move 
on to the next item in spot_cor.

This does make several assumptions.  First, it assumes that every line 
in spot_int will be unique in the first two columns -- that is, I'm 
assuming that nowhere in the file would you find lines like the following:

 5 0 123
 5 0 456
If these cases *do* happen, then all but the last such line would be 
lost, and only the last one would be left in the dictionary (and thus 
found and written out).  In your version, all lines whose first two 
columns match will be written out.  (This may be the source of your 
"extra" results.)

If you *do* need to handle lines like this, then it wouldn't be hard 
to modify my code for it.  Each value in the dictionary would become a 
list, and matching lines are appended to that list.  The line 
'element_dict[key] = line' becomes 'element_dict.setdefault(key, 
[]).append(line)', and writing to the file becomes a for-loop instead 
of a single write.

The other assumption that I make is that these files are all of 
reasonable size to fit into memory.  Your code makes this same 
assumption, too, of course.  This assumption should be valid for up to 
a few tens of thousands (maybe even hundreds of thousands, depending 
on your hardware) of items.  If you have more than this, then your 
best bet would be to start using a real database.  (I've heard good 
things about mysql and sqlite support in Python...)

Jeff Shannon
Technician/Programmer
Credit International

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2004-12-09 Thread Jeff Shannon
Liam Clarke wrote:
So, I'm going to throw caution to the wind, and try an re approach. It
can't be any more unwieldy and ugly than what I've got going at the
moment.
If you're going to try a new approach, I'd strongly suggest using a 
proper html/xml parser instead of re's.  You'll almost certainly have 
an easier time using a tool that's designed for your specific problem 
domain than you will trying to force a more general tool to work. 
Since you're specifically trying to find (and replace) certain html 
tags and attributes, and that's exactly what html parsers *do*, well, 
the conclusions seems obvious (to me at least). ;)

There are lots of html parsing tools available in Python (though I've 
never needed one myself). I've heard lots of good things about 
BeautifulSoup...

Jeff Shannon
Technician/Programmer
Credit International
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between for i in range(len(object)) and for i in object

2004-12-09 Thread Jeff Shannon
kumar s wrote:
 > Here is my code:

spot_cor=[]
Create an empty list...
for m in cor:
Now, for each element in some other list from somewhere else,
... cols = split(cor,'\t')
Ignore the element we've just isolated and try to split the entire 
list on '\t' ...

Traceback (most recent call last):
  File "", line 2, in ?
  File "/usr/local/lib/python2.3/string.py", line 121,
in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'
Yup, the list 'cor' doesn't have a split() method.  The string that 
presumably should be in 'm' would have such a method, though.


Here is 2nd way:

test_cor=[]
for m in cor:
... cols = split(cor,'\t')
Once again, you're operating on the list, not the element of the list.
Here is my 3rd way of doing this thing:
for m in range(len(cor)):
... cols = split(cor[m],'\t')
Here, you *are* operating on an element of the list.  But if I were to 
write this segment to be parallel to your previous two attempts, it 
would be:

cols = split(cor[cor],'\t')
which probably wouldn't surprise you when it doesn't work...

My question:
 Many people suggested me to avoid  iteration over  a
object using (range(len)) its index and use instead
'Python's power' by using for i in object, instead. 

However, when I tried that using some data, as
demonstrated above, I get error because append method
does not work on list.
No -- look carefully at the tracebacks, and you'll see that nowhere 
does it complain about append(), nor does it give the line numbers in 
which the append() takes place.  It complains about split(), and tells 
you exactly which line the problem is on ('File "", line 2, in 
?' -- since you're doing this in an interpreter, there's no filename 
or function name).

Jeff Shannon
Technician/Programmer
Credit International
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Kent Johnson
Using sort() with a user compare function is not recommended when you care about performance. The 
problem is that the sort function has to call back into Python code for every compare, of which 
there are many. The decorate - sort - undecorate idiom is the preferred way to do this in Python < 
2.4. Python 2.4 adds the key= parameter to sort() and does the DSU internally.

Ironically, the best way to speed up Python code is often to eliminate it - the more work you can do 
in the C runtime, the faster your code will run. Sometimes an approach that seems to do more work - 
like DSU, which builds two extra lists - is actually faster because it does more in C code.

Here is a program to time the three methods. On my machine, the ratio of times of sort1 : sort2 : 
sort3 is pretty consistently about 1 : 2 : 6-7, i.e. sort with key parameter is twice as fast as DSU 
which is three times faster than sort with cmp.

A couple of interesting observations:
- The ratio of sort times is independent of the length of the list. I expected that the performance 
of sort3 would get progressively worse as the list got longer because I expect that the cmp function 
would be called a number of times that is O(n*logn). Apparently this is not the case.

- If the list is large and already sorted, sort3 is significantly faster than sort2 - it takes about 
2/3 the time of sort2. You can try this by taking out the call to random.shuffle().

Kent
import operator, random, timeit
a = range(1)
random.shuffle(a)
dlist = [ {'name':str(i), 'size':i} for i in a ]
def sort1(ds):
''' Sort using Python 2.4 key argument '''
ds.sort(key=operator.itemgetter('size'))
def sort2(ds):
''' Sort using DSU '''
d2 = [ (d['size'], d) for d in ds ]
d2.sort()
ds[:] = [ d for (name, d) in d2 ]
def sort3(ds):
''' Sort using cmp argument to sort '''
ds.sort(lambda m, n: cmp(m['size'], n['size']))
def timeOne(fn):
setup = "from __main__ import " + fn.__name__ + ',dlist'
stmt = fn.__name__ + '(dlist[:])'
t = timeit.Timer(stmt, setup)
secs = min(t.repeat(3, 10))
print '%s: %f secs' % (fn.__name__, secs)
# Make sure they all give the same answer
a1=dlist[:]
sort1(a1)
a2=dlist[:]
sort2(a2)
a3=dlist[:]
sort3(a3)
assert a1 == a2
assert a3 == a2
timeOne(sort1)
timeOne(sort2)
timeOne(sort3)
Karl Pflästerer wrote:
On  9 Dez 2004, [EMAIL PROTECTED] wrote:

I have a list of dictionaries, each representing info about a file,
something like:
[{'name':'foo.txt','size':35}, {'name':'bar.txt','size':35}, ...]
I want to present a sorted list of all the files' data, sorting on the
keys 'name' or 'size'. The file 'name' s should be unique (I'm hoping)
across all the dictionaries. Can someone point me towards an efficient
solution for accomplishing the sort? (The list has 1000s of files).

That's easy to achieve, since sort takes a custom sort function as
optional argument.  Now you need only a function which takes the values
of the fileds and compares them.
E.g.
lst.sort(lambda m, n: cmp(m.get(field), n.get(field)))
where field is either 'name' or 'size'.
As a function:
def sort_it (lst, field):
lst.sort(lambda m, n: cmp(m.get(field), n.get(field)))
   Karl
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Removing/Handing large blocks of text

2004-12-09 Thread Jesse Noller
On Wed, 8 Dec 2004 15:11:55 +, Max Noel <[EMAIL PROTECTED]> wrote:
> 
> 
> 
> On Dec 8, 2004, at 14:42, Jesse Noller wrote:
> 
> > Hello,
> >
> > I'm trying to do some text processing with python on a farily large
> > text file (actually, XML, but I am handling it as plaintext as all I
> > need to do is find/replace/move) and I am having problems with trying
> > to identify two lines in the text file, and remove everything in
> > between those two lines (but not the two lines) and then write the
> > file back (I know the file IO part).
> 
> Okay, here are some hints: you need to identify when you enter a 
> block and when you exit a  block, keeping in mind that this may
> happen on the same line (e.g. blah). The rest is trivial.
> The rest of your message is included as a spoiler space if you want to
> find the solution by yourself -- however, a 17-line program that does
> that is included at the end of this message. It prints the resulting
> file to the standard out, for added flexibility: if you want the result
> to be in a file, just redirect stdout (python blah.py > out.txt).
> 
> Oh, one last thing: don't use readlines(), it uses up a lot of memory
> (especially with big files), and you don't need it since you're reading
> the file sequentially. Use the file iterator instead.
> 
> 
> 
> > I'm trying to do this with the re module - the two tags looks like:
> >
> > 
> > ...
> > a bunch of text (~1500 lines)
> > ...
> > 
> >
> > I need to identify the first tag, and the second, and unconditionally
> > strip out everything in between those two tags, making it look like:
> >
> > 
> > 
> >
> > I'm familiar with using read/readlines to pull the file into memory
> > and alter the contents via string.replace(str, newstr) but I am not
> > sure where to begin with this other than the typical open/readlines.
> >
> > I'd start with something like:
> >
> > re1 = re.compile('^\')
> > re2 = re.compile('^\<\/foo\>')
> >
> > f = open('foobar.txt', 'r')
> > for lines in f.readlines()
> > match = re.match(re1, line)
> >
> > But I'm lost after this point really, as I can identify the two lines,
> > but I am not sure how to do the processing.
> >
> > thank you
> > -jesse
> > ___
> > Tutor maillist  -  [EMAIL PROTECTED]
> > http://mail.python.org/mailman/listinfo/tutor
> 
> #!/usr/bin/env python
> 
> import sre
> 
> reStart = sre.compile('^\s*\')
> reEnd = sre.compile('\\s*$')
> 
> inBlock = False
> 
> fileSource = open('foobar.txt')
> 
> for line in fileSource:
>  if reStart.match(line): inBlock = True
>  if not inBlock: print line
>  if reEnd.match(line): inBlock = False
> 
> fileSource.close()
> 
> -- Max
> maxnoel_fr at yahoo dot fr -- ICQ #85274019
> "Look at you hacker... A pathetic creature of meat and bone, panting
> and sweating as you run through my corridors... How can you challenge a
> perfect, immortal machine?"
> 
> 


Thanks a bunch for all of your fast responses, they helped a lot -
I'll post what I cook up back to the list as soon as I complete it.
Thanks!

-jesse
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Larry Holish
On Thu, Dec 09, 2004 at 03:22:29PM -0500, Kent Johnson wrote:
> Using sort() with a user compare function is not recommended when you
> care about performance. The problem is that the sort function has to
> call back into Python code for every compare, of which there are many.
> The decorate - sort - undecorate idiom is the preferred way to do this
> in Python < 2.4.  Python 2.4 adds the key= parameter to sort() and
> does the DSU internally.

Karl, Kent, thanks for your prompt responses. I'm running python 2.3,
and the DSU method performs nicely.
 
Regards,
Larry
-- 
Larry Holish
<[EMAIL PROTECTED]>
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2004-12-09 Thread Liam Clarke
Hi all, 

Yeah, I should've written this in functions from the get go, but I
thought it would be a simple script. :/

I'll come back to that script when I've had some sleep, my son was
recently born and it's amazing how dramatically lack of sleep affects
my acuity. But, I want to figure out what's going wrong.

That said, the re path is bearing fruit. I love the method finditer(),
 as I can reduce my overly complicated string methods from my original
code to

x=file("toolkit.txt",'r')
s=x.read() 
x.close()
appList=[]

regExIter=reObj.finditer(s) #Here's a re obj I compiled earlier. 

for item in regExIter:
   text=gettextFunc(item.group()) #Will try and stick to string method
for this, but I'll see.
   if not text:
  text="Default" #Will give a text value for the href, so some
lucky human can change it
   url=geturlFunc(item.group()) # The simpler the better, and so far
re has been the simplest
   if not url:
 href = '"" #This will delete the applet, as there are applet's
acting as placeholders
   else:
 href='%s' % (url, text)

   appList.append(item.span(), href)

appList.reverse()

for ((start, end), href) in appList:

 codeSt=codeSt.replace(codeSt[start:end], href)


Of course, that's just a rought draft, but it seems a whole lot
simpler to me. S'pose code needs a modicum of planning.

Oh, and I d/led BeautifulSoup, but I couldn't work it right, so I
tried re, and it suits my needs.

Thanks for all the help.

Regards,

Liam Clarke
On Thu, 09 Dec 2004 11:53:46 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote:
> Liam Clarke wrote:
> 
> > So, I'm going to throw caution to the wind, and try an re approach. It
> > can't be any more unwieldy and ugly than what I've got going at the
> > moment.
> 
> If you're going to try a new approach, I'd strongly suggest using a
> proper html/xml parser instead of re's.  You'll almost certainly have
> an easier time using a tool that's designed for your specific problem
> domain than you will trying to force a more general tool to work.
> Since you're specifically trying to find (and replace) certain html
> tags and attributes, and that's exactly what html parsers *do*, well,
> the conclusions seems obvious (to me at least). ;)
> 
> There are lots of html parsing tools available in Python (though I've
> never needed one myself). I've heard lots of good things about
> BeautifulSoup...
> 
> 
> 
> Jeff Shannon
> Technician/Programmer
> Credit International
> 
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Capturing Logfile data in Windows

2004-12-09 Thread Liam Clarke
You could have something along the lines of -

import os.path
import time

curTime = os.path.getmtime('log.txt') #Or whatever it's called.

while 1: #or some other loop
   time.sleep(10) #Check logfile every ten seconds.
   if  os.path.getmtime('log.txt') != curTime:
  curTime = os.path.getmtime('log.txt')
  x=file('log.txt','r')...


So basically, just check the modification time. If  the modification
time has changed, then the file's been updated.

It's one way to do it. : )

Liam Clarke

On Thu, 09 Dec 2004 17:28:56 +, Philip Kilner <[EMAIL PROTECTED]> wrote:
> Hi List,
> 
> I have a closed-source application which creates log files. I'd like to
> capture this logfile data as it is crated, and do clever things with it!
> 
> Is this possible? I guess it must be, because there are "tail" type
> utilities for Windows...
> 
> Is it possible in Python? I'd be grateful for any pointers!
> 
> BTW, it doesn't matter either way whether I can somehow pipe the log
> output to my program /without/ the file actually being created, or if I
> can capture it /as/ it is created - I just need to get my mitts on the
> data as it is generated...it would be better if I had the file as well,
> but it is not essential...
> 
> Thanks in Anticipation!
> 
> --
> 
> Regards,
> 
> PhilK
> 
> Email: [EMAIL PROTECTED] / Voicemail & Facsimile: 07092 070518
> 
> "Work as if you lived in the early days of a better nation." - Alasdair Gray
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lock down windows with python+usb

2004-12-09 Thread Chad Crabtree
Asif Iqbal wrote:

>Hi All
>
>Has anyone done any script like this? Use a python script for
Windows XP
>that will continuosly check if my USB is plugged in. So if I unplug
my
>USB flashdrive it will fork a screensaver with password lock.
>
>Thanks for any idea/suggestion
>
>  
>
You could make screensaver program that is placed in the taskbar that

periodicly check for the usbkey.  I would look at wxwindows or
pygame, 
you would makea  full screen ap,  that somehow captures all the key 
presses except username password thing.

HTH



__ 
Do you Yahoo!? 
Read only the mail you want - Yahoo! Mail SpamGuard. 
http://promotions.yahoo.com/new_mail 
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between for i in range(len(object)) and for i inobject

2004-12-09 Thread Alan Gauld
> Here is my code:
> >>> spot_cor=[]
> >>> for m in cor:
> ... cols = split(cor,'\t')

You are splitting the list not the item

  cols = split(m, '\t')

Better to use a meaningful name too:

for line in cor:

would probably have made the mistake more obvious.

> However, when I tried that using some data, as
> demonstrated above, I get error because append method

No its the spil method that doesn't exist and that 
was because you were passing the list instead of 
the object.

Once you get used to the "foreach" semantics of "for"
it really is a better way of working than messing 
around with indexes.

Alan G.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between for i in range(len(object)) andfor i in object

2004-12-09 Thread Alan Gauld
> Personally I am getting weary of a lot of requests that to me seem
to come
> from a lack of understanding of Python..

To be fair that is what the tutor list is for - learning Python.

> Would you be willing to take a good tutorial so you understand
> basic Python concepts and apply them to your code.

But as a tutor author I do agree that I am often tempted
(and sometimes succumb) to just point at the relevant topic
in my tutorial. Particularly since the latest version tries
to answer all of the most common questions asked here, but
still they come up...

> I also despair that you don't seem to benefit from some of our
suggestions.

And this too can be frustrating but sometimes it is the case
that the "student" simply didn't fully appreciate the
significance of what was offered. I'm feeling generous tonight!

:-)

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Capturing Logfile data in Windows

2004-12-09 Thread Alan Gauld
> I have a closed-source application which creates log files. I'd like
to
> capture this logfile data as it is crated, and do clever things with
it!
>
> Is this possible? I guess it must be, because there are "tail" type
> utilities for Windows...

Depends on whether the output goes to stdout or stderr or a hard coded
file. Usually log files are hard coded so you need to write a tail
like
utility. You could do this but why not just download a free one such
as that from cygwin?

If you must write your own start off by looking at the C source code
for the standard Linux/Cygwin tail program to see how they do it.
Its probably easy to convert that to Python and safer than
guessing the best approach...

Alan G.

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem with python2.4.

2004-12-09 Thread Jacob S.
Hi all.

Nothing I can do can fix my problem. It appears as though pythonw.exe is
not working properly in the python 2.4 distribution. I have tried numerous
things to try and fix it. The only way I can run Tk scripts and the like is
to use the pythonw.exe from the 2.3 distribution. This includes idle. I am
running Windows XP and I still can't figure out what's wrong. Nothing has
changed with it since the release version of python2.4 to the final version.
Can anyone help me with my predicament? The symptoms: I click on edit with
idle--which runs the command "C:\python24\pythonw.exe"
"C:\python24\lib\idlelib\idle.pyw" -n -e "%1" --then the computer thinks for
a bit and is silent. I hit Ctrl-Alt-Del and the task manager pops up. I look
in processes, and nothing about pythonw.exe is in there like it should be.
What should I do?

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with python2.4.

2004-12-09 Thread Orri Ganel
On Thu, 9 Dec 2004 19:31:22 -0500, Jacob S. <[EMAIL PROTECTED]> wrote:
> Hi all.
> 
> Nothing I can do can fix my problem. It appears as though pythonw.exe is
> not working properly in the python 2.4 distribution. I have tried numerous
> things to try and fix it. The only way I can run Tk scripts and the like is
> to use the pythonw.exe from the 2.3 distribution. This includes idle. I am
> running Windows XP and I still can't figure out what's wrong. Nothing has
> changed with it since the release version of python2.4 to the final version.
> Can anyone help me with my predicament? The symptoms: I click on edit with
> idle--which runs the command "C:\python24\pythonw.exe"
> "C:\python24\lib\idlelib\idle.pyw" -n -e "%1" --then the computer thinks for
> a bit and is silent. I hit Ctrl-Alt-Del and the task manager pops up. I look
> in processes, and nothing about pythonw.exe is in there like it should be.
> What should I do?
> 
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
> 

I had a similar problem with some of the early versions of 2.4 (beta,
release candidate), and it seems to me, looking back, that some of the
other programs I had running simultaneously blocked the sockets or
something . . . In any case, I usually made sure I wasn't running any
other programs when *opening* IDLE, and, if that didn't help, I
restarted the computer. However, even when the IDLE shell didn't work
properly, the "Edit with IDLE" functioned operated smoothly, so I
can't say for sure that it will help. Have you tried emailing the
idle-dev mailing list?

HTH,
Orri

-- 
Email: [EMAIL PROTECTED]
AIM: singingxduck
Programming Python for the fun of it.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PDF and Python

2004-12-09 Thread Jason Child
Hey there. Does anyone know of a way to output PDFs with python? I have some
data that I have processed from a series of textfiles that I would like to
provide PDF format reports for..

Jason Christopher Child

Computer Network Services Professionals
Tech Support
505-986-1669
1-877-321-9165
[EMAIL PROTECTED]

VOZ Online
VOIP Install Tech
505-428-7500
1-877-428-7550

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2004-12-09 Thread Kent Johnson
Liam,
Here's a nifty re trick for you. The sub() method can take a function as the replacement parameter. 
Instead of replacing with a fixed string, the function is called with the match object. Whatever 
string the function returns, is substituted for the match. So you can simplify your code a bit, 
something like this:

def replaceTag(item):   # item is a match object
# This is exactly your code
text=gettextFunc(item.group()) #Will try and stick to string method
 for this, but I'll see.
if not text:
   text="Default" #Will give a text value for the href, so some
 lucky human can change it
url=geturlFunc(item.group()) # The simpler the better, and so far
 re has been the simplest
if not url:
  href = '"" #This will delete the applet, as there are applet's
 acting as placeholders
else:
  href='%s' % (url, text)
# Now return href
return href
now your loop and replacements get replaced by the single line
codeSt = reObj.sub(replaceTag, codeSt)
:-)
Kent
Liam Clarke wrote:
Hi all, 

Yeah, I should've written this in functions from the get go, but I
thought it would be a simple script. :/
I'll come back to that script when I've had some sleep, my son was
recently born and it's amazing how dramatically lack of sleep affects
my acuity. But, I want to figure out what's going wrong.
That said, the re path is bearing fruit. I love the method finditer(),
 as I can reduce my overly complicated string methods from my original
code to
x=file("toolkit.txt",'r')
s=x.read() 
x.close()
appList=[]

regExIter=reObj.finditer(s) #Here's a re obj I compiled earlier. 

for item in regExIter:
   text=gettextFunc(item.group()) #Will try and stick to string method
for this, but I'll see.
   if not text:
  text="Default" #Will give a text value for the href, so some
lucky human can change it
   url=geturlFunc(item.group()) # The simpler the better, and so far
re has been the simplest
   if not url:
 href = '"" #This will delete the applet, as there are applet's
acting as placeholders
   else:
 href='%s' % (url, text)
   appList.append(item.span(), href)
appList.reverse()
for ((start, end), href) in appList:
 codeSt=codeSt.replace(codeSt[start:end], href)
Of course, that's just a rought draft, but it seems a whole lot
simpler to me. S'pose code needs a modicum of planning.
Oh, and I d/led BeautifulSoup, but I couldn't work it right, so I
tried re, and it suits my needs.
Thanks for all the help.
Regards,
Liam Clarke
On Thu, 09 Dec 2004 11:53:46 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote:
Liam Clarke wrote:

So, I'm going to throw caution to the wind, and try an re approach. It
can't be any more unwieldy and ugly than what I've got going at the
moment.
If you're going to try a new approach, I'd strongly suggest using a
proper html/xml parser instead of re's.  You'll almost certainly have
an easier time using a tool that's designed for your specific problem
domain than you will trying to force a more general tool to work.
Since you're specifically trying to find (and replace) certain html
tags and attributes, and that's exactly what html parsers *do*, well,
the conclusions seems obvious (to me at least). ;)
There are lots of html parsing tools available in Python (though I've
never needed one myself). I've heard lots of good things about
BeautifulSoup...

Jeff Shannon
Technician/Programmer
Credit International
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] test

2004-12-09 Thread Jason Child
test

Jason Christopher Child

Computer Network Services Professionals
Tech Support 
505-986-1669
1-877-321-9165
[EMAIL PROTECTED]

VOZ Online
VOIP Install Tech
505-428-7500
1-877-428-7550

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with python2.4.

2004-12-09 Thread Danny Yoo


On Thu, 9 Dec 2004, Jacob S. wrote:



> Nothing I can do can fix my problem. It appears as though
> pythonw.exe is not working properly in the python 2.4 distribution.

[some text cut]

> The symptoms: I click on edit with
> idle--which runs the command "C:\python24\pythonw.exe"
> "C:\python24\lib\idlelib\idle.pyw" -n -e "%1" --then the computer thinks for
> a bit and is silent.


Hi Jacob,


>From the symptom report, I would not automatically suspect pythonw: I'd
instead look at IDLE.


You might be running into the same kind of issue that Mike Hansen was
running into just a few days ago:

http://mail.python.org/pipermail/tutor/2004-December/033672.html


We were able to diagnose the problem by doing this:

http://mail.python.org/pipermail/tutor/2004-December/033726.html


Can you try the same procedure?  Even if it doesn't work, the information
that Python responses with should give us a better idea of what is going
on.  Try opening up a regular console version of Python, and see what
happens when you do:

###
>>> import idlelib.PyShell
>>> idlelib.PyShell.main()
###



Good luck to you!

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with python2.4.

2004-12-09 Thread Jeff Shannon
Orri Ganel wrote:
On Thu, 9 Dec 2004 19:31:22 -0500, Jacob S. <[EMAIL PROTECTED]> wrote:
[...]  It appears as though pythonw.exe is
not working properly in the python 2.4 distribution. 

I had a similar problem with some of the early versions of 2.4 (beta,
release candidate), and it seems to me, looking back, that some of the
other programs I had running simultaneously blocked the sockets or
something . . . 
Seeing this comment reminded me of some conversations I've seen in 
comp.lang.python recently.  Apparently newer versions of IDLE create a 
subprocess to run user-code in (so that IDLE runs in a different 
interpreter than the code you type into IDLE), and communicates with 
that subprocess through sockets on the loopback interface (that is, 
the 'network connection' that connects only to itself).  Overly 
aggressive firewall programs may block those socket operations.

I'd check whether XP's built-in firewall is enabled, and if so, check 
whether it might be blocking connections to loopback / localhost / 
127.0.0.1 (all of these indicate the same thing).

Jeff Shannon
Technician/Programmer
Credit International
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PDF and Python

2004-12-09 Thread Kent Johnson
The reportlab toolkit is frequently recommended, though I haven't tried it 
myself.
http://www.reportlab.org/
Kent
Jason Child wrote:
Hey there. Does anyone know of a way to output PDFs with python? I have some
data that I have processed from a series of textfiles that I would like to
provide PDF format reports for..
Jason Christopher Child
Computer Network Services Professionals
Tech Support
505-986-1669
1-877-321-9165
[EMAIL PROTECTED]
VOZ Online
VOIP Install Tech
505-428-7500
1-877-428-7550
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Capturing Logfile data in Windows

2004-12-09 Thread Guillermo Fernandez Castellanos
Hi,

If you want an easy way of iterating over stdin, you may be interested
in using this module:
http://www.python.org/dev/doc/devel/lib/module-fileinput.html

I've used it with some of my scripts to do something similar.

It will not work for a growing file though...

Otherwise, you can always try something like this (I also use this method):
import sys

 if a_file_has_been_passed_as_an_argument :
myinput=file(os.path.expanduser(args[0]),'r')
elif otherwise:
myinput=sys.stdin

# I have to admit I did not know how to code the EOF, so I used the
# 'while line != ""' instead of 'while not EOF'
line="a"
while line!="":
line = myinput.readline()
print line[:-1]

Enjoy,

Guille




On Thu, 09 Dec 2004 17:28:56 +, Philip Kilner <[EMAIL PROTECTED]> wrote:
> Hi List,
> 
> I have a closed-source application which creates log files. I'd like to
> capture this logfile data as it is crated, and do clever things with it!
> 
> Is this possible? I guess it must be, because there are "tail" type
> utilities for Windows...
> 
> Is it possible in Python? I'd be grateful for any pointers!
> 
> BTW, it doesn't matter either way whether I can somehow pipe the log
> output to my program /without/ the file actually being created, or if I
> can capture it /as/ it is created - I just need to get my mitts on the
> data as it is generated...it would be better if I had the file as well,
> but it is not essential...
> 
> Thanks in Anticipation!
> 
> --
> 
> Regards,
> 
> PhilK
> 
> Email: [EMAIL PROTECTED] / Voicemail & Facsimile: 07092 070518
> 
> "Work as if you lived in the early days of a better nation." - Alasdair Gray
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PDF and Python

2004-12-09 Thread Alan Gauld

> Hey there. Does anyone know of a way to output PDFs with python? I
have some
> data that I have processed from a series of textfiles that I would
like to
> provide PDF format reports for..

I can't recall what its called but a couple of years ago I found a
module on the Parnassus site for processing PDFs. It was really for
extracting data from a PDF but it might be able to write them too.

HTH,

Alan g.

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor