Re: [Tutor] help with re module and parsing data

2011-03-07 Thread Kushal Kumaran
On Mon, Mar 7, 2011 at 1:24 PM, vineeth  wrote:
> Hello all I am doing some analysis on my trace file. I am finding the lines
> Recvd-Content and Published-Content. I am able to find those lines but the
> re module as predicted just gives the word that is being searched. But I
> require the entire  line similar to a grep in unix. Can some one tell me how
> to do this. I am doing the following way.
>
> import re
> file = open('file.txt','r')
> file2 = open('newfile.txt','w')
>
> LineFile = ' '
>
> for line in file:
>    LineFile += line
>
> StripRcvdCnt = re.compile('(P\w+\S\Content|Re\w+\S\Content)')
>
> FindRcvdCnt = re.findall(StripRcvdCnt, LineFile)
>
> for SrcStr in FindRcvdCnt:
>    file2.write(SrcStr)
>

Is there any particular reason why you're using regular expressions
for this?  You are already iterating over the lines in your first for
loop.  You can just make the tests you need there.

for line in file:
  if 'Recvd-Content' in line or 'Published-Content' in line:


Your regular expression seems like it will match a lot more strings
than the two you mentioned earlier.

Also, 'file' is a python built-in.  It will be best to use a different
name for your variable.

-- 
regards,
kushal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to sum weighted matrices

2011-03-07 Thread shu wei
Hello all,

I am new to python and numpy.
My question is how to sum up N weighted matrices.
For example w=[1,2] (N=2 case)
m1=[1 2 3,
   3 4 5]

m2=[3 4 5,
   4 5 6]
I want to get a matrix Y=w[1]*m1+w[2]*m2 by using a loop.

My original problem is like this
X=[1 2 3,
 3 4 5,
 4 5 6]

a1=[1 2 3]  1st row of X
m1=a1'*a1 a matirx
a2=[3 4 5] 2nd row of X
m2=a2'*a2
a3=[ 4 5 6] 3rd row of X
m3=a3'*a3

I want to get Y1=w[1]*m1+w[2]*m2
  Y2=w[1]*m2+w[2]*m3
So basically it is rolling and to sum up the weighted matries
I have a big X, the rolling window is relatively small.

I tried to use

sq=np.array([x[i].reshape(-1,1)*x[i] for i in np.arange(0,len(x)]) #
s=len(x)
m=np.array([sq[i:i+t] for i in np.arange(0,s-t+1)]) # t is the len(w)

then I was stuck, I tried to use a loop somethig like
Y=np.array([np.sum(w[i]*m[j,i],axis=0) for i in np.arange(0,t)] )
Any suggestion is welcome.

sue
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with re module and parsing data

2011-03-07 Thread wesley chun
>> import re
>> file = open('file.txt','r')
>> file2 = open('newfile.txt','w')
>>
>> LineFile = ' '
>>
>> for line in file:
>>    LineFile += line
>>
>> StripRcvdCnt = re.compile('(P\w+\S\Content|Re\w+\S\Content)')
>>
>> FindRcvdCnt = re.findall(StripRcvdCnt, LineFile)
>>
>> for SrcStr in FindRcvdCnt:
>>    file2.write(SrcStr)
>>
>
> Is there any particular reason why you're using regular expressions
> for this?  You are already iterating over the lines in your first for
> loop.  You can just make the tests you need there.
>
> for line in file:
>  if 'Recvd-Content' in line or 'Published-Content' in line:
>    
>
> Your regular expression seems like it will match a lot more strings
> than the two you mentioned earlier.
>
> Also, 'file' is a python built-in.  It will be best to use a different
> name for your variable.


i have a few suggestions as well:

1) class names should be titlecased, not ordinary variables, so
LineFile should be linefile, line_file, or lineFile.

2) you don't need to read in the file one line at-a-time. you can just
do linefile = f.read() ... this reads the entire file in as one
massive string.

3) you don't need to compile your regex (unless you will be using this
pattern over and over within one execution of this script). you can
just call findall() directly: findrcvdcnt =
re.findall('(P\w+\S\Content|Re\w+\S\Content)', LineFile)

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.chun : wescpy-gmail.com : @wescpy
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BLAS Implementation on Python

2011-03-07 Thread Stefan Behnel

Mahesh Narayanamurthi, 07.03.2011 01:50:

I am thinking of implementing a BLAS package in pure python. I am wondering
if this is a good idea. My design goals are:

[1] Efficient manipulation of Matrices and
 Vectors using pure python objects and
 python code.
[2] Targetted to run on Python3
[3] Extensive use of defensive programming
 style
[4] To serve as a reference design for
 future High Performance Code in Python
[5] To serve as a reference material in
 classroom courses on numerical computing
 or for hobbyist programmers


First question that comes to my mind: who would use this? The available 
packages are commonly not written in Python, but they are fast, which is 
why they are being used by Python programs. A quick web search brought up 
Tokyo, for example, which wraps BLAS in Cython:


https://github.com/tokyo/tokyo

I can see that it would be easier to teach the concepts based on code 
written in Python than based on the existing implementations. Even if there 
likely won't be a real use case, it may still be nice to have a pure Python 
implementation available, if it could serve as a drop-in replacement for 
faster implementations. I.e., you could present the inner workings based on 
the Python code, and then switch to a 'real' implementation for the actual 
computations.


However, even if you say "efficient" above (and the algorithms may well be 
efficient), don't expect it to be fast. Python is great for orchestrating 
high performance computations. It's less great for doing them in plain 
Python code.


Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BLAS Implementation on Python

2011-03-07 Thread Knacktus

Am 07.03.2011 01:50, schrieb Mahesh Narayanamurthi:

Hello,

I am thinking of implementing a BLAS package in pure python. I am
wondering if this is a good idea.


I don't think so. Usually people write extensions to the CPython 
implementation (when talking about performance, we need to talk about 
Python implementations like CPython, Jython or PyPy) in C to do high 
performance stuff. Pure CPython is (depeneding on the problem) 
magnitudes slower than C.


Also, there's NumPy SciPy. Check those out.

More comments below ...

My design goals are:



[1] Efficient manipulation of Matrices and
 Vectors using pure python objects and
 python code.

No, not efficient in terms of performance.

[2] Targetted to run on Python3

Good idea. NumPy and SciPy will be (are already?) ported.

[3] Extensive use of defensive programming
 style
[4] To serve as a reference design for
 future High Performance Code in Python

The future of High Performance Python is probably PyPy.

[5] To serve as a reference material in
 classroom courses on numerical computing
 or for hobbyist programmers

Good idea, as Python is clear and nice to read.


Thanks,
Mahesh Narayanamurthi



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BLAS Implementation on Python

2011-03-07 Thread Stefan Behnel

Knacktus, 07.03.2011 14:28:

Am 07.03.2011 01:50, schrieb Mahesh Narayanamurthi:

Hello,

I am thinking of implementing a BLAS package in pure python. I am
wondering if this is a good idea.


My design goals are:


[2] Targetted to run on Python3

Good idea. NumPy and SciPy will be (are already?) ported.


Yes, NumPy has been ported as of version 1.5. Not sure about the overall 
status of SciPy, their FAQ isn't up to date.




[4] To serve as a reference design for
future High Performance Code in Python

The future of High Performance Python is probably PyPy.


PyPy has been "the future" ever since they started ;-). They have gotten 
faster, but it's is still far from being suitable for any kind of numerical 
high performance computing. That will continue to be the domain of C, 
Fortran and Cython for a while, and potentially including LuaJIT2 for 
certain use cases (usable from Python through Lupa, BTW).


Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ctypes question

2011-03-07 Thread Albert-Jan Roskam
Hi,

I want to use a dll to read Spss data files. But when I use 
lib = ctypes.cdll.LoadLibary("d:/temp/spssio32.dll")
I get a WindowsError (cannot find module), even though the path exists. Why is 
that? Do I need to extend some environment variable (add another dir)? I am 
using Python 2.5 on Windows 2000, but I also unsuccessfully tried it using 
Python 2.7 on Ubuntu 10 (however, I don't want to write the program on a linux 
system because I'll be using it on windows only). I also got the same error 
when 
I tried some other dll's.

Thanks in advance!
Cheers!!
Albert-Jan 


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the 
Romans ever done for us?
~~ 


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes question

2011-03-07 Thread Pacific Morrowind

Hi

On 07/03/2011 7:02 AM, Albert-Jan Roskam wrote:

Hi,
I want to use a dll to read Spss data files. But when I use
lib = ctypes.cdll.LoadLibary("d:/temp/spssio32.dll")
I get a WindowsError (cannot find module), even though the path 
exists. Why is that? Do I need to extend some environment variable 
(add another dir)? I am using Python 2.5 on Windows 2000, but I also 
unsuccessfully tried it using Python 2.7 on Ubuntu 10 (however, I 
don't want to write the program on a linux system because I'll be 
using it on windows only). I also got the same error when I tried some 
other dll's.
but that path doesn't actually exist... replace that path with either 
r"d:/temp/spssio32.dll" or with "d://temp//spssio32.dll"; otherwise the 
/t will be transformed into a tab.

Nick

Thanks in advance!
Cheers!!
Albert-Jan

~~
All right, but apart from the sanitation, the medicine, education, 
wine, public order, irrigation, roads, a fresh water system, and 
public health, what have the Romans ever done for us?

~~


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes question

2011-03-07 Thread Joel Goldstick
On Mon, Mar 7, 2011 at 10:36 AM, Pacific Morrowind <
pacificmorrow...@gmail.com> wrote:

>  Hi
>
>
> On 07/03/2011 7:02 AM, Albert-Jan Roskam wrote:
>
>  Hi,
>
> I want to use a dll to read Spss data files. But when I use
> lib = ctypes.cdll.LoadLibary("d:/temp/spssio32.dll")
> I get a WindowsError (cannot find module), even though the path exists. Why
> is that? Do I need to extend some environment variable (add another dir)? I
> am using Python 2.5 on Windows 2000, but I also unsuccessfully tried
> it using Python 2.7 on Ubuntu 10 (however, I don't want to write the program
> on a linux system because I'll be using it on windows only). I also got the
> same error when I tried some other dll's.
>
>
> but that path doesn't actually exist... replace that path with either
> r"d:/temp/spssio32.dll" or with "d://temp//spssio32.dll"; otherwise the /t
> will be transformed into a tab.
> Nick
>
>  Thanks in advance!
>
> Cheers!!
> Albert-Jan
>
> ~~
> All right, but apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, a fresh water system, and public health,
> what have the Romans ever done for us?
> ~~
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription 
> options:http://mail.python.org/mailman/listinfo/tutor
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

"but that path doesn't actually exist... replace that path with either
r"d:/temp/spssio32.dll" or with "d://temp//spssio32.dll"; otherwise the /t
will be transformed into a tab.
Nick"

I don't think that is the problem.  '\t' would transform to tab, but not
'/t'.  Right?

-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes question

2011-03-07 Thread Alan Gauld


"Pacific Morrowind"  wrote

but that path doesn't actually exist... replace that path with 
either
r"d:/temp/spssio32.dll" or with "d://temp//spssio32.dll"; otherwise 
the

/t will be transformed into a tab.


You've got your / and \ mixed up.

Forward slashes (/) are fine in Windows paths. It's back
slashes(\) that need escaping or raw stringing. \t is tab...

On the original ask - could it be a permissions issue?
Does your app have acess to the file?

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BLAS Implementation on Python

2011-03-07 Thread Steven D'Aprano
On Tue, 8 Mar 2011 12:28:56 am Knacktus wrote:
> Am 07.03.2011 01:50, schrieb Mahesh Narayanamurthi:
> > Hello,
> >
> > I am thinking of implementing a BLAS package in pure python. I am
> > wondering if this is a good idea.

Sure, why not? Even if nobody uses it, it looks good on a resume and you 
will hopefully learn a lot.


> I don't think so. Usually people write extensions to the CPython
> implementation (when talking about performance, we need to talk about
> Python implementations like CPython, Jython or PyPy) in C to do high
> performance stuff. Pure CPython is (depeneding on the problem)
> magnitudes slower than C.

Typical pure Python code is between 10 and 100 times slower than C, but 
talking about implementations, it is the aim of the PyPy project to use 
Just In Time compilation and clever optimizations to meet *and exceed* 
the speed of static C compilers. In just a few years, with a handful of 
people working on it, and a tiny grant from the EU, they have already 
doubled the speed of CPython. If PyPy could get *half* the love and 
attention that CPython gets, who knows what they could accomplish?

(If only Google had thrown some work into PyPy, instead of wasting time 
with Unladen Swallow that never went anywhere useful...)

But the beauty of Python is that if a piece of code is too slow, you can 
rip it out into an extension written in C or Fortran while keeping the 
rest of the library in Python. See, for example, the re module, which 
has a front end written in Python and the regex engine in C.

The itertools module is another good example. Using a few primitives 
written in C, itertools allows Python code to run efficiently and 
quickly.


> Also, there's NumPy SciPy. Check those out.
>
> More comments below ...
>
> My design goals are:
> > [1] Efficient manipulation of Matrices and
> >  Vectors using pure python objects and
> >  python code.
>
> No, not efficient in terms of performance.

How do you know how efficient his code will be before he's even written 
it?

[...]
> The future of High Performance Python is probably PyPy.

Exactly why a pure-Python library *may* be useful. Today it will be 
slow, running under CPython, but in five (unlikely) or ten years 
(possibly), it may be fast, running under PyPy.

Or not. Who can tell what the future will bring?


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Init Script (RHEL 5 Linux)

2011-03-07 Thread Tom Tucker
Hello. I am trying implement a *one time* execution of an init script.  Any
recommendation on how to fork or detach this
script from the normal init 3 execution process so that it doesn't hold up
other activities?

Thanks

Here is the high level logic.


   1. Execute script
  1. perform these activities
   2. Check network connectivity
  1. If connectivity: perform these activities
 1. Delete init script os.remove(sys.argv[0])
  2. If no connectivity:
 - Sleep 15 seconds
 - Check connectivity again
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Init Script (RHEL 5 Linux)

2011-03-07 Thread Steven D'Aprano
On Tue, 8 Mar 2011 08:49:12 am Tom Tucker wrote:
> Hello. I am trying implement a *one time* execution of an init
> script.  Any recommendation on how to fork or detach this
> script from the normal init 3 execution process so that it doesn't
> hold up other activities?

This mailing list is for beginners learning Python the language. While 
we're happy to help, chances are that most people here won't be able to 
answer your question. You may have better luck with the main Python 
mailing list, python-l...@python.org, also available as a news group, 
comp.lang.python.

But having said that, my guess would be to look at the subprocess module 
and see if that will do what you are hoping for.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with re module and parsing data

2011-03-07 Thread Steven D'Aprano
On Mon, 7 Mar 2011 06:54:30 pm vineeth wrote:
> Hello all I am doing some analysis on my trace file. I am finding the
> lines Recvd-Content and Published-Content. I am able to find those
> lines but the re module as predicted just gives the word that is
> being searched. But I require the entire  line similar to a grep in
> unix. Can some one tell me how to do this. I am doing the following
> way.

If you want to match *lines*, then you need to process each line 
individually, not the whole file at once. Something like this:

for line in open('file.txt'):
if "Recvd-Content" in line or "Published-Content" in line:
process_match(line)

A simple substring test should be enough, that will be *really* fast. 
But if you need a more heavy-duty test, you can use a regex, but 
remember that regexes are usually slow.

pattern = 'whatever...'
for line in open('file.txt'):
if re.search(pattern, line):
process_match(line)


Some further comments below:


> import re
> file = open('file.txt','r')
> file2 = open('newfile.txt','w')
>
> LineFile = ' '

Why do you initialise "LineFile" to a single space, instead of the empty 
string?


> for line in file:
>  LineFile += line

Don't do that! Seriously, that is completely the wrong way.

What this does is something like this:

Set LineFile to " ".
Read one line from the file.
Make a copy of LineFile plus line 1.
Assign that new string to LineFile.
Delete the old contents of LineFile.
Read the second line from the file.
Make a copy of LineFile plus line 2.
Assign that new string to LineFile.
Delete the old contents of LineFile.
Read the third line from the file.
Make a copy of LineFile plus line 3.
and so on... 

Can you see how much copying of data is being done? If there are 1000 
lines in the file, the first line gets copied 1000 times, the second 
line 999 times, the third 998 times... See this essay for more about 
why this is s-l-o-w:

http://www.joelonsoftware.com/articles/fog000319.html

Now, it turns out that *some* versions of Python have a clever 
optimization which, *sometimes*, can speed that up. But you shouldn't 
rely on it. The better way to add many strings is:

accumulator = []
for s in some_strings:
accumulator.append(s)
result = ''.join(accumulator)

But in your case, when reading from a file, an even better way is to 
just read from the file in one chunk!

LineFile = open('file.txt','r').read()



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] [RFI] Resources for graphic modeling of dynamic systems

2011-03-07 Thread Jeff Horn
Hi tutors!

I have a very basic understanding of Python, and have played a bit
with SymPy. I'd like to use Python to accomplish many of the same
things that are done in Mathematica.

Right now, I have a project that could benefit from a dynamic model. I
envision defining a series of differential equations that describe a
model, complete with manipulable parameters. I think I'd use a Python
script to define the model and graph it with gnuplot or a similar
tool. However, I have no idea about where to start building an
interface with sliders to manipulate parameters graphically.

Would any of you recommend particular resources?

Perhaps this would necessitate becoming a nascent UI programmer, which
is something I'd like to avoid as much as possible.

For general examples of what I'm looking to produce, see:

http://demonstrations.wolfram.com/

More specifically, I want to produce a model very much like the one here:

http://demonstrations.wolfram.com/SolowGrowthModel/

-- 
Jeffrey Horn
http://www.failuretorefrain.com/jeff/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [RFI] Resources for graphic modeling of dynamic systems

2011-03-07 Thread Wayne Werner
On Mon, Mar 7, 2011 at 7:28 PM, Jeff Horn  wrote:

> Hi tutors!
>
> I have a very basic understanding of Python, and have played a bit
> with SymPy. I'd like to use Python to accomplish many of the same
> things that are done in Mathematica.
>
> Right now, I have a project that could benefit from a dynamic model. I
> envision defining a series of differential equations that describe a
> model, complete with manipulable parameters. I think I'd use a Python
> script to define the model and graph it with gnuplot or a similar
> tool. However, I have no idea about where to start building an
> interface with sliders to manipulate parameters graphically.
>
> Would any of you recommend particular resources?
>
> Perhaps this would necessitate becoming a nascent UI programmer, which
> is something I'd like to avoid as much as possible.
>
> For general examples of what I'm looking to produce, see:
>
>http://demonstrations.wolfram.com/
>
> More specifically, I want to produce a model very much like the one here:
>
>http://demonstrations.wolfram.com/SolowGrowthModel/


If you're interested in scientific programming, I would highly recommend the
Python(X, Y) package available here:
http://www.pythonxy.com/

It has a variety of tools to help you do as little
GUI programming as possible. It also comes with the inestimable Matplotlib
that is incredibly useful for drawing graphs like the ones you suggest.

Although apparently you don't even need to worry about a GUI when you can
just use matplotlib itself:
http://matplotlib.sourceforge.net/users/screenshots.html#slider-demo

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Does try-except "lock out scope" or similar?

2011-03-07 Thread Benjamin Serrato
I wrote a short script to clean up a csv file but had trouble when

date_time = time.strptime(date_string, "%m/%d/%y")

 would choke on intermittent Null characters in the file.  I put it into a
try-except, but then I found I couldn't do

del row

because I receive a "row is not defined" complaint or similar.  So, how do I
run time.strptime() without locking myself out. And, what is the pretty way
to do all of this, because a couple hours later it looks pretty ugly. I mean
it turns out I'm rewriting the file anyway so no need to delete the row.

http://pastebin.ws/e0prlj

Regards,
Benjamin Serrato
682.472.8650
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BLAS Implementation on Python

2011-03-07 Thread Knacktus

Am 07.03.2011 22:44, schrieb Steven D'Aprano:

On Tue, 8 Mar 2011 12:28:56 am Knacktus wrote:

Am 07.03.2011 01:50, schrieb Mahesh Narayanamurthi:

Hello,

I am thinking of implementing a BLAS package in pure python. I am
wondering if this is a good idea.


Sure, why not? Even if nobody uses it, it looks good on a resume and you
will hopefully learn a lot.



I don't think so. Usually people write extensions to the CPython
implementation (when talking about performance, we need to talk about
Python implementations like CPython, Jython or PyPy) in C to do high
performance stuff. Pure CPython is (depeneding on the problem)
magnitudes slower than C.


Typical pure Python code is between 10 and 100 times slower than C, but
talking about implementations, it is the aim of the PyPy project to use
Just In Time compilation and clever optimizations to meet *and exceed*
the speed of static C compilers. In just a few years, with a handful of
people working on it, and a tiny grant from the EU, they have already
doubled the speed of CPython. If PyPy could get *half* the love and
attention that CPython gets, who knows what they could accomplish?

(If only Google had thrown some work into PyPy, instead of wasting time
with Unladen Swallow that never went anywhere useful...)

But the beauty of Python is that if a piece of code is too slow, you can
rip it out into an extension written in C or Fortran while keeping the
rest of the library in Python. See, for example, the re module, which
has a front end written in Python and the regex engine in C.

The itertools module is another good example. Using a few primitives
written in C, itertools allows Python code to run efficiently and
quickly.



Also, there's NumPy SciPy. Check those out.

More comments below ...

My design goals are:

[1] Efficient manipulation of Matrices and
  Vectors using pure python objects and
  python code.


No, not efficient in terms of performance.


How do you know how efficient his code will be before he's even written
it?


He doesn't have to write it, as it is very obvious, that no Python code 
on earth (even written by Guido himself ;-)) stands a chance compared to 
Fortran or C. Look at this:


http://shootout.alioth.debian.org/u32/performance.php?test=spectralnorm

CPython implementation runs 12 mins compared to 8 secs of a Fortran 
implementation. PyPy is about 100 secs, which is remarkable but still 
far off.




[...]

The future of High Performance Python is probably PyPy.


Exactly why a pure-Python library *may* be useful. Today it will be
slow, running under CPython, but in five (unlikely) or ten years
(possibly), it may be fast, running under PyPy.

Or not. Who can tell what the future will bring?




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Does try-except "lock out scope" or similar?

2011-03-07 Thread Andre Engels
On Tue, Mar 8, 2011 at 5:40 AM, Benjamin Serrato
 wrote:
> I wrote a short script to clean up a csv file but had trouble when
> date_time = time.strptime(date_string, "%m/%d/%y")
>  would choke on intermittent Null characters in the file.  I put it into a
> try-except, but then I found I couldn't do
> del row
> because I receive a "row is not defined" complaint or similar.  So, how do I
> run time.strptime() without locking myself out. And, what is the pretty way
> to do all of this, because a couple hours later it looks pretty ugly. I mean
> it turns out I'm rewriting the file anyway so no need to delete the row.
> http://pastebin.ws/e0prlj
> Regards,
> Benjamin Serrato
> 682.472.8650

To see the problem with your code, think about the following: Suppose
that you go into the 'except' branch. What is the program going to do
_after_ the code in the except branch has been run? The code will go
on with the part after the try...except, and there will 'need' the
variable row again. You will have to add 'continue' to the except, or
put the code below it in an 'else'.

Another issue I see is the 'del row' code itself - it has no real
function, you are deleting something that will be deleted on the next
execution step anyway.


-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Expanding a variable with subprocess on Windows

2011-03-07 Thread Becky Mcquilling
I'm working on a Windows machine and want to use subprocess to run several
commands from the command line.

Normally with bat files or some of the other scripting languages on Windows,
I would set up a variable with the path to the command like so:

gpg = 'c:/program files (x86)/gnu/gnupg/gpg2.exe'

Then call the command the variable to avoid typing in the full path.

When I do this with subprocess it works:

subprocess.Popen('c:/program files (x86)/gnu/gnupg/gpg2.exe', shell=True)

However when I do this:

gpg = 'c:/program files (x86)/gnu/gnupg/gpg2.exe'

subprocess.Popen('gpg', shell=True)

It fails to run gpg and is not expanding the variable.  Is there a way to do
this that I'm missing?

Becky
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Expanding a variable with subprocess on Windows

2011-03-07 Thread Sander Sweers
On Tue,   8 Mar 2011, 07:44:31 CET, Becky Mcquilling  
wrote:
> gpg = 'c:/program files (x86)/gnu/gnupg/gpg2.exe'
> gpg = 'c:/program files (x86)/gnu/gnupg/gpg2.exe'
> 
> subprocess.Popen('gpg', shell=True)
> 
> It fails to run gpg and is not expanding the variable.   Is there a way
> to do this that I'm missing?

What you are doing is running Popen with a string 'gpg' instead of the variable 
gpg. The below should do the trick.

subprocess.Popen(gpg, shell=True) # notice no single quotes

Also you usually do not need a shell and I expect your use case runs fine 
without it.

Greets
sander___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor