[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

call 'the following function' using decorators

Aaron Brady

2/12/2008 5:21:00 PM

I assert it's easier to write:

start_new_thread( this_func )
def thrA():
normal_suite()

than

def thrA():
normal_suite()
start_new_thread( thrA )

If you don't, stop reading. If you do, accomplish it like this:

@decwrap( start_new_thread, Link, ( 2, 3 ) )
def anonfunc( a, b ):
print( a, b )

where 'Link' replaces the following func, plus in keywords too:

@decwrap( Thread, None, target= Link, args= ( 2, 3 ) )
def sampleth( a, b ):
print( a, b )
sampleth.start()
sampleth.join()

'Link' is a pseudo-constant.

Link= object()

@decwrap follows.

def decwrap( func, *ar, **kwar ):
def predec( func2 ):
ar2= list( ar )
while Link in ar2:
ar2[ ar2.index( Link ) ]= func2
kwar2= kwar.copy()
for k, v in kwar2.items():
if v is not Link: continue
kwar2[ k ]= func2
ret= func( *ar2, **kwar2 )
return ret
return predec

Further applications:

@decwrap( button.bind, "<Key-X>", Link )
def anonfunc():
print( 'Key X pressed' )

Optional stylism for readability:

@decwrap( start_new_thread, ~Link, ( 2, 3 ) )
@decwrap( Thread, None, target= ~Link, args= ( 2, 3 ) )
@decwrap( button.bind, "<Key-X>", ~Link )

where 'Link' is:

class NegMarking:
def __invert__( self ): return self

Link= NegMarking()
6 Answers

Gabriel Genellina

2/12/2008 6:05:00 PM

0

En Tue, 12 Feb 2008 15:20:32 -0200, <castironpi@gmail.com> escribi�:

> I assert it's easier to write:
>
> start_new_thread( this_func )
> def thrA():
> normal_suite()
>
> than
>
> def thrA():
> normal_suite()
> start_new_thread( thrA )
>
> If you don't, stop reading. If you do, accomplish it like this:
>
> @decwrap( start_new_thread, Link, ( 2, 3 ) )
> def anonfunc( a, b ):
> print( a, b )

And I have to *guess* that start_new_thread is called?
A @threaded decorator might be useful, but the above isn't clear at all.

`import this` inside the interpreter.

--
Gabriel Genellina

Aaron Brady

2/12/2008 6:11:00 PM

0

On Feb 12, 12:05 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Tue, 12 Feb 2008 15:20:32 -0200, <castiro...@gmail.com> escribi?:
>
>
>
>
>
> > I assert it's easier to write:
>
> > start_new_thread( this_func )
> > def thrA():
> >     normal_suite()
>
> > than
>
> > def thrA():
> >     normal_suite()
> > start_new_thread( thrA )
>
> > If you don't, stop reading.  If you do, accomplish it like this:
>
> > @decwrap( start_new_thread, Link, ( 2, 3 ) )
> > def anonfunc( a, b ):
> >    print( a, b )
>
> And I have to *guess* that start_new_thread is called?
> A @threaded decorator might be useful, but the above isn't clear at all.
>
> `import this` inside the interpreter.
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

No new guessing. It's called in

ret= func( *ar2, **kwar2 )

You might suggest a better name, though, than decwrap. Something like
@call_here
with_specified_function. What? This probably goes under "Complex is
better than complicated."

It's not a typical decorator, but the f= g( f ) semantics come handily.

Aaron Brady

2/13/2008 2:56:00 AM

0

On Feb 12, 12:10 pm, castiro...@gmail.com wrote:
> On Feb 12, 12:05 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>
>
>
>
>
> > En Tue, 12 Feb 2008 15:20:32 -0200, <castiro...@gmail.com> escribi?:
>
> > > I assert it's easier to write:
>
> > > start_new_thread( this_func )
> > > def thrA():
> > >     normal_suite()
>
> > > than
>
> > > def thrA():
> > >     normal_suite()
> > > start_new_thread( thrA )
>
> > > If you don't, stop reading.  If you do, accomplish it like this:
>
> > > @decwrap( start_new_thread, Link, ( 2, 3 ) )
> > > def anonfunc( a, b ):
> > >    print( a, b )
>
> > And I have to *guess* that start_new_thread is called?
> > A @threaded decorator might be useful, but the above isn't clear at all.
>
> > `import this` inside the interpreter.
>
> > --
> > Gabriel Genellina- Hide quoted text -
>
> > - Show quoted text -
>
> No new guessing.  It's called in
>
>                 ret= func( *ar2, **kwar2 )
>
> You might suggest a better name, though, than decwrap.  Something like
> @call_here
> with_specified_function.  What?  This probably goes under "Complex is
> better than complicated."
>
> It's not a typical decorator, but the f= g( f ) semantics come handily.- Hide quoted text -
>
> - Show quoted text -

And Link could do well to call itself Blank/FuncBlank/NextFunc/
something clever

Aaron Brady

2/16/2008 1:55:00 AM

0

> > > > I assert it's easier to write:
>
> > > > start_new_thread( this_func )
> > > > def thrA():
> > > >     normal_suite()
>
> > > > than
>
> > > > def thrA():
> > > >     normal_suite()
> > > > start_new_thread( thrA )
>
> > > > If you don't, stop reading.

Nothing beats if forkthread(): but what are the chances of getting it
in Python?

Aaron Brady

2/17/2008 8:43:00 PM

0

On Feb 15, 7:54 pm, castiro...@gmail.com wrote:
> > > > > I assert it's easier to write:
>
> > > > > start_new_thread( this_func )
> > > > > def thrA():
> > > > >     normal_suite()
>
> > > > > than
>
> > > > > def thrA():
> > > > >     normal_suite()
> > > > > start_new_thread( thrA )
>
> > > > > If you don't, stop reading.
>
> Nothing beats if forkthread(): but what are the chances of getting it
> in Python?

AIR, as I recall, the SML equivalent is:

var TM: thread_manager

in

def forkthread():
TM.add( True, False )

in

if forkthread() thing_to_do() else other_thing_to()


where programs can refer to a thread_manager along with a memory,
which yes, is even in a functional language, well-defined.
thread_manager altertantely beta-reduces one expression at a time in
round-robin succession. thread_manager.add duplicates the current
expression, and evaluates to True in one and False in the other.



RichL

10/17/2011 1:13:00 AM

0

On Oct 16, 4:44 pm, UsurperTom <Usurper...@aol.com> wrote:
> On Oct 16, 11:23 am, "who?" <yourimageunre...@sbcglobal.net> wrote:
>
> > you hold grudges for years.
>
> I haven't addressed a post by Rich in months. He was the one who
> brought up my name in this thread when I didn't even post here.

How amusing, Tom! And ironic, too!

I couldn't help seeing this post in Jeff's response, and I must
congratulate you on your improving sense of humor. Oh, and by the
way, I'd be remiss if I didn't point out that just recently (was it
earlier today? Yesterday?) that you were rambling on about my
supposed David Black fixation!

I bought up your name because the phrase "Yoko sycophant" came up, and
a quick Google search revealed that you're the phrase's most vigorous
proponent within RMB. Do you think that's untrue?

Carry on...