Questions about remembering and caching function arguments

Discussion in 'Python' started by Anand Patil, Nov 12, 2007.

  1. Anand Patil

    Anand Patil Guest

    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
     
    Anand Patil, Nov 12, 2007
    #1
    1. Advertising

  2. Anand Patil

    thebjorn Guest

    On Nov 12, 1:05 am, "Anand Patil" <>
    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
     
    thebjorn, Nov 12, 2007
    #2
    1. Advertising

  3. thebjorn a écrit :
    > On Nov 12, 1:05 am, "Anand Patil" <>
    > 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
     
    =?ISO-8859-1?Q?Aur=E9lien_Camp=E9as?=, Nov 12, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    692
    sushant
    Jan 20, 2005
  2. tutmann
    Replies:
    4
    Views:
    454
  3. jmborr
    Replies:
    1
    Views:
    446
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    851
  5. Alex Wolff
    Replies:
    0
    Views:
    154
    Alex Wolff
    Sep 17, 2004
Loading...

Share This Page