Re: Are Small Dogs Good with Kids?

2011-02-07 Thread Stefan Behnel

Bob Martin, 07.02.2011 08:19:

My two terriers absolutely love children.


H, tasty ...

Stefan

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


Re: Are Small Dogs Good with Kids?

2011-02-07 Thread Littlefield, Tyler
Not only does this have -nothing- to do with python, but you reproduced 
the spam yet again by quoting it... seriously?

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


Turbogears 2.1 with mako tmplates

2011-02-07 Thread Vineet Deodhar
Hi!,
I am a python programmer.
For web-based solutions, I have started learning TG 2.1
By and large, the documentation on TG 2.1 official site is a work-in-process.
As regards to the templates, it tells how to go about Genshi.
I wish to go with mako.
Gone through the docs of mako (they are good).
But still, it would be better if I can get a tutorial explaining using TG 2.1 
with mako.
(It will save me from re-inventing the wheel, if somebody has written docs on 
these lines).
Can anybody point to any such tutorial?
(I googled, but couldn't locate any convincing result).

Thanks,
Vineet


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


re.Scanner - match python dot notation

2011-02-07 Thread Christian K.

Hi,

I am trying to find a regexp to be used with re.Scanner that matches the 
'package.module.member' syntax. More specifically I want to parse 
function strings like


numpy.sin(x*a+w)

and sort out variables/constants.

This one here works using re.match but fails when used with Scanner (due 
to the grouping () I guess).


r'((?=.*[a-zA-Z])(?=.+[\.]).+)[\(\+\-]+'

I highly appreciate your comments.

Regards, christian

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


Re: returning all matching groups with re.search()

2011-02-07 Thread Mauro Caceres
>>> import re
>>> help(re.findall)
Help on function findall in module re:

findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.

If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.

Empty matches are included in the result.

>>> re.findall('e','fredbarneybettywilma')
['e', 'e', 'e']

-- 
Mauro Cáceres
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re.Scanner - match python dot notation

2011-02-07 Thread MRAB

On 07/02/2011 15:34, Christian K. wrote:

Hi,

I am trying to find a regexp to be used with re.Scanner that matches the
'package.module.member' syntax. More specifically I want to parse
function strings like

numpy.sin(x*a+w)

and sort out variables/constants.

This one here works using re.match but fails when used with Scanner (due
to the grouping () I guess).

r'((?=.*[a-zA-Z])(?=.+[\.]).+)[\(\+\-]+'

I highly appreciate your comments.

Regards, christian


I'm not sure about your regex, but I'd probably use this:

r'[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*'
--
http://mail.python.org/mailman/listinfo/python-list


Re: itertools.groupby usage to get structured data

2011-02-07 Thread nn
On Feb 5, 7:12 am, Peter Otten <__pete...@web.de> wrote:
> Slafs wrote:
> > Hi there!
>
> > I'm having trouble to wrap my brain around this kind of problem:
>
> > What I have :
> >   1) list of dicts
> >   2) list of keys that i would like to be my grouping arguments of
> > elements from 1)
> >   3) list of keys that i would like do "aggregation" on the elements
> > of 1) with some function e.g. sum
>
> > For instance i got:
> > 1) [ { 'g1' : 1, 'g2' : 8, 's_v1' : 5.0, 's_v2' : 3.5 },
> >       { 'g1' : 1, 'g2' : 9, 's_v1' : 2.0, 's_v2' : 3.0 },
> >       {'g1' : 2, 'g2' : 8, 's_v1' : 6.0, 's_v2' : 8.0}, ... ]
> > 2) ['g1', 'g2']
> > 3) ['s_v1', 's_v2']
>
> > To be precise 1) is a result of a values_list method from a QuerySet
> > in Django; 2) is the arguments for that method; 3) those are the
> > annotation keys. so 1) is a result of:
> >    qs.values_list('g1', 'g2').annotate(s_v1=Sum('v1'), s_v2=Sum('v2'))
>
> > What i want to have is:
> > a "big" nested dictionary with 'g1' values as 1st level keys and a
> > dictionary of aggregates and "subgroups" in it.
>
> > In my example it would be something like this:
> > {
> >   1 : {
> >           's_v1' : 7.0,
> >           's_v2' : 6.5,
> >           'g2' :{
> >                    8 : {
> >                           's_v1' : 5.0,
> >                           's_v2' : 3.5 },
> >                    9 :  {
> >                           's_v1' : 2.0,
> >                           's_v2' : 3.0 }
> >                 }
> >        },
> >   2 : {
> >            's_v1' : 6.0,
> >            's_v2' : 8.0,
> >            'g2' : {
> >                     8 : {
> >                           's_v1' : 6.0,
> >                           's_v2' : 8.0}
> >            }
> >        },
> > ...
> > }
>
> > # notice the summed values of s_v1 and s_v2 when g1 == 1
>
> > I was looking for a solution that would let me do that kind of
> > grouping with variable lists of 2) and 3) i.e. having also 'g3' as
> > grouping element so the 'g2' dicts could also have their own
> > "subgroup" and be even more nested then.
> > I was trying something with itertools.groupby and updating nested
> > dicts, but as i was writing the code it started to feel too verbose to
> > me :/
>
> > Do You have any hints maybe? because i'm kind of stucked :/
>
> > Regards
>
> > Sławek
>
> Not super-efficient, but simple:
>
> $ cat python sumover.py
> cat: python: No such file or directory
> data = [ { 'g1' : 1, 'g2' : 8, 's_v1' : 5.0, 's_v2' : 3.5 },
>          { 'g1' : 1, 'g2' : 9, 's_v1' : 2.0, 's_v2' : 3.0 },
>          {'g1' : 2, 'g2' : 8, 's_v1' : 6.0, 's_v2' : 8.0}]
> sum_over = ["s_v1", "s_v2"]
> group_by = ["g1", "g2"]
>
> wanted = {
>   1 : {
>           's_v1' : 7.0,
>           's_v2' : 6.5,
>           'g2' :{
>                    8 : {
>                           's_v1' : 5.0,
>                           's_v2' : 3.5 },
>                    9 :  {
>                           's_v1' : 2.0,
>                           's_v2' : 3.0 }
>                 }
>        },
>   2 : {
>            's_v1' : 6.0,
>            's_v2' : 8.0,
>            'g2' : {
>                     8 : {
>                           's_v1' : 6.0,
>                           's_v2' : 8.0}
>            }
>        },
>
> }
>
> def calc(data, group_by, sum_over):
>     tree = {}
>     group_by = group_by + [None]
>     for item in data:
>         d = tree
>         for g in group_by:
>             for so in sum_over:
>                 d[so] = d.get(so, 0.0) + item[so]
>             if g:
>                 d = d.setdefault(g, {}).setdefault(item[g], {})
>     return tree
>
> got = calc(data, group_by, sum_over)[group_by[0]]
> assert got == wanted
> $ python sumover.py
> $
>
> Untested.

Very clever. I didn't understand how it worked until I rewrote it like
this:

def calc(data, group_by, sum_over):
tree = {}
group_by = [None] + group_by
for item in data:
d = tree
for g in group_by:
if g:
d = d.setdefault(g, {}).setdefault(item[g], {})
for so in sum_over:
d[so] = d.get(so, 0.0) + item[so]
return tree


Processing "None" in the last round of the loop was throwing me off.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re.Scanner - match python dot notation

2011-02-07 Thread Peter Otten
Christian K. wrote:

> I am trying to find a regexp to be used with re.Scanner that matches the
> 'package.module.member' syntax. More specifically I want to parse
> function strings like
> 
> numpy.sin(x*a+w)
> 
> and sort out variables/constants.

No direct answer, but regarding your ultimate goal the ast module might be a 
better starting point.
-- 
http://mail.python.org/mailman/listinfo/python-list


splitting by double newline

2011-02-07 Thread Nikola Skoric
Hello everybody,

I'd like to split a file by double newlines, but portably. Now,
splitting by one or more newlines is relatively easy:

self.tables = re.split("[\r\n]+", bulk)

But, how can I split on double newlines? I tried several approaches,
but none worked...

-- 
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: splitting by double newline

2011-02-07 Thread Ian
On Feb 7, 10:43 am, Nikola Skoric  wrote:
> Hello everybody,
>
> I'd like to split a file by double newlines, but portably. Now,
> splitting by one or more newlines is relatively easy:
>
> self.tables = re.split("[\r\n]+", bulk)
>
> But, how can I split on double newlines? I tried several approaches,
> but none worked...

self.tables = re.split(r'(?:\r\n){2,}|\r{2,}|\n{2,}', bulk)
-- 
http://mail.python.org/mailman/listinfo/python-list


Remove whitespaces and line breaks in a XML file

2011-02-07 Thread David Vicente
Hi,

 

I´m parsing an xml file with xml.etree. It works correctly, but I have a
problem with the text attribute of the elements which should be empty. For
example, in this case:



Ken



 

The text element of “book” should be empty, but it returns me some
whitespaces and break lines. I can´t get remove these whitespaces without
remove information. 

 

Some idea?

 

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


Re: splitting by double newline

2011-02-07 Thread Peter Otten
Nikola Skoric wrote:

> Hello everybody,
> 
> I'd like to split a file by double newlines, but portably. Now,
> splitting by one or more newlines is relatively easy:
> 
> self.tables = re.split("[\r\n]+", bulk)
> 
> But, how can I split on double newlines? I tried several approaches,
> but none worked...

If you open the file in universal newline mode with

with open(filename, "U") as f:
bulk = f.read()

your data will only contain "\n". You can then split with 

blocks = bulk.split("\n\n") # exactly one empty line

or 

blocks = re.compile(r"\n{2,}").split(bulk) # one or more empty lines

One last variant that doesn't read in the whole file and accepts lines with 
only whitespace as empty:

with open(filename, "U") as f:
blocks = ("".join(group) for empty, group in itertools.groupby(f, 
key=str.isspace) if not empty)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re.Scanner - match python dot notation

2011-02-07 Thread Christian K.

Am 07.02.11 17:47, schrieb MRAB:

On 07/02/2011 15:34, Christian K. wrote:

Hi,

I am trying to find a regexp to be used with re.Scanner that matches the
'package.module.member' syntax. More specifically I want to parse
function strings like

numpy.sin(x*a+w)

and sort out variables/constants.

This one here works using re.match but fails when used with Scanner (due
to the grouping () I guess).

r'((?=.*[a-zA-Z])(?=.+[\.]).+)[\(\+\-]+'

I highly appreciate your comments.

Regards, christian


I'm not sure about your regex, but I'd probably use this:

r'[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*'


Perfect, thanks. I changed the last * to a + so that the string must 
contain a '.' to match.


Christian

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


Re: Turbogears 2.1 with mako tmplates

2011-02-07 Thread Alice Bevan–McGregor

On 2011-02-07 07:13:43 -0800, Vineet Deodhar said:


For web-based solutions, I have started learning TG 2.1
By and large, the documentation on TG 2.1 official site is a work-in-process.
As regards to the templates, it tells how to go about Genshi.
I wish to go with mako.
Gone through the docs of mako (they are good).
But still, it would be better if I can get a tutorial explaining using 
TG 2.1 with mako.
(It will save me from re-inventing the wheel, if somebody has written 
docs on these lines).

Can anybody point to any such tutorial?
(I googled, but couldn't locate any convincing result).


I believe you will have signifigantly greater success having TurboGears 
questions answered when asked on the appropriate forum:


http://groups.google.com/group/turbogears

— Alice.


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


Universal Feed Browser problem in feedparser.py

2011-02-07 Thread Virgil Stokes
I am running Python 2.6 on a Windows Vista (32-bit) platform. I recently 
installed the Universal Feed Parser package (feedparser-5-0). When I try to 
execute the following commands:


>>> import feedparser
>>> d = feedparser.parse("http://feedparser.org/docs/examples/atom10.xml";)

which is given at http://www.feedparser.org/

I get an Assertion error in the following function in feedparser.py


def __getattr__(self, key):
try:
  return self.__dict__[key]
except KeyError:
  pass
try:
  assert not key.startswith('_') <---  Error occurs when this 
statement is executed.

  return self.__getitem__(key)
except:
  raise AttributeError, "object has no attribute '%s'" % key


Why does the error occur and how should he be corrected?

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


Re: Fully functioning email client in Python

2011-02-07 Thread John Nagle

On 2/5/2011 5:34 PM, iaoua iaoua wrote:


What I really need is a ready made fully functional Python client that
already has it that offers me a simple interface to extract text from
received messages along with a message id that I can use to associate
the response to a question with and allows me to send out new
questions or answers along with a message id I can use to track future
responses with. I need something that already has the whole shebang.
Can support POP3, IMAP, SSL authentication, message threading etc.


   It sounds like you need an SMTP receiving server and an SMTP sender,
not a user-type client.  See

http://docs.python.org/library/smtpd.html

for a simple server that will handle incoming messages, and

http://docs.python.org/library/smtplib.html 

for an SMTP sender that can reply to them.  Those are standard
Python functions.

You can run this on most server-type machines.  It's best
if you have an upstream server configured to do spam filtering
and forwarding to your server, or you'll get vast amounts of
junk coming into your system.

Have the upstream machine forward to a nonstandard mail
port (like "example.com:2525") if the server machine already
has a mail server. This will allow the existing mail server to
run.

John Nagle


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


Re: Remove whitespaces and line breaks in a XML file

2011-02-07 Thread Stefan Behnel

David Vicente, 07.02.2011 18:45:

I´m parsing an xml file with xml.etree. It works correctly, but I have a
problem with the text attribute of the elements which should be empty. For
example, in this case:



 Ken





The text element of “book” should be empty, but it returns me some
whitespaces and break lines. I can´t get remove these whitespaces without
remove information.


Only a DTD (or schema) can provide the information which whitespace in an 
XML document is meaningful and which isn't, so there is no generic way to 
"do it right", especially not for something as generic as an XML parser.


What may work for you is to check if an Element has children and only 
whitespace as text ("not el.text or not el.text.strip()"), and only then 
replace it by None.


Stefan

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


Re: splitting by double newline

2011-02-07 Thread Nikola Skoric
Dana Mon, 07 Feb 2011 19:20:38 +0100, 
Peter Otten <__pete...@web.de> kaze:
> with open(filename, "U") as f:

Oh, fabulous. Thanks!

-- 
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Universal Feed Browser problem in feedparser.py

2011-02-07 Thread Miki Tebeka
>  >>> import feedparser
>  >>> d = feedparser.parse("http://feedparser.org/docs/examples/atom10.xml";)
This works for me, are you sure it's not a network problem in your side?
(what happens if you try to open this link in a browser?)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: splitting by double newline

2011-02-07 Thread Nikola Skoric
Dana Mon, 7 Feb 2011 10:02:05 -0800 (PST), 
Ian  kaze:
> self.tables = re.split(r'(?:\r\n){2,}|\r{2,}|\n{2,}', bulk)

Thanks!

I tried without "?:", but it didn't work. Can you tell me why is it
important that group is noncapturing?


-- 
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
-- 
http://mail.python.org/mailman/listinfo/python-list


PIL Open Problem

2011-02-07 Thread Richard Holmes
I'm trying to create an image for use in Tkinter. If I understand the
PIL documentation correctly, I first need to import Image, then
create an instance of the Image class and call 'open' (this according
to the documentation). When I try to do this using the model in the
documentation, I get:

Traceback (most recent call last):
  File "todo.py", line 202, in 
im = Image.open(cwd + r'\delete.GIF', 'r')
AttributeError: class Image has no attribute 'open'

where 'cwd' is the current working directory obtained using the os
module. When I import Image into IDLE and enter help(Image.open), IDLE
is quite happy to give me the info about the function.

Can someone point out to me the folly of my ways?

TIA!

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


Why not "list = ftplib.FTP.dir()" ?

2011-02-07 Thread trylks
I don't know if it is ftplib or just me, but something feels terribly wrong
in this:

from ftplib import FTP
> from functools import partial
>
> class MyFTP(FTP):
>   def dir(self):
> l = []
> super(MyFTP, self).dir(partial(lambda l, e: l.append(e.split()), l))
> return l
>

Unfortunately 'MLSD' is not available in the server I have to use. (I need
to get the list of folders and files modified since last access, hence
split).

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


Re: PIL Open Problem

2011-02-07 Thread Corey Richardson
On 02/07/2011 05:27 PM, Richard Holmes wrote:
> I'm trying to create an image for use in Tkinter. If I understand the
> PIL documentation correctly, I first need to import Image, then
> create an instance of the Image class and call 'open'

Don't do that. This is wrong:

import Image
im = Image.Image()
im = im.open(foo)

This is good:

import Image
im = Image.open(foo)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why not "list = ftplib.FTP.dir()" ?

2011-02-07 Thread Ian Kelly
On Mon, Feb 7, 2011 at 3:26 PM, trylks  wrote:
> I don't know if it is ftplib or just me, but something feels terribly wrong
> in this:
>
>> from ftplib import FTP
>> from functools import partial
>>
>> class MyFTP(FTP):
>>   def dir(self):
>>     l = []
>>     super(MyFTP, self).dir(partial(lambda l, e: l.append(e.split()), l))
>>     return l

No need to use partial here.  You can simplify that to:

super(MyFTP, self).dir(lambda e: l.append(e.split()))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL Open Problem

2011-02-07 Thread Ben Finney
Richard Holmes  writes:

> I'm trying to create an image for use in Tkinter. If I understand the
> PIL documentation correctly, I first need to import Image, then
> create an instance of the Image class and call 'open' (this according
> to the documentation). When I try to do this using the model in the
> documentation, I get:

Please show the code you used that generated this traceback.

> Traceback (most recent call last):
>   File "todo.py", line 202, in 
> im = Image.open(cwd + r'\delete.GIF', 'r')
> AttributeError: class Image has no attribute 'open'

The line of code shown in the traceback is calling the ‘open’ method on
the ‘Image’ class – which has no such method, as the error tells you.

> Can someone point out to me the folly of my ways?

It will be much easier when we see some code. Post a minimal, working
example that still shows the behaviour you want explained.

-- 
 \   “If you make people think they're thinking, they'll love you; |
  `\ but if you really make them think, they'll hate you.” —Donald |
_o__) Robert Perry Marquis |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: splitting by double newline

2011-02-07 Thread MRAB

On 07/02/2011 21:15, Nikola Skoric wrote:

Dana Mon, 7 Feb 2011 10:02:05 -0800 (PST),
Ian  kaze:

self.tables = re.split(r'(?:\r\n){2,}|\r{2,}|\n{2,}', bulk)


Thanks!

I tried without "?:", but it didn't work. Can you tell me why is it
important that group is noncapturing?


The scanner uses capture groups to identify which part matched. If you
provide additional capture groups it has problems...
--
http://mail.python.org/mailman/listinfo/python-list


Re: PIL Open Problem

2011-02-07 Thread Richard Holmes
On Mon, 07 Feb 2011 17:28:45 -0500, Corey Richardson 
wrote:

>On 02/07/2011 05:27 PM, Richard Holmes wrote:
>> I'm trying to create an image for use in Tkinter. If I understand the
>> PIL documentation correctly, I first need to import Image, then
>> create an instance of the Image class and call 'open'
>
>Don't do that. This is wrong:
>
>import Image
>im = Image.Image()
>im = im.open(foo)
>
>This is good:
>
>import Image
>im = Image.open(foo)
Uh, thanks, Corey, but that's what I'm doing. See Traceback:

Traceback (most recent call last):
  File "todo.py", line 202, in 
im = Image.open(cwd + r'\delete.GIF', 'r')
AttributeError: class Image has no attribute 'open'

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


Re: PIL Open Problem

2011-02-07 Thread Ben Finney
Richard Holmes  writes:

> On Mon, 07 Feb 2011 17:28:45 -0500, Corey Richardson 
> wrote:
> >This is good:
> >
> >import Image
> >im = Image.open(foo)
> Uh, thanks, Corey, but that's what I'm doing. See Traceback:

Without code, we can't see what you're doing.

Please post a minimal working example that demonstrates the behaviour
you want explained.

-- 
 \   “Always do right. This will gratify some people, and astonish |
  `\the rest.” —Mark Twain |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL Open Problem

2011-02-07 Thread Richard Holmes
On Tue, 08 Feb 2011 09:47:32 +1100, Ben Finney
 wrote:

>Richard Holmes  writes:
>
>> I'm trying to create an image for use in Tkinter. If I understand the
>> PIL documentation correctly, I first need to import Image, then
>> create an instance of the Image class and call 'open' (this according
>> to the documentation). When I try to do this using the model in the
>> documentation, I get:
>
>Please show the code you used that generated this traceback.
>
>> Traceback (most recent call last):
>>   File "todo.py", line 202, in 
>> im = Image.open(cwd + r'\delete.GIF', 'r')
>> AttributeError: class Image has no attribute 'open'
>
>The line of code shown in the traceback is calling the ‘open’ method on
>the ‘Image’ class – which has no such method, as the error tells you.
>
>> Can someone point out to me the folly of my ways?
>
>It will be much easier when we see some code. Post a minimal, working
>example that still shows the behaviour you want explained.

Thanks, Ben. It turns out that I imported both Image and Tkinter and
Tkinter has an Image class that masked the Image class in the Image
module. I solved the problem by moving the Image code to a separate
module

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


Re: socket.rcv timeout while-loop

2011-02-07 Thread Dwayne Blind
Dear Stephen,

Thanks for telling me this all looked very peculiar. As you said, it did not
really need solving.

Cheers,
Dwayne


2011/2/4 Stephen Hansen 

> On 2/4/11 9:16 AM, Dwayne Blind wrote:
> > @ Stephen Hansen
> > Now I am pretty much worried. :'(
>
> Why? This is all sounding like a problem that isn't actually a problem.
>
> I think you may have over-analyzed yourself into a corner and think you
> have something to solve which doesn't really need solving. :)
>
> --
>
>   Stephen Hansen
>   ... Also: Ixokai
>   ... Mail: me+list/python (AT) ixokai (DOT) io
>   ... Blog: http://meh.ixokai.io/
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Remove whitespaces and line breaks in a XML file

2011-02-07 Thread Josh English
I found the code posted at 

http://infix.se/2007/02/06/gentlemen-indent-your-xml

quite helpful in turning my xml into human-readable structures. It works
best for XML-Data.

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


Re: PIL Open Problem

2011-02-07 Thread rantingrick
On Feb 7, 5:35 pm, Richard Holmes  wrote:

> Thanks, Ben. It turns out that I imported both Image and Tkinter and
> Tkinter has an Image class that masked the Image class in the Image
> module. I solved the problem by moving the Image code to a separate
> module

Yes an another great example of why "from Tkinter import *" is a very
*very* bad idea. Use "import Tkinter as tk" to solve the dilemma. No
need to export code to another module. Next time you have an object
that should have an attribute but does not, print the repr() of the
object to find out what you are working with before clawing your
eyeballs out in frustration :).


>>> from Tkinter import *
>>> Image



use the repr() function in a script ... print repr(Image)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL Open Problem

2011-02-07 Thread Ben Finney
Richard Holmes  writes:

> Thanks, Ben. It turns out that I imported both Image and Tkinter and
> Tkinter has an Image class that masked the Image class in the Image
> module. I solved the problem by moving the Image code to a separate
> module

This is a classic problem known as “namespace clobbering”.

It is best to *avoid* the recommendations made in many libraries of
‘from foo import *’, because that will clobber any names in your
namespace that happen to match names in the ‘foo’ module.

Rather, import Tkinter and PIL as distinct namespaces::

>>> import PIL.Image
>>> import Tkinter as tk
>>> tk.Image

>>> PIL.Image
>> '/usr/lib/python2.6/dist-packages/PIL/Image.pyc'>

and then you know that none of the names from those modules will clobber
existing ones.

-- 
 \  “Those who write software only for pay should go hurt some |
  `\ other field.” —Erik Naggum, in _gnu.misc.discuss_ |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why not "list = ftplib.FTP.dir()" ?

2011-02-07 Thread rantingrick
On Feb 7, 4:48 pm, Ian Kelly  wrote:
> On Mon, Feb 7, 2011 at 3:26 PM, trylks  wrote:
> > I don't know if it is ftplib or just me, but something feels terribly wrong
> > in this:
>
> >> from ftplib import FTP
> >> from functools import partial
>
> >> class MyFTP(FTP):
> >>   def dir(self):
> >>     l = []
> >>     super(MyFTP, self).dir(partial(lambda l, e: l.append(e.split()), l))
> >>     return l
>
> No need to use partial here.  You can simplify that to:
>
> super(MyFTP, self).dir(lambda e: l.append(e.split()))

Why not...?

MyFTP.dir(self, lambda e: l.append(e.split()))

...super is not needed when you "know" the name of the object. And now
(without super) you can "see" the path of self just as Guido intended!
super can be super confusing to noobs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL Open Problem

2011-02-07 Thread Westley Martínez
On Mon, 2011-02-07 at 15:54 -0800, rantingrick wrote:

> On Feb 7, 5:35 pm, Richard Holmes  wrote:
> 
> > Thanks, Ben. It turns out that I imported both Image and Tkinter and
> > Tkinter has an Image class that masked the Image class in the Image
> > module. I solved the problem by moving the Image code to a separate
> > module
> 
> Yes an another great example of why "from Tkinter import *" is a very
> *very* bad idea. Use "import Tkinter as tk" to solve the dilemma. No
> need to export code to another module. Next time you have an object
> that should have an attribute but does not, print the repr() of the
> object to find out what you are working with before clawing your
> eyeballs out in frustration :).
> 
> 
> >>> from Tkinter import *
> >>> Image
> 
> 
> 
> use the repr() function in a script ... print repr(Image)

Or just print(Image). print() automatically calls str() on an object.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL Open Problem

2011-02-07 Thread Richard Holmes
On Tue, 08 Feb 2011 10:56:52 +1100, Ben Finney
 wrote:

>Richard Holmes  writes:
>
>> Thanks, Ben. It turns out that I imported both Image and Tkinter and
>> Tkinter has an Image class that masked the Image class in the Image
>> module. I solved the problem by moving the Image code to a separate
>> module
>
>This is a classic problem known as “namespace clobbering”.
>
>It is best to *avoid* the recommendations made in many libraries of
>‘from foo import *’, because that will clobber any names in your
>namespace that happen to match names in the ‘foo’ module.
>
>Rather, import Tkinter and PIL as distinct namespaces::
>
>>>> import PIL.Image
>>>> import Tkinter as tk
>>>> tk.Image
>
>>>> PIL.Image
>>>> '/usr/lib/python2.6/dist-packages/PIL/Image.pyc'>
>
>and then you know that none of the names from those modules will clobber
>existing ones.

Thanks. Message understood.
-- 
http://mail.python.org/mailman/listinfo/python-list


smtpd bug?

2011-02-07 Thread Ben
I've been using smtpd.py to implement a kind of cowboy SMTP server
(only overriding process_message), and inevitably after a certain time
the server stops accepting incoming connections: the socket on which
it was formerly listening gets closed.

I ran it using strace and discovered that it would get ENOTCONN on
some just-accepted socket in a call to getpeername(), and then close
the *listening* socket.

This seems to be caused by the line
   channel = SMTPChannel(self, conn, addr)
in smtpd.py and handle_error() in the definition of
asyncore.dispatcher, which finishes by calling self.handle_close()
[1]. The result of this is that when the call to getpeername() in the
constructor for SMTPChannel raises ENOTCONN (or any other exception),
it unwinds via asyncore.dispatcher.handle_read_event(), to
asyncore.read(), where the SMTPServer instance gets its handle_error
method called on it, eventually closing its listening socket. But the
error was in the socket we just accepted---not the listening socket.

[1] I'm actually using python2.4, since that's what's installed on the
server; there, the handle_error() method simply finishes by calling
self.close(). In fact, AFAICT, there's another problem in 3.1: neither
asyncore.dispatcher nor smtpd.SMTPServer (nor any of its subclasses in
smtpd.py) defines a method handle_close(); asyncore.dispatcher passes
attribute lookups to its socket object via __getattr__, but sockets
don't have a handle_close method either---so it seems as if, if
handle_close() is ever called, it'll just generate a new error.
-- 
http://mail.python.org/mailman/listinfo/python-list