Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Don't compare floating point numbers solely for equality.


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?marc nicole

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: mk1853387@gmail.com (marc nicole)
Newsgroups: comp.lang.python
Subject: Re: Can you help me with this memoization simple example?
Date: Sun, 31 Mar 2024 10:04:13 +0200
Lines: 103
Message-ID: <mailman.38.1711872047.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>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de y4jmEjhw7lXVAegjx7BWvg1BMuXFr2FI1gNCg4zfjjMw==
Cancel-Lock: sha1:jLnkWoNg6CIZIS8Nts1giedjvMo= sha256:JAH1npapqqYgZwGHNy9SWFS9DIpIFYIdFHgvwHDkXMM=
Return-Path: <mk1853387@gmail.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=gmail.com header.i=@gmail.com header.b=S5ejHG3Y;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'e.g.': 0.07;
'underlying': 0.07; 'cc:addr:python-list': 0.09; 'elif': 0.09;
'else:': 0.09; 'incorporated': 0.09; 'ok,': 0.09; 'received:mail-
pl1-x62c.google.com': 0.09; 'threshold': 0.09; '&gt;': 0.14;
'cc:no real name:2**0': 0.14; 'import': 0.15; 'url:mailman': 0.15;
'2024': 0.16; 'args[0]': 0.16; 'below).': 0.16; 'dict': 0.16;
'iterate': 0.16; 'key,': 0.16; 'subject:simple': 0.16; 'tuple':
0.16; 'wrote:': 0.16; 'values': 0.17; "can't": 0.17;
'cc:addr:python.org': 0.20; 'code': 0.23; 'list,': 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;
'cc:2**0': 0.25; 'url-ip:188.166/16': 0.25; 'do?': 0.26;
'creating': 0.27; 'else': 0.27; 'function': 0.27; 'email
addr:python.org&gt;': 0.28; '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; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'mean': 0.34; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'using': 0.37; 'thanks': 0.38;
'list': 0.39; 'use': 0.39; 'want': 0.40; 'should': 0.40; 'tell':
0.60; '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; 'subject:you':
0.84; '500000': 0.84; 'email name:&lt;python-list': 0.84;
'\xc3\xa9crit\xc2\xa0:': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1711872043; x=1712476843; darn=python.org;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:from:to:cc:subject:date:message-id:reply-to;
bh=6AsXCmAhTWxMiBfiHDXVRp/itOhTPMkXrCa8FX2gxwQ=;
b=S5ejHG3YVh70ZScjguOyU74Px1QMt2NH/kPSuY9A0gqtWHcUMH5gm2AZtbtZ0SPO2Y
XfxTWyesy07gFMa6Q82Wuf4aPqZh05mmpJ8qIQ/mif/pyX1doWQzbM/70fNx1uoh07Hr
31SvLonxN4v8CAANaXB6/9sqOjv6L+Oz6DFjT+xOhUA4m15eGXdA5xjDurEGOcU0Mu7H
+uGWqC/2715Xmv1X2uB3C9lozPstwI0qMlTnL2jP1dANZZAlQfZ65nQcuM1Am1v088eQ
dxgrWys6NIJmBQLYXDnOjbhzvEsCdvpyqjXQdBDSapuVguNOZHzLhZBTC3PO190IkGBO
Tadg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1711872043; x=1712476843;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id
:reply-to;
bh=6AsXCmAhTWxMiBfiHDXVRp/itOhTPMkXrCa8FX2gxwQ=;
b=H6swWwgk8QtmylLKtZ7vDSLuFgKoveySFZrnkelsnJ4N4286eiJWFqbXt2RwUA1LH0
LO3U/vypEhVSPXVf32Hg/v80+nq5AlFbrbpquvxrSRtdk2d9+P1mbUuzDgbZt/mG5y6B
/X60YBExq94f4fEHuhePXj9gPFKL/Q308Us5+YKfpGH9+L1bt3KQo29huvxFo6JHLcOf
U+NJ+nQxfNrepKP7oeRLn2cLa89NaY02b8Gv8QRnYSc06uPL2gcRJaGRSbEiYX+DACfo
TDHVLdZwtXfRNJuib/0XxI+681S1ZgYy9Wv2JYNW8dQH/8OCWg0jHykbMRrwB3tMqdqT
YJHw==
X-Gm-Message-State: AOJu0YynLcqkd/f9A0nno8hcfcRMcbQE+e5olvGV7F91aySDBei6TUFL
0U5UgUN5cTSPDsXT71UmLu0w1V+H+oseH3zB69dDYH1hThJDltbFZk7hAPk7U8hAsJUsdERybh0
Q8m/v49eUcqSLUpvJyJP04LcijUtEFFXw/rs=
X-Google-Smtp-Source: AGHT+IGHrQcok2Toz+PEkmtiFk1oW74ZK7FOCdQVS2kSvZUiBlc76ZFzeYSq0q/A9liEUFo/QhWxewA0sxR771GRByQ=
X-Received: by 2002:a17:90a:6007:b0:29d:f0f4:1e63 with SMTP id
y7-20020a17090a600700b0029df0f41e63mr4902975pji.39.1711872043422; Sun, 31 Mar
2024 01:00:43 -0700 (PDT)
In-Reply-To: <01e1e4ee-e770-4f69-8dfd-ba8b7bb0a6ae@mrabarnett.plus.com>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CAGJtH9TrQtjUU-qbLFA3xSmPVZ-R+CvTmpavnFTg793npdeF+w@mail.gmail.com>
X-Mailman-Original-References: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com>
<01e1e4ee-e770-4f69-8dfd-ba8b7bb0a6ae@mrabarnett.plus.com>
 by: marc nicole - Sun, 31 Mar 2024 08:04 UTC

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?

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.8
clearnet tor