Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <mailman.91.1710200583.3452.python-list@python.org>
Deutsch   English   Français   Italiano  
<mailman.91.1710200583.3452.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: "Peter J. Holzer" <hjp-python@hjp.at>
Newsgroups: comp.lang.python
Subject: Re: A Single Instance of an Object?
Date: Tue, 12 Mar 2024 00:37:14 +0100
Lines: 109
Message-ID: <mailman.91.1710200583.3452.python-list@python.org>
References: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
 <20240311233714.zvnbmgzerr5cpxjs@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
 protocol="application/pgp-signature"; boundary="ps5sbj6en6ned4o5"
X-Trace: news.uni-berlin.de YjeUKdZb5OSVVsk3tgCfRA6qv3YkNXahgYWQFqX804ww==
Cancel-Lock: sha1:Qi5LHdQIc6lhRE3GJNd8aX5VI7A= sha256:LGyzUSRwok3DQJSJMbvc5PjZMRY8N+PV6Epg9fHr6vE=
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
 dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'def':
 0.04; 'content-type:multipart/signed': 0.05; 'variable': 0.05;
 'modules': 0.07; 'that?': 0.07; '(assuming': 0.09; 'content-
 type:application/pgp-signature': 0.09; 'else:': 0.09;
 'filename:fname piece:asc': 0.09; 'filename:fname
 piece:signature': 0.09; 'filename:fname:signature.asc': 0.09;
 'lookup': 0.09; 'module:': 0.09; 'test,': 0.09; 'import': 0.15;
 '"creative': 0.16; '__/': 0.16; 'behaviour': 0.16; 'challenge!"':
 0.16; 'dict': 0.16; 'from:addr:hjp-python': 0.16;
 'from:addr:hjp.at': 0.16; 'from:name:peter j. holzer': 0.16;
 'hjp@hjp.at': 0.16; 'holzer': 0.16; 'instance': 0.16; 'loops':
 0.16; 'outline:': 0.16; 'proc': 0.16; 'reality.': 0.16;
 'refactoring': 0.16; 'stack.': 0.16; 'stross,': 0.16; 'url-
 ip:212.17.106/24': 0.16; 'url-ip:212.17/16': 0.16; 'url:hjp':
 0.16; 'variable,': 0.16; 'variable.': 0.16; '|_|_)': 0.16;
 'wrote:': 0.16; 'implement': 0.19; 'to:addr:python-list': 0.20;
 'skip:_ 10': 0.22; 'way.': 0.22; 'code': 0.23; 'cannot': 0.25;
 'object': 0.26; 'sense': 0.28; 'module': 0.31; 'think': 0.32;
 'objects': 0.32; 'python-list': 0.32; 'but': 0.32; 'there': 0.33;
 'script': 0.33; 'same': 0.34; 'header:In-Reply-To:1': 0.34;
 'one.': 0.35; 'cases': 0.36; 'functions': 0.36; "it's": 0.37;
 'class': 0.37; 'put': 0.38; 'use': 0.39; 'should': 0.40; 'method':
 0.61; 'received:212': 0.62; 'here': 0.62; 'key': 0.64; 'down':
 0.64; 'ivan': 0.64; 'your': 0.64; 'received:userid': 0.66;
 'matter': 0.68; 'ps:': 0.69; 'url-ip:212/8': 0.69; 'global': 0.73;
 'advise': 0.78; 'returned': 0.81; 'unit': 0.81; 'cleaner': 0.84;
 'global.': 0.84; 'received:at': 0.84; 'rid': 0.84; 'tables': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
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: <20240311233714.zvnbmgzerr5cpxjs@hjp.at>
X-Mailman-Original-References: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
Bytes: 7303


--ps5sbj6en6ned4o5
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On 2024-03-11 16:53:00 -0400, Ivan "Rambius" Ivanov via Python-list wrote:
> I am refactoring some code and I would like to get rid of a global
> variable. Here is the outline:

=2E..

> The global cache variable made unit testing of the lookup(key) method
> clumsy, because I have to clean it after each unit test. I refactored
> it as:
>=20
> class Lookup:
>     def __init__(self):
>         self.cache =3D {}
>=20
>     def lookup(key):
>         if key in self.cache:
>             return self.cache[key]
>=20
>         value =3D None
>=20
>         cmd =3D f"mycmd {key}"
>         proc =3D subprocess(cmd, capture_output=3DTrue, text=3DTrue, chec=
k=3DFalse)
>         if proc.returncode =3D=3D 0:
>             value =3D proc.stdout.strip()
>         else:
>             logger.error("cmd returned error")
>=20
>         self.cache[key] =3D value
>         return value
>=20
> Now it is easier to unit test, and the cache is not global. However, I
> cannot instantiate Lookup inside the while- or for- loops in main(),
> because the cache should be only one. I need to ensure there is only
> one instance of Lookup - this is why I made it a global variable, so
> that it is accessible to all functions in that script and the one that
> actually needs it is 4 levels down in the call stack.
[...]
> I am looking for the same behaviour as logging.getLogger(name).
> logging.getLogger("myname") will always return the same object no
> matter where it is called as long as the name argument is the same.
>=20
> How would you advise me to implement that?

Just add a dict of Lookup objects to your module:

lookups =3D {}

def get_lookup(name):
    if name not in lookups:
        lookups[name] =3D Lookup()
    return lookups[name]

Then (assuming your module is also called "lookup", in all other modules
do

import lookup

lo =3D lookup.get_lookup("whatever")

=2E..
v =3D lo.lookup("a key")

In your test cases where you need many different lookup tables use

lo =3D lookup.get_lookup("test1")
=2E..
lo =3D lookup.get_lookup("test2")
=2E..
lo =3D lookup.get_lookup("test3")

        hp

PS: You don't have to put that in a separate module but I think it's a
lot cleaner that way.

--=20
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

--ps5sbj6en6ned4o5
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmXvlaQACgkQ8g5IURL+
KF3XYBAAnXGsZsJfRJVxP+RX6kFJzhLVDYaHQV8dp8gpXX4XvKCCBo56Q2OyUqmk
TDXhsn/GBpNIrPWmYmPAiL8pLSV3MbcON1ABGT9KgB+pxO8KnYVpH7Il6uV6zT8H
ZvEXJwX8qjD5OtZIJ5q4pLaYW4A2l+8vvNS+pYqhFMOkRsteKld4XBVblw/+ME3/
5Kat7h1/eoa4UVkHNgxB1FEWWKH6nXbXGgdM+khBBdO7SVI6vh9ooPLGZUvNGk8+
VDv3zc0O9fauEPzfPlBnSZQiyxCFyEgs0XF0hAMGQCadm5EZXHMu1dKi8+me0he3
laTb6DX9iiZbvbPVhITGmEh3UyaqAjYLfrhQ89Vwj2n1piKX611UNayWaUCVMVPy
KfL1/lmaMc7Z1LJsOZG+kHTmSbGi6f9OTJpFZwJYOD5VP0SWVPyLGaJuO1azkP+c
+tFYpm6UXPREFOsK8mSejWiQF5/klRCWLjjqS5rNfrsjp1I+lerUL/B3/bXTHc95
phoMSuRiuBqbmNzEOUm9dxSbVWkcWIOT6cgPZnpdGdJp7hdKShbTG854X4Zlli8l
rVC1IfEn5ZIxLedVrMNccX2NDhEdyIAQuSYmtW2EJ7P2wnz/wsf3GFtyZpQrop9u
+pLPOUH4HusZzOvro/AqZQcxCO+57+F9P4i4xcv+kCuVLzK86+Q=
=o4EG
-----END PGP SIGNATURE-----

--ps5sbj6en6ned4o5--