Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Avoid strange women and temporary variables.


devel / comp.lang.python / Re: Can you help me with this memoization simple example?

SubjectAuthor
o Re: Can you help me with this memoization simple example?MRAB

1
Re: Can you help me with this memoization simple example?

<mailman.44.1711913033.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29773&group=comp.lang.python#29773

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: python@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Can you help me with this memoization simple example?
Date: Sun, 31 Mar 2024 20:23:50 +0100
Lines: 122
Message-ID: <mailman.44.1711913033.3468.python-list@python.org>
References: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com>
<01e1e4ee-e770-4f69-8dfd-ba8b7bb0a6ae@mrabarnett.plus.com>
<CAGJtH9TrQtjUU-qbLFA3xSmPVZ-R+CvTmpavnFTg793npdeF+w@mail.gmail.com>
<022811dc-dca5-400b-bf2b-3f3e3a67f151@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de fLh8/jOJ/JYXMwMJ75+yvAZ6PvK/Q09sZm0uo5ah9diw==
Cancel-Lock: sha1:2eTUCPaNByDbecx4YiAAIGw8NYI= sha256:DWh0csBHvchZ07QL8OLwOzk2POpMfzBt2rNwEFGZK/I=
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=livkYUb8;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'e.g.': 0.07;
'underlying': 0.07; 'elif': 0.09; 'else:': 0.09;
'from:addr:python': 0.09; 'incorporated': 0.09; 'ok,': 0.09;
'received:192.168.1.64': 0.09; 'skip:\xc2 20': 0.09; 'threshold':
0.09; 'import': 0.15; 'url:mailman': 0.15; '2024': 0.16;
'args[0]': 0.16; 'argument:': 0.16; 'below).': 0.16; 'dict': 0.16;
'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
'iterate': 0.16; 'key,': 0.16; 'message-id:@mrabarnett.plus.com':
0.16; 'received:plus.net': 0.16; 'subject:simple': 0.16; 'tuple':
0.16; 'wrote:': 0.16; 'values': 0.17; "can't": 0.17; 'to:addr
:python-list': 0.20; 'code': 0.23; 'list,': 0.24; 'to:name:python-
list@python.org': 0.24; 'anything': 0.25; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'cannot': 0.25; 'url-ip:188.166/16': 0.25;
'do?': 0.26; 'creating': 0.27; 'else': 0.27; 'function': 0.27;
'header:User-Agent:1': 0.30; 'comment': 0.31; 'url-ip:188/8':
0.31; 'anybody': 0.32; 'elements': 0.32; 'python-list': 0.32;
'retrieve': 0.32; 'subject:Can': 0.32; 'received:192.168.1': 0.32;
'but': 0.32; 'header:In-Reply-To:1': 0.34; 'using': 0.37;
'received:192.168': 0.37; 'thanks': 0.38; 'list': 0.39; 'use':
0.39; 'want': 0.40; 'should': 0.40; 'tell': 0.60; 'skip:\xc2 10':
0.62; 'received:212': 0.62; 'here': 0.62; 'below:': 0.63; 'great':
0.63; 'key': 0.64; 'let': 0.66; 'subject:this': 0.67; 'attached':
0.68; '"you': 0.69; 'adds': 0.69; 'mars': 0.69; 'addition,': 0.70;
'8bit%:100': 0.76; 'subject:you': 0.84; '500000': 0.84;
'\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; '\xc3\xa9crit\xc2\xa0:':
0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1711913030; bh=HyZopbKU1YkCMEPwDCaKi6FP29BVKbpW2geS/BTaoRs=;
h=Date:Subject:To:References:From:In-Reply-To;
b=livkYUb8zlcRoIw/NpwyvtoXdGdLXq8o844mWF3tnJ7lMy6HELifhEV3OnmOc+bZA
jnx5YOlUihuecE8fLHWI64NEkwLzK5rqzoUwxcmObwlNgYGgQdhn/FBPuwHtivJQap
771uyFMvjeu5HRIpQdU3Sa/cZhuyPIFS3fT4dT7uq5SYPYWzDo/FRGuGS3VOAm4wOY
ULuoPph5wP0grAKrDVPWL2V2myyioQ3uAd+4JjvmgdxTLUj13zcD9mcXOX4YkHRbte
/xtskvr/lbdxYvKmh1mTslmVpPL4XoYw/QzvfIcI51xOVv3/a+amYzRf/CpWxwttEH
E8LE5ZxuIrikw==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=TuZFhyXh c=1 sm=1 tr=0 ts=6609b846
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=OC_A1QDR1020s07x:21 a=IkcTkHD0fZMA:10 a=8AHkEIZyAAAA:8
a=OMNsVWLo-r_ySRS9oRMA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <CAGJtH9TrQtjUU-qbLFA3xSmPVZ-R+CvTmpavnFTg793npdeF+w@mail.gmail.com>
X-CMAE-Envelope: MS4xfCZyOTk7pPApOqImuwpx6vQ+kiA6GYDBsJwWA7k0bI1vymmKGGEf6TNN9zrvkyJYfFP19g+Fou3gqeCI0qUVHE7tLfh4l93DJjLllo0J5GBrs0dTsd2i
PHHAfgkoRLsmmyeOUNjSU/pxLu5l2AqBK0n+m3AmSSYFb/seSHMz7A18bW1XvrOZ6VEjwSRSG3oN++Kk45epveZXz1cYo2/rV6E=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <022811dc-dca5-400b-bf2b-3f3e3a67f151@mrabarnett.plus.com>
X-Mailman-Original-References: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com>
<01e1e4ee-e770-4f69-8dfd-ba8b7bb0a6ae@mrabarnett.plus.com>
<CAGJtH9TrQtjUU-qbLFA3xSmPVZ-R+CvTmpavnFTg793npdeF+w@mail.gmail.com>
 by: MRAB - Sun, 31 Mar 2024 19:23 UTC

On 2024-03-31 09:04, marc nicole wrote:
> Thanks for the first comment which I incorporated
>
> but when you say "You can't use a list as a key, but you can use a
> tuple as a key,
> provided that the elements of the tuple are also immutable."
>
> does it mean  the result of sum of the array is not convenient to use
> as key as I do?
> Which tuple I should use to refer to the underlying list value as you
> suggest?
>
I was suggesting using `tuple` on the argument:

def memoize(f):
     cache = {}

     def g(*args):
         key = tuple(args[0]), args[1]

         if key not in cache:
             cache[key] = f(args[0], args[1])

         return cache[key]

     return g

> Anything else is good in my code ?
>
> Thanks
>
> Le dim. 31 mars 2024 à 01:44, MRAB via Python-list
> <python-list@python.org> a écrit :
>
> On 2024-03-31 00:09, marc nicole via Python-list wrote:
> > I am creating a memoization example with a function that adds up
> / averages
> > the elements of an array and compares it with the cached ones to
> retrieve
> > them in case they are already stored.
> >
> > In addition, I want to store only if the result of the function
> differs
> > considerably (passes a threshold e.g. 500000 below).
> >
> > I created an example using a decorator to do so, the results
> using the
> > decorator is slightly faster than without the memoization which
> is OK, but
> > is the logic of the decorator correct ? anybody can tell me ?
> >
> > My code is attached below:
> >
> >
> >
> > import time
> >
> >
> > def memoize(f):
> >      cache = {}
> >
> >      def g(*args):
> >          if args[1] == "avg":
> >              sum_key_arr = sum(list(args[0])) / len(list(args[0]))
>
> 'list' will iterate over args[0] to make a list, and 'sum' will
> iterate
> over that list.
>
> It would be simpler to just let 'sum' iterate over args[0].
>
> >          elif args[1] == "sum":
> >              sum_key_arr = sum(list(args[0]))
> >          if sum_key_arr not in cache:
> >              for (
> >                  key,
> >                  value,
> >              ) in (
> >                  cache.items()
> >              ):  # key in dict cannot be an array so I use the
> sum of the
> > array as the key
>
> You can't use a list as a key, but you can use a tuple as a key,
> provided that the elements of the tuple are also immutable.
>
> >                  if (
> >                      abs(sum_key_arr - key) <= 500000
> >                  ):  # threshold is great here so that all
> values are
> > approximated!
> >                      # print('approximated')
> >                      return cache[key]
> >              else:
> >                  # print('not approximated')
> >                  cache[sum_key_arr] = f(args[0], args[1])
> >          return cache[sum_key_arr]
> >
> >      return g
> >
> >
> > @memoize
> > def aggregate(dict_list_arr, operation):
> >      if operation == "avg":
> >          return sum(list(dict_list_arr)) / len(list(dict_list_arr))
> >      if operation == "sum":
> >          return sum(list(dict_list_arr))
> >      return None
> >
> >
> > t = time.time()
> > for i in range(200, 15000):
> >      res = aggregate(list(range(i)), "avg")
> >
> > elapsed = time.time() - t
> > print(res)
> > print(elapsed)
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor