I've seen How to use a context manager inside a decorator and how to pass an object created in decorator to decorated function as well as python decorators with parameters, and I'm trying to combine the two..but I'm struggling to get my head round it.
I'd much rather use the func tools @wrap
decorator to do this if possible, as I know if will preserve the doc string.
What I want to do is this:
def pyro_opener(func,service,database,port,secret_key):
def wrapper(params):
with Pyro4.Proxy("PYRO:"+service+"@"+database+":"+port) as obj:
obj.set_secret_key(secret_key)
return obj.func(params)
return wrapper
@pyro_opener(output_service, employee_db,port=9876,secret_key="h3llow0rld")
def get_employee_names(salary):
return obj.return_employee_names(salary) # obj is clearly not in scope here
# but what else can I do?
get_employee_names(25000)
>>>> Bob, Jane, Mary
I don't think this works this way, the method return_employee_names
is on the service at the other end of the connection. Should I just return the function call? If so how do I pass the params in then?
get_employee_names
where do I get the obj param from? Also, I thought that using@wraps
mean I wouldn't need the extra level of decoratoryness...? – Michaud