Saturday, March 27, 2010

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

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

comp.lang.python@googlegroups.com

Today's topics:

* Advanced Python Programming Oxford Lectures [was: Re: *Advanced* Python book?
] - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/97229baae56563eb?hl=en
* Hello,everybody,the good shoping place,the new year approaching, click in.
Let's facelift bar! ===== HTTP://loveshopping.us ==== - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/73319c713ef17ead?hl=en
* sum for sequences? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/139c0887a359405b?hl=en
* Traversing through Dir() - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/e9c305645b4d3046?hl=en
* Any examples/documentation for python-newt - 5 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/457923039e0483d3?hl=en
* Classes as namespaces? - 7 messages, 6 authors
http://groups.google.com/group/comp.lang.python/t/ddaed9721e7bbf45?hl=en
* Automatic import ? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/5981f9a71fd4cd21?hl=en
* Simple Traits Questions - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/4f14032659afa297?hl=en
* Simple Traits - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/979f25e0901457ae?hl=en
* Call for papers (Deadline Extended): SETP-10, USA, July 2010 - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/1e7a981a05790cc5?hl=en
* GIF89A and PIL - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/ab1ab88ffdc57736?hl=en
* function decorator-like function - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/fe1b732c2b2ef890?hl=en
* Ipython(x,y) Won't Run - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/ccf4c75b20f96027?hl=en

==============================================================================
TOPIC: Advanced Python Programming Oxford Lectures [was: Re: *Advanced* Python
book?]
http://groups.google.com/group/comp.lang.python/t/97229baae56563eb?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Mar 26 2010 11:10 pm
From: Lacrima


On Mar 26, 10:22 am, Michele Simionato <michele.simion...@gmail.com>
wrote:
> On Mar 25, 2:24 pm, Michele Simionato <michele.simion...@gmail.com>
> wrote:
>
> > On Mar 25, 1:28 pm, Ethan Furman <et...@stoneleaf.us> wrote:
>
> > > Michele,
>
> > > Was wondering if you'd had a chance to re-post your lectures -- just did
> > > a search for them and came up empty, and I would love to read them!
>
> > > Many thanks in advance!
>
> > Oops, I forgot! I will try to make them available soon.
>
> Here they are:http://www.phyast.pitt.edu/~micheles/oxford-lectures.zip

Hello, Michele!
Thanks a lot for posting!!!

==============================================================================
TOPIC: Hello,everybody,the good shoping place,the new year approaching, click
in. Let's facelift bar! ===== HTTP://loveshopping.us ====
http://groups.google.com/group/comp.lang.python/t/73319c713ef17ead?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 12:53 am
From: marrylin


Hello,everybody,the good shoping place,the new year approaching, click
in. Let's facelift bar!
===== HTTP://loveshopping.us ====

Air jordan(1-24)shoes $33

UGG BOOT $50

Nike shox(R4,NZ,OZ,TL1,TL2,TL3) $35

Handbags(Coach lv fendi d&g) $35

Tshirts (Polo ,ed hardy,lacoste) $16

Jean(True Religion,ed hardy,coogi) $30

Sunglasses(Oakey,coach,gucci,Armaini) $16

New era cap $15

Bikini (Ed hardy,polo) $25

FREE SHIPPING

==============================================================================
TOPIC: sum for sequences?
http://groups.google.com/group/comp.lang.python/t/139c0887a359405b?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 3:18 am
From: Steven D'Aprano


On Fri, 26 Mar 2010 07:31:17 -0700, Steve Howell wrote:

> From a purely academic standpoint, I'm not convinced that sum() is
> inefficient in terms of big-O complexity, though.
>
> showell@showell-laptop:~$ python
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on
> linux2
> >>> class StupidList:
[...]

But it's not *sum* that is inefficient, it is sum *with a particular data
structure*.

Sure, if you create your own data structure, you can make it as efficient
as you like. Obviously the sum algorithm itself has to perform one
addition per item, or O(N), which scales tolerably well. But each
addition has a cost. If the cost is constant, then sum() as a whole
remains O(N). But if the cost of addition varies with N, sum() degrades
badly.

We can compare the performance of sum with different data structures,
starting with plain integers versus long integers:

>>> from timeit import Timer
>>> setup = 'data = [%d]*%d'
>>> for i in range(6):
... t1 = Timer('sum(data, 0)', setup % (1, 10**i))
... t2 = Timer('sum(data, 0)', setup % (10**50, 10**i))
... print min(t1.repeat(number=1000)),
... print min(t2.repeat(number=1000))
...
0.00179290771484 0.00263810157776
0.00340414047241 0.00854396820068
0.0190401077271 0.0502791404724
0.155302047729 0.645124912262
0.794432878494 2.55748295784
7.97877693176 25.3812758923

Both scale about as well, but the cost varies significantly: arithmetic
on very large longints is expensive.

Strings, with a trick to fool sum into accepting them, versus lists. Note
that I changed the number of iterations from 6 down to 5. The reason why
will become obvious:

>>> class EmptyStringStarter:
... def __add__(self, other):
... return other
...
>>> empty = EmptyStringStarter()
>>> setup = """from __main__ import empty; data = [%r]*%d"""
>>>
>>> for i in range(5):
... t1 = Timer('sum(data, empty)', setup % ('a', 10**i))
... t2 = Timer('sum(data, [])', setup % ([1], 10**i))
... print min(t1.repeat(number=1000)),
... print min(t2.repeat(number=1000))
...
0.00849103927612 0.00226998329163
0.0121459960938 0.0082700252533
0.0489149093628 0.186735153198
0.428920030594 5.28623914719
14.6552250385 589.102822065


Strings perform tolerably well, up to a point, but lists perform
terribly. And in fact, the relatively good performance of strings is an
artifact of recent versions of CPython. In Jython and IronPython, and
older versions of CPython, it will behave as poorly as lists.


> I wonder how severely sum(), without
> the restriction, would underperform join() on modern versions of Python,
> though.


Take note that, in order to get an answer in reasonable time, I've
reduced the number of timing iterations drastically:

>>> for i in range(6):
... t1 = Timer('sum(data, empty)', setup % ('a', 10**i))
... t2 = Timer('"".join(data)', setup % ('a', 10**i))
... print min(t1.repeat(number=10)),
... print min(t2.repeat(number=10))
...
8.89301300049e-05 1.09672546387e-05
0.000131845474243 2.19345092773e-05
0.000591993331909 9.29832458496e-05
0.0101289749146 0.00082802772522
0.365957021713 0.00884819030762
24.2072279453 0.0421011447906

Note the performance degradation of sum. It gets worse. Much worse:

>>> for i in range(4, 7):
... t1 = Timer('sum(data, empty)', setup % ('a', 10**i))
... t2 = Timer('"".join(data)', setup % ('a', 10**i))
... print min(t1.repeat(number=1)), # note fewer iterations
... print min(t2.repeat(number=1))
...
0.031229019165 0.000817060470581
2.45445990562 0.00365781784058
1024.79705095 0.0398509502411

This is absolutely catastrophic performance degradation.

--
Steven

==============================================================================
TOPIC: Traversing through Dir()
http://groups.google.com/group/comp.lang.python/t/e9c305645b4d3046?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 3:18 am
From: Dave Angel


Andrej Mitrovic wrote:
> On Mar 26, 9:18 am, "Alf P. Steinbach" <al...@start.no> wrote:
>
>> <snip>
>> hierarchy =nspect.getclasstree( classes )
>> # 'hierarchy' is a list array of tuples and nested list arrays of the same form.
>> # The top level is an array of two items, the first item a tuple describing the
>> 'object'
>> # class, and the second item a list array representing the BaseException hierarchy.
>> print_hierarchy( hierarchy[1], level = )
>> </code>
>>
>> Cheers & hth.,
>>
>> - Alf
>>
>
> Thanks for all of that. And yes, I've noticed I get into infinite
> recursions all the time, which is why I was asking if there was a
> simple way to do this. I'll have a look at these later.
>
> Kind regards,
> Andrej Mitrovic
>
>
I can't help with the original question, but generally the cure for
getting into infinite recursion when traversing a tree with multiple
connections is to keep a set() of all visited nodes. Whenever you hit a
node a second time, don't visit it or its dependents. It's not hard to
add to otherwise working code, and frequently the easiest place is right
at the beginning of the recursive function.
if new_node in myset:
return
myset.add(new_node)
...process_this_node...
for child in new_node:
...recurse...

HTH
DaveA


==============================================================================
TOPIC: Any examples/documentation for python-newt
http://groups.google.com/group/comp.lang.python/t/457923039e0483d3?hl=en
==============================================================================

== 1 of 5 ==
Date: Sat, Mar 27 2010 4:06 am
From: Harishankar


I am writing a fairly featureful TUI in python and I figured newt is the
best lightweight TUI which actually offers widgets. curses is low level
and its text editing capabilities are poor while UIs like urwid and stfl
too complex for simple programs like the ones I am creating.

Could anybody point me to at least a decent example program in newt/snack
and I don't mean popcorn.py or peanuts.py. Something that at least shows
how to create a basic menu-driven program in the command line with a main
menu and implementing several sub menus. Also a bit of explanation of the
behaviour of OK and Cancel buttons in standard forms would be of help.

So anybody uses newt? Or have people stopped using text based UIs
completely?

--
-- Harishankar (http://harishankar.org http://literaryforums.org)

--
-- Harishankar (http://harishankar.org http://literaryforums.org)


== 2 of 5 ==
Date: Sat, Mar 27 2010 7:44 am
From: Grant Edwards


On 2010-03-27, Harishankar <v.harishankar@gmail.com> wrote:
> I am writing a fairly featureful TUI in python and I figured newt is the
> best lightweight TUI which actually offers widgets. curses is low level
> and its text editing capabilities are poor while UIs like urwid and stfl
> too complex for simple programs like the ones I am creating.
>
> Could anybody point me to at least a decent example program in newt/snack
> and I don't mean popcorn.py or peanuts.py. Something that at least shows
> how to create a basic menu-driven program in the command line with a main
> menu and implementing several sub menus. Also a bit of explanation of the
> behaviour of OK and Cancel buttons in standard forms would be of help.
>
> So anybody uses newt? Or have people stopped using text based UIs
> completely?

I've used it for some trivial stuff, but not for anything very
sophisticated. Redhat's text-mode installer program (anaconda) was
written in Python using Newt (I believe it is why Newt was invented).
That's probably the most "full featured" example I know of.

--
Grant


== 3 of 5 ==
Date: Sat, Mar 27 2010 7:49 am
From: Harishankar


On Sat, 27 Mar 2010 14:44:23 +0000, Grant Edwards wrote:

> On 2010-03-27, Harishankar <v.harishankar@gmail.com> wrote:
>> I am writing a fairly featureful TUI in python and I figured newt is
>> the best lightweight TUI which actually offers widgets. curses is low
>> level and its text editing capabilities are poor while UIs like urwid
>> and stfl too complex for simple programs like the ones I am creating.
>>
>> Could anybody point me to at least a decent example program in
>> newt/snack and I don't mean popcorn.py or peanuts.py. Something that at
>> least shows how to create a basic menu-driven program in the command
>> line with a main menu and implementing several sub menus. Also a bit of
>> explanation of the behaviour of OK and Cancel buttons in standard forms
>> would be of help.
>>
>> So anybody uses newt? Or have people stopped using text based UIs
>> completely?
>
> I've used it for some trivial stuff, but not for anything very
> sophisticated. Redhat's text-mode installer program (anaconda) was
> written in Python using Newt (I believe it is why Newt was invented).
> That's probably the most "full featured" example I know of.

Does your code use forms in an application loop? I am having a bit of
trouble with getting the form to stack up properly when I displaying
another. As far as I know the two examples uses a single instance of a
form which is discarded immediately on exit.

If you have even a small code sample I wouldn't mind reading it!

--
-- Harishankar (http://harishankar.org http://literaryforums.org)


== 4 of 5 ==
Date: Sat, Mar 27 2010 7:56 am
From: Grant Edwards


On 2010-03-27, Harishankar <v.harishankar@gmail.com> wrote:

>> I've used it for some trivial stuff, but not for anything very
>> sophisticated. Redhat's text-mode installer program (anaconda) was
>> written in Python using Newt (I believe it is why Newt was invented).
>> That's probably the most "full featured" example I know of.
>
> Does your code use forms in an application loop?

Not really. It uses a series of forms. I don't think I ever stack
forms.

> I am having a bit of trouble with getting the form to stack up
> properly when I displaying another. As far as I know the two examples
> uses a single instance of a form which is discarded immediately on
> exit.

That's pretty much all my program did.

> If you have even a small code sample I wouldn't mind reading it!

I'm afraid I'm not at liberty to post it.

--
Grant

== 5 of 5 ==
Date: Sat, Mar 27 2010 8:12 am
From: Harishankar


On Sat, 27 Mar 2010 14:56:30 +0000, Grant Edwards wrote:

> On 2010-03-27, Harishankar <v.harishankar@gmail.com> wrote:
>
>>> I've used it for some trivial stuff, but not for anything very
>>> sophisticated. Redhat's text-mode installer program (anaconda) was
>>> written in Python using Newt (I believe it is why Newt was invented).
>>> That's probably the most "full featured" example I know of.
>>
>> Does your code use forms in an application loop?
>
> Not really. It uses a series of forms. I don't think I ever stack
> forms.
>
>> I am having a bit of trouble with getting the form to stack up properly
>> when I displaying another. As far as I know the two examples uses a
>> single instance of a form which is discarded immediately on exit.
>
> That's pretty much all my program did.
>
>> If you have even a small code sample I wouldn't mind reading it!
>
> I'm afraid I'm not at liberty to post it.

Thanks anyway.

I'm taking a look at text.py in the anaconda source code tree here:
http://git.fedorahosted.org/git/anaconda.git?
p=anaconda.git;a=blob_plain;f=text.py;hb=HEAD

I'm afraid it looks as though newt was primarily built to display a
series of forms in order, like an installer does, so it is inherently
limited by that interface. However, it might still be what I can use...

--
Harishankar (http://harishankar.org http://literaryforums.org)

==============================================================================
TOPIC: Classes as namespaces?
http://groups.google.com/group/comp.lang.python/t/ddaed9721e7bbf45?hl=en
==============================================================================

== 1 of 7 ==
Date: Sat, Mar 27 2010 4:28 am
From: Jonathan Hartley


On Mar 26, 6:26 pm, Luis M. González <luis...@gmail.com> wrote:
> On 26 mar, 11:49, kj <no.em...@please.post> wrote:
>
> > What's the word on using "classes as namespaces"?  E.g.
>
> > class _cfg(object):
> >     spam = 1
> >     jambon = 3
> >     huevos = 2
>
> > breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>
> I see no problem.
> I wouldn't mix English, French and Spanish in the same recipe though...


Hey everyone. By coincidence, only yesterday I was wondering about
using classes as a way of labeling a block of code, ie. an lightweight
alternative to defining a function that would only be called from one
location.

eg. instead of:


x = 1
((some complex logic))
y = 2


one might like to name the complex block of logic, just to make it
readable:


x = 1
def account_for_non_square_pixels(x):
((some complex logic))
account_for_non_square_pixels()
y = 2


But defining and then calling the function like that is a tad
cumbersome. So I was wondering about:

x = 1
class account_for_non_square_pixels:
((some complex logic))
y = 2


I don't exactly like this, but I think you can see what I'm getting
at. Does this fall down in some way I haven't grasped? Is it as awful
an abuse of 'class' as my intuition suggests it is? Is there a way to
do it better?


== 2 of 7 ==
Date: Sat, Mar 27 2010 4:54 am
From: Jean-Michel Pichavant


Jonathan Hartley wrote:
> On Mar 26, 6:26 pm, Luis M. González <luis...@gmail.com> wrote:
>
>> On 26 mar, 11:49, kj <no.em...@please.post> wrote:
>>
>>
>>> What's the word on using "classes as namespaces"? E.g.
>>>
>>> class _cfg(object):
>>> spam = 1
>>> jambon = 3
>>> huevos = 2
>>>
>>> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>>>
>> I see no problem.
>> I wouldn't mix English, French and Spanish in the same recipe though...
>>
>
>
> Hey everyone. By coincidence, only yesterday I was wondering about
> using classes as a way of labeling a block of code, ie. an lightweight
> alternative to defining a function that would only be called from one
> location.
>
> eg. instead of:
>
>
> x = 1
> ((some complex logic))
> y = 2
>
>
> one might like to name the complex block of logic, just to make it
> readable:
>
>
> x = 1
> def account_for_non_square_pixels(x):
> ((some complex logic))
> account_for_non_square_pixels()
> y = 2
>
>
> But defining and then calling the function like that is a tad
> cumbersome. So I was wondering about:
>
>
>
> x = 1
> class account_for_non_square_pixels:
> ((some complex logic))
> y = 2
>
>
> I don't exactly like this, but I think you can see what I'm getting
> at. Does this fall down in some way I haven't grasped? Is it as awful
> an abuse of 'class' as my intuition suggests it is? Is there a way to
> do it better?
>
on good way to label part of the code is to simply add comments. You can
also find tricks to indent this code block, but I've never seen that before.

x=1
# account for non square pixels
some complex logic
# done
y=2

I'm perfectly comfortable using classes for namespaces, 'cause classes
implement objects or entities, and a namespaces can easily be designed
as a coherent entity.
For labelling code that you will not reuse, I'm not sure classes are
suitable in the way people may issue a 'WTF' when reading your code.

JM


== 3 of 7 ==
Date: Sat, Mar 27 2010 4:54 am
From: "J. Clifford Dyer"


On Sat, Mar 27, 2010 at 04:28:56AM -0700, Jonathan Hartley wrote regarding Re: Classes as namespaces?:
>
> Hey everyone. By coincidence, only yesterday I was wondering about
> using classes as a way of labeling a block of code, ie. an lightweight
> alternative to defining a function that would only be called from one
> location.
>
> eg. instead of:
>
>
> x = 1
> ((some complex logic))
> y = 2
>
>
> one might like to name the complex block of logic, just to make it
> readable:
>
>
> x = 1
> def account_for_non_square_pixels(x):
> ((some complex logic))
> account_for_non_square_pixels()
> y = 2
>
>
> But defining and then calling the function like that is a tad
> cumbersome. So I was wondering about:
>
>
>
> x = 1
> class account_for_non_square_pixels:
> ((some complex logic))
> y = 2
>
>
> I don't exactly like this, but I think you can see what I'm getting
> at. Does this fall down in some way I haven't grasped? Is it as awful
> an abuse of 'class' as my intuition suggests it is? Is there a way to
> do it better?
> --
> http://mail.python.org/mailman/listinfo/python-list

Hmm. I don't like that because it leaves a class polluting your namespace that doesn't behave like a class. Using a function for that purpose doesn't seem as bad, because even if you don't call it again, at least you *could*, and it would behave in an expected fashion.

If you're dead-set against calling the chunk of code you just created, and you're using python 2.5 or higher, you might consider creating a no-op context manager:

x = 1
with code_block("Account for non square pixels"):
((complex_logic))
y = 2

Though in general, I think refactoring your code to reasonably scoped functions or methods is a better idea. If it's too complex to read in one block, it's probably too complex for one function.


== 4 of 7 ==
Date: Sat, Mar 27 2010 5:04 am
From: "J. Clifford Dyer"


On Fri, Mar 26, 2010 at 02:49:02PM +0000, kj wrote regarding Classes as namespaces?:
>
> What's the word on using "classes as namespaces"? E.g.
>
> class _cfg(object):
> spam = 1
> jambon = 3
> huevos = 2
>
> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>
>
> Granted, this is not the "intended use" for classes, and therefore
> could be viewed as a misuse ("that's what dictionaries are for",
> etc.). But other than this somewhat academic objection[*], I really
> can see no problem with using classes in this way.
>
> And yet, I've come across online murky warnings against using
> classes as "pseudo-namespaces". Is there some problem that I'm
> not seeing with this technique?
>
> ~K

I don't see anything wrong with this, except that I would clean it up in a couple ways. Like other posters, I would give the class a proper class name (Cfg).

I also would not assign integers to spam, jambon, or huevos. Instead I would assign each a bare object(). That way you won't get unexpected interactions with other constants outside the class. An object() is equal only to itself.

I would also not rule out letting your "pseudo-namespace" grow into a full-fledged class. If you've got a method that makes sense with your class, use it.

class Cfg(object):
spam = object()
jambon = object()
huevos = object()

def get_animal(self, meat):
if meat == self.jambon:
return 'pig'
elif meat == self.huevos:
return 'chicken'
elif meat = self.spam:
return 'spamalope'

Later, perhaps, you might refactor so that each meat type (OK so huevos aren't a meat) gets its own subclass, with a simple, one-line get_animal method.

Cheers,
Cliff


== 5 of 7 ==
Date: Sat, Mar 27 2010 5:22 am
From: Steve Holden


J. Clifford Dyer wrote:
> On Fri, Mar 26, 2010 at 02:49:02PM +0000, kj wrote regarding Classes
> as namespaces?:
>> What's the word on using "classes as namespaces"? E.g.
>>
>> class _cfg(object): spam = 1 jambon = 3 huevos = 2
>>
>> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>>
[...]
> I also would not assign integers to spam, jambon, or huevos. Instead
> I would assign each a bare object(). That way you won't get
> unexpected interactions with other constants outside the class. An
> object() is equal only to itself.
>
It also has the advantage (?) that you can use "is" (identity)
comparisons rather than testing for equality, though this is only a
readability issue, I suspect.

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/

== 6 of 7 ==
Date: Sat, Mar 27 2010 6:26 am
From: Philip Semanchuk

On Mar 27, 2010, at 8:04 AM, J. Clifford Dyer wrote:

> On Fri, Mar 26, 2010 at 02:49:02PM +0000, kj wrote regarding Classes
> as namespaces?:
>>
>> What's the word on using "classes as namespaces"? E.g.
>>
>> class _cfg(object):
>> spam = 1
>> jambon = 3
>> huevos = 2
>>
>> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>>
>>
>> Granted, this is not the "intended use" for classes, and therefore
>> could be viewed as a misuse ("that's what dictionaries are for",
>> etc.). But other than this somewhat academic objection[*], I really
>> can see no problem with using classes in this way.
>>
>> And yet, I've come across online murky warnings against using
>> classes as "pseudo-namespaces". Is there some problem that I'm
>> not seeing with this technique?
>>
>> ~K
>
> I don't see anything wrong with this, except that I would clean it
> up in a couple ways. Like other posters, I would give the class a
> proper class name (Cfg).
>
> I also would not assign integers to spam, jambon, or huevos.
> Instead I would assign each a bare object(). That way you won't get
> unexpected interactions with other constants outside the class. An
> object() is equal only to itself.

What I like about this method is that it will break the bad habit I
see in junior programmers of making assumptions about the value of the
constant. For instance, if they see that Cfg.JAMBON = 3 and hardcode 3
in their code somewhere, that will work fine until someone re-orders
the constants. Using object() instead forces them to use Cfg.JAMBON
since the value will (probably) change with every run of the program.

It will also discourage bugs-waiting-to-happen like this:
if breakfast > Cfg.SPAM:
print "Good news, breakfast is jambon or huevos"

bye
P


== 7 of 7 ==
Date: Sat, Mar 27 2010 6:34 am
From: Ethan Furman


Jonathan Hartley wrote:
> Hey everyone. By coincidence, only yesterday I was wondering about
> using classes as a way of labeling a block of code, ie. an lightweight
> alternative to defining a function that would only be called from one
> location.
>
> eg. instead of:
>
>
> x = 1
> ((some complex logic))
> y = 2
>
>
> one might like to name the complex block of logic, just to make it
> readable:
>
>
> x = 1
> def account_for_non_square_pixels(x):
> ((some complex logic))
> account_for_non_square_pixels()
> y = 2
>
>
> But defining and then calling the function like that is a tad
> cumbersome. So I was wondering about:
>
>
>
> x = 1
> class account_for_non_square_pixels:
> ((some complex logic))
> y = 2
>
>
> I don't exactly like this, but I think you can see what I'm getting
> at. Does this fall down in some way I haven't grasped? Is it as awful
> an abuse of 'class' as my intuition suggests it is? Is there a way to
> do it better?

Both solutions look horrible to me, as both hurt readability. Make your
function somewhere else, then call it in the code. Who cares if you
only use it once?

x = 1
account_for_non_square_pixels()
y = 2

Isn't that easier to read?

And when you want to (read/change) the complex code, you have an easy
place to go to do it.

~Ethan~

==============================================================================
TOPIC: Automatic import ?
http://groups.google.com/group/comp.lang.python/t/5981f9a71fd4cd21?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 5:42 am
From: Jean-Michel Pichavant


C. B. wrote:
> [snip]
> It takes time to write the relative importations, that's ok, but I
> think it could be more pleasant for the end-user to not have to write
> a huge list of "from mymodule import xxx" if it is possible to take
> advantage of automatic importations.
>
> Cheers,
>

In that particular case, replace automatic by implicit, and you got the
reason why it is not a good idea.
Maybe in your case the C habits clashes to the python habits.

Talking about python, if the user needs to know about BBB, then it has
to import it, perdiod. If the user needs to know about many objects,
then it has to import them all, explicitly.
However it looks like all your objects belong to the same module,
quoting MRAB:

"
Couldn't you just have:

import mymodule as m

and then use:

m.AAA
m.BBB
"


You don't like prefixing your object with the namespace they belong to ?
Well, you should :o)

JM


PS : I would'n have renamed the module, but that falls into personal
preferences.

==============================================================================
TOPIC: Simple Traits Questions
http://groups.google.com/group/comp.lang.python/t/4f14032659afa297?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 6:19 am
From: Ethan Furman


Okay, different post for my actual questions. :)

On the PyPI page for strait (http://pypi.python.org/pypi/strait/0.5.1)
it has the example of choosing which methods to keep in the composed class:

class TOSWidget(BaseWidget):
__metaclass__ = include(Pack, Place, Grid)
info = Pack.info.im_func
config = Pack.config.im_func
configure = Pack.configure.im_func
slaves = Pack.slaves.im_func
forget = Pack.forget.im_func
propagate = Pack.propagate.im_func

My question is:

Why use

info = Pack.info.im_func

instead of

info = Pack.info

?

Any and all enlightenment appreciated!

~Ethan~

==============================================================================
TOPIC: Simple Traits
http://groups.google.com/group/comp.lang.python/t/979f25e0901457ae?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 6:13 am
From: Ethan Furman


Wow.

I just stumbled across one of Michele Simionato's offerings, called
simple traits (strait for short) which looks *really* cool.

You can find it here: http://pypi.python.org/pypi/strait/0.5.1

Extremely quick summary:

Instead of multiple inheritance, with it's range of problems, use single
inheritance for the basics, and include traits for various extra
functionality that you want your class to have.

As a side note, one of his blog entries at Artima talks about using
composition instead of mixins when constructing a class, e.g. by doing:

class foo(object):
import baz
def __init__(self):
self.baz.do_something()

which has the benefit (over mixins) of avoiding name-space pollution, as
well as being able to tell where your methods are coming from. The cool
thing (to me, at least) was the realization of being able to use import
statements in class construction.

Thanks, Michele!!

~Ethan~

==============================================================================
TOPIC: Call for papers (Deadline Extended): SETP-10, USA, July 2010
http://groups.google.com/group/comp.lang.python/t/1e7a981a05790cc5?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 6:37 am
From: James Heralds


It would be highly appreciated if you could share this announcement
with your colleagues, students and individuals whose research is in
software engineering, software testing, software quality assurance,
software design and related areas.

Call for papers (Deadline Extended): SETP-10, USA, July 2010

The 2010 International Conference on Software Engineering Theory and
Practice (SETP-10) (website: http://www.PromoteResearch.org ) will be
held during 12-14 of July 2010 in Orlando, FL, USA. SETP is an
important event in the areas of Software development, maintenance, and
other areas of software engineering and related topics.

The conference will be held at the same time and location where
several other major international conferences will be taking place.
The conference will be held as part of 2010 multi-conference
(MULTICONF-10). MULTICONF-10 will be held during July 12-14, 2010 in
Orlando, Florida, USA. The primary goal of MULTICONF is to promote
research and developmental activities in computer science, information
technology, control engineering, and related fields. Another goal is
to promote the dissemination of research to a multidisciplinary
audience and to facilitate communication among researchers,
developers, practitioners in different fields. The following
conferences are planned to be organized as part of MULTICONF-10.

• International Conference on Artificial Intelligence and Pattern
Recognition (AIPR-10)
• International Conference on Automation, Robotics and Control
Systems (ARCS-10)
• International Conference on Bioinformatics, Computational Biology,
Genomics and Chemoinformatics (BCBGC-10)
• International Conference on Computer Communications and Networks
(CCN-10)
• International Conference on Enterprise Information Systems and Web
Technologies (EISWT-10)
• International Conference on High Performance Computing Systems
(HPCS-10)
• International Conference on Information Security and Privacy
(ISP-10)
• International Conference on Image and Video Processing and Computer
Vision (IVPCV-10)
• International Conference on Software Engineering Theory and Practice
(SETP-10)
• International Conference on Theoretical and Mathematical Foundations
of Computer Science (TMFCS-10)

MULTICONF-10 will be held at Imperial Swan Hotel and Suites. It is a
full-service resort that puts you in the middle of the fun! Located
1/2 block south of the famed International Drive, the hotel is just
minutes from great entertainment like Walt Disney World® Resort,
Universal Studios and Sea World Orlando. Guests can enjoy free
scheduled transportation to these theme parks, as well as spacious
accommodations, outdoor pools and on-site dining — all situated on 10
tropically landscaped acres. Here, guests can experience a full-
service resort with discount hotel pricing in Orlando.


We invite draft paper submissions. Please see the website
http://www.PromoteResearch.org for more details.

Sincerely
James Heralds

==============================================================================
TOPIC: GIF89A and PIL
http://groups.google.com/group/comp.lang.python/t/ab1ab88ffdc57736?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 8:17 am
From: Alain Ketterlin


Stephen Hansen <apt.shansen@gmail.invalid> writes:

> Is it possible to get PIL to save GIF's in GIF89A format, instead of
> GIF87A?

GIF89 was patented. I guess that is why it isn't used by PIL. (The
patent has expired now, IIRC.) Anyway, PNG was supposed to replace GIF.

> If not, are there any decent other image libraries out there
> that anyone's familiar with? The only one I could find was
> PythonMagick, which seems completely undocumented. Or I'm blind.

I don't know PythonMagick, but it is based on ImageMagick, which is kind
of a swiss-knife in image manipulation and conversion. You could try the
standalone tools first, to see if you get what you want/need.

> But the problem is, I have a nice, small little 72 byte GIF89A file,
> that I want to do some slight tweaking on and then re-save. I noticed
> that even if I completely skipped the tweaking step and -just- saved,
> it exploded into a 919 byte GIF87A file. And in this context, bytes
> really, really matter. I picked GIF over PNG because the same file in
> PNG was 120 bytes :)
[...]
>>>> f = open('../tiles/BaseTemplate.gif', 'rb')
>>>> d1 = f.read()
>>>> len(d1)
> 73
>>>> d1
> 'GIF89a\x10\x00\x10\x00[...]'

Hmm, a 16x16 image. Don't expect much from the most sophisticated
formats (e.g, PNG), because their overhead (headers etc.) may well be
above the size of the data. Compression isn't usually targeted at small
files.

(BTW: "slight tweaking" may have an effect on file-size if it introduces
new colors, because GIF uses a color-table. I guess you know all this.)

GIF uses the LZW algorithm, and so does zip and gzip (the latter with an
additional layer of Huffmann coding). If your images are of fixed size,
you _may_ be better off compressing the raw data with a general purpose
compressor (i.e., gzip). Check the packages gzip and zlib.

-- Alain.

==============================================================================
TOPIC: function decorator-like function
http://groups.google.com/group/comp.lang.python/t/fe1b732c2b2ef890?hl=en
==============================================================================

== 1 of 3 ==
Date: Sat, Mar 27 2010 8:24 am
From: vsoler


Hi,

Still learning Python, now decorators.

Before diving deeply into decorators, I'd like to apply a function to
another function.

My "extremely simple function" should print number 3, then the sum of
its 2 arguments.

Say that I call f(5,7)
I'd like to get, somehow, 3 then 12.

I've tried the following:

def d(f):
print 3
return f

def f(a, b):
print a+b

f=d(f)

However, it does not work. Calling f(5,7) only returns 12, (3 is
missing)
How should I proceed?


== 2 of 3 ==
Date: Sat, Mar 27 2010 9:06 am
From: Patrick Maupin


On Mar 27, 10:24 am, vsoler <vicente.so...@gmail.com> wrote:
> Hi,
>
> Still learning Python, now decorators.
>
> Before diving deeply into decorators, I'd like to apply a function to
> another function.
>
> My "extremely simple function" should print number 3, then the sum of
> its 2 arguments.
>
> Say that I call   f(5,7)
> I'd like to get, somehow, 3 then 12.
>
> I've tried the following:
>
> def d(f):
>     print 3
>     return f
>
> def f(a, b):
>     print a+b
>
> f=d(f)
>
> However, it does not work. Calling f(5,7) only returns 12, (3 is
> missing)
> How should I proceed?

>>> def d(f):
... def wrapper(*args):
... print 3
... return f(*args)
... return wrapper
...
>>> def f(a, b):
... print a + b
...
>>> f = d(f)
>>> f(5, 7)
3
12

HTH,
Pat


== 3 of 3 ==
Date: Sat, Mar 27 2010 9:21 am
From: vsoler


On 27 mar, 17:06, Patrick Maupin <pmau...@gmail.com> wrote:
> On Mar 27, 10:24 am, vsoler <vicente.so...@gmail.com> wrote:
>
>
>
> > Hi,
>
> > Still learning Python, now decorators.
>
> > Before diving deeply into decorators, I'd like to apply a function to
> > another function.
>
> > My "extremely simple function" should print number 3, then the sum of
> > its 2 arguments.
>
> > Say that I call   f(5,7)
> > I'd like to get, somehow, 3 then 12.
>
> > I've tried the following:
>
> > def d(f):
> >     print 3
> >     return f
>
> > def f(a, b):
> >     print a+b
>
> > f=d(f)
>
> > However, it does not work. Calling f(5,7) only returns 12, (3 is
> > missing)
> > How should I proceed?
> >>> def d(f):
>
> ...     def wrapper(*args):
> ...         print 3
> ...         return f(*args)
> ...     return wrapper
> ...>>> def f(a, b):
>
> ...     print a + b
> ...>>> f = d(f)
> >>> f(5, 7)
>
> 3
> 12
>
> HTH,
> Pat

Pat,

I think some lines are missing. I don't see "d" function defined. Any
lines above def wrapper?

Thank you

==============================================================================
TOPIC: Ipython(x,y) Won't Run
http://groups.google.com/group/comp.lang.python/t/ccf4c75b20f96027?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Mar 27 2010 9:21 am
From: "Larry Kizer"


I uninstalled my previous version of Python and installed Python(x,y)
ver 2.6.2.0 in Windows XP - Install seemed to work fine but when I try to
run
Ipython(x,y) I get the following error: python.exe has encountered a
problem and needs to close. We are sorry for the inconvenience.
Any ideas? Thanks.

LK


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

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