[Tutor] MemoryError

2004-12-08 Thread Liam Clarke
Hi all, 

I'm playing with a file, and attempting to replace a section with a
string, and using the following command -

seg=codeSt[element:endInd+len(endStr]
codeSt=codeSt.replace(seg, hrefString)

At the replace, I get a MemoryError. It's part of a for loop, but it
gives the error the first time around.

The Python docs say - "Raised when an operation runs out of memory but
the situation may still be rescued (by deleting some objects). The
associated value is a string indicating what kind of (internal)
operation ran out of memory. Note that because of the underlying
memory management architecture (C's malloc() function), the
interpreter may not always be able to completely recover from this
situation; it nevertheless raises an exception so that a stack
traceback can be printed, in case a run-away program was the cause. "

codeSt is a loaded text file that's 228Kb in size. The for loop that
the above code is embedded in, generates a slightly new codeSt each
time.

Now, I had imagined that codeSt=codeSt+10 would destroy the old codeSt
in memory and create a new codeSt. Am I right?

Because, even if the for loop (which will iterate 267 times) were to
keep a new copy of codeSt in memory,  it should only need 59Mb of RAM,
when I've got near half a gig of RAM and virtual memory.

So yeah, I don't know how to troubleshoot this one.

Full code is here - 

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

Any help that can be offered gratefully appreciated. I don't want to
to have to learn C++? Malloc has an evil reputation.

Oh, and Python 2.3.4, Windows XP Pro SP1, Athlon 650MHz, 256Mb RAM
20Gb HD, which has 1.6Gb free. In case any of that infos relevant.

: )

Regards

Liam Clarke
-- 
'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-08 Thread Michael Janssen
[forgot to cc to the list]


-- Forwarded message --
From: Michael Janssen <[EMAIL PROTECTED]>
Date: Wed, 8 Dec 2004 14:40:46 +0100
Subject: Re: [Tutor] MemoryError
To: Liam Clarke <[EMAIL PROTECTED]>


On Wed, 8 Dec 2004 23:29:38 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote:

> I'm playing with a file, and attempting to replace a section with a
> string, and using the following command -
>
> seg=codeSt[element:endInd+len(endStr]
> codeSt=codeSt.replace(seg, hrefString)
>
> At the replace, I get a MemoryError. It's part of a for loop, but it
> gives the error the first time around.

Hello Liam,

You might try to narrow the code in where the error happen. Read the
file and replace a dummy-seq-string and see what happens. Does this
work:

"test string".replace("st st", "?")

When it works, what is bad with your file? Consider asking
comp.lang.python for a greater audience.

BTW: some time past the base64 module (base64 is a mail attachment
encoding) has a bug leading to a MemoryError when undecoding an empty
base64 string. Any free GB of ram did not help.

You can allways work arround the replace:

new = old[:start] + relace_string + old[end:]

which is ugly but perhaps makes your script running.

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


[Tutor] Removing/Handing large blocks of text

2004-12-08 Thread Jesse Noller
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).

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


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

2004-12-08 Thread Max Noel
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?"

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


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

2004-12-08 Thread Kent Johnson
I would use a loop with a flag to indicate whether you are in the  block or not. If the tags 
are always  and  on a line by themselves you don't need an re:

lines = []
appending = True
f = open('foobar.txt', 'r')
for line in f:
  if appending:
lines.append(line)
if line.strip() == '':
  appending = False
  elif line.strip() == '':
appending = True
lines.append(line)
f.close()
At the end of this loop lines will have the lines you want to write back.
Kent
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).
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
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Dick Moores
I got this error msg for this line of code:
n = -(2(a**3.0)/27.0 - a*b/3.0 + c)
(where a = 1, b = 2, c = 3)
And was baffled until I realized the line should be
n = -(2*(a**3.0)/27.0 - a*b/3.0 + c)
But I still don't understand what "callable" means. Can someone help?
Thanks,
Dick Moores
[EMAIL PROTECTED]
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Max Noel
On Dec 8, 2004, at 17:01, Dick Moores wrote:
I got this error msg for this line of code:
n = -(2(a**3.0)/27.0 - a*b/3.0 + c)
(where a = 1, b = 2, c = 3)
And was baffled until I realized the line should be
n = -(2*(a**3.0)/27.0 - a*b/3.0 + c)
But I still don't understand what "callable" means. Can someone help?
	Basically, when you try to execute your first line, the program tries 
to call the function 2 on the argument (a**3.0). Which of course fails, 
because 2 is an int, not a "callable" object (function, method, lambda 
or class). Hence "'int' object is not callable".

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

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


Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Kent Johnson
A callable is something that can be called with functional notation. It can be a function, a class, 
or in some cases a class instance. In general, any object that has a __call__() special method is 
callable. The callable() built-in tells you if an object is callable, though you can also just try 
it. For example,

the built-in len is callable and has a __call__ attribute:
>>> callable(len)
True
>>> dir(len)
['__call__', ... ]
1 is not callable and does not have a __call__ attribute:
>>> callable(1)
False
>>> dir(1)
[ ]
As you discovered, trying to call 1 as a function doesn't work:
>>> 1()
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: 'int' object is not callable
A user-defined function is callable and has a __call__ attribute:
>>> def f(): pass
...
>>> callable(f)
True
>>> dir(f)
['__call__', ... ]
>>>
A class is callable (you call it to create an instance):
>>> class C:
...   pass
...
>>> callable(C)
True
Instances of a class, in general, are not callable:
>>> c=C()
>>> callable(c)
False
You can make a class whose instances are callable by defining a __call__ method on the class. This 
allows you to make a class whose instances behave like functions, which is sometimes handy. (This 
behaviour is built-in to Python - __call__() is called a special method. There are many special 
methods that let you customize the behaviour of a class.)

>>> class D:
...   def __call__(self, x):
... print 'x =', x
...
>>> d=D()  # Calling the class creates an instance
>>> callable(d)
True
>>> d(3)  # Calling the instance ends up in the __call__() method of the class
x = 3
Kent
Dick Moores wrote:
I got this error msg for this line of code:
n = -(2(a**3.0)/27.0 - a*b/3.0 + c)
(where a = 1, b = 2, c = 3)
And was baffled until I realized the line should be
n = -(2*(a**3.0)/27.0 - a*b/3.0 + c)
But I still don't understand what "callable" means. Can someone help?
Thanks,
Dick Moores
[EMAIL PROTECTED]
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Another ? on large text files

2004-12-08 Thread Kooser, Ara S
I have a large file (4.1 MB, 2600 pgs) of simulated power spectrums. The
problem is that there are 6 separated files all combined into this large
file. I know how to read the file in and then remove the header
information but how would I go about separating the 6 sections into
separate files?

The program I have so far reads in the data and then removes the header
and footer information (most likely in a very inefficient fashion).

The data looks something like this and I just want the numbers.


CERIUS Grapher File
! Start of definition for XY graph Power Spectrum component XX A^4/ps^2
v1
>PLOT XY DATA: "Power Spectrum component XX A^4/ps^2" 1
 0.000E+00   3.1251088E-04
..PLOT XY METHOD: "Power Spectrum component YY A^4/ps^2" 1
 COLOUR  RED 
...
The footer continues and then runs into the header of the next set of
data (no space between them)

Thanks,
Ara




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


Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Dick Moores
My thanks to both Max and Kent. So Python tries, and fails, to see 2() as 
a function!

I also got some help from 
Dick  

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


Re: [Tutor] Simple RPN calculator

2004-12-08 Thread Gregor Lingl

Terry Carroll schrieb:
On Mon, 6 Dec 2004, Chad Crabtree wrote:
 

Bob Gailer wrote:
   

For grins I just wrote one that takes '1 2.3 - 3 4 5 + * /' as
 

input 
   

and prints -0.0481481 8 lines of Python. That indeed is less
 

than 
   

100. Took about 7 minutes to code and test. 
 

I'm quite interested in seeing the sourcecode for that.
   

Me, too.  I'm always interested in cool little Python examples.
 

Not having read the thread,  this script prints the same result:
inp = raw_input("Gimmi an RPN-expression: ").split()
i=0
while len(inp)>1:
   while inp[i] not in "+-*/":
   i+=1
   print "**", i, inp #1 
   inp[i-2:i+1] = [str(eval(inp[i-2]+inp[i]+inp[i-1]))]
   i-=1
   print "***", i, inp#2
print inp[0]

If you delete the two numbered print statements it also has 8 lines
of code. (They are inserted to show what's going on!)
Excuse me if this posting suffers from redundancy.
I'm very interested in Bob's solution, too
Gregor


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

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


Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Bob Gailer
At 11:27 AM 12/8/2004, Dick Moores wrote:
My thanks to both Max and Kent. So Python tries, and fails, to see 2() as 
a function!

I also got some help from 
Note that SOME languages use () for call. There are other call constructs, 
such as:

DO function WITH parameters (FoxPro, similar in COBOL)
function parameter   or   parameter1 function parameter2 (APL)
Bob Gailer
[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell 

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


Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Bob Gailer
At 12:39 PM 12/8/2004, Bob Gailer wrote:
At 11:27 AM 12/8/2004, Dick Moores wrote:
My thanks to both Max and Kent. So Python tries, and fails, to see 2() as 
a function!

I also got some help from 
Note that SOME languages use () for call. There are other call constructs, 
such as:

DO function WITH parameters (FoxPro, similar in COBOL)
function parameter   or   parameter1 function parameter2 (APL)
I should add the Python builtin function apply: apply(function, 
parameters...)
Bob Gailer
[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell 

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


Re: [Tutor] MemoryError

2004-12-08 Thread Jeff Shannon
Liam Clarke wrote:
Hi all, 

I'm playing with a file, and attempting to replace a section with a
string, and using the following command -
seg=codeSt[element:endInd+len(endStr]
codeSt=codeSt.replace(seg, hrefString)
At the replace, I get a MemoryError. It's part of a for loop, but it
gives the error the first time around.

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.

A few other pointers --
You'll probably get better performance if you put all of this code 
inside of a function.  Even if you're only running it once, putting it 
in a function allows the interpreter to do some optimization tricks on 
locals() which can't be done at the module-global level (where you're 
running now).  It's just barely possible that just doing this will 
help with your MemoryError problem.  (You would probably benefit from 
splitting it into multiple functions, actually.  I'd have the code 
that finds text and url values each in their own function, for example.)

Try adding a line in between those two that prints out the value of 
element and endInd, and then check that those numbers really are valid 
indexes into codeSt.  While you're at it, print out hrefString and 
make sure it looks like it's supposed to.

At the start of your program, you have the following:
inp=file("toolkit.txt","r")
codeSt=inp.readlines()
inp.close()
codeSt="".join(codeSt)
Since you're not processing by lines, and are explicitly joining all 
the lines together, why have Python separate them for you?  It would 
be much more efficient to simply use 'codeSt = inp.read()', with no 
need to join() afterwards.  The readlines() method effectively does a 
read() followed by splitting the result into lines; for your purposes, 
there's no point in splitting the lines if you're just going to join() 
them immediately.

Instead of finding the start and end index of the segment you want to 
replace, making a copy of that segment, and then scanning your 
original string to replace that segment with a new chunk, it would 
probably make more sense to simply grab codeSt before the segment and 
after the segment and concatenate them with the new chunk.  Thus, your 
two lines above become

codeSt = codeSt[:element] + hrefString \
 + codeSt[endInd+len(endStr)]
Once again, this would avoid doing the same work twice.
Now, I had imagined that codeSt=codeSt+10 would destroy the old codeSt
in memory and create a new codeSt. Am I right?
Actually, this will probably raise a TypeError (cannot concatenate 
'str' and 'int' objects).  ;)  But yes, rebinding codeSt to a new 
string should allow the old string to be destroyed (if there are no 
other references to it).

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


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

2004-12-08 Thread Alan Gauld
> 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:
>
> 
> 

A very simplistic approach uses a flag:

atTag = 0
f = open(...)
while not atTag:
line = f.readline()
if line == '':
  atTag = True
  break
outFile.write(line) # + \n, I can't remember...
while atTag:
line = f.readline()
if line == '':
   atTag = False
while f:
outfile.write(f.readline())

This flag approach is sometimes called a sentinal...

I'm sure somebody can find better ways of doing this but I'm
too tired to bother right now! :-(
The sentinel approach will work...

Alan G.

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


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

2004-12-08 Thread kumar s
Dear group, 

 I have two tables:

First table: spot_cor:
432 117 
499 631 
10  0   
326 83  
62  197 
0   0   
37  551 



Second table: spot_int
0   0   98  
1   0   5470
2   0   113 
3   0   5240
4   0   82.5
5   0   92  
6   0   5012
7   0   111 
8   0   4612
9   0   115 
10  0   4676.5  



I stored these two tables as lists:

>>> spot_cor[0:5]
['432\t117', '499\t631', 10\t0', '326\t83', '62\t197']

>>> spot_int[0:5]
['  0\t  0\t18.9', '  1\t  0\t649.4', '  10\t 
0\t37.3', '  3\t  0\t901.6', '  4\t  0\t14.9']


I want to take each element from spot_cor and search
in spot_int, if they match, I want to write
all the three columns of spot_int. 



I did the following way to see what happens when I
print element1 and element 2 as tab delim. text:

code:
>>> for ele1 in spot_cor:
for ele2 in spot_int:
if ele1 in ele2:
print (ele1+'\t'+ele2)


432 117 432 117 17.3
432 117   7 432 117.9
432 117 554 432 117.7
499 631 499 631 23.1
12  185  12 185 19.6
12  185 112 185 42.6
12  185 212 185 26.3
12  185 312 185 111.9
12  185 412 185 193.1
12  185 512 185 21.9
12  185 612 185 22.0
326 83  169 326 83.7
62  197  62 197 18.9


The problem with this script is that it is printing
all unwanted element of spot_int list.  This is simply
crap for me. I want to print the columns only if first
two columns of both tables match.  

The simple reason here I asked it to see if 12 and 185
are contained in two columns and pythons tells me, yes
they are present in 112 and 185 and this is a wrong
result. 

Can you please suggest a better method for comparing
these two elements and then printing the third column.
 

thank you very much. 


Cheers
K



__ 
Do you Yahoo!? 
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com 
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2004-12-08 Thread Liam Clarke
Oh, and I never knew about .read() for a file object. I always just
used readline/readlines. Silly, really.



On Thu, 9 Dec 2004 11:31:40 +1300, Liam Clarke <[EMAIL PROTECTED]> 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?
> 
> I think I'll sprinkle print statements throughout as suggested, and
> wrap it in a function for that optimization Jeff mentioned.
> 
> Thanks for the advice, I'll let you know how it goes.
> 
> Regards,
> 
> Liam Clarke
> 
> I didn't think to
> 
> 
> On Wed, 08 Dec 2004 12:29:10 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote:
> > Liam Clarke wrote:
> >
> > > Hi all,
> > >
> > > I'm playing with a file, and attempting to replace a section with a
> > > string, and using the following command -
> > >
> > > seg=codeSt[element:endInd+len(endStr]
> > > codeSt=codeSt.replace(seg, hrefString)
> > >
> > > At the replace, I get a MemoryError. It's part of a for loop, but it
> > > gives the error the first time around.
> >
> >
> > 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.
> >
> > A few other pointers --
> >
> > You'll probably get better performance if you put all of this code
> > inside of a function.  Even if you're only running it once, putting it
> > in a function allows the interpreter to do some optimization tricks on
> > locals() which can't be done at the module-global level (where you're
> > running now).  It's just barely possible that just doing this will
> > help with your MemoryError problem.  (You would probably benefit from
> > splitting it into multiple functions, actually.  I'd have the code
> > that finds text and url values each in their own function, for example.)
> >
> > Try adding a line in between those two that prints out the value of
> > element and endInd, and then check that those numbers really are valid
> > indexes into codeSt.  While you're at it, print out hrefString and
> > make sure it looks like it's supposed to.
> >
> > At the start of your program, you have the following:
> >
> >  inp=file("toolkit.txt","r")
> >  codeSt=inp.readlines()
> >  inp.close()
> >  codeSt="".join(codeSt)
> >
> > Since you're not processing by lines, and are explicitly joining all
> > the lines together, why have Python separate them for you?  It would
> > be much more efficient to simply use 'codeSt = inp.read()', with no
> > need to join() afterwards.  The readlines() method effectively does a
> > read() followed by splitting the result into lines; for your purposes,
> > there's no point in splitting the lines if you're just going to join()
> > them immediately.
> >
> > Instead of finding the start and end index of the segment you want to
> > replace, making a copy of that segment, and then scanning your
> > original string to replace that segment with a new chunk, it would
> > probably make more sense to simply grab codeSt before the segment and
> > after the segment and concatenate them with the new chunk.  Thus, your
> > two lines above become
> >
> >  codeSt = codeSt[:element] + hrefString \
> >   + codeSt[endInd+len(endStr)]
> >
> > Once again, this would avoid doing the same work twice.
> >
> > > Now, I had imagined that codeSt=codeSt+10 would destroy the old codeSt
> > > in memory and create a new codeSt. Am I right?
> >
> > Actually, this will probably raise a TypeError (cannot concatenate
> > 'str' and 'int' objects).  ;)  But yes, rebinding codeSt to a new
> > string should allow the old string to be destroyed (if there are no
> > other references to it).
> >
> > Hope that this helps.
> >
> > 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.
> 


-- 
'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-08 Thread Liam Clarke
> 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?

I think I'll sprinkle print statements throughout as suggested, and
wrap it in a function for that optimization Jeff mentioned.

Thanks for the advice, I'll let you know how it goes.


Regards,

Liam Clarke

I didn't think to 
On Wed, 08 Dec 2004 12:29:10 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote:
> Liam Clarke wrote:
> 
> > Hi all,
> >
> > I'm playing with a file, and attempting to replace a section with a
> > string, and using the following command -
> >
> > seg=codeSt[element:endInd+len(endStr]
> > codeSt=codeSt.replace(seg, hrefString)
> >
> > At the replace, I get a MemoryError. It's part of a for loop, but it
> > gives the error the first time around.
> 
> 
> 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.
> 
> A few other pointers --
> 
> You'll probably get better performance if you put all of this code
> inside of a function.  Even if you're only running it once, putting it
> in a function allows the interpreter to do some optimization tricks on
> locals() which can't be done at the module-global level (where you're
> running now).  It's just barely possible that just doing this will
> help with your MemoryError problem.  (You would probably benefit from
> splitting it into multiple functions, actually.  I'd have the code
> that finds text and url values each in their own function, for example.)
> 
> Try adding a line in between those two that prints out the value of
> element and endInd, and then check that those numbers really are valid
> indexes into codeSt.  While you're at it, print out hrefString and
> make sure it looks like it's supposed to.
> 
> At the start of your program, you have the following:
> 
>  inp=file("toolkit.txt","r")
>  codeSt=inp.readlines()
>  inp.close()
>  codeSt="".join(codeSt)
> 
> Since you're not processing by lines, and are explicitly joining all
> the lines together, why have Python separate them for you?  It would
> be much more efficient to simply use 'codeSt = inp.read()', with no
> need to join() afterwards.  The readlines() method effectively does a
> read() followed by splitting the result into lines; for your purposes,
> there's no point in splitting the lines if you're just going to join()
> them immediately.
> 
> Instead of finding the start and end index of the segment you want to
> replace, making a copy of that segment, and then scanning your
> original string to replace that segment with a new chunk, it would
> probably make more sense to simply grab codeSt before the segment and
> after the segment and concatenate them with the new chunk.  Thus, your
> two lines above become
> 
>  codeSt = codeSt[:element] + hrefString \
>   + codeSt[endInd+len(endStr)]
> 
> Once again, this would avoid doing the same work twice.
> 
> > Now, I had imagined that codeSt=codeSt+10 would destroy the old codeSt
> > in memory and create a new codeSt. Am I right?
> 
> Actually, this will probably raise a TypeError (cannot concatenate
> 'str' and 'int' objects).  ;)  But yes, rebinding codeSt to a new
> string should allow the old string to be destroyed (if there are no
> other references to it).
> 
> Hope that this helps.
> 
> 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 printing them

2004-12-08 Thread Bob Gailer
At 02:51 PM 12/8/2004, kumar s wrote:
Dear group,
 I have two tables:
First table: spot_cor:
432 117
499 631
10  0
326 83
62  197
0   0
37  551

Second table: spot_int
0   0   98
1   0   5470
2   0   113
3   0   5240
4   0   82.5
5   0   92
6   0   5012
7   0   111
8   0   4612
9   0   115
10  0   4676.5

I stored these two tables as lists:
>>> spot_cor[0:5]
['432\t117', '499\t631', 10\t0', '326\t83', '62\t197']
Note there is no ' before the 10. That won't fly'
>>> spot_int[0:5]
['  0\t  0\t18.9', '  1\t  0\t649.4', '  10\t
0\t37.3', '  3\t  0\t901.6', '  4\t  0\t14.9']
It would be a lot easier to work with if the lists looked like (assumes all 
data are numeric):
[(432,117), (499,631), (10,0), (326,83), (62,197)]
[(0,0,18.9), (1,0,649.4), (10,0,37.3), (3,0,901.6), (4,0,14.9)]

What is the source for this data? Is it a tab-delimited file? If so the CSV 
module can help make this translation.

I also assume that you want the first 2 elements of a spot_int element to 
match a spot_cor element.

Then (for the subset of data you've provided):
>>> for ele1 in spot_cor:
...   for ele2 in spot_int:
... if ele1 == ele2[:2]:
... print "%8s %8s %8s" % ele2
...
  100 37.3
I want to write all the three columns of spot_int.
[snip]
Hope that helps.
Bob Gailer
[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell 

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


Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Alan Gauld
> Note that SOME languages use () for call. There are other call
constructs,
> such as:
>
> DO function WITH parameters (FoxPro, similar in COBOL)
>
> function parameter   or   parameter1 function parameter2 (APL)

And in Smalltalk:

object message: parameter1 : parameter2 :
parameter3

Or as an example of a message and one descriptor.

myArray put: foo at: 5

The array method is known as   "put:at:"

No parens to be seen (they have a completely different meaning in
Smalltalk)

Alan G.

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


Re: [Tutor] MemoryError

2004-12-08 Thread Alan Gauld
> Traceback: 
> 
> Line 39: seg=codeSt[element:endInd+len(endStr]
> MemoryError
> 
> Hehe. Helpful, no?

Sometimes seeing the whole traceback gives clues as 
to whats throwing the wobbly, without the full stack 
its hard to tell.

However before worrying about that I'd add a print 
statement to print out all those values:

codeStr
element,
endInd
len(endstr)

I suspect the memory error might be an indexing error, 
running off the end of the string or similar... But lets 
see those values at the point of failure.

For your own benefit it might be easier just to use the debugger, 
just set a breakpoint on the line then check the values when the 
debugger stops...

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


Re: [Tutor] MemoryError

2004-12-08 Thread Jeff Shannon
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


Re: [Tutor] Simple RPN calculator

2004-12-08 Thread Bob Gailer
At 06:05 AM 12/6/2004, you wrote:
Bob Gailer wrote:
> For grins I just wrote one that takes '1 2.3 - 3 4 5 + * /' as input
> and prints -0.0481481 8 lines of Python. That indeed is less than
> 100. Took about 7 minutes to code and test.
I'm quite interested in seeing the sourcecode for that.
I've made it interactive (enter an operand or operator and hit enter); it 
displays the top of the stack. I added . ^ and %. No error checking.

import operator as op
def rpn(o,stack = [],d = {'*':op.mul, '+':op.add, '/':op.truediv, 
'%':op.mod, '-':op.sub, '^':op.pow, '.':lambda x,y:x+.1*y}):
  if o in d: stack[-2:] = [d[o](stack[-2], stack[-1])]
  elif o: stack.append(float(o)) # could bomb here if input not floatable!
  else: return 1
  print stack[-1]
while 1:
  if rpn(raw_input('>')): break

Let me know what you think.
Bob Gailer
[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell  

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


[Tutor] Vpython

2004-12-08 Thread bigpimpin10451
I was wondering were can I find some Vpython graphics program codes that are 
readily available.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Vpython

2004-12-08 Thread Guillermo Fernandez Castellanos
Hi,

On the Vpython homepage they have links to quite a few programs:
http://vpython.org/contributed.html
http://www.physics.syr.edu/%7Esalgado/software/vpython/

Should be enough for a start.

Enjoy,

Guille


On Wed, 8 Dec 2004 22:05:31 -0500 (GMT-05:00),
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I was wondering were can I find some Vpython graphics program codes that are 
> readily available.
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
>
___
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-08 Thread kumar s
Hi, 
 thank you very much for suggesting a way. 

In fact I tried and I found another way to do it.
could you please suggest if something is wrong because
I have false positive results in the output.  That
means I getting more that the values I have in
spot_cor. For example I have 2500 elements in spot_cor
list. I am searching each element if it is in
spot_init. IF it is there then I am writing it to a
file.  What I expect is to get 2500 elements. However
I am getting 500 elements extra. I do not understand
how is this possible. 

Code:

>>> out = open('sa_int_2.txt','w')
>>> for ele1 in range(len(spot_cor)):
x = spot_cor[ele1]
for ele2 in range(len(spot_int)):
cols = split(spot_int[ele2],'\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')






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. 



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. 


thanks
Kumar



--- Bob Gailer <[EMAIL PROTECTED]> wrote:

> At 02:51 PM 12/8/2004, kumar s wrote:
> >Dear group,
> >
> >  I have two tables:
> >
> >First table: spot_cor:
> >432 117
> >499 631
> >10  0
> >326 83
> >62  197
> >0   0
> >37  551
> >
> >
> >
> >Second table: spot_int
> >0   0   98
> >1   0   5470
> >2   0   113
> >3   0   5240
> >4   0   82.5
> >5   0   92
> >6   0   5012
> >7   0   111
> >8   0   4612
> >9   0   115
> >10  0   4676.5
> >
> >
> >
> >I stored these two tables as lists:
> >
> > >>> spot_cor[0:5]
> >['432\t117', '499\t631', 10\t0', '326\t83',
> '62\t197']
> 
> Note there is no ' before the 10. That won't fly'
> 
> > >>> spot_int[0:5]
> >['  0\t  0\t18.9', '  1\t  0\t649.4', '  10\t
> >0\t37.3', '  3\t  0\t901.6', '  4\t  0\t14.9']
> 
> It would be a lot easier to work with if the lists
> looked like (assumes all 
> data are numeric):
> [(432,117), (499,631), (10,0), (326,83), (62,197)]
> [(0,0,18.9), (1,0,649.4), (10,0,37.3), (3,0,901.6),
> (4,0,14.9)]
> 
> What is the source for this data? Is it a
> tab-delimited file? If so the CSV 
> module can help make this translation.
> 
> I also assume that you want the first 2 elements of
> a spot_int element to 
> match a spot_cor element.
> 
> Then (for the subset of data you've provided):
> 
>  >>> for ele1 in spot_cor:
> ...   for ele2 in spot_int:
> ... if ele1 == ele2[:2]:
> ... print "%8s %8s %8s" % ele2
> ...
>100 37.3
> 
> >I want to write all the three columns of spot_int.
> >[snip]
> 
> Hope that helps.
> 
> Bob Gailer
> [EMAIL PROTECTED]
> 303 442 2625 home
> 720 938 2625 cell 
> 
> 




__ 
Do you Yahoo!? 
Yahoo! Mail - 250MB free storage. Do more. Manage less. 
http://info.mail.yahoo.com/mail_250
___
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-08 Thread Brian van den Broek
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