getattr

python string to function: globals() vs sys.module vs dictionary

馋奶兔 提交于 2019-12-08 04:13:34
问题 I want to know what is the best way to map the string to a function. so far I know I can use: globals()[func_string] sys.modules[__name__] funcs_dictionary["func_string" : func] Here is the code: >>> def foo(arg): ... print "I'm foo: %s" % arg ... >>> def bar(arg): ... print "I'm bar: %s" % arg ... >>> >>> foo <function foo at 0xb742c7d4> >>> bar <function bar at 0xb742c80c> >>> globals()[func_string]: >>> def exec_funcs_globals(funcs_string): ... for func_string in funcs_string: ... func =

python string to function: globals() vs sys.module vs dictionary

浪尽此生 提交于 2019-12-07 18:11:27
I want to know what is the best way to map the string to a function. so far I know I can use: globals()[func_string] sys.modules[__name__] funcs_dictionary["func_string" : func] Here is the code: >>> def foo(arg): ... print "I'm foo: %s" % arg ... >>> def bar(arg): ... print "I'm bar: %s" % arg ... >>> >>> foo <function foo at 0xb742c7d4> >>> bar <function bar at 0xb742c80c> >>> globals()[func_string]: >>> def exec_funcs_globals(funcs_string): ... for func_string in funcs_string: ... func = globals()[func_string] ... func("from globals() %s" % func) ... >>> exec_funcs_globals(["foo", "bar"]) I

Python: Convert string into function name; getattr or equal?

≯℡__Kan透↙ 提交于 2019-12-07 08:59:08
问题 I am editing PROSS.py to work with .cif files for protein structures. Inside the existing PROSS.py, there is the following functions (I believe that's the correct name if it's not associated with any class?), just existing within the .py file: ... def unpack_pdb_line(line, ATOF=_atof, ATOI=_atoi, STRIP=string.strip): ... ... def read_pdb(f, as_protein=0, as_rna=0, as_dna=0, all_models=0, unpack=unpack_pdb_line, atom_build=atom_build): I am adding an optons parser for command line arguments,

XML Parsing to get Attribute Value

那年仲夏 提交于 2019-12-06 10:55:10
问题 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();

Is there a function like __getattr__ that gets called for class variables?

邮差的信 提交于 2019-12-06 05:48:45
I want to redefine an object's get function so that I can intercept when any attribute is requested. Using the getattr function, it doesn't catch when existing variables (eg. attr1 in this case) are requested. Is there a way around this, so I can run a function when attr1 is requested? class Test(object): attr1 = 1 def __init__(self): self.attr2 = 1 def __getattr__(self, attr): print 'GETATTR', attr a = Test() a.attr1 a.attr2 a.attr3 Output: GETATTR attr3 I'd like to see GETATTR attr1 and GETATTR attr2 in the output too. It's not a matter of class or instance variables. It's a matter of

Can XML-RPC methods be called by name (as strings) in Python?

蓝咒 提交于 2019-12-06 03:49:33
In python, calling XML-RPC methods involves invoking methods on a proxy object: from xmlrpclib import ServerProxy print ServerProxy('https://example.com/rpc').api.hello_there('John') In some other languages, like perl, you pass your method name as a method parameter. use Frontier::Client; $p = Frontier::Client->new(url => 'https://example.com/rpc'); $result = $p->call('api.hello_there', 'John'); print $result; Is there a way to invoke XML-RPC methods by name, as strings, in Python? Just use getattr , as with any Python object. func = getattr(ServerProxy('https://example.com/rpc'), "api.hello

how to make classes with __getattr__ pickable

馋奶兔 提交于 2019-12-05 17:20:46
How can I modify the classes below to make them pickeable? This question: How to make a class which has __getattr__ properly pickable? is similar but refer to wrong exception in the use of getattr . This other question seems to provide meaningful insight Why does pickle.dumps call __getattr__? , however it fails to provide an example, and I honestly cannot understand what I am suppose to implement. import pickle class Foo(object): def __init__(self, dct): for key in dct: setattr(self, key, dct[key]) class Bar(object): def __init__(self, dct): for key in dct: setattr(self, key, dct[key]) def _

PHP approach to python's magic __getattr__()

六眼飞鱼酱① 提交于 2019-12-05 15:09:58
I was wondering if there was some way in PHP to duplicate some of the magic of Python attribute/key access. I use a Mongo ORM class written by Steve Lacey called Minimongo in which he utilizes the __getattr__ and __getitem__ to reroute key and attribute flavored access and preserve the 'document-oriented' nature of Mongo. val = doc.foo and val = doc['foo'] become equivalent. I was wondering if there is a similar interface in PHP that would allow the changing of how object access is handled for a class that inherits from it. I looked through the STL and couldn't find one that filled suit. It

Why can't I use __getattr__ with Django models?

≡放荡痞女 提交于 2019-12-05 02:52:16
I've seen examples online of people using __getattr__ with Django models, but whenever I try I get errors. (Django 1.2.3) I don't have any problems when I am using __getattr__ on normal objects. For example: class Post(object): def __getattr__(self, name): return 42 Works just fine... >>> from blog.models import Post >>> p = Post() >>> p.random 42 Now when I try it with a Django model: from django.db import models class Post(models.Model): def __getattr__(self, name): return 42 And test it on on the interpreter: >>> from blog.models import Post >>> p = Post() ERROR: An unexpected error

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

雨燕双飞 提交于 2019-12-04 18:57:12
问题 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