Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52008 > unrolled thread

Class hierarchy problem

Started byBrJohan <brjohan@gmail.com>
First post2013-08-06 11:10 +0200
Last post2013-08-07 13:40 +0530
Articles 15 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Class hierarchy problem BrJohan <brjohan@gmail.com> - 2013-08-06 11:10 +0200
    Re: Class hierarchy problem Chris Angelico <rosuav@gmail.com> - 2013-08-06 10:30 +0100
      Re: Class hierarchy problem Chris Angelico <rosuav@gmail.com> - 2013-08-06 15:02 +0100
        Re: Class hierarchy problem BrJohan <brjohan@gmail.com> - 2013-08-06 17:36 +0200
          Re: Class hierarchy problem Joe Junior <joe.fbs.junior@gmail.com> - 2013-08-06 13:23 -0300
          Re: Class hierarchy problem Jordi Riera <kender.jr@gmail.com> - 2013-08-06 18:13 +0200
          Re: Class hierarchy problem Chris Angelico <rosuav@gmail.com> - 2013-08-06 18:12 +0100
          Re: Class hierarchy problem Terry Reedy <tjreedy@udel.edu> - 2013-08-06 19:13 -0400
          Re: Class hierarchy problem Ben Finney <ben+python@benfinney.id.au> - 2013-08-07 11:58 +1000
    Re: Class hierarchy problem Peter Otten <__peter__@web.de> - 2013-08-06 11:34 +0200
    Re: Class hierarchy problem Steven D'Aprano <steve@pearwood.info> - 2013-08-06 09:36 +0000
    pexpect,  loading an entry field inq1ltd <inq1ltd@inqvista.com> - 2013-08-06 11:05 -0400
    Re: pexpect, loading an entry field "Lakshmipathi.G" <lakshmipathi.g@gmail.com> - 2013-08-06 22:04 +0530
    Re: pexpect, loading an entry field inq1ltd <inq1ltd@inqvista.com> - 2013-08-06 14:03 -0400
    Re: pexpect, loading an entry field "Lakshmipathi.G" <lakshmipathi.g@gmail.com> - 2013-08-07 13:40 +0530

#52008 — Class hierarchy problem

FromBrJohan <brjohan@gmail.com>
Date2013-08-06 11:10 +0200
SubjectClass hierarchy problem
Message-ID<ktqeho$f5i$1@news.albasani.net>
I'm in need of help to solve this Python (ver. 3.3) problem:

I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA, 
SubC,...), each of which is inheriting from a chain of superclasses with 
a common baseclass(Sup) on top. (So far, no problem)

Now, I want to create instances of the correct subclasstype as decided 
by the common baseclass, like this:
	
i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)

where i can be of any class except Sup itself (as decided by Sup)

Now, the problem:

How to design the __new__() and __init__() methods for the various 
classes in order to achieve what I want?

(Some ten years I had the same problem (in a different context) and was 
helped by asking in this group. However, the solution has disappeared. 
Maybe the 2.x solution is not the same as in 3.x?)

[toc] | [next] | [standalone]


#52014

FromChris Angelico <rosuav@gmail.com>
Date2013-08-06 10:30 +0100
Message-ID<mailman.235.1375781442.1251.python-list@python.org>
In reply to#52008
On Tue, Aug 6, 2013 at 10:10 AM, BrJohan <brjohan@gmail.com> wrote:
> Now, I want to create instances of the correct subclasstype as decided by
> the common baseclass, like this:
>
> i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)
>
> where i can be of any class except Sup itself (as decided by Sup)

Can you do this as a factory function instead of the class itself?
Then all you need to do is call the appropriate class.

ChrisA

[toc] | [prev] | [next] | [standalone]


#52042

FromChris Angelico <rosuav@gmail.com>
Date2013-08-06 15:02 +0100
Message-ID<mailman.254.1375797746.1251.python-list@python.org>
In reply to#52014
On Tue, Aug 6, 2013 at 2:58 PM, BrJohan <brjohan@gmail.com> wrote:
> On 06/08/2013 11:30, Chris Angelico wrote:
>>
>> On Tue, Aug 6, 2013 at 10:10 AM, BrJohan <brjohan@gmail.com> wrote:
>>>
>>> Now, I want to create instances of the correct subclasstype as decided by
>>> the common baseclass, like this:
>>>
>>> i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)
>>>
>>> where i can be of any class except Sup itself (as decided by Sup)
>>
>>
>> Can you do this as a factory function instead of the class itself?
>> Then all you need to do is call the appropriate class.
>>
>> ChrisA
>>
>
> My classhierarchy is like a multilevel tree where each non-leaf node (class)
> is given knowledge about its nearest subclasses and their 'capacities'.
>
> So, my idea is to let the 'upper' class recursively choose which of its
> nearest subclasses is the 'correct' one, until approaching a 'leaf' class
> from which the instance should be created. And, given my knowledge that a
> solution along the lines of this idea has been designed and was working, I'm
> still hopeful ... (or I'll have to investigate all those old backup-DVDs)

[ responding on-list - I hope it was mere oversight that had this come
privately to me alone ]

This is code smell; this recursive search for the "right" class seems
likely to be wrong. Can you have the classes perhaps register
themselves in some way? On what basis is a superclass to determine
that one of its subclasses should handle this request?

ChrisA

[toc] | [prev] | [next] | [standalone]


#52051

FromBrJohan <brjohan@gmail.com>
Date2013-08-06 17:36 +0200
Message-ID<ktr56j$u7i$1@news.albasani.net>
In reply to#52042
On 06/08/2013 16:02, Chris Angelico wrote:

>> My classhierarchy is like a multilevel tree where each non-leaf node (class)
>> is given knowledge about its nearest subclasses and their 'capacities'.
>>
>> So, my idea is to let the 'upper' class recursively choose which of its
>> nearest subclasses is the 'correct' one, until approaching a 'leaf' class
>> from which the instance should be created. And, given my knowledge that a
>> solution along the lines of this idea has been designed and was working, I'm
>> still hopeful ... (or I'll have to investigate all those old backup-DVDs)
>
> [ responding on-list - I hope it was mere oversight that had this come
> privately to me alone ]
>
> This is code smell; this recursive search for the "right" class seems
> likely to be wrong. Can you have the classes perhaps register
> themselves in some way? On what basis is a superclass to determine
> that one of its subclasses should handle this request?
>
> ChrisA
>


Consider a botanical classification system (somewhat analogous to my 
'problem' as it effectively is related to classification of entities):

A Domain should know about its Kingdoms,
a Kingdom should know about its Phylums,
...
a Genus should know about its Species.

Of course it is possible to implement such a decision tree as a 
'factory'. However, I would rather prefer to encapsulate those decisions 
at the class level where they 'belong'.

BrJohan

[toc] | [prev] | [next] | [standalone]


#52055

FromJoe Junior <joe.fbs.junior@gmail.com>
Date2013-08-06 13:23 -0300
Message-ID<mailman.264.1375807105.1251.python-list@python.org>
In reply to#52051
On Tue, Aug 6, 2013 at 12:36 PM, BrJohan <brjohan@gmail.com> wrote:
> On 06/08/2013 16:02, Chris Angelico wrote:
>
>>> My classhierarchy is like a multilevel tree where each non-leaf node
>>> (class)
>>> is given knowledge about its nearest subclasses and their 'capacities'.
>>>
>>> So, my idea is to let the 'upper' class recursively choose which of its
>>> nearest subclasses is the 'correct' one, until approaching a 'leaf' class
>>> from which the instance should be created. And, given my knowledge that a
>>> solution along the lines of this idea has been designed and was working,
>>> I'm
>>> still hopeful ... (or I'll have to investigate all those old backup-DVDs)
>>
>>
>> [ responding on-list - I hope it was mere oversight that had this come
>> privately to me alone ]
>>
>> This is code smell; this recursive search for the "right" class seems
>> likely to be wrong. Can you have the classes perhaps register
>> themselves in some way? On what basis is a superclass to determine
>> that one of its subclasses should handle this request?
>>
>> ChrisA
>>
>
>
> Consider a botanical classification system (somewhat analogous to my
> 'problem' as it effectively is related to classification of entities):
>
> A Domain should know about its Kingdoms,
> a Kingdom should know about its Phylums,
> ...
> a Genus should know about its Species.
>
> Of course it is possible to implement such a decision tree as a 'factory'.
> However, I would rather prefer to encapsulate those decisions at the class
> level where they 'belong'.
>
> BrJohan
> --

I think it's a "has a" vs a "is a" problem. A Domain has a Kingdom, a Kingdom is
not a Domain, so it shouldn't actually inherit Domain. In this case
you should use
composition instead of inheritance.

When you say that a A Domain should know about it's Kingdons note that you're
talking about a specific Domain and it's specific Kingdons, which
means, a Domain
instance and various Kingdom instances.

JoeS

[toc] | [prev] | [next] | [standalone]


#52056

FromJordi Riera <kender.jr@gmail.com>
Date2013-08-06 18:13 +0200
Message-ID<mailman.263.1375807105.1251.python-list@python.org>
In reply to#52051

[Multipart message — attachments visible in raw view] — view raw

Are you using a db for that?

Django models would handle that pretty easily:

class Species(models.Model):
     name = models.CharField()

class Genus(models.Model):
    name = models.CharField()
    species = models.ManyToManyField(
         Species, related_name = 'genus_species'
    )

then you can access species from genus by:
Genus.objects.all()[0].species.all()

and genus from species by:
Species.objects.all()[0].genus_species.all()

if you can afford this depends that would solve your issue I think.
more details:
http://stackoverflow.com/questions/9352662/how-to-use-the-reverse-of-a-django-manytomany-relationship

Regards
Jordi


On Tue, Aug 6, 2013 at 5:36 PM, BrJohan <brjohan@gmail.com> wrote:

> On 06/08/2013 16:02, Chris Angelico wrote:
>
>  My classhierarchy is like a multilevel tree where each non-leaf node
>>> (class)
>>> is given knowledge about its nearest subclasses and their 'capacities'.
>>>
>>> So, my idea is to let the 'upper' class recursively choose which of its
>>> nearest subclasses is the 'correct' one, until approaching a 'leaf' class
>>> from which the instance should be created. And, given my knowledge that a
>>> solution along the lines of this idea has been designed and was working,
>>> I'm
>>> still hopeful ... (or I'll have to investigate all those old backup-DVDs)
>>>
>>
>> [ responding on-list - I hope it was mere oversight that had this come
>> privately to me alone ]
>>
>> This is code smell; this recursive search for the "right" class seems
>> likely to be wrong. Can you have the classes perhaps register
>> themselves in some way? On what basis is a superclass to determine
>> that one of its subclasses should handle this request?
>>
>> ChrisA
>>
>>
>
> Consider a botanical classification system (somewhat analogous to my
> 'problem' as it effectively is related to classification of entities):
>
> A Domain should know about its Kingdoms,
> a Kingdom should know about its Phylums,
> ...
> a Genus should know about its Species.
>
> Of course it is possible to implement such a decision tree as a 'factory'.
> However, I would rather prefer to encapsulate those decisions at the class
> level where they 'belong'.
>
> BrJohan
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>



-- 
- Jordi Riera, Connecting people.
+33 662217507

[toc] | [prev] | [next] | [standalone]


#52060

FromChris Angelico <rosuav@gmail.com>
Date2013-08-06 18:12 +0100
Message-ID<mailman.267.1375809159.1251.python-list@python.org>
In reply to#52051
On Tue, Aug 6, 2013 at 4:36 PM, BrJohan <brjohan@gmail.com> wrote:
> Consider a botanical classification system (somewhat analogous to my
> 'problem' as it effectively is related to classification of entities):
>
> A Domain should know about its Kingdoms,
> a Kingdom should know about its Phylums,
> ...
> a Genus should know about its Species.

I don't believe it's that clear. In some situations it may be best to
work that way; in others, each tier should know only those above it.
Does the generic Widget know about being a PushButton, a ListBox, and
a Slider? No, but the Slider knows what a Widget is.

ChrisA

[toc] | [prev] | [next] | [standalone]


#52082

FromTerry Reedy <tjreedy@udel.edu>
Date2013-08-06 19:13 -0400
Message-ID<mailman.282.1375830834.1251.python-list@python.org>
In reply to#52051
On 8/6/2013 11:36 AM, BrJohan wrote:

> Consider a botanical classification system (somewhat analogous to my
> 'problem' as it effectively is related to classification of entities):
>
> A Domain should know about its Kingdoms,
> a Kingdom should know about its Phylums,
> ...
> a Genus should know about its Species.

As some already said, 'a domain' is an instance of Domain (or possibly 
generic Taxon). We have on Earth one instance of 'Life'.
>
> Of course it is possible to implement such a decision tree as a
> 'factory'. However, I would rather prefer to encapsulate those decisions
> at the class level where they 'belong'.

Each instance could have a .classify function that assigns instances to 
sub-instance. The master classifier function would only know how to use 
the .classify functions and have no specific content knowledge in itself.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#52097

FromBen Finney <ben+python@benfinney.id.au>
Date2013-08-07 11:58 +1000
Message-ID<mailman.293.1375840806.1251.python-list@python.org>
In reply to#52051
Terry Reedy <tjreedy@udel.edu> writes:

> On 8/6/2013 11:36 AM, BrJohan wrote:
>
> > Consider a botanical classification system (somewhat analogous to my
> > 'problem' as it effectively is related to classification of entities):
> >
> > A Domain should know about its Kingdoms,
> > a Kingdom should know about its Phylums,
> > ...
> > a Genus should know about its Species.
>
> As some already said, 'a domain' is an instance of Domain (or possibly
> generic Taxon). We have on Earth one instance of 'Life'.

I think the term for a taxonomic grouping of life, if you want to avoid
specifying some specific level of the taxonomy, is “clade”
<URL:https://en.wiktionary.org/wiki/clade>.

-- 
 \     “First they came for the verbs, and I said nothing, for verbing |
  `\    weirds language. Then, they arrival for the nouns and I speech |
_o__)                           nothing, for I no verbs.” —Peter Ellis |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#52015

FromPeter Otten <__peter__@web.de>
Date2013-08-06 11:34 +0200
Message-ID<mailman.236.1375781680.1251.python-list@python.org>
In reply to#52008
BrJohan wrote:

> I'm in need of help to solve this Python (ver. 3.3) problem:
> 
> I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA,
> SubC,...), each of which is inheriting from a chain of superclasses with
> a common baseclass(Sup) on top. (So far, no problem)
> 
> Now, I want to create instances of the correct subclasstype as decided
> by the common baseclass, like this:
> 
> i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)
> 
> where i can be of any class except Sup itself (as decided by Sup)
> 
> Now, the problem:
> 
> How to design the __new__() and __init__() methods for the various
> classes in order to achieve what I want?
> 
> (Some ten years I had the same problem (in a different context) and was
> helped by asking in this group. However, the solution has disappeared.
> Maybe the 2.x solution is not the same as in 3.x?)

Keep it simple, use a function:

def make(*args):
    class_ = deduce_correct_class(*args)
    return class_(*args)

That way you won't even need any __new__() methods.

[toc] | [prev] | [next] | [standalone]


#52016

FromSteven D'Aprano <steve@pearwood.info>
Date2013-08-06 09:36 +0000
Message-ID<5200c393$0$29986$c3e8da3$5496439d@news.astraweb.com>
In reply to#52008
On Tue, 06 Aug 2013 11:10:17 +0200, BrJohan wrote:

> I'm in need of help to solve this Python (ver. 3.3) problem:
> 
> I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA,
> SubC,...), each of which is inheriting from a chain of superclasses with
> a common baseclass(Sup) on top. (So far, no problem)


Well, I don't know about that, a deep and complex chain of super- and sub-
classes already sounds like a problem. But putting that aside...


> Now, I want to create instances of the correct subclasstype as decided
> by the common baseclass, like this:
> 	
> i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)

Inheritance doesn't, or at least shouldn't, work like that. The 
superclasses shouldn't be expected to know about their subclasses. 
Knowledge goes the other way: each class knows its parents, and so 
recursively can go all the way to the common base class.


> where i can be of any class except Sup itself (as decided by Sup)
> 
> Now, the problem:
> 
> How to design the __new__() and __init__() methods for the various
> classes in order to achieve what I want?

You don't :-)

Instead, have a factory function:

def sup(args):
    if args:
        return Sup(...)
    elif condition():
        return SupA(...)
    elif condition2() or condition3():
        return SupB(...)
    ...


sort of thing.


Another possibility would be to start with the child class, and give it 
the ability to return an instance of itself, or its direct parent. 
Completely untested:


class SupABC(SupABB):
    def __new__(cls, args):
        if condition():
            # Return an instance of myself.
            ...
         else:
            # Let my parent (maybe) return an instance.
            return SupABB(args)


Either way, the design risks becoming a horrible mess, but you might be 
able to use it. Good luck!


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#52052 — pexpect, loading an entry field

Frominq1ltd <inq1ltd@inqvista.com>
Date2013-08-06 11:05 -0400
Subjectpexpect, loading an entry field
Message-ID<mailman.261.1375803546.1251.python-list@python.org>
In reply to#52008

[Multipart message — attachments visible in raw view] — view raw

python help;

I am using pexpect to open my program.
Can someone tell me how to get data to appear in 
an entry field.

After pexpect opens the my program I have tried to use 
send, sendline, and write functions to try to put data into 
the program's entry field.

However, the data is going to the terminal window, 
the window that is used to initiate the call to pexpect 
but not to the entry field in the open program.

I would appreciate suggestions.
jol

 

[toc] | [prev] | [next] | [standalone]


#52057 — Re: pexpect, loading an entry field

From"Lakshmipathi.G" <lakshmipathi.g@gmail.com>
Date2013-08-06 22:04 +0530
SubjectRe: pexpect, loading an entry field
Message-ID<mailman.265.1375807105.1251.python-list@python.org>
In reply to#52008
pexpect looks simple to use. Please check this example
http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect-module-in-python/

-- 
----
Cheers,
Lakshmipathi.G
FOSS Programmer.
www.giis.co.in

On Tue, Aug 6, 2013 at 8:35 PM, inq1ltd <inq1ltd@inqvista.com> wrote:
> python help;
>
>
>
> I am using pexpect to open my program.
>
> Can someone tell me how to get data to appear in
>
> an entry field.
>
>
>
> After pexpect opens the my program I have tried to use
>
> send, sendline, and write functions to try to put data into
>
> the program's entry field.
>
>
>
> However, the data is going to the terminal window,
>
> the window that is used to initiate the call to pexpect
>
> but not to the entry field in the open program.
>
>
>
> I would appreciate suggestions.
>
> jol
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#52062 — Re: pexpect, loading an entry field

Frominq1ltd <inq1ltd@inqvista.com>
Date2013-08-06 14:03 -0400
SubjectRe: pexpect, loading an entry field
Message-ID<mailman.270.1375814257.1251.python-list@python.org>
In reply to#52008

[Multipart message — attachments visible in raw view] — view raw

> pexpect looks simple to use. Please check this example
> http://www.pythonforbeginners.com/systems-programming/how-to-use-the-
pexpect
> -module-in-python/
> > python help;
> > 
> > I am using pexpect to open my program.
> > Can someone tell me how to get data to appear in
> > an entry field.
> > After pexpect opens the my program I have tried to use
> > send, sendline, and write functions to try to put data into
> > the program's entry field.
> > However, the data is going to the terminal window, 
> > the window that is used to initiate the call to pexpect
> > but not to the entry field in the open program. 
> > I would appreciate suggestions.
> > 
> > jol
> > 

Thanks for the response.

I have been there but that site gives me 
the same information that I get from 
noah.org.  I have the pexpect docs and they presume 
that this problem doesn't exist.

None of the information in the NOAH site or the FAQ's 
address this question. 

I'm using suse linux running python 2.7 and pexpect.

The data, when using send() method is going to the terminal window, 
This is the same window that is used to initiate the call to pexpect.

Pexpect opens the program but the send method sends
 data to the terminal window, not the entry field located
in the child spawned by pexpect.

interact works, as does close(), TIMEOUT, EOF, before, after, 
expect_exact and methods.  But the entry field does not 
load.

So my question is;

Why??




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

[toc] | [prev] | [next] | [standalone]


#52117 — Re: pexpect, loading an entry field

From"Lakshmipathi.G" <lakshmipathi.g@gmail.com>
Date2013-08-07 13:40 +0530
SubjectRe: pexpect, loading an entry field
Message-ID<mailman.305.1375863171.1251.python-list@python.org>
In reply to#52008
Hi -
I'm using Python 2.7.3 (Fedora 17) . I tried a simple example with
pexpect to copy a file to remote system. It works

$ cat pex.py
import pexpect
s = pexpect.spawn ('scp pex.py root@10.30.77.244:/tmp')
s.expect ('Password:')
s.sendline ('a')
s.expect(pexpect.EOF,timeout=20)

Execute above program (change remote ip and password) - If it works
then its not an issue with pexpect or python
environment.


-- 
----
Cheers,
Lakshmipathi.G
FOSS Programmer.
www.giis.co.in



On Tue, Aug 6, 2013 at 11:33 PM, inq1ltd <inq1ltd@inqvista.com> wrote:
>
>
>> pexpect looks simple to use. Please check this example
>
>>
>> http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect
>
>> -module-in-python/
>
>> > python help;
>
>> >
>
>> > I am using pexpect to open my program.
>
>> > Can someone tell me how to get data to appear in
>
>> > an entry field.
>
>> > After pexpect opens the my program I have tried to use
>
>> > send, sendline, and write functions to try to put data into
>
>> > the program's entry field.
>
>> > However, the data is going to the terminal window,
>
>> > the window that is used to initiate the call to pexpect
>
>> > but not to the entry field in the open program.
>
>> > I would appreciate suggestions.
>
>> >
>
>> > jol
>
>> >
>
>
>
> Thanks for the response.
>
>
>
> I have been there but that site gives me
>
> the same information that I get from
>
> noah.org. I have the pexpect docs and they presume
>
> that this problem doesn't exist.
>
>
>
> None of the information in the NOAH site or the FAQ's
>
> address this question.
>
>
>
> I'm using suse linux running python 2.7 and pexpect.
>
>
>
> The data, when using send() method is going to the terminal window,
>
> This is the same window that is used to initiate the call to pexpect.
>
>
>
> Pexpect opens the program but the send method sends
>
> data to the terminal window, not the entry field located
>
> in the child spawned by pexpect.
>
>
>
> interact works, as does close(), TIMEOUT, EOF, before, after,
>
> expect_exact and methods. But the entry field does not
>
> load.
>
>
>
> So my question is;
>
>
>
> Why??
>
>
>
>
>
>
>
>
>
>> >
>
>> >
>
>> >
>
>> > --
>
>> > http://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web