Deutsch   English   Français   Italiano  
<mailman.85.1710190394.3452.python-list@python.org>

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

Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: "Ivan \"Rambius\" Ivanov" <rambiusparkisanius@gmail.com>
Newsgroups: comp.lang.python
Subject: A Single Instance of an Object?
Date: Mon, 11 Mar 2024 16:53:00 -0400
Lines: 84
Message-ID: <mailman.85.1710190394.3452.python-list@python.org>
References: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 2AKLnl3UCu4bZyjbzJBPdg/hW3MzMHYFWSwHcMXFL48A==
Cancel-Lock: sha1:jf1t9LnNHuN8NUPSYR00GjdvBFk= sha256:7Yn+4AO6CB8ssZ5jJMKjMmy9pY9DoLTRPys2zcVaIxg=
Return-Path: <rambiusparkisanius@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=AAzEDRj5;
 dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.008
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'argument': 0.04; 'def':
 0.04; 'variable': 0.05; 'that?': 0.07; 'avoided': 0.09; 'else:':
 0.09; 'lookup': 0.09; 'situations': 0.09; 'test,': 0.09; 'import':
 0.15; 'behaviour': 0.16; 'instance': 0.16; 'loops': 0.16;
 'outline:': 0.16; 'proc': 0.16; 'received:mail-
 lj1-x235.google.com': 0.16; 'refactoring': 0.16; 'stack.': 0.16;
 'variable,': 0.16; 'variable.': 0.16; 'python': 0.16; 'implement':
 0.19; 'to:addr:python-list': 0.20; 'returns': 0.22; 'skip:_ 10':
 0.22; 'code': 0.23; 'command': 0.23; 'cannot': 0.25; 'object':
 0.26; 'done': 0.28; 'to:name:python': 0.32; 'message-
 id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'script':
 0.33; 'same': 0.34; 'received:google.com': 0.34; 'one.': 0.35;
 'from:addr:gmail.com': 0.35; 'functions': 0.36; 'class': 0.37;
 'error,': 0.38; 'hello,': 0.40; 'should': 0.40; 'method': 0.61;
 'here': 0.62; 'key': 0.64; 'down': 0.64; 'matter': 0.68; 'global':
 0.73; '....': 0.76; 'implemented': 0.76; 'advise': 0.78;
 'returned': 0.81; 'unit': 0.81; 'global.': 0.84; 'mega': 0.84;
 'rid': 0.84; 'true:': 0.84; 'url-ip:195/8': 0.84; 'deliberately':
 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1710190393; x=1710795193; darn=python.org;
 h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
 :date:message-id:reply-to;
 bh=tkjoxHC9Edn5ij4ngyKkupz5ICkr51tQ1jnpxo+H6uw=;
 b=AAzEDRj5UORG4DHt/Z2Hrce/i5DQ2al9Hji4IghA/C/BrwneY+NXPG8q+B9BBCy1yF
 0nT2yssmlwkUsLgx5qz7cTzhlBX9i7/wYcDVsAC1tFepe1LlXHVwyob00OXzOcD969hg
 tPg8YqbuhYcBMy7bleTzsRGzaiaMT099tuDbxvbdfx8k9zlRzICbKMLCvRduLpZ5CBuF
 WHEGztokysZaou2HtQcc3hi/SS/+GMrZ/CS0PueOQ7+iK0K7YQRkKO3Eu1w3f+VXdzmL
 QbiYpDmvC4uxTEl9Qscq9t6VE5vVyCG4O3UjDrgF1reUQYigKqrN72XFwfzARbxG8gxQ
 l5Qw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1710190393; x=1710795193;
 h=to:subject:message-id:date:from:mime-version:x-gm-message-state
 :from:to:cc:subject:date:message-id:reply-to;
 bh=tkjoxHC9Edn5ij4ngyKkupz5ICkr51tQ1jnpxo+H6uw=;
 b=t28gaShdep8KmZxD8Bjb319/yssJ1DwK5hiZxt9jvQ11N7/920IQZgo+5Kt9PDN2z+
 yVCMdHrMop6dPLNWbJUByVw2TqCEwuhNaiYkK9kUf8+mSzxbf+RQF34Hqnv2mJVb1sRs
 GGuZkCt7n2pRRiFOmfgK/LtAUiwFTCTVAe41tSP8C2S1pkbq1bvR9DA4f0rCJG6zFyvy
 +v4X6yfWkHa40Yy6Is+wrXB5el7qjME0EWAVCQZTQXMbMkyBVb4MCqKhCdBCICtE5ADp
 AfMcTA6YV6ZKEkq6YJJy/5tHDrUmGqLLzDEi4donAjhQOYmR7PXEcN+NJNVNJManyHBQ
 UO+Q==
X-Gm-Message-State: AOJu0YzLxx8Y5aMAagol7qE1SFcG+UjNwLdCZ8okD6UBpNgBNgJ9XYjt
 vaGyaCx1hib/SLwEPOXzOAvN0dXhEwRhAfepjMjk6L8EAzqCdDAaVpqN3aWOxA/DhQcAcHvDOsV
 I9+IQTRx828HTwfnfBvJXLRuSZnVD8erVWTI=
X-Google-Smtp-Source: AGHT+IHhSzMa0a4Zf3K0tEzWuoU3OI/Y6x8KC2k/YWgXNtn7zDiBRUB5PSapzVQxuDGs7nqszLHYSP14IUt14WDGS/I=
X-Received: by 2002:a05:651c:b0f:b0:2d2:74ed:37b0 with SMTP id
 b15-20020a05651c0b0f00b002d274ed37b0mr6062562ljr.11.1710190392496; Mon, 11
 Mar 2024 13:53:12 -0700 (PDT)
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: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
Bytes: 7235

Hello,

I am refactoring some code and I would like to get rid of a global
variable. Here is the outline:

import subprocess

CACHE = {}

def lookup(key):
    """"Runs the command cmd, parses its output, extract's the key's value,
    caches it and returns it. If the key has already been in the cache,
    returns its cached value. If the command cmd returns an error, the
    value is set to None and cached as None."""

    if key in CACHE:
        return 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")

    CACHE[key] = value
    return value
....
def main():
    while True:
        keys = load_keys()
        for key in keys:
            call_function_that_call_function_that_calls_lookup(key)


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 have never done that in Python because I deliberately avoided such
complicated situations up to now. I know about the Singleton pattern,
but I have never implemented it in Python and I don't know if it is
Pythonish.

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?

Regards
rambius

-- 
Tangra Mega Rock: http://www.radiotangra.com