I need to convert from strings to the code named references. The only currently known option is a script that makes a script, and I was thinking it could be inline like exec.
class finder:
def __init__(self, parent):
self.parent = parent
def isin(self, what):
return what in self.parent
def find(self, what):
if self.isin(what): return self.parent.__dict__[what.__name__]
else: return __import__("pull").__dict__["downloader"](self.parent.hosts).download(what)
def put(self, what):
f = self.find(what)
self.parent.__dict__[f.__name__] = f
from sock import Socket
from unpack import unpacker
…
class downloader:
def __init__(self, hosts):
self.hosts = hosts
def download(name):
for host in hosts:
pack = unpacker()
s = Socket(host[0], host[1])
s.send("get " + name)
f = pack.unpack(s)
if f is not None: return f
I may be wrong, but I think what you’re looking for is the ability to find the symbol for a method or attribute based on a string that is determined at runtime. If that’s the case, I would pursue a different approach than a strategy based around
execorevalwhich have problems, like potential python injection attacks and tricky debugging…In python everything is an object, including modules, classes and methods. How this helps you is that a module, let’s call it
spammay have a method, let’s call iteggsyou can access theeggsmethod by it’s name like this:Now you may be asking “But how do I find the
spammodule by it’s name?” This can done by importing the module by name using__import__:So, to call the
eggsmethod with no arguments: