3

I'm trying to set werkzeug LocalProxy from varying tags.

The best I've been able to do so far is:

for tag in self.filters.iterkeys():
    c = "{}_context".format(tag)
    setattr(self, c, LocalProxy(lambda: getattr(_request_ctx_stack.top.g,
                                                tag,
                                                None)))

Where if there is an attribute x set on g (g.x), my_instance.x return g.x

BUT what I really want to do is to format c above to return the local proxy without digging through my_instance

x_context = LocalProxy(lambda: g.x)

instead of what I can do now:

my_instance.x_context = LocalProxy(lambda: g.x)

only its from a list where I might need to return g.x for x_context, g.y for y_context, etc.

Doing:

c = LocalProxy(lambda: getattr(_request_ctx_stack.top.g, tag, None))

above does not work.

Its sounds even more convoluted now as I write this out, and I'm not grasping it this very second, so let me know what clarifications are in order.

EDIT:

For the curious, here is the context of where this is from:

https://github.com/thrisp/flarf/blob/master/flask_flarf/flarf.py

blueblank
  • 4,724
  • 9
  • 48
  • 73

0 Answers0