Re: [Tutor] Accessing callers context from callee method

2009-02-25 Thread mobiledreamers
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'
''
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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing callers context from callee method

2009-02-25 Thread mobiledreamers
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'
''
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  wrote:

> mobiledream...@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@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing callers context from callee method

2009-02-24 Thread A.T.Hofkamp

mobiledream...@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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing callers context from callee method

2009-02-24 Thread Alan Gauld


 wrote

when i call a method foo from another method func. can i access func 
context

variables or locals() from foo


Yes and there are several ways to do it, but nearly always this is a
bad idea. It will make foo almost totally unusable in any other
context since it will rely on the calling function having local 
variables

of a specific name (and possibly type). It is usually much better to
pass the variables into foo at call time. Or even to use global 
variables!



def func():
 i=10
 foo()


How would you write foo? Would you require the variable in func()
to be called i? Or would you assign a referece to it using a 
dictionary

to access it?

Can you explain what you really want to do with this? What is the
motivation behind the request? There is probably a better
alternative...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing callers context from callee method

2009-02-24 Thread wesley chun
> 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


A. python has statically-nested scoping, so you can do it as long as you:

1. define foo() as an inner function -- its def is contained within
func()'s def:
def func():
i = 10
def foo():
print i

2. you don't define a variable with the same name locally. in other
words, you do have access to func()'s 'i' as long as you don't create
another 'i' within foo() -- if you do, only your new local 'i' will be
within your scope.

B. another alterative is to pass in all of func()'s local variables to foo():

foo(**locals())

this will require you to accept the incoming dictionary in foo() and
access the variables from it instead of having them be in foo()'s
scope.

C. in a related note, your question is similar to that of global vs.
local variables. inside a function, you have access to the global as
long as you don't define a local using the same name. should you only
wish to manipulate the global one, i.e. assign a new value to it
instead of creating a new local variable with that name, you would
need to use the "global" keyword to specify that you only desire to
use and update the global one.

i = 0
def func():
i = 10

in this example, the local 'i' in func() hides access to the global
'i'. in the next code snippet, you state you only want to
access/update the global 'i'... IOW, don't create a local one:

i = 0
def func():
global i
i = 10

D. this doesn't work well for inner functions yet:

i = 0
def func():
i = 10
def foo():
i = 20

the 'i' in foo() shadows/hides access to func()'s 'i' as well as the
global 'i'. if you issue the 'global' keyword, that only gives you
access to the global one:

i = 0
def func():
i = 10
def foo():
global i
i = 20

you cannot get access to func()'s 'i' in this case.

E. however, starting in Python 3.x, you'll be able to do somewhat
better with the new 'nonlocal' keyword:

i = 0
print('globally, i ==', i)
def func():
i = 10
print('in func(), i ==', i)
def foo():
nonlocal i
i = 20
print('in foo(), i ==', i)
foo()
print('in func() after calling foo(), i ==', i)
func()

in this case, foo() modified func()'s 'i'.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing callers context from callee method

2009-02-24 Thread Chris Rebert
On Tue, Feb 24, 2009 at 10:53 AM,   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

You can, but it's an evil hack that I would avoid if at all possible;
there are almost certainly better ways to accomplish whatever your
goal is.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing callers context from callee method

2009-02-24 Thread spir
Le Tue, 24 Feb 2009 10:53:29 -0800,
mobiledream...@gmail.com s'exprima ainsi:

> 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
> Thanks a lot

def func():
  i=10
  foo(i)

--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Accessing callers context from callee method

2009-02-24 Thread mobiledreamers
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
Thanks a lot
-- 
Bidegg worlds best auction site
http://bidegg.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor