Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.083 X-Spam-Evidence: '*H*': 0.83; '*S*': 0.00; 'that?': 0.05; 'tree': 0.05; 'responding': 0.07; 'cc:addr:python-list': 0.11; 'django': 0.11; '"right"': 0.16; 'created.': 0.16; 'investigate': 0.16; 'nearest': 0.16; 'subclasses': 0.16; 'think.': 0.16; 'wrote:': 0.18; 'seems': 0.21; '8bit%:5': 0.22; '>>>': 0.22; 'aug': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'subject:problem': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'related': 0.29; 'skip:g 30': 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'node': 0.31; 'oversight': 0.31; 'way?': 0.31; 'class': 0.32; 'themselves': 0.32; 'url:python': 0.33; 'alone': 0.33; 'skip:b 30': 0.33; 'knowledge': 0.35; 'classes': 0.35; 'one,': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; "i'll": 0.36; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'level': 0.37; 'implement': 0.38; 'skip:& 10': 0.38; '8bit%:4': 0.38; 'depends': 0.38; 'handle': 0.38; 'issue': 0.38; 'pm,': 0.38; 'rather': 0.38; 'skip:b 40': 0.39; '8bit%:6': 0.40; 'url:mail': 0.40; 'solve': 0.60; 'hope': 0.61; 'kingdom': 0.61; 'course': 0.61; 'name': 0.63; 'such': 0.63; 'choose': 0.64; 'connecting': 0.64; 'more': 0.64; 'by:': 0.65; 'to:addr:gmail.com': 0.65; 'effectively': 0.66; 'determine': 0.67; 'details:': 0.80; "'upper'": 0.84; '5:36': 0.84; 'working,': 0.84; 'afford': 0.91; 'subject:Class': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Ble7W5ONs4kY1kwp51Qh/s0ouPuaPvrEr9d+iOz/ZMA=; b=sBaZILYDJSPn3Kh+n0FgvRerUtPPr9bjUlpyNtsKop0hOC1mclzgQTIk/2+/ovk2+4 2Bits6DPWlNd3QuCgdgNPWegNhsxPl+vQ7xUlMfX1g4hMN0cLaf//OXy70kSoL/yLh9c s10n/+qTjCAPY93HiX4hGaK1yzru5fqqIgkFPEPDWBjQUpWfZhImzU6yxHKykkHZ97AM 4zhv/E7jkgac52YfSAiQ2KpsUrWQptdLyCegZuTzOPeaDArKoo7V4Lq9n4KJdnH6ZVT6 LrfgjA3flH5nxFIyklQuk0THPYFqnEZ/aXa3L+404Ei3IYCeg69c6A2FDRcw0Cu4Dwv/ RXPw== MIME-Version: 1.0 X-Received: by 10.66.240.67 with SMTP id vy3mr4002164pac.141.1375805635597; Tue, 06 Aug 2013 09:13:55 -0700 (PDT) In-Reply-To: References: <520100F9.4010805@gmail.com> Date: Tue, 6 Aug 2013 18:13:55 +0200 Subject: Re: Class hierarchy problem From: Jordi Riera To: BrJohan Content-Type: multipart/alternative; boundary=047d7b15ab7f309bf604e349b603 X-Mailman-Approved-At: Tue, 06 Aug 2013 18:38:24 +0200 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 170 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375807105 news.xs4all.nl 15879 [2001:888:2000:d::a6]:53527 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52056 --047d7b15ab7f309bf604e349b603 Content-Type: text/plain; charset=ISO-8859-1 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 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 > -- - Jordi Riera, Connecting people. +33 662217507 --047d7b15ab7f309bf604e349b603 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Are you using a db for that?

Django mod= els would handle that pretty easily:

class Species= (models.Model):
=A0 =A0 =A0name =3D models.CharField()
=
class Genus(models.Model):
=A0 =A0 name =3D models.CharField= ()
=A0 =A0 species =3D models.ManyToManyField(
=A0 =A0 = =A0 =A0 =A0Species, related_name =3D 'genus_species'
=A0 = =A0 )

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.

Regards
Jordi


On Tue, Aug 6, 2013 at 5:36 PM, B= rJohan <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&#= 39;.

So, my idea is to let the 'upper' class recursively choose which of= its
nearest subclasses is the 'correct' one, until approaching a 'l= eaf' 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&= #39;m
still hopeful ... (or I'll have to investigate all those old backup-DVD= s)

[ 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 s= eems
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 'p= roblem' 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 'factor= y'. However, I would rather prefer to encapsulate those decisions at th= e class level where they 'belong'.

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



--
- Jordi Riera,=A0Connecting people.
+33 662217507
--047d7b15ab7f309bf604e349b603--