473,608 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find the closest relative

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_re lative(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

May 25 '07 #1
5 2591
On May 25, 12:31 am, "jm.sur...@no.s pam.gmail.com"
<jm.sur...@gmai l.comwrote:
This is how I implemented; I guess there must be elegant way to do
this...

def find_closest_re lative(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.

May 25 '07 #2
On May 25, 12:40 pm, 7stud <bbxx789_0...@y ahoo.comwrote:
On May 25, 12:31 am, "jm.sur...@no.s pam.gmail.com"

<jm.sur...@gmai l.comwrote:
This is how I implemented; I guess there must be elegant way to do
this...
def find_closest_re lative(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.
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(AutoRi cksaw) =>
Instance(Lorry1 )
May 25 '07 #3
On May 25, 12:40 pm, 7stud <bbxx789_0...@y ahoo.comwrote:
On May 25, 12:31 am, "jm.sur...@no.s pam.gmail.com"

<jm.sur...@gmai l.comwrote:
This is how I implemented; I guess there must be elegant way to do
this...
def find_closest_re lative(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:
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.
>
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.

May 25 '07 #4
En Fri, 25 May 2007 05:09:00 -0300, jm*******@no.sp am.gmail.com
<jm*******@gmai l.comescribió:
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(AutoRi cksaw) =>
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).

--
Gabriel Genellina

May 25 '07 #5
On May 25, 12:08 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Fri, 25 May 2007 05:09:00 -0300, jm.sur...@no.sp am.gmail.com
<jm.sur...@gmai l.comescribió:
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(AutoRi cksaw) =>
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.

May 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4274
by: gsb | last post by:
I'd like to get the offset coordinates, top & left, of an embedded Flash movie. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.ca b#version=7,0,14,0" id="tabMenu" width="720" height="420"> <param name="movie" value="tabMenu.swf"> <param name="menu" value="false">
13
12304
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist. Thanks ever so much in advance
2
4511
by: Joe | last post by:
Hello all! I need to display a list of names similar to a spell checker where the list is the closest match to the name entered. Not too sure where to begin with this - any suggestions? Thanks, Joe
4
26483
by: vunet.us | last post by:
I have a DIV element. How can I find mouse position (top and left) inside of this DIV? <div onMouseMove="getPositions();" style="width:200px;height:100px"></div> function getPositions(ev){ ???????????????? }
2
7250
by: sturgeob | last post by:
Yep. Newby. I am asked to find the closest value to user defined value if not right on the mark. I can get it to determin it I have selected one of the generated numbers, but am at a loss how to have it find the closes. here is my code. Thanks if anyone could assist. #include "stdafx.h" #include <iostream> using std::cout; using std::cin; using std::endl;
3
4412
by: Alan Cohen | last post by:
This seems like a really, really dumb question, but I can't seem to find the simple answer that it seems to deserve. My C#/ASP.net 2.0 app needs to email a URL link back to the site from a web page. Sending the email is no problem, but generating this URL is giving me an annoying issue in testing because of the difference in URLs between the VS web server and IIS. Specifically, the URL that I need to send when testing locally (VS...
4
3047
by: Thomas Mlynarczyk | last post by:
Hello, I have two arrays like this: $aSearch = array( 'A', 'B', 'C.D', 'E', 'F' ); $aSubject = array( 'A' =0, 'A.B' =1, 'X' =2, 'C.D.E' =3, 'A.B.C' => 4 ); Now I want to search $aSubject for the longest key that consists of the first x elements of $aSearch concatenated by "." (where x = 1...count(
22
4706
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex: collection keys are 20, 30, 40, 50, 60, 70, 80 find key which is >= 35. would return the 30 key.
5
2843
by: p309444 | last post by:
Hi. I have an application in which the user can select a location and view it's distance from several Points Of Interests (POIs). When I retrieve these distances, I would also like to retrieve the ID's of the locations that are the next closest and the next furthest away from each POI. eg. If we have 10 locations, each of them a mile further away from a certain POI the I would like to return: The name of the POI, The distance from that...
0
8478
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8152
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6817
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1598
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.