Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 March 24, 2008 11:19:22

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


Hi all,

This is more a Python issue than a Django one, but I thought the
Python-aware people that you are might be able to help me :)

I would like to do something like:

def called(arg)
if arg==True:
!!magic!!caller.return 1

def caller(arg)
called(arg)
return 2

Here, the fake !!!magic!!! represents a statement (which I ignore)
that would make the caller function return a value different from what
it'd return normally.

For example, caller(True) would return 1, and caller(False) would
return 2. The reason I want that is because I don't want the caller
function to know what's going on in the called function, and be
shortcut if the called function think it's necessary.

Would you know if that's possible, and if so, how?

I've done a bit of research and I think I've found some good pointers,
in particular using the 'inspect' library:

import inspect

def called(arg)
if arg==True:
caller_frame = inspect.stack()
...

Here 'caller_frame' contains the frame of the caller function. Now,
how can I make that frame return a particular value?

Hope that was clear... :/

Thanks!

Julien
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 24, 2008 11:49:40

Malcolm T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


On Mon, 2008-03-24 at 03:19 -0700, Julien wrote:
> Hi all,
>
> This is more a Python issue than a Django one, but I thought the
> Python-aware people that you are might be able to help me :)
>
> I would like to do something like:
>
> def called(arg)
> if arg==True:
> !!magic!!caller.return 1
>
> def caller(arg)
> called(arg)
> return 2

We seem to be turning into comp.lang.python here. :-(

Sounds like you want to use an exception and have caller catch an
exception of a particular type and then return the value in the
exception object.


> def called(arg)
> if arg==True:
> caller_frame = inspect.stack()
> ...
>
> Here 'caller_frame' contains the frame of the caller function. Now,
> how can I make that frame return a particular value?

That would be incredibly fragile and very poor programming practice, so
I'm not going to encourage it by providing an answer. Bytecode level
changes are considered hacks for good reason.

Malcolm

--
How many of you believe in telekinesis? Raise my hand...http://www.pointy-stick.com/blog/--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 24, 2008 12:01:11

Evert R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


> This is more a Python issue than a Django one, but I thought the
> Python-aware people that you are might be able to help me :)
>
> I would like to do something like:
>
> def called(arg)
> if arg==True:
> !!magic!!caller.return 1
>
> def caller(arg)
> called(arg)
> return 2
>
> Here, the fake !!!magic!!! represents a statement (which I ignore)
> that would make the caller function return a value different from what
> it'd return normally.
>
> For example, caller(True) would return 1, and caller(False) would
> return 2. The reason I want that is because I don't want the caller
> function to know what's going on in the called function, and be
> shortcut if the called function think it's necessary.
>
> Would you know if that's possible, and if so, how?

What is wrong with:

def called(arg):
if arg == True:
return True
.
.
return False

def caller(arg)
if called(arg):
return 1
return 2

If you like different return values, something like:
return (True, <other values>)
in called() and
shortcut, <return values> = called(arg)
if shortcut:
return <return values>
in caller() works for me.

As Malcolm mentioned, throwing an exception might be another good way
to do things.
Or did I miss the point of what you're trying to do?


> I've done a bit of research and I think I've found some good pointers,
> in particular using the 'inspect' library:
>
> import inspect
>
> def called(arg)
> if arg==True:
> caller_frame = inspect.stack()
> ...
>
> Here 'caller_frame' contains the frame of the caller function. Now,
> how can I make that frame return a particular value?
>
> Hope that was clear... :/
>
> Thanks!
>
> Julien
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#4 March 24, 2008 12:29:58

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


> We seem to be turning into comp.lang.python here. :-(

Good point, I'll try my chance over there.

> Sounds like you want to use an exception and have caller catch an
> exception of a particular type and then return the value in the
> exception object.

In fact, I don't want 'called' to throw an exception and 'caller' to
catch it (as suggested by Malcolm), nor do I want 'caller' to test the
result of 'called' (as suggested by Evert). I would like things to
remain completely transparent for 'caller', so the power remains in
the hands of 'called'.

I'll ask on the Python group and will post an answer here if I get
something interesting, just for the sake of closing this post.

Thanks!

Julien
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#5 March 24, 2008 12:35:41

Ned B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


I don't know how to do this in Python, if you even can. Inspect may be
just the dark scary corner to explore to find this capability if it
exists. But why would you want to do something like this? Even if
inspect could do this, it looks like called would have to have some very
specific knowledge of caller's structure to make it work.

Much much better would be to get caller and called to cooperate, and
have called simply return a value that caller would check. Or for the
two to be based on a template pattern (template as in the Gang of Four
book, not as in a Django template).

Maybe if you told us a bit more about the problem, we could help you
find a solution.

--Ned.http://nedbatchelder.com/blogJulien wrote:
> Hi all,
>
> This is more a Python issue than a Django one, but I thought the
> Python-aware people that you are might be able to help me :)
>
> I would like to do something like:
>
> def called(arg)
> if arg==True:
> !!magic!!caller.return 1
>
> def caller(arg)
> called(arg)
> return 2
>
> Here, the fake !!!magic!!! represents a statement (which I ignore)
> that would make the caller function return a value different from what
> it'd return normally.
>
> For example, caller(True) would return 1, and caller(False) would
> return 2. The reason I want that is because I don't want the caller
> function to know what's going on in the called function, and be
> shortcut if the called function think it's necessary.
>
> Would you know if that's possible, and if so, how?
>
> I've done a bit of research and I think I've found some good pointers,
> in particular using the 'inspect' library:
>
> import inspect
>
> def called(arg)
> if arg==True:
> caller_frame = inspect.stack()
> ...
>
> Here 'caller_frame' contains the frame of the caller function. Now,
> how can I make that frame return a particular value?
>
> Hope that was clear... :/
>
> Thanks!
>
> Julien
> >
>
>

--
Ned Batchelder,http://nedbatchelder.com--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#6 March 24, 2008 13:29:02

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


In fact, what I want is to hijack a view:

def hijacker(request):
if I_feel_its_necessary:
hijack_caller_view_and_return(some_json_content)

def my_view(request):
hijacker(request)
... # Continue as normal
return some_html_content

I have some long views which I'd like to modify as little as possible
and insert some extra behaviour handled by 'hijacker'. Adding just a
call to 'hijacker' to all of these views would be ideal for me because
that would be quick to modify them and also easier to maintain.
I don't want the views to care about what 'hijacker' will do. If
'hijacker' thinks it's necessary, it would force returning a certain
value (some json content in this case), otherwise it would let the
caller view do its normal business.

Any idea on how to do that, if that's even possible?

Thanks!

Julien


On Mar 24, 10:35 pm, Ned Batchelder <> wrote:
> I don't know how to do this in Python, if you even can. Inspect may be
> just the dark scary corner to explore to find this capability if it
> exists. But why would you want to do something like this? Even if
> inspect could do this, it looks like called would have to have some very
> specific knowledge of caller's structure to make it work.
>
> Much much better would be to get caller and called to cooperate, and
> have called simply return a value that caller would check. Or for the
> two to be based on a template pattern (template as in the Gang of Four
> book, not as in a Django template).
>
> Maybe if you told us a bit more about the problem, we could help you
> find a solution.
>
> --Ned.http://nedbatchelder.com/blog>
>
>
> Julien wrote:
> > Hi all,
>
> > This is more a Python issue than a Django one, but I thought the
> > Python-aware people that you are might be able to help me :)
>
> > I would like to do something like:
>
> > def called(arg)
> > if arg==True:
> > !!magic!!caller.return 1
>
> > def caller(arg)
> > called(arg)
> > return 2
>
> > Here, the fake !!!magic!!! represents a statement (which I ignore)
> > that would make the caller function return a value different from what
> > it'd return normally.
>
> > For example, caller(True) would return 1, and caller(False) would
> > return 2. The reason I want that is because I don't want the caller
> > function to know what's going on in the called function, and be
> > shortcut if the called function think it's necessary.
>
> > Would you know if that's possible, and if so, how?
>
> > I've done a bit of research and I think I've found some good pointers,
> > in particular using the 'inspect' library:
>
> > import inspect
>
> > def called(arg)
> > if arg==True:
> > caller_frame = inspect.stack()
> > ...
>
> > Here 'caller_frame' contains the frame of the caller function. Now,
> > how can I make that frame return a particular value?
>
> > Hope that was clear... :/
>
> > Thanks!
>
> > Julien
>
> --
> Ned Batchelder,http://nedbatchelder.com--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#7 March 24, 2008 13:39:13

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


Oh, I forgot to say. For while I thought I'd make hijacker a
decorator:

@hijacker
def my_view(request):
....

But that wouldn't work, since some views need to do some processing
before calling the hijacker. I think a decorator is systematically
called before the view itself so no prior processing can be done by
the view...

On Mar 24, 11:28 pm, Julien <> wrote:
> In fact, what I want is to hijack a view:
>
> def hijacker(request):
> if I_feel_its_necessary:
> hijack_caller_view_and_return(some_json_content)
>
> def my_view(request):
> hijacker(request)
> ... # Continue as normal
> return some_html_content
>
> I have some long views which I'd like to modify as little as possible
> and insert some extra behaviour handled by 'hijacker'. Adding just a
> call to 'hijacker' to all of these views would be ideal for me because
> that would be quick to modify them and also easier to maintain.
> I don't want the views to care about what 'hijacker' will do. If
> 'hijacker' thinks it's necessary, it would force returning a certain
> value (some json content in this case), otherwise it would let the
> caller view do its normal business.
>
> Any idea on how to do that, if that's even possible?
>
> Thanks!
>
> Julien
>
> On Mar 24, 10:35 pm, Ned Batchelder <> wrote:
>
> > I don't know how to do this in Python, if you even can. Inspect may be
> > just the dark scary corner to explore to find this capability if it
> > exists. But why would you want to do something like this? Even if
> > inspect could do this, it looks like called would have to have some very
> > specific knowledge of caller's structure to make it work.
>
> > Much much better would be to get caller and called to cooperate, and
> > have called simply return a value that caller would check. Or for the
> > two to be based on a template pattern (template as in the Gang of Four
> > book, not as in a Django template).
>
> > Maybe if you told us a bit more about the problem, we could help you
> > find a solution.
>
> > --Ned.http://nedbatchelder.com/blog>
> > Julien wrote:
> > > Hi all,
>
> > > This is more a Python issue than a Django one, but I thought the
> > > Python-aware people that you are might be able to help me :)
>
> > > I would like to do something like:
>
> > > def called(arg)
> > > if arg==True:
> > > !!magic!!caller.return 1
>
> > > def caller(arg)
> > > called(arg)
> > > return 2
>
> > > Here, the fake !!!magic!!! represents a statement (which I ignore)
> > > that would make the caller function return a value different from what
> > > it'd return normally.
>
> > > For example, caller(True) would return 1, and caller(False) would
> > > return 2. The reason I want that is because I don't want the caller
> > > function to know what's going on in the called function, and be
> > > shortcut if the called function think it's necessary.
>
> > > Would you know if that's possible, and if so, how?
>
> > > I've done a bit of research and I think I've found some good pointers,
> > > in particular using the 'inspect' library:
>
> > > import inspect
>
> > > def called(arg)
> > > if arg==True:
> > > caller_frame = inspect.stack()
> > > ...
>
> > > Here 'caller_frame' contains the frame of the caller function. Now,
> > > how can I make that frame return a particular value?
>
> > > Hope that was clear... :/
>
> > > Thanks!
>
> > > Julien
>
> > --
> > Ned Batchelder,http://nedbatchelder.com--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#8 March 24, 2008 14:28:03

Medardo R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


On Mon, Mar 24, 2008 at 6:19 AM, Julien <> wrote:
> I would like to do something like:
>
> def called(arg)
> if arg==True:
> !!magic!!caller.return 1
>
> def caller(arg)
> called(arg)
> return 2
>


"called" look like a precondition constrain, then It will be nice
to program these features adding this paradigm to python. Next is just
an very reduced example:

<code>
#!/usr/bin/python

class PreconditionViolationError(AssertionError): pass

def requires(predicate, help=None):
'Pre-condition as inhttp://en.wikipedia.org/wiki/Design_by_contract'
def decorator(wrapped):
def wrapper(*args, **kwds):
if predicate(*args, **kwds):
return wrapped(*args, **kwds)
else:
aux = help or predicate.__doc__ or wrapper.__doc__
raise PreconditionViolationError, "%s, (%s)" %
(wrapper.__name__, help)
# return update_wrapper(wrapper, wrapped) # Requires Python 2.5
return wrapper
return decorator


def validate(called):
'Special pre-condition to return a value'
def decorator(wrapped):
def wrapper(*args, **kwds):
try:
return called(*args, **kwds)
except:
return wrapped(*args, **kwds)
return wrapper
return decorator

def called(arg):
if arg:
return 1
else:
raise PreconditionViolationError, "False arg"

@validate(called)
def caller(arg):
return 2

if __name__ == '__main__':
print 'OK: %s' % caller(True)
print 'NOT OK: %s' % caller(False)
</code>

I included a real-precondition I use in my programs. "validate" is a
metaphor for the sake of this example, to answer the Julien's question
my way.

Regards

http://en.wikipedia.org/wiki/Design_by_contract--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#9 March 25, 2008 01:45:43

Ned B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

A Python question


Since you are modifying my_view to add the call to hijacker anyway, why
not do this?

def hijacker(request):
if I_feel_its_necessary:
return HttpResponse(some_json_content)

def my_view(request):
hret = hijacker(request)
if hret: return hret
... # Continue as normal
return some_html_content

It's one more line than you are looking for, but is simple, and everyone
will understand it, and you don't have to find something complicated: it
works.

--Ned.http://nedbatchelder.com/blogJulien wrote:
> In fact, what I want is to hijack a view:
>
> def hijacker(request):
> if I_feel_its_necessary:
> hijack_caller_view_and_return(some_json_content)
>
> def my_view(request):
> hijacker(request)
> ... # Continue as normal
> return some_html_content
>
> I have some long views which I'd like to modify as little as possible
> and insert some extra behaviour handled by 'hijacker'. Adding just a
> call to 'hijacker' to all of these views would be ideal for me because
> that would be quick to modify them and also easier to maintain.
> I don't want the views to care about what 'hijacker' will do. If
> 'hijacker' thinks it's necessary, it would force returning a certain
> value (some json content in this case), otherwise it would let the
> caller view do its normal business.
>
> Any idea on how to do that, if that's even possible?
>
> Thanks!
>
> Julien
>
>
> On Mar 24, 10:35 pm, Ned Batchelder <> wrote:
>
>> I don't know how to do this in Python, if you even can. Inspect may be
>> just the dark scary corner to explore to find this capability if it
>> exists. But why would you want to do something like this? Even if
>> inspect could do this, it looks like called would have to have some very
>> specific knowledge of caller's structure to make it work.
>>
>> Much much better would be to get caller and called to cooperate, and
>> have called simply return a value that caller would check. Or for the
>> two to be based on a template pattern (template as in the Gang of Four
>> book, not as in a Django template).
>>
>> Maybe if you told us a bit more about the problem, we could help you
>> find a solution.
>>
>> --Ned.http://nedbatchelder.com/blog>>
>>
>>
>> Julien wrote:
>>
>>> Hi all,
>>>
>>> This is more a Python issue than a Django one, but I thought the
>>> Python-aware people that you are might be able to help me :)
>>>
>>> I would like to do something like:
>>>
>>> def called(arg)
>>> if arg==True:
>>> !!magic!!caller.return 1
>>>
>>> def caller(arg)
>>> called(arg)
>>> return 2
>>>
>>> Here, the fake !!!magic!!! represents a statement (which I ignore)
>>> that would make the caller function return a value different from what
>>> it'd return normally.
>>>
>>> For example, caller(True) would return 1, and caller(False) would
>>> return 2. The reason I want that is because I don't want the caller
>>> function to know what's going on in the called function, and be
>>> shortcut if the called function think it's necessary.
>>>
>>> Would you know if that's possible, and if so, how?
>>>
>>> I've done a bit of research and I think I've found some good pointers,
>>> in particular using the 'inspect' library:
>>>
>>> import inspect
>>>
>>> def called(arg)
>>> if arg==True:
>>> caller_frame = inspect.stack()
>>> ...
>>>
>>> Here 'caller_frame' contains the frame of the caller function. Now,
>>> how can I make that frame return a particular value?
>>>
>>> Hope that was clear... :/
>>>
>>> Thanks!
>>>
>>> Julien
>>>
>> --
>> Ned Batchelder,http://nedbatchelder.com>>
> >
>
>

--
Ned Batchelder,http://nedbatchelder.com--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support