Suppose I will send self.f to a global method,
def write_to_report(file_hdlr, content):
If I want get the what the object is the file_hdlr belonged to.
How to get it ? Because I need to know the file_hanlder is belonging which Class,
and use the class name to do somthing
def write_to_report(file_hdlr, content):
file_hdlr.__self__ # ??? NO
CODE
class MemoryInfo(threading.Thread):
def __init__(self, config):
self.f = get_export_file_hdlr(self.__class__.__name__)
class CPUInfo(threading.Thread):
def __init__(self, config):
self.f = get_export_file_hdlr(self.__class__.__name__)
You can't.
A different approach:
class ReportWriterMixin(object):
def write_to_report(self):
this_class = self.__class__
# do thing with self.f based on this_class
class CPUInfo(threading.Thread, ReportWriterMixin):
def __init__(self, config):
self.f = get_export_file_hdlr(self.__class__.__name__)
self.write_to_report()
This way, while your method is no longer "global", it can be made available to any class you like
If you want to know the type of file_hdlr variable, you can use function type:
file_hdlr_type = type(file_hdlr)
Related
lets say we have class A and it has one instance - x. How to make a child class of class A where I would be able to pass x as an argument and get all its parameters and pass it to child class object. precisely speaking I want to do something like this.
class A:
def __init__(self, parameter1, parameter2):
self.parameter1 = parameter1
self.parameter2 = parameter2
class B(A):
def __init__(self, Ainstance, someParameter):
super().__init__(**Ainstance.__dict__)
self.someParameter = someParameter
x = A(parameter1='1', parameter2='2')
x = B(x, someParameter='3')
print(x.parameter1)
print(x.parameter2)
print(x.someParameter)
the goal is to create a class where I would be able to get all the parameters of parent class object, and add my own attributes. The problem in the code above is I won't be able to do that with all classes because not all of them has __dict__ attribute.
I have this example code which I use to remind myself how to construct a proxy.
#soProxyPattern
class Example:
def __init__(self):
self.tag_name = 'name'
def foo(self):
return 'foo'
def bar(self, param):
return param
class Container:
def __init__(self, contained):
self.contained = contained
self.user_name = 'username'
def zoo(self):
return 0
def __getattr__(self, item):
if hasattr(self.contained, item):
return getattr(self.contained,item)
#raise item
c = Container(Example())
print(c.zoo())
print(c.foo())
print(c.bar('BAR'))
print(c.tag_name)
print(c.user_name)
The output is:
0
foo
BAR
name
username
This shows that Container can have its own attributes (methods or variables) which you can access over and above all of the attributes of the contained instance.
Instead of dict you could use the dir and getattr like this:
class A:
def __init__(self, parameter1, parameter2):
self.parameter1 = parameter1
self.parameter2 = parameter2
class B(A):
def __init__(self, Ainstance, someParameter):
parameters = {param: getattr(Ainstance, param) for param in dir(Ainstance) if not param.startswith("__")}
super().__init__(**parameters)
self.someParameter = someParameter
For a more detailed explanation see: Get all object attributes in Python?
Sorry for the confusing question.
Say there are two instances of two different classes (e.g. 'big_instance' and 'little_instance').
The little_instance is defined as an attribute of the big instance.
How would a method in the little class access an attribute of the big instance.
An example is below.
The line 'return parent.attribute1' is basically pseudo code. How would this line be written properly?
class BigClass:
def __init__(self, att):
self.attribute1 = att
self.little_instance = LittleClass()
class LittleClass:
def parents_att(self):
return parent.attribute1
big_instance = BigClass(1)
print(big_instance.little_instance.parents_att())
ah yes I got it. Read the comments for explanation.
The test code at the end shows that it works even after attribute1 changes :)
class BigClass:
def __init__(self, att):
self.attribute1 = att
# pass in self
self.little_instance = LittleClass(self)
class LittleClass:
def __init__(self, the_big_class):
# the big class is held in an instance var
self.the_big_class = the_big_class
def parents_att(self):
# the instance var is used to reference the attribute
return self.the_big_class.attribute1
big_instance = BigClass(1)
print(big_instance.little_instance.parents_att())
big_instance.attribute1 = 2
print(big_instance.little_instance.parents_att())
You can do the following if you want to access attribute from BigClass into LittleClass.
class BigClass:
def __init__(self, att):
self.attribute1 = att
class LittleClass(BigClass):
def __init__(self, att):
BigClass.__init__(self, att)
def parent_att(self):
return self.attribute1
small_instance = LittleClass(1)
print(small_instance.parent_att)
I have the next code:
class PythagoreanProof(Scene):
CONFIG={
"color_triangulos":YELLOW,
"color_rect_c":RED,
"color_rect_b":ORANGE,
"color_rect_a":ORANGE,
"color_cuadrado_c":ORANGE,
"opacidad_triangulos":0.6,
"opacidad_cuadradro_a":0.6,
"opacidad_cuadradro_b":0.6,
"opacidad_cuadradro_c":0.6,
"grosor_lineas":1,
"l_a":5/5,
"l_b":12/5,
"l_c":13/5,
}
def construct(self):
self.wait()
self.pre_cuadrado()
self.pos_cuadrado()
self.tran_pre_pos_cuadrado()
self.wait()
def pre_cuadrado(self):
cuadro=Square(side_length=self.l_a+self.l_b)
As you can see, I can access the key "l_a" through self.l_a
But in this much simpler code I get the following error
class Example():
CONFIG = {'spam':25}
def __init__(self, value):
self.data = value
def display(self):
print(self.data, self.spam)
x=Example(2)
x.display()
AttributeError: 'Example' object has no attribute 'spam'
Why does it work in the first example? How does it actually work?
Thanks!
As for why is works when Scene is inherited to the class PythagoreanProof you need to have a look at the code of Scene to see what it is actually doing.
In the class Example you have defined CONFIG as a class level object. Your method display needs to point to this object with self.CONFIG and then use the key 'spam' to get its value '25'.
In order ro make the keys in CONFIG attributes you can use the #poperty wrapper.
See the example below
class Example():
CONFIG = {'spam': 25}
def __init__(self, value):
self.data = value
def display(self):
print(self.data, self.CONFIG['spam'])
#property
def spam(self):
return self.CONFIG['spam']
x = Example(2)
x.display()
print(x.spam)
2 25
25
Note some of you idents are incorrect. Method display should not be a function under __init__ but a method under class Example.
I suspect there's some magic in the Scene class that plays with the __getattr__ method. Like this :
class Scene:
# Minimalistic example.
# See https://docs.python.org/3.8/reference/datamodel.html#object.__getattr__
def __getattr__(self, name):
return self.CONFIG[name]
class Example(Scene):
CONFIG = {'spam': 25}
def __init__(self, value):
self.data = value
def display(self):
print(self.data, self.spam)
x = Example(2)
x.display()
# -> 2 25
I would like to decorate certain instance functions with a decorator from a "parent" instance, is there a way that I can use the instance to decorate the functions.
Here is the thought on what I need to do;
class Foo(object):
def __init__(self):
pass
def set_configuration(self, function):
def config(*args, **kwargs):
# NOTE: this function needs to use instance variables.
print 'foo ' + function()
return config()
class Bar(object):
def __init__(self, parent):
self.parent = parent
#self.parent.set_configuration
def set_config_2(self)
return 'bar'
foo = Foo()
foo.bar = Bar(foo)
foo.bar.set_config_2
EDIT:
Ok guys here is the actual issue, I have a device that i need to interact with. So a device may have several levels to it ie a device a
has multiple interfaces and an interface may have multiple vlans attached. So the idea is that if I want to change a vlan on an interface, instead of building a full command I would like to allow the parent class to handle the building of it's level of the command. So I would like to just call the "change vlan" function and it will send it's part of the command to the next level to be wrapped and sent up the chain till it hits the device level and the full command is sent to the device.
class Device(object):
def __init__(self):
self.interfaces = list()
self.ssh = ssh('blah')
def set_device(self, function):
self.ssh.command('setup commands')
self.ssh.command(wrapped command here)
self.ssh.command('exit commands')
class Interface(object):
def __init__(self, name, parent):
self.name
self.parent
self.vlan = Vlan('name')
def set_interface(self):
return self.name
class Vlan(object):
def __init__(self, name, parent):
self.name = name
self.parent = parent
def set_vlan(self):
return self.name
I hope this makes more sense. if not please let me know.
No, you cannot use decorators here, because at definition time of Bar, parent is not known.
Simply use set_configuration with a argument:
class Foo(object):
def __init__(self):
pass
def set_configuration(self, function):
def config(*args, **kwargs):
# NOTE: this function needs to use instance variables.
print 'foo ' + function()
return config
class Bar(object):
def __init__(self, parent):
self.parent = parent
def set_config_2(self, args)
def inner_function():
return 'bar'
return self.parent.set_configuration(inner_function)(args)
foo = Foo()
foo.bar = Bar(foo)
foo.bar.set_config_2(123)
Python is a dynamic language so many things are possible. I'm making no comment about whether this is a good thing to do or not - and I really can't understand the purpose of your logic.
To make this possible you will need dynamically create the set_config_2 in Bar.__init__ as parent is unknown at the class definition time:
from types import MethodType
class Foo(object):
def __init__(self):
pass
def set_configuration(self, f):
def config(inst, *args, **kwargs):
print('foo', f(inst, *args, **kwargs))
return config
class Bar(object):
def __init__(self, parent):
self.parent = parent
#self.parent.set_configuration
def set_config_2(inst):
return 'bar'
self.set_config_2 = MethodType(set_config_2, self)
foo = Foo()
foo.bar = Bar(foo)
foo.bar.set_config_2()
Output:
foo bar
This is desperately ugly and there must be a better way of doing what you are attempting. Perhaps you can ask a different question explaining what you are trying to achieve.
Your decorator does not have to use instance methods, since that's the wrapping function config who needs them. Therefore, the decorator does not have to be a method. For example:
def set_configuration(func):
#functools.wraps(func) # copy function's metadata
def wrapper(self, *args, **kwargs):
# do whatever you want to fetch the config data
return 'foo' + func(self, *args, **kwargs)
return wrapper
That said, there likely is a more straightforward and explicit way, depending on what exactly you want.
I'm pretty sure you can do this without making the decorator an instance. Here are a couple ideas.
Invert the hierarchy
It seems to me like the hierarchy you have is backwards. My understanding:
Device is only providing the ssh instance
The common method you want to call is something the VLAN defines
The setup and exit commands are constants
By making the hierarchy go the other way, you can define the "change VLAN" method to access stuff from the lower levels that it needs.
class Device(object):
def __init__(self):
self.ssh = ssh('blah')
class Interface(object):
def __init__(self, name, device):
self.name
self.device = device
class Vlan(object):
def __init__(self, name, change_command, interface):
self.name = name
# How you actually store this command is completely up to you.
# You might want to shove it in an abstract method
# and subclass Vlan, but the point is make it part of the
# Vlan somehow.
self.change_command = change_command
self.interface = interface
def change_vlan(self):
ssh = self.interface.device.ssh
ssh.command('setup commands')
ssh.command(self.change_command)
ssh.command('exit commands')
device1 = Device()
device2 = Device()
interface1 = Interface('i1', device1)
interface2 = Interface('i2', device1)
interface3 = Interface('i3', device2)
vlans = [
Vlan('v1', 'change 1', interface1)
Vlan('v2', 'change 2', interface1)
Vlan('v3', 'change 3', interface2)
Vlan('v4', 'change 4', interface3)
]
This might not show exactly what you want to do, but hopefully it demonstrates how you can set this up with the hierarchy going the other way.
Make The decorator accept a Device
Alternatively, if you still think decorating is a better option, you can make the decorate accept the instances you need.
def ssh_command(device, function):
def execute_ssh_command(*args, **kwargs):
device.ssh.command('setup commands')
device.ssh.command(wrapped command here)
device.ssh.command('exit commands')
# Note: no parentheses. You're returning the function itself
return execute_ssh_command
class Interface(object):
def __init__(self, name, parent):
self.name
self.parent
self.vlan = Vlan('name')
#ssh_command
def set_interface(self):
return self.name
Note you'll need to make a separate subclass per whatever thing uses the decorator.
What is the best practice when using a setter method which only calls out to an external method. I don't want the user to change this value.
I have the following structure
class Instruction(object):
def __init__(self, address):
super(Instruction, self).__init__()
self.address = address
self.mnem = self.__set_mnem()
#property
def mnem(self):
return self.__mnem
def __set_mnem(self):
return extModule.GetMnem(self.address)
Should I instead have this
#mnem.setter
def mnem(self, mnem=None):
if not mnem:
self.__mnem = extModule.GetMnem(self.address)
else:
self.__mnem = None
If your property should be read-only, you simply do not give it a setter, no. Set the private attribute that the property accesses:
class Instruction(object):
def __init__(self, address):
super(Instruction, self).__init__()
self.address = address
self.__mnem = extModule.GetMnem(self.address)
#property
def mnem(self):
return self.__mnem
Now Instruction().mnem is read-only, and the value for it is set when you create the instance.