Re: Is this possible in Python? SOLUTION FOUND

2006-03-14 Thread Steven D'Aprano
On Tue, 14 Mar 2006 13:33:01 -0800, alainpoint wrote:

> 
> Kay Schluehr wrote:
>> [EMAIL PROTECTED] wrote:
>> > jalanb wrote:
>> > > You might like the version here:
>> > > http://www.jorendorff.com/toys/out.html
>> > >
>> > > Especially the "need to know" presentation, which is cute
>> > >
>> > > --
>> > > Alan
>> > > http://aivipi.blogspot.com
>> >
>> > Thank you for the tip.
>> > Meanwhile, I found a shorter solution to my problem:
>> > def magic(arg):
>> >import inspect
>> >return inspect.stack()[1][4][0].split("magic")[-1][1:-1]
>> >
>> > assert magic(3+4)=="3+4"
>> >
>> > Alain
>>
>> Does it? Using your function I keep an assertion error. Storing the
>> return value of magic()in a variable s I receive the following result:
>>
>> def magic(arg):
>> import inspect
>> return inspect.stack()[1][4][0].split("magic")[-1][1:-1]
>>
>> s = magic(3+4) # magic line
>>
>> >>> s
>> 'lin'
>>
>>
>> BTW grepping the stack will likely cause context sensitive results.
>>
>> Kay
> 
> 
> This is no production-ready code, just a proof of concept.
> Adding 3 or 4 lines would make it more robust.
> Just hope someone else will benefit from this discussion.

Doesn't work for me either:

>>> def magic(arg):
... import inspect
... return inspect.stack()[1][4][0].split("magic")[-1][1:-1]
...
>>> magic(3+4)
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 3, in magic
TypeError: unsubscriptable object


Kay gets an AssertionError, I get a TypeError. I think describing it as
"proof of concept" is rather optimistic.

Here is the inspect.stack() I get:

[(, '', 2, 'magic', None, None),
(, '', 1, '?', None, None)]


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this possible in Python? SOLUTION FOUND

2006-03-14 Thread alainpoint

Kay Schluehr wrote:
> [EMAIL PROTECTED] wrote:
> > jalanb wrote:
> > > You might like the version here:
> > > http://www.jorendorff.com/toys/out.html
> > >
> > > Especially the "need to know" presentation, which is cute
> > >
> > > --
> > > Alan
> > > http://aivipi.blogspot.com
> >
> > Thank you for the tip.
> > Meanwhile, I found a shorter solution to my problem:
> > def magic(arg):
> > import inspect
> > return inspect.stack()[1][4][0].split("magic")[-1][1:-1]
> >
> > assert magic(3+4)=="3+4"
> >
> > Alain
>
> Does it? Using your function I keep an assertion error. Storing the
> return value of magic()in a variable s I receive the following result:
>
> def magic(arg):
> import inspect
> return inspect.stack()[1][4][0].split("magic")[-1][1:-1]
>
> s = magic(3+4) # magic line
>
> >>> s
> 'lin'
>
>
> BTW grepping the stack will likely cause context sensitive results.
>
> Kay


This is no production-ready code, just a proof of concept.
Adding 3 or 4 lines would make it more robust.
Just hope someone else will benefit from this discussion.

Alain

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this possible in Python? SOLUTION FOUND

2006-03-14 Thread Kay Schluehr

[EMAIL PROTECTED] wrote:
> jalanb wrote:
> > You might like the version here:
> > http://www.jorendorff.com/toys/out.html
> >
> > Especially the "need to know" presentation, which is cute
> >
> > --
> > Alan
> > http://aivipi.blogspot.com
>
> Thank you for the tip.
> Meanwhile, I found a shorter solution to my problem:
> def magic(arg):
>   import inspect
>   return inspect.stack()[1][4][0].split("magic")[-1][1:-1]
>
> assert magic(3+4)=="3+4"
>
> Alain

Does it? Using your function I keep an assertion error. Storing the
return value of magic()in a variable s I receive the following result:

def magic(arg):
import inspect
return inspect.stack()[1][4][0].split("magic")[-1][1:-1]

s = magic(3+4) # magic line

>>> s
'lin'


BTW grepping the stack will likely cause context sensitive results.

Kay

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this possible in Python? SOLUTION FOUND

2006-03-14 Thread alainpoint

jalanb wrote:
> You might like the version here:
> http://www.jorendorff.com/toys/out.html
>
> Especially the "need to know" presentation, which is cute
>
> --
> Alan
> http://aivipi.blogspot.com

Thank you for the tip.
Meanwhile, I found a shorter solution to my problem:
def magic(arg):
import inspect
return inspect.stack()[1][4][0].split("magic")[-1][1:-1]

assert magic(3+4)=="3+4"

Alain

-- 
http://mail.python.org/mailman/listinfo/python-list