I want to create a class derived from a Borg class that can
instantiated as part of a script or be contained in other classes.
When methods from the Borg class are called, I would like to know the
name of the class that contains the Borg class.
I've played a bit with inspect and _getframe from the sys module but
get inconsistent results. The main problem is if the Borg class is
instantiated outside a containing class, then I need to go up a
different number of stack frames. But this information isn't
available till after I've run out of stack frames.
Hopefully the following code better describes what I'm looking to do.
import sys
class Borg:
_shared_state = {}
def __init__(self):
self.__dict__=s elf._shared_sta te
class Assimilated(Bor g):
valueByCaller = {}
def __init__(self, setupvalue):
print "In Assimilated.__i nit__()"
print "setupvalue is: " + str(setupvalue)
# would like key to be name of class (or module) that
# contins Assimilated
callerID = sys._getframe(1 ).f_code.co_nam e
self.valueByCal ler[callerID] = setupvalue
print self.valueByCal ler
def action(self, calledvalue):
print "In Assimilated.act ion()"
print "self.__classna me__: " + self.__class__. __name__
print "calledvalu e is: " + str(calledvalue )
print "self.valueByCa ller"
print self.valueByCal ler
# need to get proper key depending on which class (or module)
# made the call
# print "0: " + sys._getframe(0 ).f_code.co_nam e
# print "1: " + sys._getframe(1 ).f_code.co_nam e
# print "2: " + sys._getframe(2 ).f_code.co_nam e
# print "3: " + sys._getframe(3 ).f_code.co_nam e
callerID = sys._getframe(2 ).f_code.co_nam e
print "callerID"
print callerID
if(self.valueBy Caller[callerID] <= calledvalue):
print "doing the action"
class A:
assim_object = Assimilated(2)
def __init__(self):
self.assim_obje ct.action(2)
self.assim_obje ct.action(3)
class B:
assim_object = Assimilated(3)
def __init__(self):
self.assim_obje ct.action(3)
self.assim_obje ct.action(4)
class C:
assim_object = Assimilated(4)
def __init__(self):
self.assim_obje ct.action(4)
self.assim_obje ct.action(5)
a=A()
b=B()
c=C()
obj=Assimilated (3)
#obj.action(3)
When I run this, I get the following output:
In Assimilated.__i nit__()
setupvalue is: 2
{'A': 2}
In Assimilated.__i nit__()
setupvalue is: 3
{'A': 2, 'B': 3}
In Assimilated.__i nit__()
setupvalue is: 4
{'A': 2, 'C': 4, 'B': 3}
In Assimilated.act ion()
self.__classnam e__: Assimilated
calledvalue is: 2
self.valueByCal ler
{'A': 2, 'C': 4, 'B': 3}
callerID
<module>
Traceback (most recent call last):
File "\CallerID. py", line 67, in <module>
a=A()
File "\CallerID. py", line 49, in __init__
self.assim_obje ct.action(2)
File "\CallerID. py", line 41, in action
if(self.valueBy Caller[callerID] <= calledvalue):
KeyError: '<module>'
What I found most peculiar when I started this was that the
valueByCaller dictionary was completely populated before the __init__
method of a was executed. I'm pretty sure that this has to do with
the difference between when the object gets instanced and when it gets
initialized, but I need to do some more research and reading to be
able to explain it to myself.
Thanks for any help you can give me.
Kevin