Find the closest relative

J

jm.suresh

Hi I have three objects, all of them are instances of classes derived
from a base class. Now, given one of the instance, I want to find the
closest relative of the other two. How can I do this?

This is how I implemented; I guess there must be elegant way to do
this...

def find_closest_relative(a,b,c):
c1 = b.__class__
c2 = b.__class__

while True:
if isinstance(a, c1):
return b
if isinstance(a, c2):
return c
c1 = c1.__base__
c2 = c1.__base__



-
Suresh
 
7

7stud

This is how I implemented; I guess there must be elegant way to do
this...

def find_closest_relative(a,b,c):
c1 = b.__class__
c2 = b.__class__

while True:
if isinstance(a, c1):
return b
if isinstance(a, c2):
return c
c1 = c1.__base__
c2 = c1.__base__

-
Suresh

I can't see how your code does what you describe.
Now, given one of the instance, I want to find the
closest relative of the other two.

What influence would an object have over the closest relative of two
other objects? The closet relative of two other objects is
independent of any third object. Do you want to find the closest
relative of 3 objects? If so, this might work:

import inspect

class A(object): pass
class X(object): pass

class B(A, X): pass #an object of this class has A as a base class
class C(A, X): pass
class D(A, X): pass

class E(C): pass #an object of this class has A as a base class
class F(D): pass #an object of this class has A as a base class

def closestRelative(x, y, z):
b1 = inspect.getmro(x.__class__)
b2 = inspect.getmro(y.__class__)
b3 = inspect.getmro(z.__class__)

for elmt in b1:
if elmt in b2 and elmt in b3:
return elmt
return None

b = B()
e = E()
f = F()

print closestRelative(b, e, f)

However, you should probably post an example of a class structure and
describe what you want to happen when you have three instance of the
various classes.
 
J

jm.suresh

I can't see how your code does what you describe.


What influence would an object have over the closest relative of two
other objects? The closet relative of two other objects is
independent of any third object. Do you want to find the closest
relative of 3 objects? If so, this might work:

import inspect

class A(object): pass
class X(object): pass

class B(A, X): pass #an object of this class has A as a base class
class C(A, X): pass
class D(A, X): pass

class E(C): pass #an object of this class has A as a base class
class F(D): pass #an object of this class has A as a base class

def closestRelative(x, y, z):
b1 = inspect.getmro(x.__class__)
b2 = inspect.getmro(y.__class__)
b3 = inspect.getmro(z.__class__)

for elmt in b1:
if elmt in b2 and elmt in b3:
return elmt
return None

b = B()
e = E()
f = F()

print closestRelative(b, e, f)

However, you should probably post an example of a class structure and
describe what you want to happen when you have three instance of the
various classes.

Vehicle
|
|--- Two Wheeler
| |
| |--- BatteryPowered
| |--- PetrolPowered
| |--- DieselPowered
|
|--- Three Wheeler
| |
| |--- AutoRicksaw
|
|--- Four Wheeler
| |
| |--- GeneralTrans
| | |--- Car
| | |--- Car1
| | |--- Car2
| | |--- Car3
| |
| |--- PublicTrans
| | |--- Bus
| | |--- Bus1
| | |--- Bus2
| |--- Goods
| |--- Lorry
|--- Lorry1
|--- Lorry2
|--- Lorry3

Now given one instance of some type, I want to choose between second
and third, whichever
is closest relative to the first object.
Eg.
Instance(Car1), Instance(Lorry1), Instance(AutoRicksaw) =>
Instance(Lorry1)
 
J

jm.suresh

I can't see how your code does what you describe.


What influence would an object have over the closest relative of two
other objects? The closet relative of two other objects is
independent of any third object. Do you want to find the closest
relative of 3 objects? If so, this might work:

Sorry, It was nor phrased well. I want to find the closest relative of
the first object among the second and third.i.e. I want to choose
either second or third object based on how close they are on the class
hierarchy to the first object.
 
G

Gabriel Genellina

En Fri, 25 May 2007 05:09:00 -0300, (e-mail address removed)
Vehicle
|
|--- Two Wheeler
| |
| |--- BatteryPowered
| |--- PetrolPowered
| |--- DieselPowered
|
|--- Three Wheeler
| |
| |--- AutoRicksaw
|
|--- Four Wheeler
| |
| |--- GeneralTrans
| | |--- Car
| | |--- Car1
| | |--- Car2
| | |--- Car3
| |
| |--- PublicTrans
| | |--- Bus
| | |--- Bus1
| | |--- Bus2
| |--- Goods
| |--- Lorry
|--- Lorry1
|--- Lorry2
|--- Lorry3

Now given one instance of some type, I want to choose between second
and third, whichever
is closest relative to the first object.
Eg.
Instance(Car1), Instance(Lorry1), Instance(AutoRicksaw) =>
Instance(Lorry1)

If your classes actually form a tree (you have only single inheritance)
then you may use the mro():

Car1.mro() = [Car, GeneralTrans, FourWheeler, Vehicle, object]
Lorry1.mro() = [Lorry, Goods, FourWheeler, Vehicle, object]
AutoRicksaw.mro() = [ThreeWeeler, Vehicle, object]

Now you have to find the first item in Lorr1.mro that appears also in
Car1.mro, and the same for AutoRicksaw.mro; the least index on Car1.mro
wins (Or the least index in the other list; or the least sum; that depends
on your exact definition of "closest relative").
(Under the Argentinian law, you measure how "close" two relatives are,
starting with one person, going up the tree until you find a common
ancestor, and going down to the other person. That is, summing up the
indices on both "mro" lists for the common ancestor).
 
M

MRAB

En Fri, 25 May 2007 05:09:00 -0300, (e-mail address removed)
<[email protected]> escribió:


Vehicle
|
|--- Two Wheeler
| |
| |--- BatteryPowered
| |--- PetrolPowered
| |--- DieselPowered
|
|--- Three Wheeler
| |
| |--- AutoRicksaw
|
|--- Four Wheeler
| |
| |--- GeneralTrans
| | |--- Car
| | |--- Car1
| | |--- Car2
| | |--- Car3
| |
| |--- PublicTrans
| | |--- Bus
| | |--- Bus1
| | |--- Bus2
| |--- Goods
| |--- Lorry
|--- Lorry1
|--- Lorry2
|--- Lorry3
Now given one instance of some type, I want to choose between second
and third, whichever
is closest relative to the first object.
Eg.
Instance(Car1), Instance(Lorry1), Instance(AutoRicksaw) =>
Instance(Lorry1)

If your classes actually form a tree (you have only single inheritance)
then you may use the mro():

Car1.mro() = [Car, GeneralTrans, FourWheeler, Vehicle, object]
Lorry1.mro() = [Lorry, Goods, FourWheeler, Vehicle, object]
AutoRicksaw.mro() = [ThreeWeeler, Vehicle, object]

Now you have to find the first item in Lorr1.mro that appears also in
Car1.mro, and the same for AutoRicksaw.mro; the least index on Car1.mro
wins (Or the least index in the other list; or the least sum; that depends
on your exact definition of "closest relative").
(Under the Argentinian law, you measure how "close" two relatives are,
starting with one person, going up the tree until you find a common
ancestor, and going down to the other person. That is, summing up the
indices on both "mro" lists for the common ancestor).
The distance between 2 classes is (roughly) the sum of the distances
to the root class minus twice the distance from their common ancestor
to the root class, or:

def Distance(class1, class2):
set1, set2 = set(class1.mro()), set(class2.mro())
return len(set1) + len(set2) - 2 * len(set1 & set2)

Seems to work OK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top