Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Questions about remembering and caching function arguments

Reply
Thread Tools

Questions about remembering and caching function arguments

 
 
Anand Patil
Guest
Posts: n/a
 
      11-12-2007
Hi all,

I have two questions about a class, which we'll call MyWrapperClass,
in a package to which I'm contributing.


1) MyWrapperClass wraps functions. Each instance has an attribute
called 'value' and a method called 'eval', which calls the wrapped
function. An instance D that depends on instances A, B and C can be
created as follows:

@mywrapperclass
def D(A, B, C):
return foo(A.value, B.value, C.value)

Now that D exists, the call D.eval() will work without any arguments
(D remembers that the arguments are A, B and C and passes their values
to foo). What is the standard terminology for such classes, and does
anyone know of a package that implements them in a nice way? (It's
easy enough to roll our own, but reading about other implementations
might give us good ideas).


2) D.eval() will frequently be called multiple times in succession
before A.value, B.value or C.value has had the chance to change. It
would be _extremely_ helpful to have D detect this situation and skip
recomputation. I'm looking for the fastest safe way to do this.
There's no restriction on what kind of object A.value, etc. are, so
unfortunately they might be mutable.

Our current solution is to have D compare A.value, B.value and C.value
to an internal cache using the 'is' operator (and put a big warning in
the docs about not changing 'value' attributes in-place). Not exactly
safe, but the speed savings over comparison with '==' will be
significant. Is this OK, bad or an abomination?

Again it would be helpful to know the terminology associated with the
behavior I'm looking for and any packages that implement it nicely.


Many thanks in advance and apologies for the long post,
Anand
 
Reply With Quote
 
 
 
 
thebjorn
Guest
Posts: n/a
 
      11-12-2007
On Nov 12, 1:05 am, "Anand Patil" <(E-Mail Removed)>
wrote:
> Hi all,
>
> I have two questions about a class, which we'll call MyWrapperClass,
> in a package to which I'm contributing.
>
> 1) MyWrapperClass wraps functions. Each instance has an attribute
> called 'value' and a method called 'eval', which calls the wrapped
> function. An instance D that depends on instances A, B and C can be
> created as follows:
>
> @mywrapperclass
> def D(A, B, C):
> return foo(A.value, B.value, C.value)
>
> Now that D exists, the call D.eval() will work without any arguments
> (D remembers that the arguments are A, B and C and passes their values
> to foo). What is the standard terminology for such classes, and does
> anyone know of a package that implements them in a nice way? (It's
> easy enough to roll our own, but reading about other implementations
> might give us good ideas).
>
> 2) D.eval() will frequently be called multiple times in succession
> before A.value, B.value or C.value has had the chance to change. It
> would be _extremely_ helpful to have D detect this situation and skip
> recomputation. I'm looking for the fastest safe way to do this.
> There's no restriction on what kind of object A.value, etc. are, so
> unfortunately they might be mutable.
>
> Our current solution is to have D compare A.value, B.value and C.value
> to an internal cache using the 'is' operator (and put a big warning in
> the docs about not changing 'value' attributes in-place). Not exactly
> safe, but the speed savings over comparison with '==' will be
> significant. Is this OK, bad or an abomination?
>
> Again it would be helpful to know the terminology associated with the
> behavior I'm looking for and any packages that implement it nicely.
>
> Many thanks in advance and apologies for the long post,
> Anand


Cells (A dataflow extension to CLOS) seems like what you want:

http://common-lisp.net/project/cells/

I think there was talk of a Python version a while back...

-- bjorn

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Aur=E9lien_Camp=E9as?=
Guest
Posts: n/a
 
      11-12-2007
thebjorn a écrit :
> On Nov 12, 1:05 am, "Anand Patil" <(E-Mail Removed)>
> wrote:
>> Hi all,
>>
>> I have two questions about a class, which we'll call MyWrapperClass,
>> in a package to which I'm contributing.
>>
>> 1) MyWrapperClass wraps functions. Each instance has an attribute
>> called 'value' and a method called 'eval', which calls the wrapped
>> function. An instance D that depends on instances A, B and C can be
>> created as follows:
>>
>> @mywrapperclass
>> def D(A, B, C):
>> return foo(A.value, B.value, C.value)
>>
>> Now that D exists, the call D.eval() will work without any arguments
>> (D remembers that the arguments are A, B and C and passes their values
>> to foo). What is the standard terminology for such classes, and does
>> anyone know of a package that implements them in a nice way? (It's
>> easy enough to roll our own, but reading about other implementations
>> might give us good ideas).
>>
>> 2) D.eval() will frequently be called multiple times in succession
>> before A.value, B.value or C.value has had the chance to change. It
>> would be _extremely_ helpful to have D detect this situation and skip
>> recomputation. I'm looking for the fastest safe way to do this.
>> There's no restriction on what kind of object A.value, etc. are, so
>> unfortunately they might be mutable.
>>
>> Our current solution is to have D compare A.value, B.value and C.value
>> to an internal cache using the 'is' operator (and put a big warning in
>> the docs about not changing 'value' attributes in-place). Not exactly
>> safe, but the speed savings over comparison with '==' will be
>> significant. Is this OK, bad or an abomination?
>>
>> Again it would be helpful to know the terminology associated with the
>> behavior I'm looking for and any packages that implement it nicely.
>>
>> Many thanks in advance and apologies for the long post,
>> Anand

>
> Cells (A dataflow extension to CLOS) seems like what you want:
>
> http://common-lisp.net/project/cells/
>
> I think there was talk of a Python version a while back...
>
> -- bjorn
>


pycells : http://pycells.pdxcb.net/

but also trellis : http://peak.telecommunity.com/DevCenter/Trellis

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM
IIS remembering/caching password indefinately. Alex Wolff ASP General 0 09-17-2004 12:59 PM



Advertisments