[Tutor] Accessing callers context from callee method

mobiledreamers at gmail.com mobiledreamers at gmail.com
Wed Feb 25 23:24:27 CET 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
*

*utils.py*
@log_time_func
def renderpage(key, htmlfile, deleteafter=3600):
    from globaldb import mc
    try:page = mc.get(key)
    except:
        page=None
        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)
        except:
            clogger.info('except error mc.set '+ key)
    return page

@log_time_func
@memcachethis
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
http://www.devx.com/opensource/Article/31593/1954'<http://www.devx.com/opensource/Article/31593/1954%27>
''
    f = sys._getframe(2+decorators)
    args = inspect.getargvalues(f)
    return args[3]

*Usage*
        key=facebookstuff.APP_NAME+'newstart'+str(uid)
        return utils.renderpage(key, 'pick.html')



-- 
Bidegg worlds best auction site
http://bidegg.com

On Tue, Feb 24, 2009 at 11:21 PM, A.T.Hofkamp <a.t.hofkamp at tue.nl> wrote:

> mobiledreamers at gmail.com wrote:
>
>> when i call a method foo from another method func. can i access func
>> context
>> variables or locals() from foo
>> so
>> def func():
>>  i=10
>>  foo()
>>
>> in foo, can i access func's local variables on in this case i
>>
>
> As others have already pointed out, this is a really bad idea.
> Instead you can do:
>
>
> def func():
>  i = 10
>  i = foo(i)
>
> def foo(i):
>  i = i + 1
>  return i
>
> Sincerely,
> Albert
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090225/456c2037/attachment-0001.htm>


More information about the Tutor mailing list