Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Disobedience: The silver lining to the cloud of servitude. -- Ambrose Bierce


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?<avi.e.gross

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: Can you help me with this memoization simple example?
Date: Sun, 31 Mar 2024 16:56:19 -0400
Lines: 150
Message-ID: <mailman.45.1711918584.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>
<008901da83ad$e17541f0$a45fc5d0$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de e2Kwv+I5UlS6vhgRLjoLTQ9rwhGIm9Qg1wI7b0CeiVRg==
Cancel-Lock: sha1:Lr8w52RAu8G1BWPV+0IX9y8JjSs= sha256:+krclI24zdIrt4VoNMMYQ1Kc9IQdJqH6jjVjpIrkwiQ=
Return-Path: <avi.e.gross@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=C6ct4HRT;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '31,': 0.05;
'e.g.': 0.07; 'underlying': 0.07; 'elif': 0.09; 'else:': 0.09;
'incorporated': 0.09; 'ok,': 0.09; 'overhead': 0.09;
'received:108': 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; 'iterate': 0.16; 'key,':
0.16; 'relatively': 0.16; 'subject:simple': 0.16; 'tuple': 0.16;
'tuples': 0.16; 'wrote:': 0.16; 'python': 0.16; 'values': 0.17;
"can't": 0.17; 'message-id:@gmail.com': 0.18; 'uses': 0.19;
'to:addr:python-list': 0.20; 'code': 0.23; 'list,': 0.24;
'anything': 0.25; 'skip:- 10': 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; 'requests':
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; 'but': 0.32; 'there': 0.33; 'march': 0.33;
'mean': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'from:addr:gmail.com': 0.35; 'lists': 0.37; 'using': 0.37;
'thanks': 0.38; 'enough': 0.39; 'list': 0.39; 'use': 0.39;
'decide': 0.39; 'want': 0.40; 'should': 0.40; 'tell': 0.60;
'from:': 0.62; 'to:': 0.62; 'here': 0.62; 'below:': 0.63; 'great':
0.63; 'copy': 0.63; 'key': 0.64; 'clear': 0.64; 're:': 0.64;
'thus': 0.64; 'similar': 0.65; 'look': 0.65; 'let': 0.66;
'subject:this': 0.67; 'attached': 0.68; '"you': 0.69; 'adds':
0.69; 'latter': 0.69; 'mars': 0.69; 'addition,': 0.70; 'sent:':
0.78; 'subject:you': 0.84; '500000': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1711918581; x=1712523381; darn=python.org;
h=content-language:thread-index:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=s2icV53d6sfPMlb32I0l1/Ft/JSgterPUQXtJXFMe54=;
b=C6ct4HRTBAgzbNzIkY3MrjsLRpS11TjPtcrena00o5grWL+HBexcH7y5rulqi0Xsc9
PlCeAPqOJGlO28IiwAVM9vexIUtQa/e95122YmJX9lZJgyegW47YU/pb8qmw0Zu7KQw1
/T3vaK+xMqJ/VjgBW6fLk4xyzB+nHzfOGXYQ27lLN3T6vSZLUyCvtVKRTYztu8eyUiKV
c29RYwMAO36eruN+GHZQWKXVkPOQrT4LehMJPVO6r6f3YXCgIgIf56yNduIwtnZ43QBP
cvvSWMceVUiefKCP2lDMfgbJ08DoDw1oJs9BptUunNdqQAh7rIjDqnoWKpN4CsTDqRbi
R+1Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1711918581; x=1712523381;
h=content-language:thread-index:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=s2icV53d6sfPMlb32I0l1/Ft/JSgterPUQXtJXFMe54=;
b=Hwzi3puUXpTfnQQP+3byKJuiPNicjaJnyqhNkXOaKq/B4eQAvrhlQQrw+jlI3GsLP1
kaLB/kpU4mHBZEIpgfz+cryCFhEzPJGNYGhXfNo2o7IKVSBUQmuDIhraMYoehzbcFpHJ
0PgV/ktTL94caDnwnlvoioLNpy8yTfFa+VRAA2p3juib4rOxAhznz9w/M8vqWaOJh+yA
/Cg17dvFMeIhoaO3l11mpyzhZH87U0v9yya0c1OgS3uSN4Bw50DW3mJUZyC3rlatXCnf
y2GWPqgnx7uoESEzfhvwJEawKSSlgt9Anp4Tc6XB1COX37TQIvVVlRhgyOb64nBhBwMi
SxvA==
X-Gm-Message-State: AOJu0YyPzjJEqZiexbHSdGD6XYZKyYWVBukuMC6Bt+87Qa9A9lqu8MRb
owjl/R49+DkhisIOr1ccOT3wYOX6cyamCWJbs5VkFUtbQ8MK/pHArNDaRVuT
X-Google-Smtp-Source: AGHT+IGYdFrtctodcra8DVsEQMkFIk4jb9cyLi5Au2wWyaRy/Z6SZ1Et4vq3n/d1EBpVDfmTh60VKw==
X-Received: by 2002:ac8:7d92:0:b0:431:80cd:56ae with SMTP id
c18-20020ac87d92000000b0043180cd56aemr9820727qtd.40.1711918580711;
Sun, 31 Mar 2024 13:56:20 -0700 (PDT)
In-Reply-To: <022811dc-dca5-400b-bf2b-3f3e3a67f151@mrabarnett.plus.com>
X-Mailer: Microsoft Outlook 16.0
Thread-Index: AQKgSzR2sup5soWXzbkqj8ZlA6bguQKjvvXcAlehSNECkZvnua+KW4qw
Content-Language: en-us
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: <008901da83ad$e17541f0$a45fc5d0$@gmail.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>
<022811dc-dca5-400b-bf2b-3f3e3a67f151@mrabarnett.plus.com>
 by: <avi.e.gross@gmail.com> - Sun, 31 Mar 2024 20:56 UTC

I am not sure if it was made clear that there is a general rule in python for what is HASHABLE and lists are changeable while tuples are not so the latter can be hashed as a simple copy of a list, albeit the contents must also be immutable.

The memorize function uses a dictionary to store things and thus the things are hashed to decide how to store it in the inner representation of a dictionary and anything new that you want to look up in the dictionary has similar considerations as it is hashed to see where in the dictionary to look for it.

Of course, if you add enough overhead and the memorize function you make gets relatively few requests that are identical, it may not be worthwhile.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of MRAB via Python-list
Sent: Sunday, March 31, 2024 3:24 PM
To: python-list@python.org
Subject: Re: Can you help me with this memoization simple example?

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
>
--
https://mail.python.org/mailman/listinfo/python-list

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor