Re: Method much slower than function?

2007-06-13 Thread Bruno Desthuilliers
Neil Cerutti a écrit :
(snip)
> class bar:
> def readgenome(self, filehandle):
> self.s = ''.join(line.strip() for line in filehandle)

=>
   self.s = ''.join(line.strip() for line in filehandle if not 
'>' in line)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dynamically generated runtime methods & reflection

2007-06-13 Thread Josiah Carlson
Jay Loden wrote:
> Hi all,
> 
> First, apologies if anyone gets this twice, but it took me quite a
> while to figure out that Python.org is evidently rejecting all mail
> from my mail server because I don't have reverse DNS configured.
> Anyway:
> 
> I'm not even sure how to phrase this question properly or the right
> terminology on this so bear with me. What I'm looking to find out is
> 
> a) is there a functionality in Python where I can call a method I have
> not defined ahead of time, and based on the method name, change the
> functionality of the method at runtime?

Yes.  Implement a __getattr__ method on your class (which you mention).

> b) if not, what is the "Pythonic" approach to the problem outlined
> below? Any recommendations on how to approach the problem differently
> are welcome.
> 
> I've googled and read my Python reference pretty extensively and I've
> found some hints but nothing that really answered my questions, so
> here I am  :-)  I did figure out that you can overload __getattr__ in
> a clas to define a new method at runtime, but I got stuck when I
> couldn't figure out how to have a method know what name it was
> originally called with. That's the basic question, see below for the
> context I'm asking the question in and *why* I want to do the above
> :-)

Ahh, so you want to pass the method name to the method that you are 
returning to be called.  No problem.

 >>> import functools
 >>>
 >>> class foo:
... def __getattr__(self, name):
... return functools.partial(self.ActualMethod, name)
...
... def ActualMethod(self, name, *args, **kwargs):
... #handle *args and **kwargs based on name!
... print name, args, kwargs
...
 >>> foo().bar('hello', world=1)
bar ('hello',) {'world': 1}
 >>>


  - Josiah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to save python codes in files?

2007-06-13 Thread Gabriel Genellina
En Thu, 14 Jun 2007 01:56:13 -0300, why? <[EMAIL PROTECTED]> escribió:

> I tried but its not working. Here's a code for sum of two numbers. Now
> how do i save it?
>
  #! /usr/bin/env python
> ...
 def sum(x,y):
> ... return x+y
> ...
 x=int(raw_input('Enter a number: '))
> Enter a number: 35
 y=int(raw_input('Enter a number: '))
> Enter a number: 7
 print 'sum is', sum(x,y)
> sum is 42


You *don't* write your program inside Python; the interactive prompt is  
just for testing a few lines of code. Instead, use another program (a text  
editor) to create a file containing your source code, save it using a name  
like test1.py, and run it from a shell prompt using: python test1.py

I think that Red Hat comes with gedit as the default editor, you should  
find it under Accesories, Text Editor or similar; any other editor (like  
nano) would be fine too. Open it and write your program:

=== begin ===
#! /usr/bin/env python

def sum(x,y):
 return x+y

x=int(raw_input('Enter a number: '))
y=int(raw_input('Enter a number: '))
print 'sum is', sum(x,y)
=== end ===

Copy all text between the marks === begin ===, and === end === (but not  
including those marks).
Save it into your home directory - when prompted for a name, type  
"test1.py" (without quotes). (If you don't know what is your home  
directory, read your OS documentation).
Now open a shell prompt (or terminal). Run the command "ls" (without  
quotes; press ENTER). You should see test1.py listed. (If not, use the cd  
command to go to your home directory and try again). Now execute "python  
test1.py" (again, without quotes) and you should be asked for the numbers,  
and the program should display the sum (as you already have seen inside  
the Python interpreter).
If you want to change the program: go back into the editor, modify it,  
save it (with the same name, or a different one), switch to the shell  
prompt and run it again.
These are the very basics of writing a text file and executing a Python  
program from the shell prompt. There are more powerful editors that are  
aware of Python syntax, by example, and can display the source code with  
different colors for keywords, numbers, comments, etc. Other programs  
combine an editor + debugger + code autocompletion + other nice features  
(they're called IDEs, in general).

I hope this is of some help. You should read the OS documentation for more  
info on how to edit a file and such things.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to save python codes in files?

2007-06-13 Thread why?
Also, how can i save a file using text editor in linux?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-13 Thread Leo Kislov
On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
> Hi all,
>
> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> is substantially slower when in a method than in a function.
>
> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>
>  20004 function calls in 10.214 CPU seconds

> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>
>  20004 function calls in 0.041 CPU seconds
>

I suspect open files are cached so the second reader
picks up where the first one left: at the of the file.
The second call doesn't do any text processing at all.

  -- Leo

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-13 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> take virtually the same amount of time on my machine (2.5), and the
> non-join version is clearer, IMO.  I'd still use join in case I wind
> up running under an older Python, but it's probably not a big issue here.

You should not rely on using 2.5 or even on that optimization staying in
CPython.  Best is to use StringIO or something comparable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-13 Thread Gabriel Genellina
En Thu, 14 Jun 2007 01:39:29 -0300, [EMAIL PROTECTED]  
<[EMAIL PROTECTED]> escribió:

> Gabriel Genellina wrote:
>> In addition, += is rather inefficient for strings; the usual idiom is
>> using ''.join(items)
>
> Ehh.  Python 2.5 (and probably some earlier versions) optimize += on
> strings pretty well.
>
> a=""
> for i in xrange(10):
> a+="a"
>
> and:
>
> a=[]
> for i in xrange(10):
> a.append("a")
> a="".join(a)
>
> take virtually the same amount of time on my machine (2.5), and the
> non-join version is clearer, IMO.  I'd still use join in case I wind
> up running under an older Python, but it's probably not a big issue
> here.

Yes, for concatenating a lot of a's, sure... Try again using strings  
around the size of your expected lines - and make sure they are all  
different too.

py> import timeit
py>
py> def f1():
...   a=""
...   for i in xrange(10):
...   a+=str(i)*20
...
py> def f2():
...   a=[]
...   for i in xrange(10):
...   a.append(str(i)*20)
...   a="".join(a)
...
py> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
[0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
py> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)

...after a few minutes I aborted the process...

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save class

2007-06-13 Thread Josiah Carlson
Gabriel Genellina wrote:
> En Wed, 13 Jun 2007 23:11:22 -0300, nik <[EMAIL PROTECTED]> escribió:
>> It would seem that I want to actually save the source code for the
>> class. I know that I could of course open up an editor and just make
>> it, but my ideal would be to have the base class, Map, be able to make
>> the sub-classes. I don't want the class definition. What I want is an
>> actual class that I could later import and use somewhere else. I am
>> planning to have each one of these map objects contain a different
>> dictionary and then be able to import the map into the application,
>> but have certain methods defined in the Map super-class to draw data
>> out of the specific map's specific dictionary. I hope that makes
>> sense.
>>
>> Something like,
>> class Map:
>>  dict = {}
>>  def DoSomething(self):
>>  pass
>>
>>  def MakeNewMapSubClass(self, newclassname):
>>  """ make a new file, newclassname.py that contains a new
>> class
>>  newclassname(Map) that inherits from base-class Map.
> 
> And are you sure you actually need different subclasses? Will you 
> construct them several instances of each subclass? From the above 
> description I feel you want just different Map *instances*, each with 
> its own dict, not different *subclasses*.

What you said, and that his solution sounds like a Java approach to the 
problem (subclass an abstract base class that calls specific methods on 
the subclass to "do the right thing").

To offer the OP source he can use...

class Map:
 def __init__(self):
 self.dict = {}
 def DoSomething(self):
 #do something with self.dict

Every instance gets a new dictionary.  Now, if he actually wants to 
change the behavior of the DoSomething method, of course then it would 
make sense to subclass Map.


  - Josiah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing arguments to tcpserver classes

2007-06-13 Thread Mark T

"Eric Spaulding" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Is there an easy way to pass arguments to a handler class that is used by 
> the standard TCPServer?
>
> normally --> srvr =SocketServer.TCPServer(('',port_num), TCPHandlerClass)
>
> I'd like to be able to: srvr =SocketServer.TCPServer(('',port_num), 
> TCPHandlerClass, (arg1,arg2))
>
> And have arg1, arg2 available via TCPHandlerClass.__init__ or some other 
> way.
>
> Where TCPHandlerClass:
>
> class TCPHandlerClass(SocketServer.StreamRequestHandler):
>def handle(self):
>   #handle stream events here#
>
>
> Thanks for any advice.
>

In the handler class, self.server refers to the server object, so subclass 
the server and override __init__ to take any additional server parameters 
and store them as instance variables.

import SocketServer

class MyServer(SocketServer.ThreadingTCPServer):
def __init__(self, server_address, RequestHandlerClass,arg1,arg2):

SocketServer.ThreadingTCPServer.__init__(self,server_address,RequestHandlerClass)
self.arg1 = arg1
self.arg2 = arg2

class MyHandler(SocketServer.StreamRequestHandler):
def handle(self):
print self.server.arg1
print self.server.arg2

if __name__ == '__main__':
srv = MyServer(('',5000),MyHandler,123,456)
srv.serve_forever()

--Mark

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to save python codes in files?

2007-06-13 Thread why?
I tried but its not working. Here's a code for sum of two numbers. Now
how do i save it?

>>>  #! /usr/bin/env python
...
>>> def sum(x,y):
... return x+y
...
>>> x=int(raw_input('Enter a number: '))
Enter a number: 35
>>> y=int(raw_input('Enter a number: '))
Enter a number: 7
>>> print 'sum is', sum(x,y)
sum is 42

-- 
http://mail.python.org/mailman/listinfo/python-list


dynamically generated runtime methods & reflection

2007-06-13 Thread Jay Loden
Hi all,

First, apologies if anyone gets this twice, but it took me quite a
while to figure out that Python.org is evidently rejecting all mail
from my mail server because I don't have reverse DNS configured.
Anyway:

I'm not even sure how to phrase this question properly or the right
terminology on this so bear with me. What I'm looking to find out is

a) is there a functionality in Python where I can call a method I have
not defined ahead of time, and based on the method name, change the
functionality of the method at runtime?

b) if not, what is the "Pythonic" approach to the problem outlined
below? Any recommendations on how to approach the problem differently
are welcome.

I've googled and read my Python reference pretty extensively and I've
found some hints but nothing that really answered my questions, so
here I am  :-)  I did figure out that you can overload __getattr__ in
a clas to define a new method at runtime, but I got stuck when I
couldn't figure out how to have a method know what name it was
originally called with. That's the basic question, see below for the
context I'm asking the question in and *why* I want to do the above
:-)

-

The software product I work on has a socket-based API that can be
accessed directly via telnet & manually typing commands, or by a set
of Perl modules that wrap the socket functionality. I am looking to
write a set of Python modules that match the same functionality.
Basically you can telnet to a port and type something like

item.list "param1=value1", "param2=value2"

And it will return the results (if any) to you in a given format along
with a response code, e.g. 200 OK or 400 ERROR. The Perl modules just
wrap this so that instead of the above, you can do something like
this:

$MySocketServer = new SocketServer(Host=>'127.0.0.'1, Port=>'');

if (! $MySocketServer->ListItem(itemName=>$item_name)) {
print "failed to retrieve list";
print "reason: " . $MySocketServer->GetErrorMsg();
exit;
}


The ListItem() method handles the work of communicating across the
socket, waiting for the response, and determine the success/failure
based on the return code. The part where it gets interesting is that
our Perl modules don't actually have a ListItem() method. Instead,
there is a generalized "_api_func()" method that looks at the name of
the method you called (using Autoload I believe, for any Perlheads out
there), and bases the action taken on the name.

In the example above, for instance, it sees that you called
ListItem(), so it transforms that to item.list and makes the
parameters of the method the parameters that are passed to item.list
via the socket server. Then it takes the response back from the socket
to determine the function return code. If there are any results such
as a list of items, they are then able to be accessed through a
reference to a hash of the results, and there is a GetResultRef
accessor function. In essence, our Perl modules are an incredibly
lightweight, simplistic wrapper for the socket-based API, and almost
all of the work is offloaded to the API implementation server-side.

I welcome any suggestions, feedback, sample code, documentation,
whitepapers etc. The only thing I don't want to have to do is go and
define a separate method for every single one of the possible
permutations of socket commands if I can avoid it!

-

Thanks in advance,

-Jay
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing arguments to tcpserver classes

2007-06-13 Thread Justin Ezequiel
On Jun 13, 10:19 pm, Eric Spaulding <[EMAIL PROTECTED]> wrote:
> Is there an easy way to pass arguments to a handler class that is used
> by the standard TCPServer?
>
> normally --> srvr =SocketServer.TCPServer(('',port_num), TCPHandlerClass)
>
> I'd like to be able to: srvr =SocketServer.TCPServer(('',port_num),
> TCPHandlerClass, (arg1,arg2))
>
> And have arg1, arg2 available via TCPHandlerClass.__init__ or some other
> way.
>

I use the following method.
Would also like to know if there's another way to do this.

class SVNUpdateRequestHandler(SocketServer.BaseRequestHandler):
def __init__(self, svn, wms, *args, **kwargs):
self.svn = svn
self.wms = wms
# NEED to set additional attributes before parent init
SocketServer.BaseRequestHandler.__init__(self, *args,
**kwargs)

def handle(self):
pass

def get_handler(svn, wms):
class RequestHandler(SVNUpdateRequestHandler):
def __init__(self, *args, **kwargs):
SVNUpdateRequestHandler.__init__(self, svn, wms,
 *args, **kwargs)

return RequestHandler

def main(port, requesthandler):
server = SVNUpdateServer(('', port), requesthandler)
while 1:
server.handle_request()

if __name__ == '__main__':
svn, wms = sys.argv[1:]

requesthandler = get_handler(svn, wms)
main(port, requesthandler)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-13 Thread [EMAIL PROTECTED]
Gabriel Genellina wrote:
> In the function above, s is a local variable, and accessing local
> variables is very efficient (using an array of local variables, the
> compiler assigns statically an index for each one).
> Using self.s, on the other hand, requires a name lookup for each access.
> The most obvious change would be to use a local variable s, and assign
> self.s = s only at the end. This should make both methods almost identical
> in performance.

Yeah, that's a big deal and makes a significant difference on my
machine.

> In addition, += is rather inefficient for strings; the usual idiom is
> using ''.join(items)

Ehh.  Python 2.5 (and probably some earlier versions) optimize += on
strings pretty well.

a=""
for i in xrange(10):
a+="a"

and:

a=[]
for i in xrange(10):
a.append("a")
a="".join(a)

take virtually the same amount of time on my machine (2.5), and the
non-join version is clearer, IMO.  I'd still use join in case I wind
up running under an older Python, but it's probably not a big issue
here.

> And since you have Python 2.5, you can use the file as its own iterator;
> combining all this:
>
>  return ''.join(line.strip() for line in filehandle if '>' not in line)

That's probably pretty good.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Build EXE on Mac OsX 10.4

2007-06-13 Thread Sherm Pendley
"Gabriel Genellina" <[EMAIL PROTECTED]> writes:

> En Wed, 13 Jun 2007 17:35:19 -0300, Paul McNett <[EMAIL PROTECTED]> escribió:
>
>> Tempo wrote:
>>> Has anyone sucesfully built a *.exe file on a mac operating system
>>> before from a *.py file? I have been trying to do this with
>>> pyinstaller, but I keep getting errors and I don't know how to
>>> install  [...]
>>
>> You need to build Mac Apps on Mac, Windows EXE's on Windows, and Linux
>> ELF's on Linux. You can't build a windows.exe from Mac, just as you
>> can't build a mac.app from Windows.
>
> That's not entirely true. gcc on linux can generate a Windows EXE, and
> using: python setup.py bdist_wininst, you can generate a complete
> binary  installer for Windows. I'm not sure if this can be done on a
> Mac too.

In principle, certainly - there's even a MacPort package for a complete
cygwin installation. I've built a number of packages with it - SDL and
several related libraries, for instance. There are also ELF cross-compiler
MacPort packages, presumably for building Linux binaries.

On the other hand, I *haven't* tried any of those compilers with setup.py,
and I have no idea if it can support those targets in practice. :-(

sherm--

-- 
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-13 Thread Grant Edwards
On 2007-06-14, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> The method takes > 10 seconds, the function call 0.041 seconds!

What happens when you run them in the other order?

The first time you read the file, it has to read it from disk.
The second time, it's probably just reading from the buffer
cache in RAM.

-- 
Grant Edwards   grante Yow!  Catsup and Mustard
  at   all over the place! It's
   visi.comthe Human Hamburger!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-13 Thread Steven Bethard
Rob Wolfe wrote:
> # HTML page
> dinner_recipe = '''
> Recipe
> 
> amtunititem
> 24slicesbaguette
> 2+tbspolive_oil
> 1cuptomatoes
> 1-2tbspgarlic
> 1/2cupParmesan
> 1jarpesto
> 
> '''
> 
> # program
> import xml.etree.ElementTree as etree
> tree = etree.fromstring(dinner_recipe)
> 
> #import ElementSoup as etree # for invalid HTML
> #from cStringIO import StringIO  # use this
> #tree = etree.parse(StringIO(dinner_recipe)) # wrapper for BeautifulSoup
> 
> pantry = set(['olive oil', 'pesto'])
> 
> for ingredient in tree.getiterator('tr'):
> amt, unit, item = ingredient.getchildren()
> if item.tag == "td" and item.text not in pantry:
> print "%s: %s %s" % (item.text, amt.text, unit.text)

I posted a slight variant of this, trimmed down a bit to 21 lines.

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-13 Thread Steven Bethard
James Turk wrote:
> It actually occured to me that I could use a @classmethod to do the
> loading and take that out of the BaseClass constructor.  What I have
> makes more sense and eliminates the unecessary constructors.
> 
> ie.
> 
> class BaseClass:
> @classmethod
> def loadData(params):
> #expensive load here
> 
> class ChildClass1(BaseClass):
>  dataset = BaseClass.loadData(params)
> 
> This is pretty much along the lines of what you suggested, thank you
> for the hint in the right direction.
> 
> I realized that this still doesn't meet my needs exactly as I only
> want the expensive dataset to be loaded if/when a class is actually
> used (there are potentially many of these and only a few will be
> used).

Seems like you want a lazy class attribute. How about something like::

 >>> class LazyClassAttribute(object):
... def __init__(self, func):
... self.func = func
... def __get__(self, obj, cls=None):
... value = self.func(cls)
... setattr(cls, self.func.__name__, value)
... return value
...
 >>> class Base(object):
... @LazyClassAttribute
... def dataset(cls):
... print 'calculating dataset'
... return 'dataset(%s)' % cls.params
...
 >>> class Child1(Base):
... params = 'foo'
...
 >>> class Child2(Base):
... params = 'bar'
...
 >>> Child1.dataset
calculating dataset
'dataset(foo)'
 >>> Child1.dataset
'dataset(foo)'
 >>> Child2.dataset
calculating dataset
'dataset(bar)'
 >>> Child2.dataset
'dataset(bar)'

The idea is basically similar to the @classmethod approach except that 
instead of @classmethod, we use a custom descriptor that calls the 
method the first time it's accessed and then stores that value 
afterwards. This means that instead of explicitly calling the 
@classmethod, the method will be called whenever the attribute is first 
accessed.

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python optimization (was Python's "only one way to do it" philosophy isn't good?)

2007-06-13 Thread John Nagle
Paul Rubin wrote:
> "Diez B. Roggisch" <[EMAIL PROTECTED]> writes:
> 
>>And if only the html-parsing is slow, you might consider creating an
>>extension for that. Using e.g. Pyrex.
> 
> 
> I just tried using BeautifulSoup to pull some fields out of some html
> files--about 2 million files, output of a web crawler.  It parsed very
> nicely at about 5 files per second.  

 That's about what I'm seeing.  And it's the bottleneck of
"sitetruth.com".

>  By
> simply treating the html as a big string and using string.find to
> locate the fields I wanted, I got it up to about 800 files/second,
> which made each run about 1/2 hour. 

 For our application, we have to look at the HTML in some detail,
so we really need it in a tree form.

 > Simplest still would be if Python
> just ran about 100x faster than it does, a speedup which is not
> outlandish to hope for.

Right.  Looking forward to ShedSkin getting good enough to run
BeautifulSoup.

(Actually, the future of page parsing is probably to use some kind
of stripped-down browser that reads the page, builds the DOM,
runs the startup JavaScript, then lets you examine the DOM.  There
are too many pages now that just come through as blank if you don't
run the OnLoad JavaScript.)

John Nagle
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save class

2007-06-13 Thread Gabriel Genellina
En Wed, 13 Jun 2007 23:11:22 -0300, nik <[EMAIL PROTECTED]> escribió:

> On Jun 13, 6:48 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> wrote:
>> En Wed, 13 Jun 2007 22:20:16 -0300, nik <[EMAIL PROTECTED]> escribió:
>>
>> > I would like to create a class and then save it for re-use later. I
>> > have tried to usepickle, but am not sure if that is right. I am
>> > sorry, but I am new to python.
>>
>> Do you want to save the *source*code* of your class, or do you want to
>> save created *instances* -objects- of your classes to retrieve them  
>> later (like a database)?

> It would seem that I want to actually save the source code for the
> class. I know that I could of course open up an editor and just make
> it, but my ideal would be to have the base class, Map, be able to make
> the sub-classes. I don't want the class definition. What I want is an
> actual class that I could later import and use somewhere else. I am
> planning to have each one of these map objects contain a different
> dictionary and then be able to import the map into the application,
> but have certain methods defined in the Map super-class to draw data
> out of the specific map's specific dictionary. I hope that makes
> sense.
>
> Something like,
> class Map:
>  dict = {}
>  def DoSomething(self):
>  pass
>
>  def MakeNewMapSubClass(self, newclassname):
>  """ make a new file, newclassname.py that contains a new
> class
>  newclassname(Map) that inherits from base-class Map.

And are you sure you actually need different subclasses? Will you  
construct them several instances of each subclass? From the above  
description I feel you want just different Map *instances*, each with its  
own dict, not different *subclasses*.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-13 Thread Steve Howell

--- Steven Bethard <[EMAIL PROTECTED]> wrote:

> Rob Wolfe wrote:
> > Steve Howell wrote:
> > 
> >> I suggested earlier that maybe we post multiple
> >> solutions.  That makes me a little nervous, to
> the
> >> extent that it shows that the Python community
> has a
> >> hard time coming to consensus on tools sometimes.
> > 
> > We agree that BeautifulSoup is the best for
> parsing HTML. :)
> > 
> >> This is not a completely unfair knock on Python,
> >> although I think the reason multiple solutions
> tend to
> >> emerge for this type of thing is precisely due to
> the
> >> simplicity and power of the language itself.
> >>
> >> So I don't know.  What about trying to agree on
> an XML
> >> parsing example instead?
> >>
> >> Thoughts?
> > 
> > I vote for example with ElementTree (without
> xpath)
> > with a mention of using ElementSoup for invalid
> HTML.
> 
> Sounds good to me.  Maybe something like::
> 
> import xml.etree.ElementTree as etree
> dinner_recipe = '''
> 
>
24slicesbaguette
>
2+tbspolive_oil
>
1cuptomatoes
>
1-2tbspgarlic
>
1/2cupParmesan
>
1jarpesto
> '''
> pantry = set(['olive oil', 'pesto'])
> tree = etree.fromstring(dinner_recipe)
> for item_elem in tree.getiterator('item'):
>  if item_elem.text not in pantry:
>  print item_elem.text
> 
> Though I wouldn't know where to put the ElementSoup
> link in this one...
> 

Whatever makes the most sense, please post it.  Sorry
for not responding earlier.




   

Building a website is a piece of cake. Yahoo! Small Business gives you all the 
tools to get online.
http://smallbusiness.yahoo.com/webhosting 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cretins.

2007-06-13 Thread Cousin Stanley

> On Thu, 14 Jun 2007 09:32:10 +1000, Ben Finney wrote:
>
>> "Dr. Pastor" <[EMAIL PROTECTED]> writes:
>> 
>>> Please do not do business with those cretins
>>> who without authorization attaching [spam footers]
>> 
>> Indeed. The cost of Usenet access should not be translated 
>> to spam on messages.
>
> Out of curiosity, who are these cretins ? 
  
  NewsFeeds.Com  

  I  don't  do business with them either  

  But my ISP does, apparently farming out 
  their news service  

  I complained about the addition of spammed footers
  by NewsFeeds.Com to my ISP several years back
  but it didn't help at all  

  My news client is configured to use my ISP's news server
  but it gets spun off to NewsFeeds.Com 

  Anything below  Phoenix, Arizona  in my signature
  was  not  added by me but by NewsFeeds.Com 


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona


== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet 
News==
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ 
Newsgroups
= East and West-Coast Server Farms - Total Privacy via Encryption =
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python optimization (was Python's "only one way to do it" philosophy isn't good?)

2007-06-13 Thread Paul Rubin
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes:
> And if only the html-parsing is slow, you might consider creating an
> extension for that. Using e.g. Pyrex.

I just tried using BeautifulSoup to pull some fields out of some html
files--about 2 million files, output of a web crawler.  It parsed very
nicely at about 5 files per second.  Of course Python being Python, I
wanted to run the program a whole lot of times, modifying it based on
what I found from previous runs, and at 5/sec each run was going to
take about 4 days (OK, I probably could have spread it across 5 or so
computers and gotten it to under 1 day, at the cost of more effort to
write the parallelizing code and to scare up extra machines).  By
simply treating the html as a big string and using string.find to
locate the fields I wanted, I got it up to about 800 files/second,
which made each run about 1/2 hour.  Simplest still would be if Python
just ran about 100x faster than it does, a speedup which is not
outlandish to hope for.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save class

2007-06-13 Thread nik
On Jun 13, 6:48 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Wed, 13 Jun 2007 22:20:16 -0300, nik <[EMAIL PROTECTED]> escribió:
>
> > I would like to create a class and then save it for re-use later. I
> > have tried to usepickle, but am not sure if that is right. I am
> > sorry, but I am new to python.
>
> Do you want to save the *source*code* of your class, or do you want to
> save created *instances* -objects- of your classes to retrieve them later
> (like a database)?
>
> > Basically, I have a class, Map. I want to be able to create new maps:
> > MapA, MapB... that have Map as the base class.
>
> > start with-
> > class Map:
> > pass
>
> > and then get a different class
>
> > class MapA(Map):
> > pass
>
> > that can be saved in a .py file for re-use
>
> You just create the .py file with any text editor, containing the source
> code for all your classes.
>
> > so far I thought that -
> > cls = new.classobj('MapA', (Map, ), {})
> > file = open('somefile', mode='w')
> >pickle.dump(cls, file)
>
> > -might work, but it didn't can anybody point me in the right
> > direction? I know that classes must get saved from the interactive
> > console, so I would think that it would be a standard thing to do.
>
> This would try to save the *class* definition, which is usually not
> required because they reside on your source files.
> If this is actually what you really want to do, try to explain us exactly
> why do you think so. Chances are that there is another solution for this.
>
> --
> Gabriel Genellina

Thanks for the response.

It would seem that I want to actually save the source code for the
class. I know that I could of course open up an editor and just make
it, but my ideal would be to have the base class, Map, be able to make
the sub-classes. I don't want the class definition. What I want is an
actual class that I could later import and use somewhere else. I am
planning to have each one of these map objects contain a different
dictionary and then be able to import the map into the application,
but have certain methods defined in the Map super-class to draw data
out of the specific map's specific dictionary. I hope that makes
sense.

Something like,
class Map:
 dict = {}
 def DoSomething(self):
 pass

 def MakeNewMapSubClass(self, newclassname):
 """ make a new file, newclassname.py that contains a new
class
 newclassname(Map) that inherits from base-class Map.

Thanks,
Nik

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: save class

2007-06-13 Thread Gabriel Genellina
En Wed, 13 Jun 2007 22:20:16 -0300, nik <[EMAIL PROTECTED]> escribió:

> I would like to create a class and then save it for re-use later. I
> have tried to use pickle, but am not sure if that is right. I am
> sorry, but I am new to python.

Do you want to save the *source*code* of your class, or do you want to  
save created *instances* -objects- of your classes to retrieve them later  
(like a database)?

> Basically, I have a class, Map. I want to be able to create new maps:
> MapA, MapB... that have Map as the base class.
>
> start with-
> class Map:
> pass
>
> and then get a different class
>
> class MapA(Map):
> pass
>
> that can be saved in a .py file for re-use

You just create the .py file with any text editor, containing the source  
code for all your classes.

> so far I thought that -
> cls = new.classobj('MapA', (Map, ), {})
> file = open('somefile', mode='w')
> pickle.dump(cls, file)
>
> -might work, but it didn't can anybody point me in the right
> direction? I know that classes must get saved from the interactive
> console, so I would think that it would be a standard thing to do.

This would try to save the *class* definition, which is usually not  
required because they reside on your source files.
If this is actually what you really want to do, try to explain us exactly  
why do you think so. Chances are that there is another solution for this.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-13 Thread James Turk
On Jun 13, 9:03 pm, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Wed, 13 Jun 2007 23:55:02 +, James Turk wrote:
> > On Jun 13, 6:54 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> >> James Turk wrote:
> >> > Hi,
>
> >> > I have a situation where I have some class members that should only be
> >> > done once.  Essentially my problem looks like this:
>
> >> > class Base(object):
> >> > dataset = None
>
> >> > def __init__(self, param):
> >> > if type(self).dataset is None:
> >> > # code to load dataset based on param, expensive
>
> >> > class ChildClass1(Base):
> >> > def __init__(self):
> >> > Base.__init__(self, data_params)
>
> >> > class AnotherChildClass(Base):
> >> > def __init__(self):
> >> > Base.__init__(self, other_data_params)
>
> >> > This seems to work, initialization is only done at the first creation
> >> > of either class.  I was just wondering if this is the 'pythonic' way
> >> > to do this as my solution does feel a bit hackish.
>
> >> What should happen with code like::
>
> >>  ChildClass1('foo')
> >>  ChildClass1('bar')
>
> >> The 'param' is different, but 'dataset' should only get set the first time?
>
> >> STeVe
>
> > ChildClass doesn't take the parameter in it's constructor, it supplies
> > it for the BaseClass.  Every ChildClass of the same type should use
> > the same dataset.
>
> Then each type of ChildClass should be a sub-class, and provide it's own
> dataset:
>
> class BaseClass:
> dataset = None
> # blah blah blah...
>
> class ChildClass1(BaseClass):
> dataset = SomethingUseful
>
> class ChildClass2(BaseClass):
> dataset = SomethingElse
>
> --
> Steven.

It actually occured to me that I could use a @classmethod to do the
loading and take that out of the BaseClass constructor.  What I have
makes more sense and eliminates the unecessary constructors.

ie.

class BaseClass:
@classmethod
def loadData(params):
#expensive load here

class ChildClass1(BaseClass):
 dataset = BaseClass.loadData(params)

This is pretty much along the lines of what you suggested, thank you
for the hint in the right direction.

I realized that this still doesn't meet my needs exactly as I only
want the expensive dataset to be loaded if/when a class is actually
used (there are potentially many of these and only a few will be
used).  I believe I have two options:

1) put each ChildClass in a separate file
2) in each ChildClass constructor put a check if the dataset has been
loaded yet, so that the first time an instance is created it can call
the BaseClass.loadData

for now I have chosen the second option, which is to change the child
classes to resemble

class ChildClass(BaseClass):

dataset = None

def __init__(self):
if BaseClass.dataset is None:
self(type).dataset = BaseClass.loadData(params)


I am still doing the self(type) access, but I like it more now that
I've taken it out of the BaseClass constructor.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I capture all exceptions especially when os.system() fail? Thanks

2007-06-13 Thread Gabriel Genellina
En Wed, 13 Jun 2007 21:47:16 -0300, mike <[EMAIL PROTECTED]> escribió:

> Following piece of code can capture IOError when the file doesn't
> exist, also, other unknown exceptions can be captured when I press
> Ctrl-C while the program is sleeping(time.sleep). Now the question is:
> when I run the non-exist command, the exception cannot be captured.

> So far so good, then I changed the code to run a non-exist command
> "wrong_command_test"(commented the open and sleep lines), then the
> script printed:
> sh: wrong_command_test: command not found
> well Done

That's because it is not an exception, it is an error message coming from  
your shell, not from Python.
You can extract the exit status from what os.system returns (see the  
details on the docs for os.system); in particular, usually "command not  
found" is error 127
This is a list of more-or-less standard exit codes:  
http://www.faqs.org/docs/abs/HTML/exitcodes.html

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-13 Thread Gabriel Genellina
En Wed, 13 Jun 2007 21:40:12 -0300, <[EMAIL PROTECTED]> escribió:

> Hi all,
>
> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> is substantially slower when in a method than in a function.
>
> # START SOURCE #
> # The function
>
> def readgenome(filehandle):
>   s = ''
>   for line in filehandle.xreadlines():
>   if '>' in line:
>   continue
>   s += line.strip()
>   return s
>
> # The method in a class
> class bar:
>   def readgenome(self, filehandle):
>   self.s = ''
>   for line in filehandle.xreadlines():
>   if '>' in line:
>   continue
>   self.s += line.strip()
>

In the function above, s is a local variable, and accessing local  
variables is very efficient (using an array of local variables, the  
compiler assigns statically an index for each one).
Using self.s, on the other hand, requires a name lookup for each access.  
The most obvious change would be to use a local variable s, and assign  
self.s = s only at the end. This should make both methods almost identical  
in performance.
In addition, += is rather inefficient for strings; the usual idiom is  
using ''.join(items)
And since you have Python 2.5, you can use the file as its own iterator;  
combining all this:

 return ''.join(line.strip() for line in filehandle if '>' not in line)

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-13 Thread Neil Cerutti
On 2007-06-14, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> is substantially slower when in a method than in a function.
>
> # START SOURCE #
> # The function
>
> def readgenome(filehandle):
>   s = ''
>   for line in filehandle.xreadlines():
>   if '>' in line:
>   continue
>   s += line.strip()
>   return s
>
> # The method in a class
> class bar:
>   def readgenome(self, filehandle):
>   self.s = ''
>   for line in filehandle.xreadlines():
>   if '>' in line:
>   continue
>   self.s += line.strip()
>
> # END SOURCE ##
> When running the function and the method on a 20,000 line text file, I
> get the following:
>
 cProfile.run("bar.readgenome(open('cb_foo'))")
>  20004 function calls in 10.214 CPU seconds
>
>Ordered by: standard name
>
>ncalls  tottime  percall  cumtime  percall
> filename:lineno(function)
> 10.0000.000   10.214   10.214 :1()
> 1   10.205   10.205   10.214   10.214 reader.py:11(readgenome)
> 10.0000.0000.0000.000 {method 'disable' of
> '_lsprof.Profiler' objects}
> 10.0090.0000.0090.000 {method 'strip' of 'str'
> objects}
> 10.0000.0000.0000.000 {method 'xreadlines' of
> 'file' objects}
> 10.0000.0000.0000.000 {open}
>
>
 cProfile.run("z=r.readgenome(open('cb_foo'))")
>  20004 function calls in 0.041 CPU seconds
>
>Ordered by: standard name
>
>ncalls  tottime  percall  cumtime  percall
> filename:lineno(function)
> 10.0000.0000.0410.041 :1()
> 10.0350.0350.0410.041 reader.py:2(readgenome)
> 10.0000.0000.0000.000 {method 'disable' of
> '_lsprof.Profiler' objects}
> 10.0070.0000.0070.000 {method 'strip' of 'str'
> objects}
> 10.0000.0000.0000.000 {method 'xreadlines' of
> 'file' objects}
> 10.0000.0000.0000.000 {open}
>
>
> The method takes > 10 seconds, the function call 0.041 seconds!
>
> Yes, I know that I wrote the underlying code rather
> inefficiently, and I can streamline it with a single
> file.read()  call instead if an xreadlines() + strip loop.
> Still, the differences in performance are rather staggering!
> Any comments?

It is likely the repeated attribute lookup, self.s, that's
slowing it down in comparison to the non-method version.

Try the following simple optimization, using a local variable
instead of an attribute to build up the result.

# The method in a class
class bar:
def readgenome(self, filehandle):
s = ''
for line in filehandle.xreadlines():
if '>' in line:
continue
s += line.strip()
self.s = s

To further speed things up, think about using the str.join idiom
instead of str.+=, and using a generator expression instead of an
explicit loop.

# The method in a class
class bar:
def readgenome(self, filehandle):
self.s = ''.join(line.strip() for line in filehandle)

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


save class

2007-06-13 Thread nik
Hi,

I would like to create a class and then save it for re-use later. I
have tried to use pickle, but am not sure if that is right. I am
sorry, but I am new to python.

Basically, I have a class, Map. I want to be able to create new maps:
MapA, MapB... that have Map as the base class.

start with-
class Map:
pass

and then get a different class

class MapA(Map):
pass

that can be saved in a .py file for re-use

so far I thought that -
cls = new.classobj('MapA', (Map, ), {})
file = open('somefile', mode='w')
pickle.dump(cls, file)

-might work, but it didn't can anybody point me in the right
direction? I know that classes must get saved from the interactive
console, so I would think that it would be a standard thing to do.

Thank you,
Nik

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-13 Thread Gabriel Genellina
En Wed, 13 Jun 2007 22:03:50 -0300, Steven D'Aprano  
<[EMAIL PROTECTED]> escribió:

> On Wed, 13 Jun 2007 23:55:02 +, James Turk wrote:
>>> James Turk wrote:
>>>
>>> > I have a situation where I have some class members that should only  
>>> be
>>> > done once.  Essentially my problem looks like this:
>>>
>> ChildClass doesn't take the parameter in it's constructor, it supplies
>> it for the BaseClass.  Every ChildClass of the same type should use
>> the same dataset.
>
> Then each type of ChildClass should be a sub-class, and provide it's own
> dataset:
>
>
> class BaseClass:
> dataset = None
> # blah blah blah...
>
>
> class ChildClass1(BaseClass):
> dataset = SomethingUseful
> class ChildClass2(BaseClass):
> dataset = SomethingElse

But the OP also stated that creating the dataset is expensive. The  
original code does what you say (each ChildClass is a subclass and  
provides its own dataset) so I think is an acceptable solution:

py> def build_dataset(x):
... print "building dataset:",x
... return [x]
...
py> class Base(object):
... dataset = None
... def __init__(self, param):
... if type(self).dataset is None:
... type(self).dataset = build_dataset(param)
...
py> class ChildClass1(Base):
... def __init__(self):
... Base.__init__(self, "Params for ChildClass1")
...
py> class AnotherChildClass(Base):
... def __init__(self):
... Base.__init__(self, "Params for AnotherChildClass")
...
py> c1 = ChildClass1()
building dataset: Params for ChildClass1
py> c2 = AnotherChildClass()
building dataset: Params for AnotherChildClass
py> c3 = ChildClass1()
py> print Base.dataset
None
py> print ChildClass1.dataset
['Params for ChildClass1']
py> print AnotherChildClass.dataset
['Params for AnotherChildClass']
py> print c1.dataset
['Params for ChildClass1']
py> print c3.dataset
['Params for ChildClass1']
py> print c1.dataset is c3.dataset
True
py>


-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Build EXE on Mac OsX 10.4

2007-06-13 Thread Gabriel Genellina
En Wed, 13 Jun 2007 17:35:19 -0300, Paul McNett <[EMAIL PROTECTED]> escribió:

> Tempo wrote:
>> Has anyone sucesfully built a *.exe file on a mac operating system
>> before from a *.py file? I have been trying to do this with
>> pyinstaller, but I keep getting errors and I don't know how to install  
>> [...]
>
> You need to build Mac Apps on Mac, Windows EXE's on Windows, and Linux
> ELF's on Linux. You can't build a windows.exe from Mac, just as you
> can't build a mac.app from Windows.

That's not entirely true. gcc on linux can generate a Windows EXE, and  
using: python setup.py bdist_wininst, you can generate a complete binary  
installer for Windows. I'm not sure if this can be done on a Mac too.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-13 Thread Steven D'Aprano
On Wed, 13 Jun 2007 23:55:02 +, James Turk wrote:

> On Jun 13, 6:54 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
>> James Turk wrote:
>> > Hi,
>>
>> > I have a situation where I have some class members that should only be
>> > done once.  Essentially my problem looks like this:
>>
>> > class Base(object):
>> > dataset = None
>>
>> > def __init__(self, param):
>> > if type(self).dataset is None:
>> > # code to load dataset based on param, expensive
>>
>> > class ChildClass1(Base):
>> > def __init__(self):
>> > Base.__init__(self, data_params)
>>
>> > class AnotherChildClass(Base):
>> > def __init__(self):
>> > Base.__init__(self, other_data_params)
>>
>> > This seems to work, initialization is only done at the first creation
>> > of either class.  I was just wondering if this is the 'pythonic' way
>> > to do this as my solution does feel a bit hackish.
>>
>> What should happen with code like::
>>
>>  ChildClass1('foo')
>>  ChildClass1('bar')
>>
>> The 'param' is different, but 'dataset' should only get set the first time?
>>
>> STeVe
> 
> ChildClass doesn't take the parameter in it's constructor, it supplies
> it for the BaseClass.  Every ChildClass of the same type should use
> the same dataset.

Then each type of ChildClass should be a sub-class, and provide it's own
dataset:


class BaseClass:
dataset = None
# blah blah blah...


class ChildClass1(BaseClass):
dataset = SomethingUseful
   
class ChildClass2(BaseClass):
dataset = SomethingElse





-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


How can I capture all exceptions especially when os.system() fail? Thanks

2007-06-13 Thread mike
Hi Guys,

Following piece of code can capture IOError when the file doesn't
exist, also, other unknown exceptions can be captured when I press
Ctrl-C while the program is sleeping(time.sleep). Now the question is:
when I run the non-exist command, the exception cannot be captured.

Here is the code:
===
#!/usr/bin/python
import os
import sys
import time

try:
fh = open("tt.py")
time.sleep(10)
#os.system("wrong_command_test")
except IOError:
print 'failed to open.'
sys.exit(0)
except:
print 'Some exceptions occurred.'
else:
print 'well',

print 'Done'

===
when the tt.py doesn't exist, the script printed:
failed to open.
when the tt.py exists, the script printed:
well done
when I press Ctrl-C while the program is sleeping, the script printed:
Some exceptions occurred.
Done

So far so good, then I changed the code to run a non-exist command
"wrong_command_test"(commented the open and sleep lines), then the
script printed:
sh: wrong_command_test: command not found
well Done


Any opinions would be appreciated.

Mike

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cretins.

2007-06-13 Thread Steven D'Aprano
On Thu, 14 Jun 2007 09:32:10 +1000, Ben Finney wrote:

> "Dr. Pastor" <[EMAIL PROTECTED]> writes:
> 
>> Please do not do business with
>> those cretins who without authorization
>> attaching [spam footers]
> 
> Indeed. The cost of Usenet access should not be translated to spam on
> messages.

Out of curiosity, who are these cretins? Because Dr Pastor's original post
seems to have been eaten by the news server, perhaps because it includes
too much spam.

-- 
Steven.


-- 
http://mail.python.org/mailman/listinfo/python-list


Method much slower than function?

2007-06-13 Thread idoerg
Hi all,

I am running Python 2.5 on Feisty Ubuntu. I came across some code that
is substantially slower when in a method than in a function.

# START SOURCE #
# The function

def readgenome(filehandle):
s = ''
for line in filehandle.xreadlines():
if '>' in line:
continue
s += line.strip()
return s

# The method in a class
class bar:
def readgenome(self, filehandle):
self.s = ''
for line in filehandle.xreadlines():
if '>' in line:
continue
self.s += line.strip()

# END SOURCE ##
When running the function and the method on a 20,000 line text file, I
get the following:

>>> cProfile.run("bar.readgenome(open('cb_foo'))")
 20004 function calls in 10.214 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall
filename:lineno(function)
10.0000.000   10.214   10.214 :1()
1   10.205   10.205   10.214   10.214 reader.py:11(readgenome)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}
10.0090.0000.0090.000 {method 'strip' of 'str'
objects}
10.0000.0000.0000.000 {method 'xreadlines' of
'file' objects}
10.0000.0000.0000.000 {open}


>>> cProfile.run("z=r.readgenome(open('cb_foo'))")
 20004 function calls in 0.041 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall
filename:lineno(function)
10.0000.0000.0410.041 :1()
10.0350.0350.0410.041 reader.py:2(readgenome)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}
10.0070.0000.0070.000 {method 'strip' of 'str'
objects}
10.0000.0000.0000.000 {method 'xreadlines' of
'file' objects}
10.0000.0000.0000.000 {open}


The method takes > 10 seconds, the function call 0.041 seconds!

Yes, I know that I wrote the underlying code rather inefficiently, and
I can streamline it with a single file.read()  call instead if an
xreadlines() + strip loop. Still, the differences in performance are
rather staggering! Any comments?

Thanks,

Iddo

-- 
http://mail.python.org/mailman/listinfo/python-list


native python matrix class (2D list), without inverse

2007-06-13 Thread DarrenWeber

# Copyright (C) 2007 Darren Lee Weber
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

__version__ = "$Revision: 1.9 $" # $Date: 2007/06/14 00:24:57 $

class Matrix:
"""
Create and manipulate a matrix object

Matrix(data, dim)

data = list of lists (currently only 2D)
dim=(row,col) tuple of int

For example,

#data = [[0.0] * c for i in xrange(r)]
data = [[0.0,0.1],[1.0,1.1],[2.0,2.1]]
rowN =len(data)
colN =len(data[0])
m = Matrix(data)
m = Matrix(data,dim=(rowN, colN))

d1 = [[0.0, 0.1], [1.0, 1.1], [2.0, 2.1]] # 3x2 matrix
d2 = [[0.0, 0.1, 0.2], [1.0, 1.1, 1.2]]   # 2x3 matrix
m1 = Matrix(d1)
m2 = Matrix(d2)
#m3 = m1 + m2 # dimension error
m3 = m1 + m2.transpose()
m3 = m1 - m2.transpose()
m3 = m1 * m2# 3x3
m3 = m2 * m1# 2x2

m1[2,:]
m1[:,2]
"""

def __init__(self, data=None, dim=None):
"""
create a matrix instance.

m = Matrix([data [, dim]])

 is a 2D matrix comprised of a nested list of floats
 is a tuple of int values for the row and column size
(r,c)

eg:
data = [[0.0,0.1],[1.0,1.1],[2.0,2.1]]
dim = (3,2) # or (len(data),len(data[0]))
"""

if data != None:
# check data for the cell types (ensure float)?
self.data = data
r = len(data)
c = len(data[0])

# Are all the rows the same length?
rowLenCheck = sum([len(data[i]) != c for i in range(r)])
if rowLenCheck > 0:
raise ValueError
else:
self.dim = (r,c)

if dim != None:
if (dim[0] == r) and (dim[1] == c):
self.dim = (r,c)
else:
# over-ride the dim input, do not reshape data!
# print a warning?
self.dim = (r,c)
else:
if dim != None:
if len(dim) == 2:
self.dim = tuple(dim)
r = dim[0]
c = dim[1]
else:
# maybe a new exception type?
arg = ("len(dim) != 2: ", dim)
raise ValueError, arg

# BEGIN ALT 
# Does this give unique memory for each element?
# self.data = [[0.0] * c for i in xrange(r)]

# It seems that the initialization does not generate
# unique memory elements because all list elements
# refer to the same number object (0.0), but
# modification of any element creates a unique value,
# without changing any other values, eg:

##>>> x = [[0.0] * 3 for i in xrange(2)]
##>>> id(x)
# 3079625068L
# >>> id(x[0][0])
# 136477300
# >>> id(x[0][1])
# 136477300
# >>> id(x[1][1])
# 136477300
# >>> x[0][0] = 1.0
# >>> x
# [[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
# >>>
# END ALT 

# create a zero row vector, with unique memory for
each element
self.data = [[x * 0.0 for x in range(c)]]
for i in range(1,r):
self.data.append([x * 0.0 for x in range(c)])
else:
self.data = []
self.dim = (0,0)
#print self.__doc__

def __getitem__(self, i):
"""
matrix[r,c] returns values from matrix.data, eg:
data = [[0.0,0.1],[1.0,1.1],[2.0,2.1]]
m = Matrix(data)
m[2,:]
>> [2.0, 2.1001]
"""
r = i[0]
c = i[1]
#print "index: (%s, %s)" % (r,c)
#print "value: ", self.data[r][c]
return self.data[r][c]

def reshape(self, newdim=None):
'reshape a matrix object: matrix.reshape(newdim)'
print "something to implement later"
pass

def transpose(self):
'transpose a matrix: m2 = m1.transpose()'
 

Re: one-time initialization of class members

2007-06-13 Thread James Turk
On Jun 13, 8:00 pm, Larry Bates <[EMAIL PROTECTED]> wrote:
> James Turk wrote:
> > Hi,
>
> > I have a situation where I have some class members that should only be
> > done once.  Essentially my problem looks like this:
>
> > class Base(object):
> > dataset = None
>
> > def __init__(self, param):
> > if type(self).dataset is None:
> > # code to load dataset based on param, expensive
>
> > class ChildClass1(Base):
> > def __init__(self):
> > Base.__init__(self, data_params)
>
> > class AnotherChildClass(Base):
> > def __init__(self):
> > Base.__init__(self, other_data_params)
>
> > This seems to work, initialization is only done at the first creation
> > of either class.  I was just wondering if this is the 'pythonic' way
> > to do this as my solution does feel a bit hackish.
>
> I could be missing something but dataset is shared among all the class
> instances.  If you reset it based on param it will be reset every time
> you create a new instance of the Base class with a different param.  Is
> that really what you want to do?  If so just use:
>
> class Base(object):
> dataset = None
>
> def __init__(self, param):
> if self.dataset is None:
> # code to load dataset based on param, expensive
>
> -Larry

I'm sorry, I somehow omitted the fact that the dataset does indeed
need to vary based on the child class, actually this is the main
difference between child classes.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-13 Thread Larry Bates
James Turk wrote:
> Hi,
> 
> I have a situation where I have some class members that should only be
> done once.  Essentially my problem looks like this:
> 
> class Base(object):
> dataset = None
> 
> def __init__(self, param):
> if type(self).dataset is None:
> # code to load dataset based on param, expensive
> 
> class ChildClass1(Base):
> def __init__(self):
> Base.__init__(self, data_params)
> 
> class AnotherChildClass(Base):
> def __init__(self):
> Base.__init__(self, other_data_params)
> 
> 
> This seems to work, initialization is only done at the first creation
> of either class.  I was just wondering if this is the 'pythonic' way
> to do this as my solution does feel a bit hackish.
> 
I could be missing something but dataset is shared among all the class
instances.  If you reset it based on param it will be reset every time
you create a new instance of the Base class with a different param.  Is
that really what you want to do?  If so just use:

class Base(object):
dataset = None

def __init__(self, param):
if self.dataset is None:
# code to load dataset based on param, expensive


-Larry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-13 Thread James Turk
On Jun 13, 6:54 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> James Turk wrote:
> > Hi,
>
> > I have a situation where I have some class members that should only be
> > done once.  Essentially my problem looks like this:
>
> > class Base(object):
> > dataset = None
>
> > def __init__(self, param):
> > if type(self).dataset is None:
> > # code to load dataset based on param, expensive
>
> > class ChildClass1(Base):
> > def __init__(self):
> > Base.__init__(self, data_params)
>
> > class AnotherChildClass(Base):
> > def __init__(self):
> > Base.__init__(self, other_data_params)
>
> > This seems to work, initialization is only done at the first creation
> > of either class.  I was just wondering if this is the 'pythonic' way
> > to do this as my solution does feel a bit hackish.
>
> What should happen with code like::
>
>  ChildClass1('foo')
>  ChildClass1('bar')
>
> The 'param' is different, but 'dataset' should only get set the first time?
>
> STeVe

ChildClass doesn't take the parameter in it's constructor, it supplies
it for the BaseClass.  Every ChildClass of the same type should use
the same dataset.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cretins.

2007-06-13 Thread Ben Finney
"Dr. Pastor" <[EMAIL PROTECTED]> writes:

> Please do not do business with
> those cretins who without authorization
> attaching [spam footers]

Indeed. The cost of Usenet access should not be translated to spam on
messages.

-- 
 \   "If consumers even know there's a DRM, what it is, and how it |
  `\   works, we've already failed."  -- Peter Lee, Disney |
_o__)corporation, 2005 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bytes/File Size Format Function

2007-06-13 Thread Ben Finney
Avell Diroll <[EMAIL PROTECTED]> writes:

> I have to disagree: 'mb' should stand for "milli-bit"  :)

Yes, you're right. My "metre-bit" was wrong.

-- 
 \   "Whenever you read a good book, it's like the author is right |
  `\   there, in the room talking to you, which is why I don't like to |
_o__)read good books."  -- Jack Handey |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-13 Thread Steven Bethard
James Turk wrote:
> Hi,
> 
> I have a situation where I have some class members that should only be
> done once.  Essentially my problem looks like this:
> 
> class Base(object):
> dataset = None
> 
> def __init__(self, param):
> if type(self).dataset is None:
> # code to load dataset based on param, expensive
> 
> class ChildClass1(Base):
> def __init__(self):
> Base.__init__(self, data_params)
> 
> class AnotherChildClass(Base):
> def __init__(self):
> Base.__init__(self, other_data_params)
> 
> 
> This seems to work, initialization is only done at the first creation
> of either class.  I was just wondering if this is the 'pythonic' way
> to do this as my solution does feel a bit hackish.

What should happen with code like::

 ChildClass1('foo')
 ChildClass1('bar')

The 'param' is different, but 'dataset' should only get set the first time?

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mapping subintervals

2007-06-13 Thread Nis Jørgensen
Matteo skrev:

> OK - I'm going to assume your intervals are inclusive (i.e. 34-51
> contains both 34 and 51).
> 
> If your intervals are all really all non-overlapping, one thing you
> can try is to put all the endpoints in a single list, and sort it.
> Then, you can use the bisect module to search for intervals, which
> will give you a logarithmic time algorithm.
> 
> Here, I'm going to assume you just need the index of the containing
> interval. If you really need a name (i.e. 'a1' or 'a2'), you can use a
> list of names, and index into that.
> 
> I hope those assumptions are valid! if so, the following should work:

I have taken the liberty of simplifying your code, using the fact that
tuples are sorted lexicographically. Note that this requires all
intervals to be tuples and not lists (since list(a) < tuple(b) is always
True).

from bisect import bisect

def test_interval(ivl,intervals):

  # Find where ivl would lie in the list
  # i.e. the index of the first interval sorting as larger than ivl
  idx=bisect(intervals,ivl)
  # Left endpoints equal is a special case - a matching interval will be
  # to the right of the insertion point
  if idx < len(intervals) and intervals[idx][0] == ivl[0]:
if intervals[idx][1] >= ivl[1]:
return idx
else:
return None
  # Otherwise, we need to check to the left of the insertion point
  if idx > 0 and intervals[idx-1][1] >= ivl[1]:
return idx-1
  else:
return None

>>> intervals =[(10, 21), (34, 51), (77, 101)]
>>> print test_interval((34,35),intervals)
1
>>> print test_interval((34,53),intervals)
None
>>> print test_interval((77,53),intervals)
2
>>> print test_interval((77,83),intervals)
2
>>> print test_interval((77,102),intervals)
None
>>> print test_interval((77,101),intervals)
2

u"Nis J\xf8rgensen"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Build EXE on Mac OsX 10.4

2007-06-13 Thread Kevin Walzer
Paul McNett wrote:
> Tempo wrote:
>> Has anyone sucesfully built a *.exe file on a mac operating system
>> before from a *.py file? I have been trying to do this with
>> pyinstaller, but I keep getting errors and I don't know how to install
>> UPX properly. I tried putting the linux UPX folder in my python 2.4
>> directory, but that didn't work. I am just generally confused right
>> now. Ha. If anybody can lend me some insight I would really appreciate
>> it. Thank you for taking the time to read this post.
> 
> You need to build Mac Apps on Mac, Windows EXE's on Windows, and Linux 
> ELF's on Linux. You can't build a windows.exe from Mac, just as you 
> can't build a mac.app from Windows.
> 
Also, use py2app--that's the standard on the Mac. I don't believe 
pyinstaller works on OS X.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


one-time initialization of class members

2007-06-13 Thread James Turk
Hi,

I have a situation where I have some class members that should only be
done once.  Essentially my problem looks like this:

class Base(object):
dataset = None

def __init__(self, param):
if type(self).dataset is None:
# code to load dataset based on param, expensive

class ChildClass1(Base):
def __init__(self):
Base.__init__(self, data_params)

class AnotherChildClass(Base):
def __init__(self):
Base.__init__(self, other_data_params)


This seems to work, initialization is only done at the first creation
of either class.  I was just wondering if this is the 'pythonic' way
to do this as my solution does feel a bit hackish.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows XP timezone language issue

2007-06-13 Thread MRAB
On Jun 13, 7:31 am, Paul Sijben <[EMAIL PROTECTED]> wrote:
> I ran into an internationalization issue. I need a consistent idea about
> the timezone my application is running on. However when I run the following:
>  >>> import time
>  >>> time.tzname
>
> I get back ('West-Europa (standaardtijd)', 'West-Europa (zomertijd)')
> which is in dutch (the language of the host machine) and verbose.
> I wanted to get ('CEST','CET') or something international so I can work
> with itin the same way on all platforms.
>
> That is the right way to find out the timezone in a consistent way
> across platforms (windows/linux/mac) and languages?
>
Well, time.timezone will return the timezone as an integer.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-13 Thread infidel
# writing/reading CSV files, tuple-unpacking, cmp() built-in
import csv

writer = csv.writer(open('stocks.csv', 'wb'))
writer.writerows([
('GOOG', 'Google, Inc.', 505.24, 0.47, 0.09),
('YHOO', 'Yahoo! Inc.', 27.38, 0.33, 1.22),
('CNET', 'CNET Networks, Inc.', 8.62, -0.13, -1.49)
])

stocks = csv.reader(open('stocks.csv', 'rb'))
for ticker, name, price, change, pct in stocks:
print '%s is %s (%s%%)' % (
name,
{-1: 'down', 0: 'unchanged', 1: 'up'}[cmp(float(change),
0.0)],
pct
)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mapping subintervals

2007-06-13 Thread Matteo
On Jun 13, 2:32 pm, Lee Sander <[EMAIL PROTECTED]> wrote:
> hi,
> I have the following problem which is turning out to be non-trivial. I
> realize that this is not
> exactly a python problem but more of an algorithm problem -- but I
> post it here because
> I want to implement this in python.
>
> I want to write a code that given an interval (integer tuple:
> start,stop) will find which other
> interval it matches to. Here is an example,
> suppose I have the following NON-OVERLAPPING intervals
>
> 10 - 21   ==> 'a1'
> 34 - 51   ==> 'a2'
> 77 - 101 ==> 'a3'
> etc
>
> So, suppose I'm give an interval such as (42,50), I should go through
> the list and map it to "a2" etc
> if the region is a subset of an exiting interval.  If the query
> interval does not exist in the table or
> maps to two or more intervals (eg, (45, 80)) the program return a
> None.
...snip...
> Many thanks
> Lee

OK - I'm going to assume your intervals are inclusive (i.e. 34-51
contains both 34 and 51).

If your intervals are all really all non-overlapping, one thing you
can try is to put all the endpoints in a single list, and sort it.
Then, you can use the bisect module to search for intervals, which
will give you a logarithmic time algorithm.

Here, I'm going to assume you just need the index of the containing
interval. If you really need a name (i.e. 'a1' or 'a2'), you can use a
list of names, and index into that.

I hope those assumptions are valid! if so, the following should work:

from bisect import bisect

# assume initial intervals are sorted
intervals=[(10,21),(34,51), (77,101)]
# get a sorted list of endpoints
endpts=sorted([a for a,b in intervals]+[b for a,b in intervals])

def test_interval(ivl,endpts):
  # Find where the left endpoint of ivl would lie in the list
  # i.e. the index of the first element greater than ivl[0]
  l_idx=bisect(endpts,ivl[0])
  # If l_idx is even, then it lies between two intervals, and thus
  # is not contained in any interval. Otherwise it returns the index
  if l_idx % 2 == 0: return None
  # if l_idx is out of bounds (i.e. ==len(endpts)), then ivl is
  # not contained in any interval (too large)
  if l_idx==len(endpts): return None
  # Now, all we need to do is check that the right endpoint of ivl is
  # less than or equal to the corresponding endpoint in the list.
  # Happily, that endpoint is at index l_idx
  if ivl[1]<=endpts[l_idx]:
# So return the index of the interval:
return (l_idx-1)/2
  else:
return None


Then, from a shell:
>>> print tst.test_interval((0,13),endpts)
None
>>> print tst.test_interval((15,21),endpts)
0
>>> print tst.test_interval((35,40),endpts)
1
>>> print tst.test_interval((40,80),endpts)
None
>>> print tst.test_interval((109,200),endpts)
None

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a tuple quickly with list comprehension?

2007-06-13 Thread Terry Reedy

<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
| I can use list comprehension to create list quickly. So I expected that I
| can created tuple quickly with the same syntax. But I found that the
| same syntax will get a generator, not a tuple. Here is my example:
|
| In [147]: a = (i for i in range(10))
|
| In [148]: b = [i for i in range(10)]
|
| In [149]: type(a)
| Out[149]: 
|
| In [150]: type(b)
| Out[150]: 
|
| Is there a way to create a tuple like (1, 2, 3, 4, 5, 6, 7, 8, 9)
| quickly? I already I can use tuple() on a list which is created by list
| comprehension to get a desired tuple.

But why do you 'desire' a tuple (rather than a list)?  There are only a few 
situations where it makes a difference.  One is dictionary keys, but a 
sequnce of several, rather that a few, is not typical of keys.

tjr



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Build EXE on Mac OsX 10.4

2007-06-13 Thread -b
Okay. Great. Thanks for clarifying that for me.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Goto

2007-06-13 Thread HMS Surprise
Thanks folks!

jh

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Terry Reedy

"Steve Howell" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
|
| You would just change the language definition to say
| that once you enter f(), any call to f() from within
| f() behaves as if the recursively called f() still
| points to the originally bound version of f.

I am pretty sure such a context-dependent rule cannot be written as a 
context-free grammar rule.  In any case, the function object does not exist 
when code is being compiled to a code object.  So this requires 
implementation-dependent post-patching of the code object.  R. 
Hetchinger(sp?) posted a Cookbook recipe for doing this for CPython. 
Anyone wanting the speedup (with CPython) can use it.

tjr



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Build EXE on Mac OsX 10.4

2007-06-13 Thread Paul McNett
Tempo wrote:
> Has anyone sucesfully built a *.exe file on a mac operating system
> before from a *.py file? I have been trying to do this with
> pyinstaller, but I keep getting errors and I don't know how to install
> UPX properly. I tried putting the linux UPX folder in my python 2.4
> directory, but that didn't work. I am just generally confused right
> now. Ha. If anybody can lend me some insight I would really appreciate
> it. Thank you for taking the time to read this post.

You need to build Mac Apps on Mac, Windows EXE's on Windows, and Linux 
ELF's on Linux. You can't build a windows.exe from Mac, just as you 
can't build a mac.app from Windows.

-- 
pkm ~ http://paulmcnett.com
-- 
http://mail.python.org/mailman/listinfo/python-list


wx Listbox event

2007-06-13 Thread Marcpp
Hi, I need to charge a list when starts the program. I've tried a few
events like:


self.llistatids = wx.ListBox(self, -1, choices=['a'],
style=wx.LB_SINGLE|wx.LB_ALWAYS_SB)
self.llistatids.SetBackgroundColour(wx.Colour(255, 255, 220))
self.llistatids.Bind(wx.EVT_LISTBOX, self.carrega_llistatids)

But I cannot do it, any idea?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Goto

2007-06-13 Thread Carsten Haese
On Wed, 2007-06-13 at 12:20 -0700, HMS Surprise wrote:
> How does one effect a goto in python?

One doesn't.

>  I only want to use it for debug.
> I dasn't slap an "if" clause around the portion to dummy out, the
> indentation police will nab me.

If you want to disable a code block without indenting it into an "if
False:" block, use triple quotes, either ''' or """, to turn it into a
long string.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-13 Thread Steven Bethard
Rob Wolfe wrote:
> Steven Bethard <[EMAIL PROTECTED]> writes:
> 
>>> I vote for example with ElementTree (without xpath)
>>> with a mention of using ElementSoup for invalid HTML.
>> Sounds good to me.  Maybe something like::
>>
>> import xml.etree.ElementTree as etree
>> dinner_recipe = '''
>> 
>> 24slicesbaguette
>> 2+tbspolive_oil
>   ^
> Is that a typo here?

Just trying to make Thunderbird line-wrap correctly. ;-) It's better 
with a space instead of an underscore.

>> 1cuptomatoes
>> 1-2tbspgarlic
>> 1/2cupParmesan
>> 1jarpesto
>> '''
>> pantry = set(['olive oil', 'pesto'])
>> tree = etree.fromstring(dinner_recipe)
>> for item_elem in tree.getiterator('item'):
>> if item_elem.text not in pantry:
>> print item_elem.text
> 
> That's nice example. :)
> 
>> Though I wouldn't know where to put the ElementSoup link in this one...
> 
> I had a regular HTML in mind, something like:
> 
> 
> # HTML page
> dinner_recipe = '''
> Recipe
> 
> amtunititem
> 24slicesbaguette
> 2+tbspolive_oil
> 1cuptomatoes
> 1-2tbspgarlic
> 1/2cupParmesan
> 1jarpesto
> 
> '''
> 
> # program
> import xml.etree.ElementTree as etree
> tree = etree.fromstring(dinner_recipe)
> 
> #import ElementSoup as etree # for invalid HTML
> #from cStringIO import StringIO  # use this
> #tree = etree.parse(StringIO(dinner_recipe)) # wrapper for BeautifulSoup
> 
> pantry = set(['olive oil', 'pesto'])
> 
> for ingredient in tree.getiterator('tr'):
> amt, unit, item = ingredient.getchildren()
> if item.tag == "td" and item.text not in pantry:
> print "%s: %s %s" % (item.text, amt.text, unit.text)
> 
> 
> But if that's too complicated I will not insist on this. :)
> Your example is good enough.

Sure, that looks fine to me. =)

Steve
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: In C extension .pyd, sizeof INT64 = 4?

2007-06-13 Thread Gabriel Genellina
On 12 jun, 17:06, "Martin v. Lo"wis" <[EMAIL PROTECTED]> wrote:
>
> What *is*INT64? It's not a builtin type of standard C, it isn't
> defined by Microsoft C, and it isn't predefined by Python.
>
> So it must be something that you have defined, and apparently
> incorrectly. How did you define it?

It is defined in basetsd.h, included by winnt.h; the OP should not
redefine it, and that also explains why I could compile my test
program without any problem.

--
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


mapping subintervals

2007-06-13 Thread Lee Sander
hi,
I have the following problem which is turning out to be non-trivial. I
realize that this is not
exactly a python problem but more of an algorithm problem -- but I
post it here because
I want to implement this in python.

I want to write a code that given an interval (integer tuple:
start,stop) will find which other
interval it matches to. Here is an example,
suppose I have the following NON-OVERLAPPING intervals

10 - 21   ==> 'a1'
34 - 51   ==> 'a2'
77 - 101 ==> 'a3'
etc

So, suppose I'm give an interval such as (42,50), I should go through
the list and map it to "a2" etc
if the region is a subset of an exiting interval.  If the query
interval does not exist in the table or
maps to two or more intervals (eg, (45, 80)) the program return a
None.

One naive way to solve this problem is to create an array such as
follows:
[None, None, None, , None, a1, a1, a1, ..., a1, None, None ...,
None, a2, ... etc] at indicies
  123  9   10  11  12   21
22  23  33 34, ...

now with this in place I can easily solve the problem. However, this
is not a feasable solution
because the initial list has intervals whose range can go to billions!
So I need a smarter
idea. So what I can do is sort the initial list and then go through
each element from the start
and test if the  a > X[i][0] and b < X[i][1]
where (a,b) is my query start and stop
and X is a n x 2 array with the known intervals.
I don't like this solution because it can be really really slow as for
each query I'm doing a
linear search and on average I'll be searching almost half the list
before I find the right
interval.
Is there a smarter way to do this?
I've my problem is not clear please let me know and I'll try to
explain the unclear parts again.
Many thanks
Lee

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-13 Thread Rob Wolfe
Steven Bethard <[EMAIL PROTECTED]> writes:

>> I vote for example with ElementTree (without xpath)
>> with a mention of using ElementSoup for invalid HTML.
>
> Sounds good to me.  Maybe something like::
>
> import xml.etree.ElementTree as etree
> dinner_recipe = '''
> 
> 24slicesbaguette
> 2+tbspolive_oil
  ^

Is that a typo here?

> 1cuptomatoes
> 1-2tbspgarlic
> 1/2cupParmesan
> 1jarpesto
> '''
> pantry = set(['olive oil', 'pesto'])
> tree = etree.fromstring(dinner_recipe)
> for item_elem in tree.getiterator('item'):
> if item_elem.text not in pantry:
> print item_elem.text

That's nice example. :)

> Though I wouldn't know where to put the ElementSoup link in this one...

I had a regular HTML in mind, something like:


# HTML page
dinner_recipe = '''
Recipe

amtunititem
24slicesbaguette
2+tbspolive_oil
1cuptomatoes
1-2tbspgarlic
1/2cupParmesan
1jarpesto

'''

# program
import xml.etree.ElementTree as etree
tree = etree.fromstring(dinner_recipe)

#import ElementSoup as etree # for invalid HTML
#from cStringIO import StringIO  # use this
#tree = etree.parse(StringIO(dinner_recipe)) # wrapper for BeautifulSoup

pantry = set(['olive oil', 'pesto'])

for ingredient in tree.getiterator('tr'):
amt, unit, item = ingredient.getchildren()
if item.tag == "td" and item.text not in pantry:
print "%s: %s %s" % (item.text, amt.text, unit.text)


But if that's too complicated I will not insist on this. :)
Your example is good enough.

-- 
Regards,
Rob
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Goto

2007-06-13 Thread Daniel Nogradi
> How does one effect a goto in python? I only want to use it for debug.
> I dasn't slap an "if" clause around the portion to dummy out, the
> indentation police will nab me.


http://entrian.com/goto/
-- 
http://mail.python.org/mailman/listinfo/python-list


Build EXE on Mac OsX 10.4

2007-06-13 Thread Tempo
Has anyone sucesfully built a *.exe file on a mac operating system
before from a *.py file? I have been trying to do this with
pyinstaller, but I keep getting errors and I don't know how to install
UPX properly. I tried putting the linux UPX folder in my python 2.4
directory, but that didn't work. I am just generally confused right
now. Ha. If anybody can lend me some insight I would really appreciate
it. Thank you for taking the time to read this post.

-b

-- 
http://mail.python.org/mailman/listinfo/python-list


Goto

2007-06-13 Thread HMS Surprise

How does one effect a goto in python? I only want to use it for debug.
I dasn't slap an "if" clause around the portion to dummy out, the
indentation police will nab me.

Thanx,

jh

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Neil Cerutti
On 2007-06-13, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> On 2007-06-13, Anders J. Munch <[EMAIL PROTECTED]> wrote:
>> General tail-call optimisation is of course completely
>> out-of-bounds for Python, because it ruins tracebacks.  Unlike
>> tail recursion, which could use recursion counters.
>
> Is it really ruined? To use a similar example:

I found some interesting notes by Alex Martelli pertaining to
tail-call optimisation, and my assumption that tail-call
optimization is easier to implement than tail-recursive
optimization may have been naive. ;)

http://groups.google.com/group/comp.lang.python/msg/1a7103c1bd70?hl=en&;

Moreover, there are (or were) technical reasons that you can't do
tail-call optimization in Python, which can't even recognize
tail-calls at compile time. According to Tim Peters:

http://groups.google.com/group/comp.lang.python/msg/ea1de1e35aefb828?hl=en&;

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Carsten Haese
On Wed, 2007-06-13 at 18:22 +, Neil Cerutti wrote:
> On 2007-06-13, Anders J. Munch <[EMAIL PROTECTED]> wrote:
> > General tail-call optimisation is of course completely
> > out-of-bounds for Python, because it ruins tracebacks.  Unlike
> > tail recursion, which could use recursion counters.
> 
> Is it really ruined? To use a similar example:
> 
> def foo(x):
> bar(x+1)
> 
> def bar(x):
> if x > 10:
> raise ValueError
> else:
> foo(x+2)
> 
> Today, when I call foo(4), I get something like:
> 
> C:\WINNT\system32\cmd.exe /c python temp.py
> Traceback (most recent call last):
>   File "temp.py", line 529, in 
> foo(4)
>   File "temp.py", line 521, in foo
> bar(x+1)
>   File "temp.py", line 527, in bar
> foo(x+2)
>   File "temp.py", line 521, in foo
> bar(x+1)
>   File "temp.py", line 527, in bar
> foo(x+2)
>   File "temp.py", line 521, in foo
> bar(x+1)
>   File "temp.py", line 525, in bar
> raise ValueError
> ValueError
> shell returned 1
> 
> With tail-call optimization you'd get something like:
> 
> C:\WINNT\system32\cmd.exe /c python temp.py
> Traceback (most recent call last):
>   File "temp.py", line 529, in 
> foo(4)
>   File "temp.py", line 525, in bar
> raise ValueError
> ValueError
> shell returned 1
> 
> What makes the latter harder to work with?

The fact that you don't see how many call levels down your algorithm got
before throwing an exception. This may be an important clue in debugging
a recursive algorithm.

-- 
Carsten Haese
http://informixdb.sourceforge.net


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Neil Cerutti
On 2007-06-13, Anders J. Munch <[EMAIL PROTECTED]> wrote:
> General tail-call optimisation is of course completely
> out-of-bounds for Python, because it ruins tracebacks.  Unlike
> tail recursion, which could use recursion counters.

Is it really ruined? To use a similar example:

def foo(x):
bar(x+1)

def bar(x):
if x > 10:
raise ValueError
else:
foo(x+2)

Today, when I call foo(4), I get something like:

C:\WINNT\system32\cmd.exe /c python temp.py
Traceback (most recent call last):
  File "temp.py", line 529, in 
foo(4)
  File "temp.py", line 521, in foo
bar(x+1)
  File "temp.py", line 527, in bar
foo(x+2)
  File "temp.py", line 521, in foo
bar(x+1)
  File "temp.py", line 527, in bar
foo(x+2)
  File "temp.py", line 521, in foo
bar(x+1)
  File "temp.py", line 525, in bar
raise ValueError
ValueError
shell returned 1

With tail-call optimization you'd get something like:

C:\WINNT\system32\cmd.exe /c python temp.py
Traceback (most recent call last):
  File "temp.py", line 529, in 
foo(4)
  File "temp.py", line 525, in bar
raise ValueError
ValueError
shell returned 1

What makes the latter harder to work with?

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging module and threading

2007-06-13 Thread Vinay Sajip
On Jun 13, 1:28 am, "James T. Dennis" <[EMAIL PROTECTED]> wrote:
>  This sounds like a job for the Queue class/module to me.
>  Could you create a Queue such that all your worker threads
>  are producers to it and you have one dedicated thread as a
>  consumer that relays log entries from the Queue into your loggers?

Or, use a SocketHandler to serialize the events over a socket, and de-
mux them on the receiving end. The docs have an example:

http://docs.python.org/lib/network-logging.html

Regards,

Vinay Sajip

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Help!!!

2007-06-13 Thread Christof Winter
Elfine Peterson Tjio wrote:
> I'm trying to make a program that reads Fasta file and print it out. I used
> the SeqIO module and the results is:
> 
> 'ATGGTCATSingleAlphabet()'
> 
> For this purpose, should I use SeqIO or Fasta?
> 
> for example:
> 
> from Bio import SeqIO
> 
> or
> 
> from Bio import Fasta
> 
> I want it to print every letter. Can anyone point me to the right direction.
> The newest biopython tutorial or book recommendation will be appreciated,
> too.

Dear Elfine:

The correct place for such a question is the BioPython discussion list at
[EMAIL PROTECTED]

You can subscribe to it here:
http://lists.open-bio.org/mailman/listinfo/biopython/

The newest BioPython tutorial (last updated 16 March 2007) can be found at
http://biopython.org/DIST/docs/tutorial/Tutorial.pdf

SeqIO and Fasta should both work fine. You could also try

 >>> help(SeqIO)
 >>> help(Fasta)

after your import for some further information.

Cheers,
Christof
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem on waiting exit thread and write on file

2007-06-13 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Flyzone wrote:

> I need to run in thread a command, one thread for one parameter.
> So i made a for loop, creating 5 threads and waiting their stop with:
> Main {
>open file BW2 for write
>. (creating list of thread with a counter)
>Stopped=False
>while (Stopped == False):
>   if not thread5.isAlive():
>   if not thread4.isAlive():
>   if not thread3.isAlive():
>   if not thread2.isAlive():
>   if not thread1.isAlive():
> Stopped=True
>   if (Stopped == False):
>  time.sleep(0.3)

If you create a list with threads where do the names `thread1` to
`thread5` come from?  This snippet expects the threads in a list or
another iterable:

for thread in threads:
thread.join()

Much shorter, isn't it!?  :-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Focus to be removed

2007-06-13 Thread Gabriel Genellina
En Wed, 13 Jun 2007 07:13:18 -0300, <[EMAIL PROTECTED]> escribió:

> I'm very pleased to have your help about to solve a technical issue
> related to HTML Editlet Editor. In this I' m facing the problem that

You should ask the author - I don't think this product has anything to do  
with Python.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows build of PostgreSQL library for 2.5

2007-06-13 Thread George Sakkis
On Jun 13, 12:57 pm, Ben Sizer <[EMAIL PROTECTED]> wrote:
> On 30 May, 16:20, Ben Sizer <[EMAIL PROTECTED]> wrote:
>
> > On 30 May, 15:42, Frank Millman <[EMAIL PROTECTED]> wrote:
>
> > > On May 30, 4:15 pm,BenSizer<[EMAIL PROTECTED]> wrote:
>
> > > > I've been looking for a Windows version of a library to interface to
> > > > PostgreSQL, but can only find ones compiled under Python version 2.4.
> > > > Is there a 2.5 build out there?
>
> > > Is this what you are looking for?
>
> > >http://stickpeople.com/projects/python/win-psycopg/
>
> > It may well be, thanks.
>
> On second thoughts, is there one anywhere without an extra multi-
> megabyte dependency? This seems to rely on the eGenix 'mx' library.
>
> --
> Ben Sizer

IIRC version 2 (psycopg2) doesn't depend on mx:
http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.0.5.1.win32-py2.5-pg8.2.0-release.exe

George

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Optimizing constants in loops

2007-06-13 Thread Michael Hoffman
Thomas Heller wrote:

> Just use the builtin __debug__ variable for that purpose.
> __debug__ is 'True' if Python is run normally, and 'False'
> if run with the '-O' or '-OO' command line flag.
> The optimizer works in the way you describe above (which
> it will not if you use a custom variable).

Thanks, I didn't know that __debug__ was optimized like this. But that 
was really just a specific example of the general case.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Help!!!

2007-06-13 Thread Nathan Harmston
Hi, you could try this:

def parse(self, ifile):
id=""
seq=""
for line in open(ifile, 'r'):
if '>'==line[0]:
if id!="" and len(seq)>0:
yield id,seq
seq = ""
id=line[1:].strip("\n")
elif id!="":
for word in line.split():
seq += word
if id!="" and len(seq)>0:
yield id,seq

for id, seq in parse("some.fa"):
print "%s \n %s" %(id, seq)

Its adapted from the fasta parser in PyGr.

>From what I understand biopython isnt very active and I think theres a
re-factor of it going on at the moment in the form of corebio.

Hope this helps;

Thanks

Nathan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bytes/File Size Format Function

2007-06-13 Thread Avell Diroll
Ben Finney wrote:
> The symbol for "bit" is 'b'. The symbol for "byte" is 'B'. 'kb' is
> 'kilobit', i.e. 1000 bits. 'mb' is a "metre-bit", a combination of two
> units. And so on. The SI units have definitions that are only muddied
> by misusing them this way.

I have to disagree: 'mb' should stand for "milli-bit"  :)
which could be considered as the probability of a bit
... this might be useful for quantum computing.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows build of PostgreSQL library for 2.5

2007-06-13 Thread Ben Sizer
On 30 May, 16:20, Ben Sizer <[EMAIL PROTECTED]> wrote:
> On 30 May, 15:42, Frank Millman <[EMAIL PROTECTED]> wrote:
>
> > On May 30, 4:15 pm,BenSizer<[EMAIL PROTECTED]> wrote:
>
> > > I've been looking for a Windows version of a library to interface to
> > > PostgreSQL, but can only find ones compiled under Python version 2.4.
> > > Is there a 2.5 build out there?
>
> > Is this what you are looking for?
>
> >http://stickpeople.com/projects/python/win-psycopg/
>
> It may well be, thanks.

On second thoughts, is there one anywhere without an extra multi-
megabyte dependency? This seems to rely on the eGenix 'mx' library.

--
Ben Sizer


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Anders J. Munch
Alexander Schmolck wrote:
> "Anders J. Munch" <[EMAIL PROTECTED]> writes:
> 
>> Like Steven said, tail-call optimisation is not necessary as you can always
>> hand-optimise it yourself.
> 
> Care to demonstrate on some code written in CPS (a compiler or parser, say)?

I meant tail recursion, not tail-call, sorry, that was just my fingers trying 
to 
save typing.

- Anders
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Anders J. Munch
Neil Cerutti wrote:
> On 2007-06-12, Anders J. Munch <[EMAIL PROTECTED]> wrote:
>> Converting tail-recursion to iteration is trivial, and
>> perfectly reasonable for a human to do by hand.  
> 
> For simple recursive tail calls, yeah, it can be. Translating a
> tail-recursive Factorial function into a while loop is easy. But
> tail-call optimization technically works for any tail-call,
> including mutual recursion, and non-recursive tail-calls. You
> can't reasonably hand-optimize away the stack frame for all
> tail-calls.

I may have misunderstood, I thought we were talking about tail recursion only. 
The general tail-call optimisation, where all leaf calls become jumps and the 
called function usurps the current stack frame, is a different ballgame 
entirely.  There's no pure-Python transformation for that, but that still 
doesn't mean you need CPS.

General tail-call optimisation is of course completely out-of-bounds for 
Python, 
because it ruins tracebacks.  Unlike tail recursion, which could use recursion 
counters.

- Anders

-- 
http://mail.python.org/mailman/listinfo/python-list


Cretins.

2007-06-13 Thread Dr. Pastor
Please do not do business with
those cretins who without authorization
attaching the following text to my postings:

== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet 
News==
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ 
Newsgroups
= East and West-Coast Server Farms - Total Privacy via Encryption =

== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet 
News==
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ 
Newsgroups
= East and West-Coast Server Farms - Total Privacy via Encryption =
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bytes/File Size Format Function

2007-06-13 Thread samuraisam
Haha, you guys. Use it however you want. But trust me, if you put MiB
and GiB instead of the more-common mb and gb [MB and GB] in your
applications, your users will probably have a harder time
understanding what you mean.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MS Word parser

2007-06-13 Thread [EMAIL PROTECTED]
On Jun 13, 1:28 am, Tim Golden <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Hi all,
> > I'm currently using antiword to extract content from MS Word files.
> > Is there another way to do this without relying on any command prompt
> > application?
>
> Well you haven't given your environment, but is there
> anything to stop you from controlling Word itself via
> COM? I'm no Word expert, but looking around, this
> seems to work:
>
> 
> import win32com.client
> word = win32com.client.Dispatch ("Word.Application")
> doc = word.Documents.Open ("c:/temp/temp.doc")
> text = doc.Range ().Text
>
> open ("c:/temp/temp.txt", "w").write (text.encode ("UTF-8"))
> 
>
> TJG

Tim,
I'm on Linux (RedHat) so using Word is not an option for me.  Any
other suggestions?

-- 
http://mail.python.org/mailman/listinfo/python-list


problem on waiting exit thread and write on file

2007-06-13 Thread Flyzone
I have a list of parameters.
I need to run in thread a command, one thread for one parameter.
So i made a for loop, creating 5 threads and waiting their stop with:
for parameter in parameters

Thread{

write on BW2

}

Main {
   open file BW2 for write
   . (creating list of thread with a counter)
   Stopped=False
   while (Stopped == False):
  if not thread5.isAlive():
  if not thread4.isAlive():
  if not thread3.isAlive():
  if not thread2.isAlive():
  if not thread1.isAlive():
Stopped=True
  if (Stopped == False):
 time.sleep(0.3)
   ..
   close file BW2
}

Somethimes i get however the error that i can't write on a file
already closed
There is a way more easy to wait that all children exit and to run a
queue of threads?
I tried also this:
   a=0
   while (a == 0):
   try:
   os.waitpid(-2, 0)
   except OSError, exc:
   # all CHILD finished
   a=1
but the same problem persist. The only way to don't have error is to
had a time.sleep(4) before closing the file from the main program.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Optimizing constants in loops

2007-06-13 Thread Thomas Heller
Michael Hoffman schrieb:
> The peephole optimizer now takes things like
> 
> if 0:
> do_stuff()
> 
> and optimizes them away, and optimizes away the conditional in "if 1:".
> 
> What if I had a function like this?
> 
> def func(debug=False):
>  for index in xrange(100):
>  if debug:
>  print index
>  do_stuff(index)
> 
> Could the "if debug" be optimized away on function invocation if debug 
> is immutable and never reassigned in the function? When performance 
> really matters in some inner loop, I usually move the conditional 
> outside like this:
> 
> def func(debug=False):
>  if debug:
>  for index in xrange(100):
>  print index
>  do_stuff(index)
>  else:
>  for index in xrange(100):
>  do_stuff(index)
> 
> It would be nice if this sort of thing could be done automatically, 
> either by the interpreter or a function decorator.

Just use the builtin __debug__ variable for that purpose.
__debug__ is 'True' if Python is run normally, and 'False'
if run with the '-O' or '-OO' command line flag.
The optimizer works in the way you describe above (which
it will not if you use a custom variable).

Thomas

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-13 Thread Steven Bethard
Rob Wolfe wrote:
> Steve Howell wrote:
> 
>> I suggested earlier that maybe we post multiple
>> solutions.  That makes me a little nervous, to the
>> extent that it shows that the Python community has a
>> hard time coming to consensus on tools sometimes.
> 
> We agree that BeautifulSoup is the best for parsing HTML. :)
> 
>> This is not a completely unfair knock on Python,
>> although I think the reason multiple solutions tend to
>> emerge for this type of thing is precisely due to the
>> simplicity and power of the language itself.
>>
>> So I don't know.  What about trying to agree on an XML
>> parsing example instead?
>>
>> Thoughts?
> 
> I vote for example with ElementTree (without xpath)
> with a mention of using ElementSoup for invalid HTML.

Sounds good to me.  Maybe something like::

import xml.etree.ElementTree as etree
dinner_recipe = '''

24slicesbaguette
2+tbspolive_oil
1cuptomatoes
1-2tbspgarlic
1/2cupParmesan
1jarpesto
'''
pantry = set(['olive oil', 'pesto'])
tree = etree.fromstring(dinner_recipe)
for item_elem in tree.getiterator('item'):
 if item_elem.text not in pantry:
 print item_elem.text

Though I wouldn't know where to put the ElementSoup link in this one...

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


Python Job Opportunity

2007-06-13 Thread Brettmf
My company : EWT, LLC

EWT is a proprietary securities trading company and is a member firm
of major stock and futures exchanges. Founded in 2002, EWT was formed
to capitalize on the shift of the securities industry towards
electronic platforms in the United States and abroad.

Location: Beverly Hills, CA - USA

Position: We are seeking developers to work on proprietary trading
strategies that will run on our high-performance, electronic trading
platform. Responsibilities include improving the scalability,
performance and efficiency of existing strategies. This position
involves strong collaboration with traders to research and prototype
new strategies.

·   Candidates should have an interest in trading and financial markets
·   Candidates should enjoy working in a fast-paced, dynamic
environment
·   Analytic ability and raw talent are required
·   Strong mathematical aptitude; background in math, physics, or
statistics is a plus
·   Proficiency with Python is required
·   Experience in distributed and/or highly concurrent systems is a plus

As a highly successful firm in the finance industry, we are able to
offer extremely competitive compensation, well above market value.  We
are always looking for people with extraordinary skills, experience,
creativity and drive to join our rapidly expanding company. If this
describes you, then please apply.

Please email resumes to [EMAIL PROTECTED] or, preferably, apply
through our website at www.ewtcareers.com (follow the Careers tab).

-- 
http://mail.python.org/mailman/listinfo/python-list


Optimizing constants in loops

2007-06-13 Thread Michael Hoffman
The peephole optimizer now takes things like

if 0:
do_stuff()

and optimizes them away, and optimizes away the conditional in "if 1:".

What if I had a function like this?

def func(debug=False):
 for index in xrange(100):
 if debug:
 print index
 do_stuff(index)

Could the "if debug" be optimized away on function invocation if debug 
is immutable and never reassigned in the function? When performance 
really matters in some inner loop, I usually move the conditional 
outside like this:

def func(debug=False):
 if debug:
 for index in xrange(100):
 print index
 do_stuff(index)
 else:
 for index in xrange(100):
 do_stuff(index)

It would be nice if this sort of thing could be done automatically, 
either by the interpreter or a function decorator.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there any way to catch expections when call python method in C++

2007-06-13 Thread Robert Bauck Hamar
Allen wrote:

> I use try catch, but cannot catch the execeptions of execution python
> method.
> 
> PYCALL_API void PyCall(const char * pszModule, const char * pszFunc,
> void * pArg)
> {
> if (pszModule == NULL || pszFunc == NULL)
> {
> return;
> }
> 
> Py_Initialize();
> 
> PyObject * pModule = NULL;
> PyObject * pFunc   = NULL;
> 
> try {
> 
> pModule = PyImport_ImportModule(pszModule);
> pFunc   = PyObject_GetAttrString(pModule, pszFunc);
> 
> PyEval_CallObject(pFunc, (PyObject*)pArg);
> } catch (...) {
>fprintf(stderr, "Error: call python method failed");
> }
> 
> Py_Finalize();
> }
> 
> Can I catch it from C++?

No. CPython is written in C, not C++, and C has no concept of exceptions.
Exceptions in Python is usually indicated by return value in the
interpreter, and has no mapping to the C++ exception model. You should
never let C++ exceptions propagate into the python functions either.
PyImport_ImportModule will return NULL if an exception occured, and so will
also PyObject_GetAttrString and PyEval_CallObject do.

-- 
rbh
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Postpone creation of attributes until needed

2007-06-13 Thread Steven D'Aprano
On Wed, 13 Jun 2007 02:15:11 -0700, Frank Millman wrote:

> Thanks very much for all your attempts to help me, Steven. You have
> succeeded in getting me to think properly about my problem and come up
> with a much cleaner solution. I really appreciate it.

Glad to be of help.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: httplib / connection

2007-06-13 Thread rhXX
> The
> httplib.HTTP class that you were using is very old and deprecated for
> several years now.
>
> --
> Gabriel Genellina

:-(  oh , tks!

i took it from

www.ug.it.usyd.edu.au/~comp5315/lec-09.html

which class must i use?

tks in advance

-- 
http://mail.python.org/mailman/listinfo/python-list


passing arguments to tcpserver classes

2007-06-13 Thread Eric Spaulding
Is there an easy way to pass arguments to a handler class that is used 
by the standard TCPServer?

normally --> srvr =SocketServer.TCPServer(('',port_num), TCPHandlerClass)

I'd like to be able to: srvr =SocketServer.TCPServer(('',port_num), 
TCPHandlerClass, (arg1,arg2))

And have arg1, arg2 available via TCPHandlerClass.__init__ or some other 
way.

Where TCPHandlerClass:

class TCPHandlerClass(SocketServer.StreamRequestHandler):
def handle(self):
   #handle stream events here#


Thanks for any advice.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert binary string to something meaningful??

2007-06-13 Thread Grant Edwards
On 2007-06-13, supercooper <[EMAIL PROTECTED]> wrote:
> I have this string that is being returned from a query on a DBISAM
> database. The field must be some sort of blob, and when I connect to
> the database thru ODBC in MS Access, the field type comes thru as OLE
> Object, and Access cannot read the field (it usually crashes Access).
> I can sorta pick out the data that I need (7.0, 28, 5TH PRINCIPAL MRD,
> 10.0 - and these occur in roughly the same place in every record
> returned from the db), but I am wondering if there is a way to convert
> this. I looked at the struct module, but I really dont know the format
> of the data for sure. For starters, could someone tell me what '\x00'
> more than likely is?

It's a byte where all 8 bits are zeros.  Your data probably has
fixed field widths, and unused bytes are just zero-filled.

-- 
Grant Edwards   grante Yow! How's the wife?
  at   Is she at home enjoying
   visi.comcapitalism?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cgi.FieldStorage() not working on Windows

2007-06-13 Thread arorap
OOps .. yes I mean mod_python. I've been using PHP way too long :P ..
hence the typo


On Jun 13, 4:01 am, Graham Dumpleton <[EMAIL PROTECTED]>
wrote:
> On Jun 13, 12:58 pm,arorap<[EMAIL PROTECTED]> wrote:
>
> > Thanks for your reply.
>
> > The reason I want to run it as CGI (even though mod_php is available
> > on my local computer
>
> Why do you keep mentioning mod_php, surely you mean mod_python.
>
> > is that the target machine to which I will
> > finally be uploading my scripts runs CGI.
>
> > cgihandler should work just like CGI.
>
> I wouldn't rely on it being exactly the same. The way it works uses a
> number of kludges. Also, the mod_python.cgihandler code in mod_python
> doesn't really get much attention from mod_python developers anymore
> and not sure if it was even specifically retested when mod_python 3.3
> was released.
>
> > Any clue why the
> > cgi.FieldStorage()might not be working ?
>
> Have no idea why it doesn't work as works as written on MacOS X even
> when mod_python.cgihandler is used.
>
> You'll have to get someone else who has Windows to try it. You might
> be better off going to the mod_python mailing list to get help, or
> just use plain old CGI instead since using mod_python isn't really
> going to gain you much anyway.
>
> Graham
>
>
>
> > On Jun 12, 7:59 pm, Graham Dumpleton <[EMAIL PROTECTED]>
> > wrote:
>
> > > On Jun 13, 1:17 am,arorap<[EMAIL PROTECTED]> wrote:
>
> > > > I've mod_php installed with Apache 2.2. In one of my folders, I'm
> > > > using the cgihandler as the PythonHandler as my target host runs
> > > > python only as CGI. Here cgi.FieldStorage() doesn't seem to work. I
> > > > can see the form data in sys.stdin but cgi.FieldStorage() returns an
> > > > empty dictionary. Here's the code for the test script I am posting to
> > > > -
>
> > > > --
> > > > #!/usr/bin/python
>
> > > > import os
> > > > import cgi
> > > > import sys
>
> > > > print "Content Type: text/plain\n\n"
> > > > print "Hello CGI World !\n"
>
> > > > for key in os.environ:
> > > >   print key + "= " + os.environ[key]
>
> > > > print cgi.FieldStorage()
>
> > > > print sys.stdin.read()
> > > > --
>
> > > > And here's the output I see ..
>
> > > > --
> > > > Hello CGI World !
>
> > > > HTTP_REFERER=http://learnpython/form.htm
> > > > SERVER_SOFTWARE= Apache/2.2.4 (Win32)mod_python/3.3.1 Python/2.5.1
> > > > SCRIPT_NAME= /mptest.py
> > > > SERVER_SIGNATURE=
> > > > REQUEST_METHOD= POST
> > > > SERVER_PROTOCOL= HTTP/1.1
> > > > QUERY_STRING= abc=ayz
> > > > PATH= C:\Program Files\Internet Explorer;;C:\WINDOWS\system32;C:
> > > > \WINDOWS;C:\WINDOWS\System32\Wbem;q:\bin;m:\cm\clearcase\bin;M:\PERL\NT
> > > > \EXEC\BIN;m:\cm\clearcase\bin\nt;M:\Perl\NT\EXEC\BIN;m:\perl\nt\exec
> > > > \bin;m:\cm\clearcase\utils;q:\bin;m:\opus;m:\tvcs;C:\highc331\bin;C:
> > > > \Program Files\Rational\ClearCase\bin;C:\Program Files\Rational\common
> > > > CONTENT_LENGTH= 86
> > > > HTTP_USER_AGENT= Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
> > > > SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
> > > > HTTP_CONNECTION= Keep-Alive
> > > > SERVER_NAME= learnpython
> > > > REMOTE_ADDR= 127.0.0.1
> > > > PATHEXT= .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
> > > > SERVER_PORT= 80
> > > > SERVER_ADDR= 127.0.0.1
> > > > DOCUMENT_ROOT= D:/Projects/LearnPython/www
> > > > COMSPEC= C:\WINDOWS\system32\cmd.exe
> > > > SCRIPT_FILENAME= D:/Projects/LearnPython/www/mptest.py
> > > > SERVER_ADMIN= [EMAIL PROTECTED]
> > > > HTTP_HOST= learnpython
> > > > SystemRoot= C:\WINDOWS
> > > > HTTP_CACHE_CONTROL= no-cache
> > > > REQUEST_URI= /mptest.py?abc=ayz
> > > > HTTP_ACCEPT= */*
> > > > WINDIR= C:\WINDOWS
> > > > GATEWAY_INTERFACE= Python-CGI/1.1
> > > > REMOTE_PORT= 1081
> > > > HTTP_ACCEPT_LANGUAGE= en-us
> > > > CONTENT_TYPE= application/x-www-form-urlencoded
> > > > HTTP_ACCEPT_ENCODING= gzip, deflate
>
> > > > FieldStorage(None, None, [])
>
> > > > firstName=puneet&address=hawaii
> > > > --
>
> > > > I am posting to this script using a form with two text fields named
> > > > firstName and address.
>
> > > > any clue where am I going wrong ?
>
> > > You don't need mod_python/cgihandler to run CGI scripts. Rather than
> > > bring mod_python into the picture and confuse things, set up Apache to
> > > run your script as a traditional CGI script instead.
>
> > > BTW, the fact that mod_python is loaded means that CGI scripts aren't
> > > the only way of using Python available to you as you seem to think.
> > > So, suggest you do some research as to what the differences are
> > > between CGI and mod_python.- Hide quoted text -
>
> - Show quoted text -


-- 
http://mail.python.org/mailman/listinfo/python-list


Convert binary string to something meaningful??

2007-06-13 Thread supercooper
I have this string that is being returned from a query on a DBISAM
database. The field must be some sort of blob, and when I connect to
the database thru ODBC in MS Access, the field type comes thru as OLE
Object, and Access cannot read the field (it usually crashes Access).
I can sorta pick out the data that I need (7.0, 28, 5TH PRINCIPAL MRD,
10.0 - and these occur in roughly the same place in every record
returned from the db), but I am wondering if there is a way to convert
this. I looked at the struct module, but I really dont know the format
of the data for sure. For starters, could someone tell me what '\x00'
more than likely is? Any hints on modules to look at or esp code
snippets would be greatly appreciated. Thanks.

\x9c
\x01\x00\x007.0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
28.0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
10.0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00WN
\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x10\xa3@
[EMAIL PROTECTED] NW SE
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00CONGRESS
QTR\x00\x00\x00\x005\x00\x005TH PRINCIPAL MRD
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

-- 
http://mail.python.org/mailman/listinfo/python-list


shape recognition

2007-06-13 Thread madprocessor
hi there,

does anybody know about shape / gesture recognition librarys for
python?
like cali: http://immi.inesc-id.pt/cali/

thx

joerg

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to format a string from an array?

2007-06-13 Thread Gerard Flanagan
On Jun 13, 11:11 am, Allen <[EMAIL PROTECTED]> wrote:
> a = range(256)
> I want to output the formated string to be:
> 00 01 02 03 04 05 06 07   08 09 0a 0b 0c 0d 0e 0f
> 10 11 12 13 14 15 16 17   18 19 1a 1b 1c 1d 1e 1f
> 
> f0 f1 f2 f3 f4 f5 6 f7   f8 f9 fa fb fc fd fe ff
>
> How to do it?

a = range(256)

for i in xrange(0, 256, 16):
print ' '.join('%02x' % n for n in a[i:i+16])

00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff

-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: xlrd 0.6.1 final is now available

2007-06-13 Thread John Machin

The final release of version 0.6.1 of xlrd is now available from
http://www.lexicon.net/sjmachin/xlrd.htm and from the Cheeseshop
(http://cheeseshop.python.org/pypi/xlrd).

What is xlrd? It's a small (download approx 0.1 Mb) pure-Python
library for extracting information  from Microsoft Excel (tm) files,
anywhere Python 2.1 or later will run -- no need for Excel itself, nor
COM, nor even Windows. Further info: follow the links on the home
page.

This major release incorporates the functionality of 0.6.0 which was
not released independently for various reasons including the need to
push ahead with the 0.6.1 functionality.

New in 0.6.0: facility to access named cell ranges and named
constants (Excel UI: Insert/Name/Define).

New in 0.6.1: extracts formatting information for cells (font, "number
format", background, border, alignment and protection) and rows/
columns (height/width etc). To save memory and time for those who
don't need it, this information is extracted only if formatting_info=1
is supplied to the open_workbook() function. The cell records BLANK
and MULBLANKS which contain no data, only formatting information, will
continue to be ignored in the default (no formatting info) case.

There have been several changes made to handle anomalous files
(written by 3rd party software) which Excel will open without complaint, 
but failed with xlrd, usually because an assertion fails or xlrd 
deliberately raises an exception. Refer to HISTORY.html for details. 
These have been changed to accept the anomaly either silently or with a 
NOTE message  or a WARNING message, as appropriate.

Many thanks are due to Simplistix Ltd
(http://www.simplistix.co.uk) for funding the new functionality in
0.6.1.

Since 0.6.1a4 was released in February, only one bug-fix and some
tidying up have been done --  see HISTORY.html for details.

Feedback: general discussion on the python-excel newsgroup (sign
up at http://groups.google.com/group/python-excel?lnk=li&hl=en) or
e-mail to sjmachin at lexicon dot net, preferably with [xlrd] as part of 
the message subject.

Cheers,
John



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Neil Cerutti
On 2007-06-13, Steve Howell <[EMAIL PROTECTED]> wrote:
> You would just change the language definition to say that once
> you enter f(), any call to f() from within f() behaves as if
> the recursively called f() still points to the originally bound
> version of f.  To want any other behavior would be absurd,
> anyhow. 

There's a reason it's generally refered to as "tail-call"
optimization and not "tail-recursive" optimization. The former is
more general, and, I believe, easier to implement than the
latter.

-- 
Neil Cerutti
The peace-making meeting scheduled for today has been cancelled due to a
conflict. --Church Bulletin Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-13 Thread Neil Cerutti
On 2007-06-12, Anders J. Munch <[EMAIL PROTECTED]> wrote:
> Paul Rubin wrote:
>> Steven D'Aprano <[EMAIL PROTECTED]> writes:
 Not tail calls, in general, no.
>>> Sorry, how does that work? You're suggesting that there is an
>>> algorithm which the compiler could follow to optimize away
>>> tail-recursion, but human beings can't follow the same
>>> algorithm?
>>>
>>> Now I'm confused.
>> 
>> The usual compiler method is to translate the code into
>> continuation-passing style and thereby gain tail-recursion
>> optimization automagically.  
>
> There's no need to go into CPS just to optimise tail-recursion.
> After all, compilers were optimising tail-calls decades before
> Appel's work on SML/NJ.
>
> Converting tail-recursion to iteration is trivial, and
> perfectly reasonable for a human to do by hand.  

For simple recursive tail calls, yeah, it can be. Translating a
tail-recursive Factorial function into a while loop is easy. But
tail-call optimization technically works for any tail-call,
including mutual recursion, and non-recursive tail-calls. You
can't reasonably hand-optimize away the stack frame for all
tail-calls.

def foo(x)
  bar(x)

The only way to hand-optimize the call to bar is to inline it
yourself.

-- 
Neil Cerutti
Will the highways on the Internet become more few? --George W. Bush
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there any way to catch expections when call python method in C++

2007-06-13 Thread cptnwillard
One way is to create an intermediate python function, which returns a
special value when an exception is caught.

def ExceptionCatcher(FunctionToCall):
def F():
try: FunctionToCall()
except: return -1
return 0
return F

Then instead of calling your function, you would call
ExceptionCatcher(YourFunction). You can then check the return value in
your C++ code.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SimplePrograms challenge

2007-06-13 Thread Rob Wolfe

Steve Howell wrote:

> I suggested earlier that maybe we post multiple
> solutions.  That makes me a little nervous, to the
> extent that it shows that the Python community has a
> hard time coming to consensus on tools sometimes.

We agree that BeautifulSoup is the best for parsing HTML. :)

> This is not a completely unfair knock on Python,
> although I think the reason multiple solutions tend to
> emerge for this type of thing is precisely due to the
> simplicity and power of the language itself.
>
> So I don't know.  What about trying to agree on an XML
> parsing example instead?
>
> Thoughts?

I vote for example with ElementTree (without xpath)
with a mention of using ElementSoup for invalid HTML.

--
Regards,
Rob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a tuple quickly with list comprehension?

2007-06-13 Thread Dustan
On Jun 13, 5:37 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> In <[EMAIL PROTECTED]>, Diez B. Roggisch wrote:
>
> > No need to create the intermediate list, a generator expression works just
> > fine:
>
> > a = tuple(i for i in range(10))
>
> But `range()` creates the intermediate list anyway.  ;-)

I imagine that's special case.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why can't easy_install Django

2007-06-13 Thread Paul Boddie
On 13 Jun, 03:21, kernel1983 <[EMAIL PROTECTED]> wrote:
> I look it up in PyPI

[...]

> Django is a big project. Why no one maintain this? Did the guy lost
> his password?

Who knows? But then, if you're running a GNU/Linux distribution like
Debian, it's quite possible to use a system package, anyway:

http://packages.debian.org/python-django
http://packages.ubuntu.com/python-django

Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >