Thursday, March 11, 2010

comp.lang.python - 25 new messages in 17 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* Anything like "Effective Java" for Python? - 5 messages, 5 authors
http://groups.google.com/group/comp.lang.python/t/47674ff2d2c78fa6?hl=en
* pexpect and logging integration - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/4a3fbcbcf86faa7e?hl=en
* EOFError: EOF when reading a line - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/3e25a5c68131742b?hl=en
* Multiple import of the same module under different names - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/a946dfb65bd26b31?hl=en
* How to handle file uploads with http.server - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/fae1c3826fd6edaf?hl=en
* logging: local functions ==> loss of lineno - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/1282b9ec08adc2a6?hl=en
* Python Script to get Info from Site not working - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/4741fd660fb9b691?hl=en
* Get a method instance through 'getattr' but not superclass's method - 1
messages, 1 author
http://groups.google.com/group/comp.lang.python/t/17fe44173a924c48?hl=en
* Can't define __call__ within __init__? - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/6c1e03e9d76cf596?hl=en
* a newbie's question - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/320e51e1c2180149?hl=en
* Python, Reportlabs, Pil and Windows 7 (64bit) - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/8f0199cb036d7949?hl=en
* Need advice on starting a Python group - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/0a24bf882c72c555?hl=en
* show image in python - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/028dadaea24719e0?hl=en
* I get the error: ImportError: No module named ffnet - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/2716fc6c0a3213cc?hl=en
* Ideas for creating processes - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/104bababcbab6607?hl=en
* NoSQL Movement? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/942e22a0145599b2?hl=en
* Insert missing keys using defaultdict - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/78157c74ab9c2e32?hl=en

==============================================================================
TOPIC: Anything like "Effective Java" for Python?
http://groups.google.com/group/comp.lang.python/t/47674ff2d2c78fa6?hl=en
==============================================================================

== 1 of 5 ==
Date: Thurs, Mar 11 2010 12:30 am
From: James Harris


On 10 Mar, 15:19, kj <no.em...@please.post> wrote:
> Subject line pretty much says it all: is there a book like "Effective
> Java" for Python.  I.e. a book that assumes that readers are
> experienced programmers that already know the basics of the language,
> and want to focus on more advanced programming issues?

I don't know about the Java book you mention but I find Python in a
Nutshell published by O'Reilly to be a good reference.

James


== 2 of 5 ==
Date: Thurs, Mar 11 2010 6:24 am
From: Stefan Behnel


James Harris, 11.03.2010 09:30:
> On 10 Mar, 15:19, kj<no.em...@please.post> wrote:
>> Subject line pretty much says it all: is there a book like "Effective
>> Java" for Python. I.e. a book that assumes that readers are
>> experienced programmers that already know the basics of the language,
>> and want to focus on more advanced programming issues?
>
> I don't know about the Java book you mention but I find Python in a
> Nutshell published by O'Reilly to be a good reference.

There's also the Python Cookbook that has a couple of useful recipes that
can become handy at times.

http://code.activestate.com/recipes/langs/python/

Stefan

== 3 of 5 ==
Date: Thurs, Mar 11 2010 7:05 am
From: kj


In <mailman.561.1268239578.23598.python-list@python.org> Chris Withers <chris@simplistix.co.uk> writes:

>kj wrote:
>>
>>
>> Subject line pretty much says it all: is there a book like "Effective
>> Java"

>oxymoronic, no?

>Sorry, couldn't resist ;-)

I hear you, but still: read "Effective Java" some day; it will make
you a better programmer, whatever your language preference. I'm
certainly no fan of Java, but I still rank Effective Java as one
of my best reads on programming ever. I felt that my maturity as
a programmer went up a notch or two after digesting this book.

Cheers,

~K


== 4 of 5 ==
Date: Thurs, Mar 11 2010 7:16 am
From: Steve Howell


On Mar 11, 7:05 am, kj <no.em...@please.post> wrote:
> In <mailman.561.1268239578.23598.python-l...@python.org> Chris Withers <ch...@simplistix.co.uk> writes:
>
> >kj wrote:
>
> >> Subject line pretty much says it all: is there a book like "Effective
> >> Java"
> >oxymoronic, no?
> >Sorry, couldn't resist ;-)
>
> I hear you, but still: read "Effective Java" some day; it will make
> you a better programmer, whatever your language preference.  I'm
> certainly no fan of Java, but I still rank Effective Java as one
> of my best reads on programming ever.  I felt that my maturity as
> a programmer went up a notch or two after digesting this book.
>

I'll second that. I much prefer Python to Java, but Effective Java is
an excellent book.

Many of its topics would be applicable to Python, although the
solutions in Python would obviously be different.

Eliminate obsolete object references (use None to break reference
counts)
Avoid finalizers (use the "with" statement)
Favor immutability
Design method signatures carefully (including the x=[] gotcha)
Optimize judiciously
Don't ignore exceptions
Avoid excessive synchronization

There are also some topics in Effective Java where the advice is
almost the opposite of Python tradition.

== 5 of 5 ==
Date: Thurs, Mar 11 2010 7:44 am
From: Kevin Walzer


On 3/10/10 10:19 AM, kj wrote:
> Subject line pretty much says it all: is there a book like "Effective
> Java" for Python. I.e. a book that assumes that readers are
> experienced programmers that already know the basics of the language,
> and want to focus on more advanced programming issues?
>
> ~K
I haven't read "Effective Java," but I have found Lutz's "Programming
Python" to be a very useful guide to solving various programming issues
with the language. It's a big, big book, so there's lots to look at.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com

==============================================================================
TOPIC: pexpect and logging integration
http://groups.google.com/group/comp.lang.python/t/4a3fbcbcf86faa7e?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 12:43 am
From: Lars Stavholm


It works like a charm, thank you!
/Lars

Jean-Michel Pichavant wrote:
> Lars Stavholm wrote:
>> Hi all,
>>
>> has anyone managed to integrate pexpect and logging?
>>
>> I.e., I'd like to be able to pick up the dialog,
>> commands sent and responses received, in my logging.
>> I know about the pexpect logfile, and I can log things
>> to stdout or stderr, but I really need to log using the
>> python logging library.
>>
>> Any thoughts appreciated
>> /Lars
>>
>>
> I had to implement this.
> It's a bit of a hack, but it does the job.
>
> The following code is tested with python 2.5, I remember pexpect behaves
> slightly differently in python 2.3.
>
> import logging
> import pexpect
> import re
>
> # this will be the method called by the pexpect object to log
> def _write(*args, **kwargs):
> content = args[0]
> # let's ignore other params, pexpect only use one arg AFAIK
> if content in [' ', '', '\n', '\r', '\r\n']:
> return # don't log empty lines
> for eol in ['\r\n', '\r', '\n']:
> # remove ending EOL, the logger will add it anyway
> content = re.sub('\%s$' % eol, '', content)
> return logger.info(content) # call the logger info method with the
> reworked content
>
>
> # our flush method
> def _doNothing():
> pass
>
> # get the logger
> logger = logging.getLogger('foo')
>
> # configure the logger
> logger.handlers=[]
> logger.addHandler(logging.StreamHandler())
> logger.handlers[-1].setFormatter(logging.Formatter("%(asctime)s -
> %(name)s - %(levelname)s - %(message)s"))
> logger.setLevel(logging.INFO)
>
> # give the logger the methods required by pexpect
> logger.write = _write
> logger.flush = _doNothing
>
> p = pexpect.spawn('echo "hello world !!"', logfile=logger)
> p.expect('!!')
>
> ... 2010-03-10 15:01:31,234 - foo - INFO - hello world !!
>
> Hope it helps.
>
> JM
>
>

==============================================================================
TOPIC: EOFError: EOF when reading a line
http://groups.google.com/group/comp.lang.python/t/3e25a5c68131742b?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 1:12 am
From: Mihir Patel


I am trying to use the subprocess to send the data to child process. I
am not sure why i keep getting "EOFError: EOF when reading a line"

i am using Python 2.4.3, GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on
64bit linux ,centos

Thanks

output :
=====
Traceback (most recent call last):
File "test_input.py", line 3, in ?
x = raw_input()
EOFError: EOF when reading a line
output: hello

Main Program:
===========

command_line = 'python test_input.py'
p =subprocess.Popen(command_line, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
print 'output:', p.communicate()[0]

o,e = p.communicate('test')

print "out:",o
print "error:",e
p.stdout.close()

print "Exit Success"

test_input.py
==========
print 'hello\n'

x = raw_input()

print x

==============================================================================
TOPIC: Multiple import of the same module under different names
http://groups.google.com/group/comp.lang.python/t/a946dfb65bd26b31?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 2:21 am
From: George Sakkis


The import mechanism is not very smart in identifying whether two
modules imported under different name are actually the same module, at
least when dealing with implicit relative imports and sys.path
manipulation. However, at least in cases of plain file modules, the
module's __file__ would be adequate (or at least better than __name__)
in determining this. Below is an illustration of the different
situations (absolute imports, explicit relative imports, implicit
relative import, sys.path tweaking). So why does not import consult
__file__ before deciding to create a new module instance ?

George


=== File structure =============

~/pkg/
__init__.py # empty
mod1.py
mod2.py
mod3.py
mod4.py
main.py
subpkg/
__init__.py # empty
foo.py # empty

=== Run =============

~$ PYTHONPATH=. python pkg/main.py

=== Output =============

Imported foo from pkg.subpkg: <module 'pkg.subpkg.foo' from
'/home/george/pkg/subpkg/foo.py'>
Imported foo from subpkg: <module 'subpkg.foo' from
'/home/george/pkg/subpkg/foo.pyc'>
Imported foo from pkg.mod1: <module 'pkg.subpkg.foo' from
'/home/george/pkg/subpkg/foo.py'>
Imported foo from mod1: <module 'subpkg.foo' from
'/home/george/pkg/subpkg/foo.pyc'>
Imported foo from pkg.mod2: <module 'pkg.subpkg.foo' from
'/home/george/pkg/subpkg/foo.py'>
Failed to import foo from mod2: Attempted relative import in non-package
Imported foo from pkg.mod3: <module 'pkg.subpkg.foo' from
'/home/george/pkg/subpkg/foo.py'>
Imported foo from mod3: <module 'pkg.subpkg.foo' from
'/home/george/pkg/subpkg/foo.py'>
Imported foo from pkg.mod4: <module 'foo' from
'/home/george/pkg/subpkg/foo.pyc'>
Imported foo from mod4: <module 'foo' from '/home/george/pkg/subpkg/foo.pyc'>

* 9 total module(s)
* 3 distinct module(s)
<module 'subpkg.foo' from '/home/george/pkg/subpkg/foo.pyc'>
<module 'pkg.subpkg.foo' from '/home/george/pkg/subpkg/foo.py'>
<module 'foo' from '/home/george/pkg/subpkg/foo.pyc'>
* 1 distinct file(s)
/home/george/pkg/subpkg/foo.py

=== Code =============

### mod1.py ###
# implicit relative import
from subpkg import foo

### mod2.py ###
# explicit relative import
from .subpkg import foo

### mod3.py ###
# absolute import
from pkg.subpkg import foo

### mod4.py ###
# absolute import after tweaking sys.path
import sys
from os.path import dirname,join
sys.path.append(join(dirname(__file__), 'subpkg'))
import foo

### main.py ###
#!/usr/bin/env python

from os.path import abspath, normpath

def test(*modules):
module_set = set(modules)
file_set = set(module_file(m) for m in modules)
print '* %d total module(s)' % len(modules)
print '* %d distinct module(s)' % len(module_set)
for m in module_set:
print '\t', m
print '* %d distinct file(s)' % len(file_set)
for f in file_set:
print '\t', f

def module_file(mod):
f = abspath(normpath(mod.__file__))
if f.endswith('.pyc'):
f = f[:-1]
return f

if __name__ == '__main__':
from pkg.subpkg import foo
print 'Imported foo from pkg.subpkg:\t', foo
from subpkg import foo as rel_foo
print 'Imported foo from subpkg:\t\t', rel_foo

from pkg.mod1 import foo as foo1
print 'Imported foo from pkg.mod1:\t', foo1
from mod1 import foo as rel_foo1
print 'Imported foo from mod1:\t\t', rel_foo1

from pkg.mod2 import foo as foo2
print 'Imported foo from pkg.mod2:\t', foo2
try: from mod2 import foo as rel_foo2
except ValueError, ex:
print 'Failed to import foo from mod2:', ex

from pkg.mod3 import foo as foo3
print 'Imported foo from pkg.mod3:\t', foo3
from mod3 import foo as rel_foo3
print 'Imported foo from mod3:\t\t', rel_foo3

from pkg.mod4 import foo as foo4
print 'Imported foo from pkg.mod4:\t', foo4
from mod4 import foo as rel_foo4
print 'Imported foo from mod4:\t\t', rel_foo4

print
test(foo, rel_foo,
foo1, rel_foo1,
foo2, # rel_foo2,
foo3, rel_foo3,
foo4, rel_foo4)

==============================================================================
TOPIC: How to handle file uploads with http.server
http://groups.google.com/group/comp.lang.python/t/fae1c3826fd6edaf?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 2:30 am
From: "Neil Blue"


Hello,

I have a basic http.server instance running (class
HTTPHandler(http.server.BaseHTTPRequestHandler), with python 3.1, and I
would like to upload files with multipart forms.

def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
if ctype=='multipart/form-data':
print('parsing...')
query=cgi.parse_multipart(self.rfile, pdict)
print(query)

However the file never seems to finish being parsed. There are no errors,
but the call hangs at: query=cgi.parse_multipart(self.rfile, pdict)

Please can anyone offer some insight into this or somewhere else I may find
some more information.

Thanks
Neil

*********************************************

The information contained in this message is likely to be confidential. It is intended only for the person named above. Any dissemination, distribution, copying, disclosure or use of this message or its contents unless authorised by BioWisdom Ltd is strictly prohibited. Any views or opinions expressed within this e-mail are those of the author and do not necessarily represent those of BioWisdom Ltd. If you have received this message in error, please immediately notify us and delete it. Thank you. Registered Office: BioWisdom Ltd, Harston Mill, Harston, Cambridge, CB22 7GG. Registered in England: (GB) 3861669. VAT registered: (GB) 750899881. Tel: +44 (0)1223 874800, Fax: +44 (0) 1223 874801, Internet:www.biowisdom.com

*********************************************


==============================================================================
TOPIC: logging: local functions ==> loss of lineno
http://groups.google.com/group/comp.lang.python/t/1282b9ec08adc2a6?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Mar 11 2010 2:44 am
From: Jean-Michel Pichavant


Hellmut Weber wrote:
> Hi Vinay Sajip,
> I'm very glad discoverd your logging module ;-)
> (That's what I would have liked 25 years ago when I was working as a
> technical software developper!)
>
> Now I'm writing just some personal tools, I like python and want to
> use logging on a regular basis.
>
> Logging works very well giving the filename and line number of the
> point where it is called. As long as I use the loggers directly.
> BUT when I have to wrap the logger call in some other function, I
> always get file name and line number of the call of the logger inside
> the wrapping function.
>
> Is there a possibility to get this information in this situation too?
>
> TIA
>
> Hellmut
>
You have to specify the file and line number by yourself, the logging
feature can only assume that you want the line number of the logger call.

i.e.

in test.py:

import logging
import inspect

_logger = logging.getLogger(__name__)

class Foo:
def __init__(self):
self._logger = _logger

def info(self, msg):
previousFrame = inspect.currentframe().f_back
self._logger.info(msg,
extra={'custom_lineno':previousFrame.f_lineno, 'custom_filename':
previousFrame.f_code.co_filename})

if __name__ == '__main__':
_logger.handlers=[]
_logger.addHandler(logging.StreamHandler())
_logger.handlers[-1].setFormatter(logging.Formatter('file
%(custom_filename)s line %(custom_lineno)d : %(message)s'))
_logger.setLevel(logging.DEBUG)
foo = Foo()
foo.info('a foo info')

In [3]: run test.py
file test.py line 20 : a foo info


note that you cannot override the logging builtin 'lineno' key with the
extra dictionary, that is why I'm using the 'custom_lineno' key. I don't
know why the logger forbids it though, would have been nice to override
lineno without the need of adding a new key.

JM


== 2 of 2 ==
Date: Thurs, Mar 11 2010 3:14 am
From: Peter Otten <__peter__@web.de>


Hellmut Weber wrote:

> Logging works very well giving the filename and line number of the point
> where it is called. As long as I use the loggers directly.
> BUT when I have to wrap the logger call in some other function, I always
> get file name and line number of the call of the logger inside the
> wrapping function.
>
> Is there a possibility to get this information in this situation too?

The official way is probably to write a custom Logger class that overrides
the findCaller() method.

Below is a hack that monkey-patches the logging._srcfile attribute to ignore
user-specified modules in the call stack:

$ cat wrapper.py
import logging
import os
import sys

logger = logging.getLogger()

class SrcFile(object):
def __init__(self, exclude_files):
self.files = set(exclude_files)
def __eq__(self, other):
return other in self.files

def fixname(filename):
if filename.lower().endswith((".pyc", ".pyo")):
filename = filename[:-4] + ".py"
return os.path.normcase(filename)

if "--monkey" in sys.argv:
print "patching"
logging._srcfile = SrcFile([logging._srcfile, fixname(__file__)])

def warn(*args, **kw):
logger.warn(*args, **kw)

$ cat main.py
import logging
logging.basicConfig(format="%(filename)s <%(lineno)s>: %(message)s")
import wrapper
wrapper.warn("foo")
wrapper.warn("bar")
wrapper.warn("baz")

$ python main.py
wrapper.py <23>: foo
wrapper.py <23>: bar
wrapper.py <23>: baz

$ python main.py --monkey
patching
main.py <4>: foo
main.py <5>: bar
main.py <6>: baz

$ python -V
Python 2.6.4

Peter

==============================================================================
TOPIC: Python Script to get Info from Site not working
http://groups.google.com/group/comp.lang.python/t/4741fd660fb9b691?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 4:37 am
From: Steve Holden


Jimbo wrote:
> On Mar 11, 12:38 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>> En Wed, 10 Mar 2010 20:06:41 -0300, Jimbo <nill...@yahoo.com> escribió:
>>
>>> I found a semi Python & internet tutorial here if anyone else would
>>> like ithttp://www.upriss.org.uk/python/session6.html
>>> My script is meant to find which radio button is selected & tell me
>>> that. But it just keeps saying "No Drink Selected!"
>> Are you sure? From the code you posted, the message should read "You need
>> to select a drink!", not that one.
>
>> Replace the last line with something like this, to see what you are
>> getting exactly:
>>
>> print("Sorry, no %r (%r) available." % (
>> drink,
>> cgi.escape(repr(type(drink)))))
>>
>> BTW, which Python version are you using? The tutorial you linked to is
>> aimed at Python 2.x, but your print syntax suggests you're using Python 3.x
>>
>> --
>> Gabriel Genellina
>
> Yeah that("You need to select a drink!") was the string I was getting
> but I should be getting hot chocolate or something else(which ever one
> I have selected)
>
> I am using 3.x, do you think I should be using something else? Also
> the your code I put in does the some thing, just now it says "Sorry no
> none drink available" all the time.

It may not seem like it, but you *are* making progress here (of a sort).

>From the information you have given us so far it appears that your call
to form.getvalue() is returning None (which is of type <type 'NoneType'>
- your browser is probably not displaying this because it sees it as a
bogus HTML tag.

Perhaps you haven't yet realised the importance of exact copy-and-paste
here. What I'd like you to do is change the line

drink = form.getvalue("drink")

to

drink = form.getfirst("drink", "No drink selected")

then have you browser display the HTML source of the response page your
program is returning and paste that into a reply to this mail.

You might also want to take a look at

http://docs.python.org/3.1/library/cgi.html

for further information about the Python library your are trying to use.
In particular I'd recommend heeding its advice about use of the cgitb
module.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/


==============================================================================
TOPIC: Get a method instance through 'getattr' but not superclass's method
http://groups.google.com/group/comp.lang.python/t/17fe44173a924c48?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 4:42 am
From: Steve Holden


Radhakrishna Bhat wrote:
> I am using getattr to get a method instance from a class. But it also
> returns methods from the superclass. How to detect if an attribute is
> from superclass?
>
You could try, if x is in instance, looking to see whether the name is
defined in x.__class__.__dict__.

>>> class A:
... def method1(self):
... print "A1"
... def method2(self):
... print "A2"
...
>>> class B(A):
... def method2(self):
... print"B2"
... def method3(self):
... print "B3"
...
>>> a = A()
>>> b = B()
>>> b.method2()
B2
>>> b.method1()
A1
>>> b.method3()
B3
>>> b.__class__.__dict__
{'__module__': '__main__', 'method2': <function method2 at 0x7ff1d6bc>,
'method3': <function method3 at 0x7ff1d3ac>, '__doc__': None}
>>> a.__class__.__dict__
{'__module__': '__main__', 'method2': <function method2 at 0x7ff1d064>,
'__doc__': None, 'method1': <function method1 at 0x7ff1d6f4>}
>>>

I'd be interested to know why this is so important.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/


==============================================================================
TOPIC: Can't define __call__ within __init__?
http://groups.google.com/group/comp.lang.python/t/6c1e03e9d76cf596?hl=en
==============================================================================

== 1 of 4 ==
Date: Thurs, Mar 11 2010 4:56 am
From: Neal Becker


Steven D'Aprano wrote:

> On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote:
>
>> Want to switch __call__ behavior. Why doesn't this work? What is the
>> correct way to write this?
>>
>> class X (object):
>> def __init__(self, i):
>> if i == 0:
>> def __call__ (self):
>> return 0
>> else:
>> def __call_ (self):
>> return 1
>
>
> Others have already pointed out that there are two reasons that won't
> work:
>
> (1) you define __call__ as a local variable of the __init__ method which
> then disappears as soon as the __init__ method completes; and
>
> (2) special methods like __call__ are only called on the class, not the
> instance, so you can't give each instance its own method.
>
>
> Perhaps the best way to solve this is to use delegation:
>
>
> def zero_returner():
> return 0
>
> def one_returner():
> return 1
>
>
> class X (object):
> def __init__(self, i):
> if i == 0:
> self.func = zero_returner
> else:
> self.func = one_returner
> def __call__(self, *args, **kwargs):
> return self.func(*args, **kwargs)
>
>
> zero_returner and one_returner can be any callable object, not
> necessarily a function.
>
> Of course, all this assumes that your solution isn't even simpler:
>
> class X (object):
> def __init__(self, i):
> self.i = i
> def __call__(self):
> return self.i
>
> but I assume if it was that simple, you would have done that already.
>
>
>
The example I showed was just a toy problem. The real problem is
I expect to call a function many times, and I want to avoid the overhead of
the 'if blah' everytime.

== 2 of 4 ==
Date: Thurs, Mar 11 2010 5:30 am
From: Steve Holden


Neal Becker wrote:
> Steven D'Aprano wrote:
>
>> On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote:
>>
>>> Want to switch __call__ behavior. Why doesn't this work? What is the
>>> correct way to write this?
>>>
>>> class X (object):
>>> def __init__(self, i):
>>> if i == 0:
>>> def __call__ (self):
>>> return 0
>>> else:
>>> def __call_ (self):
>>> return 1
>>
>> Others have already pointed out that there are two reasons that won't
>> work:
>>
>> (1) you define __call__ as a local variable of the __init__ method which
>> then disappears as soon as the __init__ method completes; and
>>
>> (2) special methods like __call__ are only called on the class, not the
>> instance, so you can't give each instance its own method.
>>
>>
>> Perhaps the best way to solve this is to use delegation:
>>
>>
>> def zero_returner():
>> return 0
>>
>> def one_returner():
>> return 1
>>
>>
>> class X (object):
>> def __init__(self, i):
>> if i == 0:
>> self.func = zero_returner
>> else:
>> self.func = one_returner
>> def __call__(self, *args, **kwargs):
>> return self.func(*args, **kwargs)
>>
>>
>> zero_returner and one_returner can be any callable object, not
>> necessarily a function.
>>
>> Of course, all this assumes that your solution isn't even simpler:
>>
>> class X (object):
>> def __init__(self, i):
>> self.i = i
>> def __call__(self):
>> return self.i
>>
>> but I assume if it was that simple, you would have done that already.
>>
>>
>>
> The example I showed was just a toy problem. The real problem is
> I expect to call a function many times, and I want to avoid the overhead of
> the 'if blah' everytime.
>
This is a premature optimization. First, make it work. Then (if it
doesn't work fast enough) make it work faster.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

== 3 of 4 ==
Date: Thurs, Mar 11 2010 6:14 am
From: Andre Engels


On Thu, Mar 11, 2010 at 2:30 PM, Steve Holden <steve@holdenweb.com> wrote:

>> The example I showed was just a toy problem.  The real problem is
>> I expect to call a function many times, and I want to avoid the overhead of
>> the 'if blah' everytime.
>>
> This is a premature optimization. First, make it work. Then (if it
> doesn't work fast enough) make it work faster.

Corrolary: When you do make it faster, make it faster where it is slow.
Second corrolary: If making it fast is so important that these two
rules do not apply, Python is not your language of choice.


--
André Engels, andreengels@gmail.com


== 4 of 4 ==
Date: Thurs, Mar 11 2010 8:01 am
From: MRAB


Andre Engels wrote:
> On Thu, Mar 11, 2010 at 2:30 PM, Steve Holden <steve@holdenweb.com> wrote:
>
>>> The example I showed was just a toy problem. The real problem is
>>> I expect to call a function many times, and I want to avoid the overhead of
>>> the 'if blah' everytime.
>>>
>> This is a premature optimization. First, make it work. Then (if it
>> doesn't work fast enough) make it work faster.
>
> Corrolary: When you do make it faster, make it faster where it is slow.
> Second corrolary: If making it fast is so important that these two
> rules do not apply, Python is not your language of choice.
>
Addendum: a bad algorithm is bad, whatever language it's written in.

==============================================================================
TOPIC: a newbie's question
http://groups.google.com/group/comp.lang.python/t/320e51e1c2180149?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 5:10 am
From: Jean-Michel Pichavant


PEYMAN ASKARI wrote:
> Hello
>
> I need some help dynamically reloading modules. As it turns out, it is
> not as simple as calling reload, as outlined here
> http://pyunit.sourceforge.net/notes/reloading.html
>
> Is there builtin support for this? The example they gave does not seem
> to work for me, and I am unclear as to what PyUnit is.
>
> Thanks
>
>
> Peyman
>

Please don't top post.
As for your question, there's no built-in method to reload a module
because it is not possible (in a generic manner).

However if you *really* want to do it you can do it for your particular
module (and only for this one).
It is tough to explain though. You have to know perfectly the python
object model and I'm not familiar enough with it to do the explanation.

So:

1/ try to live without dynamic reload
2/ if you *really* require dynamic reload, you can try to execute the
module in a subprocess. That way, every time you start a new subprocess,
the last up to date version of the module is used
3/ reload the module and all it's related objects currently in memory,
this is painfull and there are many traps to fall into. I'll let someone
else answer this point.

JM

==============================================================================
TOPIC: Python, Reportlabs, Pil and Windows 7 (64bit)
http://groups.google.com/group/comp.lang.python/t/8f0199cb036d7949?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 5:55 am
From: Astley Le Jasper


I have a Windows 7 (64bit AMD) machine and am having quite a lot of
problems installing Reportlabs and Pil. I wondered if anyone else has
had the same issues and what the best way of dealing with it.

So far I've tried:

1. Reportlabs / Pil 32 installers - I've tried using these but they
can't find python. I also tried registering Python (http://effbot.org/
zone/python-register.htm
) but this also fails.
2. Reportlabs / Pil Source - I downloaded each of these and tried to
do a "python setup.py install". However, both complain that they can't
find "vcvarsall.bat". I've done some checking and it's because the
compiler isn't present. Everyone is suggesting downloading Visual
Studio Express C++, but this only comes with the 32bit compiler. There
seems to be quite a lot of work to get 64bit VSE working on a 64bit
machine (http://jenshuebel.wordpress.com/2009/02/12/visual-c-2008-
express-edition-and-64-bit-targets/
).

But before I start down that path, I wondered if anyone had any advice
(.... and no I don't mean suggesting I swap to Linux).

ALJ

==============================================================================
TOPIC: Need advice on starting a Python group
http://groups.google.com/group/comp.lang.python/t/0a24bf882c72c555?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 6:57 am
From: gb345

I'm hoping to get advice from anyone with prior experience setting
up a Python group.

A friend of mine and I have been trying to start a
scientific-programming-oriented Python group in our school (of
medecine and bio research), with not much success.

The main problem is attendance. Even though a *ton* of people have
told us that it's a great idea, that they're *very* interested,
and have asked to be added to our mailing list, the attendance to
our first few meeting has never been more than 5, including my
friend and I. Last time just he and I showed up.

The second problem is getting content. The format we'd envisioned
for this group was centered around code review (though not limited
to it). The idea was that at every meeting a different member
would show some code. This could be for any of a number of reasons,
such as, for example, 1) illustrate a cool module or technique; 2)
present a scientific research problem and how they used Python to
solve it, or get help solving it; 3) get general feedback (e.g. on
code clarity, software usability, module architecture, etc.). But
in principle just about anything is OK: e.g. a talk on favorite
Python resources, or a comparison of Python with some other language,
or an overview of Python gotchas would all be fair game.

Also, we stressed that the talks were not expected to be polished:
no need for PowerPoint slides, etc. Just project any old code onto
the screen, and talk about it, or scribble stuff on the chalkboard.

Still, we have a hard time finding volunteers.

And even when we've had volunteers, hardly anyone shows up!

Any suggestions would be appreciated.

GB

P.S. There's a Python Meetup we could go to, but it does not fit
the bill for us: it doesn't meet often enough, it's sort of out of
the way, and has practically no one doing scientific programming.

==============================================================================
TOPIC: show image in python
http://groups.google.com/group/comp.lang.python/t/028dadaea24719e0?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 7:09 am
From: Philip Semanchuk

On Mar 10, 2010, at 5:03 PM, mohamed issolah wrote:

> Hey, This is my program
>
> 1 #!/usr/bin/python
> 2 import PIL
> 3 import numpy
> 4 import Image
> 5 import ImageOps
> 6 import sys
> 7
> 8 def Matimg(path):
> 9 """transforme image en matrice"""
> 10 Img = Image.open(str(path))
> 11 Img1 = ImageOps.grayscale(Img)
> 12 largeur,hauteur = Img1.size
> 13 imdata = Img1.getdata()
> 14 tab = numpy.array(imdata)
> 15 matrix = numpy.reshape(tab,(hauteur,largeur))
> 16 return matrix
> 17
> 18 def Creeimg():
> 19 """transforme matrice en image"""
> 20 img = Image.new ("L",(8,8))
> 21 matrix = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
> 22 img.putdata(matrix)
> 23 img.show()
> 24 img.save(fp="./ana.bmp")
> 25
> 26 if __name__== '__main__':
> 27 if len(sys.argv) < 2 :
> 28 print "Usage: img.py <image>"
> 29 sys.exit(0)
> 30 path = sys.argv[1]
> 31 matrix = Matimg(path)
> 32 print matrix
> 33 Creeimg()
>
> My probeleme : In line 23 "img.show()" Don't work, normally I show
> the image
> but it's not work, but strangely in line 24 "img.save(fp="./
> ana.bmp")" it's
> work
> WHERE IS THE PROBLEME.
>
> I have this error in shell : "(eog:3176): GLib-WARNING **: GError
> set over
> the top of a previous GError or uninitialized memory.
> This indicates a bug in someone's code. You must ensure an error is
> NULL
> before it's set.
> The overwriting error message was: Error in getting image file info "
>
>
> os: ubuntu 9.10

Hi issolah,
I don't know what your problem is but I have a few suggestions --
1) You say that img.show() doesn't work. How does it fail? Is that
where you get the GLib warning?
2) I'm glad you posted your code, but because it has line numbers,
it's awkward to copy & paste into a local example. Please show your
code without line numbers.

I'm unfamiliar with PIL, so this is just a wild guess, but based on
the GLib error it seems like you haven't initialized something
properly. Sorry I couldn't be more helpful. Maybe someone who knows
more will answer.

Good luck
Philip


==============================================================================
TOPIC: I get the error: ImportError: No module named ffnet
http://groups.google.com/group/comp.lang.python/t/2716fc6c0a3213cc?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 7:43 am
From: " Cal Who"


I have the .py file in Eclipse
#@PydevCodeAnalysisIgnore
from ffnet import ffnet, mlgraph
topology = mlgraph( (2, 3, 1) )
nn = ffnet(topology)

I select RunAs / Python Run

I get the error
from ffnet import ffnet, mlgraph
ImportError: No module named ffnet

In the folder Python26\lib\site-packages I see
ffnet-0.6.2-py2.6.egg-info

also a folder ffnet
in that folder there is a folder Examples
If I click one of the examples pyrhon.exe opens and runs it OK.

Is there enough in the above for you to tell me how to procede?

Thanks in advance for any help at all


==============================================================================
TOPIC: Ideas for creating processes
http://groups.google.com/group/comp.lang.python/t/104bababcbab6607?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 7:54 am
From: bobicanprogram


On Mar 10, 4:52 pm, J <dreadpiratej...@gmail.com> wrote:
> I'm working on a project and thought I'd ask for a suggestion on how
> to proceed (I've got my own ideas, but I wanted to see if I was on the
> right track)
>
> For now, I've got this:
>
> def main():
> ## get our list of directories to refresh
> releases=sys.argv[1:]
> if len(releases) < 1:
> print "You need to provide at least one dir to update"
> sys.exit()
>
> ## Lets figure out what there is to update
> updateDirs = []
> for rel in releases:
> currentDir = os.path.join(homedir, rel)
> for item in os.listdir(currentDir):
> updateDirs += [os.path.join(homedir, rel, item)]
>
> which returns a list of full pathnames to directories that need to be
> updated (updates will be carried out by calling rsync or zsync
> eventually)
>
> The directory hierarchy looks like this:
>
> /home/user/files
> /home/user/files/version1
> /home/user/files/version1/type1
> /home/user/files/version1/type2
> /home/user/files/version2
> /home/user/files/version2/type1
>
> and the list ends up looking like this:
>
> ['/home/user/files/version1/type1','/home/user/files/version1/type2','/home/user/files/version2/type1','/home/user/files/version2/type2']
>
> the next thing I need to do is figure out how to update those.
>
> the quick and dirty would be (as I'm imagining it at the moment):
> for path in pathlist:
> chdir into path
> execute rsync or zsync
>
> but that gets me moving into one dir, updating, then moving into another.
>
> What I was wondering about though, is spawning off separate rsync
> processes to run concurrently (each rsync will hit a different remote
> dir for each local dir)
>
> so I'm trying to figure out a good way to do this:
>
> for path in pathlist:
> kick off an individual rsync|zsync process to update path
>
> wait for all child processes to end
> exit program.
>
> I've been looking at subprocess because at the moment, that's all I
> really know...
> But is there a better way of kicking off multiple simultaneous
> processes so I can update all dirs at once instead of one at a time?
>
> No, this isn't homework, it's something I'm working on to sync a
> couple directories of ISO images to grab nightly builds
> Yes, there are plenty of pre-made scripts out there, but I don't want
> to even look at those because I figured this would be a good learning
> experience, and I want to try to solve this as much on my own as I can
> without just cut and pasting from someone elses program.
>
> So, with that said, any ideas on the best way to proceed? I'm going
> to start looking at ways to use subprocess to do this, or would there
> be a better way (multi-threading maybe?)
>
> Or am I even in the right ballpark?
>
> Cheers
> Jeff


You might be able to use the SIMPL toolkit for this one.
(http://www.icanprogram.com/06py/lesson1/lesson1.html)

You could wrap the rsync executable as a SIMPL receiver module and
then message to that from inside your Python script to kick it off and
synchronize actions.

bob

==============================================================================
TOPIC: NoSQL Movement?
http://groups.google.com/group/comp.lang.python/t/942e22a0145599b2?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 8:00 am
From: dkeeney


On Mar 8, 12:14 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:

> You've totally missed the point. It isn't the size of the data you have
> today that matters, it's the size of data you could have in several years'
> time.
>
> Maybe today you've got 10 users each with 10 megabytes of data, but you're
> aspiring to become the next twitter/facebook or whatever. It's a bit late
> as you approach 100 million users (and a petabyte of data) to discover that
> your system isn't scalable: scalability needs to be built in from day one.

Do you have examples of sites that got big by planning their site
architecture from day 0 to be big?

Judging from published accounts, even Facebook and Twitter did not
plan to be 'the next twitter/facebook'; each started with routine
LAMP stack architecture and successfully re-engineered the
architecture multiple times on the way up.

Is there compelling reason to think the 'next twitter/facebook' can't
and won't climb a very similar path?

I see good reasons to think that they *will* follow the same path, in
that there are motivations at both ends of the path for re-engineering
as you go. When the site is small, resources commited to the backend
are not spent on making the frontend useful, so business-wise the best
backend is the cheapest one. When the site becomes super-large, the
backend gets re-engineered based on what that organization learned
while the site was just large; Facebook, Twitter, and Craigslist all
have architectures custom designed to support their specific needs.
Had they tried to design for large size while they were small, they
would have failed; they couldn't have known enough then about what
they would eventually need.

The only example I can find of a large site that architected large
very early is Google, and they were aiming for a market (search) that
was already known to be huge.

Its reasonable to assume that the 'next twitter/facebook' will *not*
be in web search, social-networking, broadcast instant messaging, or
classified ads, just because those niches are taken already. So
whichever 'high-scalability' model the aspiring site uses will be the
wrong one. They might as well start with a quick and cheap LAMP
stack, and re-engineer as they go.

Just one internet watcher's biased opinion...

David


www.rdbhost.com -> SQL databases via a web-service


==============================================================================
TOPIC: Insert missing keys using defaultdict
http://groups.google.com/group/comp.lang.python/t/78157c74ab9c2e32?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Mar 11 2010 8:02 am
From: Gnarlodious


I am trying to grok this documentation but need help:
http://docs.python.org/library/collections.html#defaultdict-examples

In a perfect world the dict looks like this:
plistDict={'Style':'ExternalURL', 'Ref':'http://Gnarlodious.com/',
'Tip':'Opens in a new window', 'Text':'Gnarlodious.com'}

Let's say I want to prep a dict from a plist to insert the values into
an HTML link string:
"<a class='%(Style)s' href='%(Ref)s' title='%(Tip)s'>%(Text)s</a>" %
plistDict

However, in this imperfect world the dict might look like this:
plistDict={'Ref':'http://Gnarlodious.com/', 'Text':'Gnarlodious.com'}

which would error:
KeyError: 'Style'

So using defaultdict:
from collections import defaultdict

How do create a dict assigning every missing key with a default
string?

-- Gnarlie


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate