[Tutor] Accessing callers context from callee method

mobiledreamers at gmail.com mobiledreamers at gmail.com
Wed Feb 25 17:24:55 EST 2009

i found the solution
This is my first attempt at memcaching html page using cheetah
since cheetah render needs locals() i use getCallerInfo() to get the
locals() and send to memcached
let me know if it is possible to better do this
*notice getCallerInfo

def renderpage(key, htmlfile, deleteafter=3600):
    from globaldb import mc
    try:page = mc.get(key)
        clogger.info('except error mc.get '+ key)
    if not page:
        clogger.info(key+ ' rendering cheetah page')
        terms = getCallerInfo(1)
        #print terms
        page = str(web.render(htmlfile, asTemplate=True, terms=terms))
        try:mc.set(key, page, deleteafter)
            clogger.info('except error mc.set '+ key)
    return page

def mcrenderpage(key, htmlfile, deleteafter=3600):
    terms = getCallerInfo(2)
    #print terms
    return str(web.render(htmlfile, asTemplate=True, terms=terms))

def getCallerInfo(decorators=0):
    '''returns locals of caller using frame.optional pass number of
decorators\nFrom Dig deep into python internals
    f = sys._getframe(2+decorators)
    args = inspect.getargvalues(f)
    return args[3]

        return utils.renderpage(key, 'pick.html')

Bidegg worlds best auction site
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090225/40cd69af/attachment.html>

More information about the Python-list mailing list