Python lookup function

2019-09-06 13:32发布

问题:

I want to set up a lookup function with mako. on top of the template, i have

<%!
    lookup = { 'key': function }
%>

<%def name="function()">
    Output
</%def>

so i can use it later

<%def name="body()">
    ${lookup['key']()}
</%def>

this gives me a function is not a defined error. can i get around this?

i know why it doesn't work, as it runs first, before the method gets loaded, but how else would i set this up?

回答1:

I can tell you why it isn't working, but I do not have a clean solution at this point. Your template as given compiles into this Python code:

# -*- encoding:utf-8 -*-
from mako import runtime, filters, cache
UNDEFINED = runtime.UNDEFINED
__M_dict_builtin = dict
__M_locals_builtin = locals
_magic_number = 5
_modified_time = 1285968547.0498569
_template_filename='<snip>'
_template_uri='<snip>'
_template_cache=cache.Cache(__name__, _modified_time)
_source_encoding='utf-8'
_exports = ['function']


# SOURCE LINE 1

lookup = { 'key': function }


def render_body(context,**pageargs):
    context.caller_stack._push_frame()
    try:
        __M_locals = __M_dict_builtin(pageargs=pageargs)
        __M_writer = context.writer()
        # SOURCE LINE 3
        __M_writer(u'\n\n')
        # SOURCE LINE 7
        __M_writer(u'\n')
        return ''
    finally:
        context.caller_stack._pop_frame()


def render_function(context):
    context.caller_stack._push_frame()
    try:
        __M_writer = context.writer()
        # SOURCE LINE 5
        __M_writer(u'\n    Output\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

As you can see your function has actually been defined as render_function. The Mako docs specify how to call defs from outside a template, but they do not indicate how to do this properly at runtime. The code that I have linked too simply does a lookup for "render_%s" % name (see mako.template, line 217), so you might consider just doing that.



回答2:

Maybe you could delay the lookup of function from dict-creation time to invocation time?

<%!
    lookup = { 'key': lambda: function() }
%>

I haven't used Mako, but it works in the Python shell:

>>> x = lambda: foo()
>>> x
<function <lambda> at 0x10047e050>
>>> x()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
NameError: global name 'foo' is not defined
>>> def foo():
...    print "Test"
... 
>>> x()
Test


标签: python mako