I am wondering how I could use a property of one threaded class as a variable in all other classes (kind of like a global variable), in python. Here is a brief outline.
class A(Thread):
def__init__(self):
Thread.__init__(self)
global globalTime
self.actualTime = gpsd.utc #This gets the real time from a gps module
globalTime = self.actualTime
self.classAvar1 = 1
self.start()
class B(Thread):
def __init__(self):
Thread.__init__(self)
self.classBvar2 = 2
self.actualTime = globalTime
self.start()
class C(Thread):
def __init__(self):
Thread.__init__(self)
self.classCvar3 = 3
self.actualTime = globalTime
self.start()
I am trying to declare actualTime in class A and get the time from data read from a gps module. I am then trying to pass this and only this property into classes B and C. Right now I get around it by declaring a global variable called globalTime, and setting it equal to the actualTime property in class A.
Is this the right approach? Thanks for the help!
Related
I'm trying to configure a decorator at run time. This is somewhat related to my earlier question: How to configure a decorator in Python
The motivation for this is that I'm trying to use the Thespian troupe code "as-is".
Is it legal to have this code here, where I've defined the class (and therefore called the decorator) inside a class method? Again, the reason for this is that I could feed the max_count argument prior to the decorator being call.
The module is calculator.calculator (yes, bad choice perhaps)
class Scheduler:
def __init__(self):
self.actor_system = None
def start(self):
self.actor_system = ActorSystem('multiprocTCPBase')
def stop(self):
self.actor_system.shutdown()
def launch(self, count, func_and_data, status_cb):
class CalcPayload:
def __init__(self, func_and_data, status_cb):
self.func_and_data = func_and_data
self.status_cb = status_cb
#troupe(max_count=count)
class Execute(ActorTypeDispatcher):
def receiveMsg_CalcPayload(self, msg, sender):
func = msg.func_and_data['func']
data = msg.func_and_data['data']
status_cb = msg.status_cb
self.send(sender, func(data, status_cb))
exec_actor = self.actor_system.createActor(Execute)
for index in range(len(func_and_data)):
calc_config = CalcPayload(func_and_data[index], status_cb)
self.actor_system.tell(exec_actor, calc_config)
for index in range(len(func_and_data)):
result = self.actor_system.listen(timeout)
self.actor_system.tell(exec_actor, ActorExitRequest())
For various reasons, I can't apply the decorator to the class when I use it. There is a brief discussion on this in the question I referenced.
While not invalid, it is generally inadvisable to define a class as a local variable inside a function, as it would make access to the class difficult outside the function.
Instead, you can define the classes outside the function, and apply the decorator function to the class when it's actually needed by calling the decorator function with the class object:
class CalcPayload:
def __init__(self, func_and_data, status_cb):
self.func_and_data = func_and_data
self.status_cb = status_cb
class Execute(ActorTypeDispatcher):
def receiveMsg_CalcPayload(self, msg, sender):
func = msg.func_and_data['func']
data = msg.func_and_data['data']
status_cb = msg.status_cb
self.send(sender, func(data, status_cb))
class Scheduler:
def __init__(self):
self.actor_system = None
def start(self):
self.actor_system = ActorSystem('multiprocTCPBase')
def stop(self):
self.actor_system.shutdown()
def launch(self, count, func_and_data, status_cb):
exec_actor = self.actor_system.createActor(troupe(max_count=count)(Execute))
for index in range(len(func_and_data)):
calc_config = CalcPayload(func_and_data[index], status_cb)
self.actor_system.tell(exec_actor, calc_config)
for index in range(len(func_and_data)):
result = self.actor_system.listen(timeout)
self.actor_system.tell(exec_actor, ActorExitRequest())
The actor_system is going to want to build instances of your class. That means it needs to be able to derive the class object- you cannot define it inside of a method.
If you really need to apply the decorator separately, you maybe could do
def launch(self, count, func_and_data, status_cb):
wrapped = troupe(max_count=count)(Executor)
exec_actor = self.actor_system.createActor(wrapped)
I have a question regarding the classes in python3. In my programm i have a "main" class that is getting started first and sets some parameters that are needed for the other parts of the programm. But now my other classes need some objects of the main class. How can i use the objects of the main class without initialising the main class everytime the subclass needs the object ? I looked into python inheritance but maybe i didnt understand it correctly
class Chunk(main.Main):
def __init__(self,pos_in_world_x,pos_in_world_y):
#self.chunksize = settings.blocks_per_chunk
self.prog = main.Main.prog
self.ctx = main.Main.ctx
this is my code for the subclass
You can use class variables:
class Main():
prog = 1
ctx = 2
def __init__(self):
pass
class Chunk(Main):
def __init__(self, pos_in_world_x, pos_in_world_y):
self.prog = Main.prog
self.ctx = Main.ctx
a = Chunk(3, 4)
print(a.prog) # 1
print(a.ctx) # 2
If you can't change Main to move your definitions outside __init__, then what you're asking for is impossible. If your variable is defined in __init__, you can't access it before calling __init__, i.e. initialising the class. You can't get round this.
You need a super __init__ call in your subclass and if you have that you only need to do:
class Chunk(main.Main):
def __init__(self,pos_in_world_x,pos_in_world_y):
super().__init__()
#self.chunksize = settings.blocks_per_chunk
Both of the assignments are done by Main's __init__.
But I think that Chunk has not an is-a but rather a has-a relation with Main so Main should actually be an argument rather than a super class:
class Chunk(object):
def __init__(self, main, pos_in_world_x,pos_in_world_y):
#self.chunksize = settings.blocks_per_chunk
self.prog = main.prog
self.ctx = main.ctx
Any one that creates a Chunk must pass a Main instance.
It is actually difficult to hide information in Python, so here are a few options which don't require inheritance (see jpp's answer for the inheritance option). In general, the code is easier to understand when you pass information explicitly (option 1 below, or inheritance), but there may be cases where you'd prefer the other options.
Option 1: The needed info can simply be passed as additional arguments to Chunk's __init__ (either by passing the prog and ctx values themselves, or by passing the Main class or its instance - depending on whether the needed info is stored as class or instance variables). Here's an example of passing Main as an argument:
class Main():
prog = 1
ctx = 2
def __init__(self):
pass
class Chunk():
def __init__(self, pos_in_world_x, pos_in_world_y, M):
#self.chunksize = settings.blocks_per_chunk
self.prog = M.prog
self.ctx = M.ctx
c = Chunk(3, 4, Main)
print(c.prog) # 1
print(c.ctx) # 2
Option 2: Chunk can access Main's class variables directly:
class Main():
prog = 1
ctx = 2
def __init__(self):
pass
class Chunk():
def __init__(self, pos_in_world_x, pos_in_world_y):
#self.chunksize = settings.blocks_per_chunk
self.prog = Main.prog
self.ctx = Main.ctx
c = Chunk(3, 4)
print(c.prog) # 1
print(c.ctx) # 2
Option 3: Chunk can access the instance variables of the Main instance directly (Main needs to be instantiated before Chunk is instantiated):
class Main():
def __init__(self):
self.prog = 1
self.ctx = 2
class Chunk():
def __init__(self, pos_in_world_x, pos_in_world_y):
#self.chunksize = settings.blocks_per_chunk
self.prog = m.prog
self.ctx = m.ctx
m = Main()
c = Chunk(3, 4)
print(c.prog) # 1
print(c.ctx) # 2
Hi I have staring programming with c and I conld'nt understand value scope in python.
here is my code
class ScenarioEnvironment():
def __init__(self):
print(self)
class report():
config = ScenarioEnvironment()
def __init__(self):
self.config = ScenarioEnvironment()
what happens passing config and config at init()?
and I wonder the value scope which config will be class valuable?
You need to know the differences between class attribute and instance object attribute.
Maybe these codes will help you:
class TestConfig1(object):
config = 1
def __init__(self):
self.config = 2
class TestConfig2(object):
config = 1
def __init__(self):
self.config2 = 2
if __name__ == "__main__":
print TestConfig1.config
t = TestConfig1()
print t.config
t2 = TestConfig2()
print t2.config
print t2.config2
more you can see the python blog.click here
Since your question seems a bit ambiguous, I'll just comment/fix your code:
class ScenarioEnvironment():
def __init__(self,x):
self.x = x # Assigning instance variable x to constructor parameter x.
print(self) # You're printing the object instance.
class report():
# Static variable shared amongst all classes.
config = ScenarioEnvironment(None) # Assigned to new instance of ScenarioEnvironment.
def __init__(self):
# No argument, must pass one (None).
# self.config is to a new ScenarioEnvironment instance.
self.config = ScenarioEnvironment(None)
Lets try out the classes.
Output:
s = ScenarioEnvironment(None)
r = report()
>>> <__main__.ScenarioEnvironment instance at 0x026F4238>
>>> <__main__.ScenarioEnvironment instance at 0x026F4300>
>>> <__main__.ScenarioEnvironment instance at 0x026F4350>
I've written a good chunk of code that relies heavily on both inheritances and composition. Now i've run into a problem where my hierarchy classes need variables to be shared to work between each other, but that causes the composited classes to be shared too, meaning my separate instances of these classes will share values where i don't want them to. I obviously suck at explaining this with words so i wrote some code.
class First(object):
def __init__(self):
self.subvar1 = 0
self.subvar2 = 10
class Second(object):
variable3 = First()
class Third(Second):
def __init__(self):
self.variable4 = self.variable3.subvar2
Firstinstance = Third()
Secondinstance = Third()
Firstinstance.variable3.subvar1 = 50
Secondinstance.variable3.subvar2 = 0
print Firstinstance.variable3.subvar2 #<-prints 0 but i want 10
print Secondinstance.variable3.subvar1 #<-prints 50 but i want 0
Except for dumping the hierarchy system and writing one massive class where i can prevent composited classes from having their valued shared, is there any other way for me to work around this?
The problem comes from Second.variable3 being a class attribute - that is, shared by all instances of the class and it's subclasses. You want:
class Second(object):
def __init__(self):
self.variable3 = First()
class Third(Second):
def __init__(self):
super(Third, self).__init__()
self.variable4 = self.variable3.subvar2
which yields the desired results.
One way is to add
self.variable3 = First()
into the __init__ of Third, so you can keep the First and Second unchanged and prevent these values to be shared.
My problem is how to share a variable or a buffer between more than one class
e.g. writing into a single buffer from multiple classes knowing that some classes are running in a threaded environment
example
class my1(object):
def __init__(self):
self.buffer=[0]*5
self.size=0
def insert(self,data):
self.data=data
self.buffer[self.size]=self.data
self.size+=1
class my2(my1):
def __init__(self):
self.insert('data1')
class my3(my1):
def __init__(self):
self.insert('data2')
The desired result would be the buffer containing both data1 and data2 to be processed
yet the buffer within class my1 is defined inside the (init) section and cannot be shared
any suggestions?
Thanks alot
You're doing it wrong.
Just create an object of class my1 and pass it to objects of class my2 and my3.
# leave my1 as it is
class my2(): # no need to inherit from my1
def __init__(self, my1obj): # buffer is object of my1
my1obj.insert('data1')
class my3():
def __init__(self, my1obj):
my1obj.insert('data2')
mybuffer = my1()
my2obj = my2(mybuffer)
my3obj = my3(mybuffer)
If you really want the buffer to be bound to the My1 class, you can use a static class variable
class My1(object):
buffer = [0] * 5