Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

The meek are contesting the will.


devel / comp.lang.python / Re: A Single Instance of an Object?

SubjectAuthor
o Re: A Single Instance of an Object?Peter J. Holzer

1
Re: A Single Instance of an Object?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-python@hjp.at (Peter J. Holzer)
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>
 by: Peter J. Holzer - Mon, 11 Mar 2024 23:37 UTC
Attachments: signature.asc (application/pgp-signature)

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:

...

> 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:
>
> class Lookup:
> def __init__(self):
> self.cache = {}
>
> def lookup(key):
> if key in self.cache:
> return self.cache[key]
>
> value = None
>
> cmd = f"mycmd {key}"
> proc = subprocess(cmd, capture_output=True, text=True, check=False)
> if proc.returncode == 0:
> value = proc.stdout.strip()
> else:
> logger.error("cmd returned error")
>
> self.cache[key] = value
> return value
>
> 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.
>
> How would you advise me to implement that?

Just add a dict of Lookup objects to your module:

lookups = {}

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

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

import lookup

lo = lookup.get_lookup("whatever")

...
v = lo.lookup("a key")

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

lo = lookup.get_lookup("test1")
...
lo = lookup.get_lookup("test2")
...
lo = 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.

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

Attachments: signature.asc (application/pgp-signature)
1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor