getattr

XML Parsing to get Attribute Value

爱⌒轻易说出口 提交于 2019-12-04 16:19:06
I am parsing a xml using SAX Parser. Everythings working fine when the data I need to get is the body of a xml tag. The only problem I am getting is when the data I need is the attribute value of that XML tag. How do i get this attribute value? <xml att="value"> Body </xml> Suppose this is a tag, I am able to get Body but not value code I am using is: URL url = new URL("http://example.com/example.xml"); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); ExampleHandler myExampleHandler = new ExampleHandler(); xr

setattr and getattr with methods

雨燕双飞 提交于 2019-12-04 07:29:31
I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self): self.refClass.action1() def action2(self): self.refClass.action2() def action3(self): self.refClass.action3() This is the refClass: class RefClass(): def __init__(self): self.myClass = MyClass(self) def action1(self): #Stuff to execute action1 def action2(self): #Stuff to execute action2 def action3(self): #Stuff to execute action3 I'd like to use Python Metaprogramming to make this more elegant and

doc for __getattr__ defined attributes

蹲街弑〆低调 提交于 2019-12-03 16:15:51
I have to customize __getattr__ to call another function to read. This works well except the help(object.attr) does not work. This code is used in an interactive environment, so the help() becomes important to us. Is there a better design to achieve same features but with help() working well. The text that is used for "help" is indeed the " __doc__ " attribute of an object. The matter is that depending on the object you have, you can't simply set the __doc__ attribute on it. If what you need is " help(object.attr) " to work (and not that help(object) shows you all possible attributes) it is a

Is it bad practice to use python's getattr extensively?

只谈情不闲聊 提交于 2019-12-03 12:20:21
I'm creating a shell-like environment. My original method of handleing user input was to use a dictionary mapping commands (strings) to methods of various classes, making use of the fact that functions are first class objects in python. For flexibility's sake (mostly for parsing commands), I'm thinking of changing my setup such that I'm using getattr(command), to grab the method I need and then passing arguments to it at the end of my parser. Another advantage of this approach is not having to update my (currently statically implemented) command dictionary every time I add a new method/command

How are arguments passed to a function through __getattr__

随声附和 提交于 2019-12-03 07:27:38
问题 Consider the following code example (python 2.7): class Parent: def __init__(self, child): self.child = child def __getattr__(self, attr): print("Calling __getattr__: "+attr) if hasattr(self.child, attr): return getattr(self.child, attr) else: raise AttributeError(attr) class Child: def make_statement(self, age=10): print("I am an instance of Child with age "+str(age)) kid = Child() person = Parent(kid) kid.make_statement(5) person.make_statement(20) it can be shown, that the function call

How to get foreign key values with getattr from models

落爺英雄遲暮 提交于 2019-12-03 00:15:52
i have a model Project and i am getting the attributes of that with the following instr attr = getattr(project, 'id', None) project is the instance, id is the field and None is the default return type. my question is what if i want to get the F.K keys with this? Get customer name project.customer.name How to get customer name with the above condition? Already Tried if callable(attr): context[node][field] = '%s' % attr() Current Code context = {'project': {}} fields = ('id', 'name', 'category', 'created_by', customer) for field in fields: attr = getattr(project, field, None) if callable(attr):

How are arguments passed to a function through __getattr__

纵然是瞬间 提交于 2019-12-02 20:58:07
Consider the following code example (python 2.7): class Parent: def __init__(self, child): self.child = child def __getattr__(self, attr): print("Calling __getattr__: "+attr) if hasattr(self.child, attr): return getattr(self.child, attr) else: raise AttributeError(attr) class Child: def make_statement(self, age=10): print("I am an instance of Child with age "+str(age)) kid = Child() person = Parent(kid) kid.make_statement(5) person.make_statement(20) it can be shown, that the function call person.make_statement(20) calls the Child.make_statement function through the Parent 's __getattr__

python getattr built-in method executes default arguments

我们两清 提交于 2019-12-01 21:29:28
I dont know if this is something an expected behavior of getattr built_in method. getattr executes the default(3rd) argument as well even if the actual argument(2nd) satisfies the condition. Example: def func(): print('In Function') class A: def __init__(self): self.param = 12 a = A() When I run getattr(a, 'param', func()) it gives this result: In Function 12 Note the In Function which I don't want. But it works perfectly fine when I execute getattr(a, 'param1', func()) i.e output In Function But I only want result as 12 if satisfied the condition. Please let me know why getattr has such

Why is __getattr__ not called for indexing operations?

醉酒当歌 提交于 2019-12-01 14:10:37
My question: It seems that __getattr__ is not called for indexing operations, ie I can't use __getattr__ on a class A to provide A[...] . Is there a reason for this? Or a way to get around it so that __getattr__ can provide that functionality without having to explicitly define __getitem__ , __setitem__ , etc on A ? Minimal Example: Let's say I define two nearly identical classes, Explicit and Implicit . Each creates a little list self._arr on initiation, and each defines a __getattr__ that just passes all attribute requests to self._arr . The only difference is that Explicit also defines _

Why is __getattr__ not called for indexing operations?

回眸只為那壹抹淺笑 提交于 2019-12-01 12:48:23
问题 My question: It seems that __getattr__ is not called for indexing operations, ie I can't use __getattr__ on a class A to provide A[...] . Is there a reason for this? Or a way to get around it so that __getattr__ can provide that functionality without having to explicitly define __getitem__ , __setitem__ , etc on A ? Minimal Example: Let's say I define two nearly identical classes, Explicit and Implicit . Each creates a little list self._arr on initiation, and each defines a __getattr__ that