[Tutor] Tutor Digest, Vol 83, Issue 74

Art Cla Media artclamedia at yahoo.com
Tue Jan 18 12:32:21 CET 2011


mai  date  ma in morti  mati  cu  masurile  tale  de handicapat

--- On Tue, 1/18/11, tutor-request at python.org <tutor-request at python.org> wrote:

From: tutor-request at python.org <tutor-request at python.org>
Subject: Tutor Digest, Vol 83, Issue 74
To: tutor at python.org
Date: Tuesday, January 18, 2011, 10:32 AM

Send Tutor mailing list submissions to
    tutor at python.org

To subscribe or unsubscribe via the World Wide Web, visit
    http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
    tutor-request at python.org

You can reach the person managing the list at
    tutor-owner at python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

   1. Re: Why super does not work ! (Steven D'Aprano)
   2. Re: Why super does not work ! (Alan G)
   3. Re: Why super does not work ! (Karim)
   4. Re: Exactly duplicating strftime behavior? (Izz ad-Din Ruhulessin)
   5. How to plot graph? (tee chwee liong)


----------------------------------------------------------------------

Message: 1
Date: Tue, 18 Jan 2011 10:09:35 +1100
From: Steven D'Aprano <steve at pearwood.info>
To: python mail list <tutor at python.org>
Subject: Re: [Tutor] Why super does not work !
Message-ID: <4D34CC2F.1010707 at pearwood.info>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Karim wrote:
> 
> 
> Hello,
> 
> I implemented Observer DP on a listbox (Tkinter) as follows and I don't 
> understand why super() is not working and Observable.__init__(self) is 
> working, cf below:

You seem to be confused about your class design. On the one hand, you 
inherit from Listbox, but then you *also* use composition and/or 
delegation on a Listbox instance:

class ListObservable(Listbox, Observable):
     def __init__(self):
     [...]
     self.liste = Listbox(listvariable=self.listeContenu,
                  selectmode='single')


That means that a ListObservable instance both *is* a Listbox and 
*contains* a Listbox at the same time. I'm not saying this is 
necessarily wrong, but it is unusual, and confuses the model. At the 
very least, you need to document why you have done this.


> class ListObservable(Listbox, Observable):
>     """Creation de widget Listbox"""
>     def __init__(self):
>         super(ListObservable, self).__init__()
>         #Observable.__init__(self)

Are both Listbox and Observable documented as suitable for multiple 
inheritance? My guess is that Listbox is not, and I can see from your 
source code that Observable is *not* suitable for multiple inheritance.

If Listbox is documented as suitable for multiple inheritance, then it 
is a bug in Listbox. If it is not, then it is a bug in your code, by 
using it for multiple inheritance.

Multiple inheritance in Python is cooperative, not enforced, and it is 
tricky to get right and is *very* sensitive to any class which fails to 
cooperate. Thousands and thousands of words have been written on the 
perils and difficulties of multiple inheritance, particularly by Michele 
Simionato who I consider to be THE authority on MI in Python.

Some of the most important articles are:

Michele Simionato:
Things to Know About Python Super
     http://www.artima.com/weblogs/viewpost.jsp?thread=236275
     http://www.artima.com/weblogs/viewpost.jsp?thread=236278
     http://www.artima.com/weblogs/viewpost.jsp?thread=237121
Mixins considered harmful:
     http://www.artima.com/weblogs/viewpost.jsp?thread=246341
     http://www.artima.com/weblogs/viewpost.jsp?thread=246483
     http://www.artima.com/weblogs/viewpost.jsp?thread=254367
     http://www.artima.com/weblogs/viewpost.jsp?thread=254507
Generic functions vs mixins:
     http://www.artima.com/weblogs/viewpost.jsp?thread=237764
Straits:
     http://www.artima.com/weblogs/viewpost.jsp?thread=246488

James Knight:
Python's Super Considered Harmful
     http://fuhm.net/super-harmful/


Summary: this is *not* a bug in super. It's really a side-effect of the 
fact that multiple inheritance itself is often the wrong thing to use, 
and even when it is right, it is often tricky to get it right. To put it 
another way: don't use multiple inheritance unless you have to, there 
are better ways, such as by composition.



-- 
Steven


------------------------------

Message: 2
Date: Tue, 18 Jan 2011 01:32:39 +0000 (UTC)
From: Alan G <alan.gauld at btinternet.com>
To: tutor at python.org
Subject: Re: [Tutor] Why super does not work !
Message-ID: <loom.20110118T022639-166 at post.gmane.org>
Content-Type: text/plain; charset=us-ascii

Steven D'Aprano <steve <at> pearwood.info> writes:

> fact that multiple inheritance itself is often the wrong thing to use, 
> and even when it is right, it is often tricky to get it right. To put it 
> another way: don't use multiple inheritance unless you have to, there 
> are better ways, such as by composition.

Or use a language where MI is the normal and idiomatic way 
to do things because the language assumes it and so it just 
works. There are very few such languages but Lisp is one :-)

Sadly Python isn't, and when using MI I always avoid super()
Which is a huge shame since MI is where super() should 
be most useful... But in my experience MI in Python is 
definitely a place where explicit is better than implicit.

Alan G.





------------------------------

Message: 3
Date: Tue, 18 Jan 2011 07:31:10 +0100
From: Karim <karim.liateni at free.fr>
Cc: tutor at python.org
Subject: Re: [Tutor] Why super does not work !
Message-ID: <4D3533AE.4050702 at free.fr>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed


Thanks Izz, Luke, Steven and Alan!
That's I figured out with MI and super.
Steven I understand the point to have Listbox contains a Listbox.
Before the code was in App class and I extracted it to do an outside 
class making the mistake.
But magic of Python it's working (but I know it's awful).
The code is working but I am not totally happy because of many 
EntryObserver almost identical objects (except from update and grid() 
options)
Is it possible to simplify this structure?

Regards
Karim

The rest of the code is below:

#!/usr/bin/env python2.7
"""Module ObserverGraphique.py

Une implementation du design pattern Observer.
"""


from Tkinter import *
from observable import Observable
from observer import AbstractObserver


data_base = {
             'Employe1': ['Joel',   'Durant',  '0623'],
             'Employe2': ['Marc',   'Donce',   '0624'],
             'Employe3': ['George', 'Roux',    '0625'],
             'Employe4': ['Alain',  'Richard', '0626']
             }


__all__ = ['App', 'ListObservable', 'Entry1Observer', 'Entry2Observer', 
'Entry3Observer']


class App(Frame):
     """Application graphique avec Tkinter implementant un Observer 
Design Pattern."""
     def __init__(self, master=None):
         Frame.__init__(self, master)
         self.master.title("Exemple : Observer Design Pattern")
         self.grid()
         self.createLabels()
         self.createObservable()
         self.createObservers()
         self.registerObservers()

    def createLabels(self):
         """Creation de widgets Label"""
         self.label1 = Label(text="Nom :")
         self.label2 = Label(text="Prenom :")
         self.label3 = Label(text="Poste :")
         self.label1.grid(row=1, column=1, sticky=W)
         self.label2.grid(row=2, column=1, sticky=W)
         self.label3.grid(row=3, column=1, sticky=W)

     def createObservable(self):
         """Creation de la listBox observable."""
         self.sujet  = ListObservable()

     def createObservers(self):
         """Creation des champs d'entre texte observateurs de la liste."""
         self.nom    = Entry1Observer(self.sujet)
         self.prenom = Entry2Observer(self.sujet)
         self.poste  = Entry3Observer(self.sujet)

     def registerObservers(self):
         """Enregistrement des observateurs."""
         self.sujet.attach(self.nom)
         self.sujet.attach(self.prenom)
         self.sujet.attach(self.poste)

class ListObservable(Listbox, Observable):
     """Creation de widget Listbox"""
     def __init__(self):
         #super(ListObservable, self).__init__()
         Observable.__init__(self)
         self._value = None
         self.listeContenu = StringVar()
         self.listeContenu.set(' '.join(sorted(data_base.keys())))
         self.liste = Listbox(listvariable=self.listeContenu, 
selectmode='single')
         self.liste.grid(row=0, column=1, sticky=N+S+W)
         self.liste.bind('<Double-1>', self.onSelect)
         self.liste.selection_set(0)

     def onSelect(self, e):
         if not self.liste.curselection():
             self.setValue(0)
         else:
             self.setValue(self.liste.get(self.liste.curselection()))
         self.notify()

     def setValue(self, select):
         self._value = select

     def getValue(self):
         return self._value

class Entry1Observer(Entry, AbstractObserver):
     """Creation de widget Entry 1"""
     def __init__(self, sujet=None):
         #super(Entry1Observer, self).__init__(sujet)
         AbstractObserver.__init__(self, sujet)
         self.text = StringVar()
         self.entry = Entry(textvariable=self.text)
         self.entry.grid(row=1, column=2)

     def update(self):
         a = self.sujet.getValue()
         self.text.set(data_base[a][1])
         print a

class Entry2Observer(Entry, AbstractObserver):
     """Creation de widget Entry 2"""
     def __init__(self, sujet=None):
         AbstractObserver.__init__(self, sujet)
         self.text  = StringVar()
         self.entry = Entry(textvariable=self.text)
         self.entry.grid(row=2, column=2)

     def update(self):
         a = self.sujet.getValue()
         self.text.set(data_base[a][0])

class Entry3Observer(Entry, AbstractObserver):
     """Creation de widget Entry"""
     def __init__(self, sujet=None):
         AbstractObserver.__init__(self, sujet)
         self.text  = StringVar()
         self.entry = Entry(textvariable=self.text)
         self.entry.grid(row=3, column=2)

     def update(self):
         a = self.sujet.getValue()
         self.text.set(data_base[a][2])


if __name__ == '__main__':
     app=App()
     app.mainloop()

# ----- end of file ----- #

class AbstractObserver(object):
     """Interface general des observateurs"""
     def __init__(self, sujet):
         """Constructeur"""
         self.sujet = sujet

     def update(self):
         """Methode a implementer par les observateurs"""
         pass


On 01/18/2011 02:32 AM, Alan G wrote:
> Steven D'Aprano<steve<at>  pearwood.info>  writes:
>
>> fact that multiple inheritance itself is often the wrong thing to use,
>> and even when it is right, it is often tricky to get it right. To put it
>> another way: don't use multiple inheritance unless you have to, there
>> are better ways, such as by composition.
> Or use a language where MI is the normal and idiomatic way
> to do things because the language assumes it and so it just
> works. There are very few such languages but Lisp is one :-)
>
> Sadly Python isn't, and when using MI I always avoid super()
> Which is a huge shame since MI is where super() should
> be most useful... But in my experience MI in Python is
> definitely a place where explicit is better than implicit.
>
> Alan G.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



------------------------------

Message: 4
Date: Tue, 18 Jan 2011 07:35:30 +0100
From: Izz ad-Din Ruhulessin <izzaddin.ruhulessin at gmail.com>
To: Hugo Arts <hugo.yoshi at gmail.com>
Cc: tutor at python.org
Subject: Re: [Tutor] Exactly duplicating strftime behavior?
Message-ID:
    <AANLkTin-GdXd0rfupKHa=-RN+6d_ywYawDrv2Nsw=mVy at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi Hugo,

Problem solved, thanks!

Kind regards,

Izz ad-Din

2011/1/17 Hugo Arts <hugo.yoshi at gmail.com>

> On Mon, Jan 17, 2011 at 8:29 PM, Izz ad-Din Ruhulessin
> <izzaddin.ruhulessin at gmail.com> wrote:
> > Hi all,
> > I'm designing a module which aims to provide full compatibility with
> > Pythons' datetime module.
> > However, I can't find out how some operators in strftime function,
> namely:
> > those who use the locale. (%a, %A, %b, etc.)
> > How do I access this locale and it's values?
> > Thanks in advance.
> > Kind regards,
> > Izz ad-Din
>
> Check out the locale module:
>
> http://docs.python.org/library/locale.html#locale.nl_langinfo
>
> HTH,
> Hugo
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/522e990e/attachment-0001.html>

------------------------------

Message: 5
Date: Tue, 18 Jan 2011 10:30:53 +0000
From: tee chwee liong <tcl76 at hotmail.com>
To: <tutor at python.org>
Subject: [Tutor] How to plot graph?
Message-ID: <BAY156-w28C9E6F96071B97A33CDFEB5F70 at phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"


hi all, 
 
i'm new to python and this is advanced for me. is there a way to plot data with python? i want to plot EyVt, EyHt on the Y-axis and Lane on the X-axis as attached or below. 
currently i'm using Python2.5 and Win XP. thanks a lot. 
Eg: 
Platform: PC
Tempt : 25
TAP0 :0
TAP1 :1
+++++++++++++++++++++++++++++++++++++++++++++
Port Chnl Lane EyVt EyHt
+++++++++++++++++++++++++++++++++++++++++++++
0  1  1  75  55
0  1  2  10 35
0  1  3  25 35 
0  1  4  35 25
0  1  5  10 20
+++++++++++++++++++++++++++++++++++++++++++++
Time: 20s                           
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/80b250af/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: sampledata.txt
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/80b250af/attachment.txt>

------------------------------

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 83, Issue 74
*************************************



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/6c1829a7/attachment-0001.html>


More information about the Tutor mailing list