Re: A Single Instance of an Object?

2024-03-11 Thread Peter J. Holzer via Python-list
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.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A Single Instance of an Object?

2024-03-11 Thread Chris Angelico via Python-list
On Tue, 12 Mar 2024 at 08:04, Ivan "Rambius" Ivanov
 wrote:
> > A Singleton is just a global variable. Why do this? Did someone tell
> > you "global variables are bad, don't use them"?
>
> I have bad experience with global variables because it is hard to
> track what and when modifies them. I don't consider them bad, but if I
> can I avoid them.
>
If you have a singleton, how will you track "what and when modifies"
it? How is it any different from a global?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A Single Instance of an Object?

2024-03-11 Thread Ivan "Rambius" Ivanov via Python-list
On Mon, Mar 11, 2024 at 5:06 PM dn via Python-list
 wrote:
>
> Good question Rambius!
>
> On 12/03/24 09:53, Ivan "Rambius" Ivanov via Python-list wrote:
> > Hello,
> >
> > I am refactoring some code and I would like to get rid of a global
> > variable. Here is the outline:
> >
> > import subprocess
> >
> > CACHE = {}
>
> First thought: don't reinvent-the-wheel, use lru_cache
> (https://docs.python.org/3/library/functools.html)
>
>
> > 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 = {}
> >
>
> Change "cache" to be a class-attribute (it is currently an instance.
>
> Then, code AFTER the definition of Lookup can refer to Lookup.cache,
> regardless of instantiation, and code within Lookup can refer to
> self.cache as-is...
>

Thank you for your suggestions. I will research them!

Regards
rambius


-- 
Tangra Mega Rock: http://www.radiotangra.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A Single Instance of an Object?

2024-03-11 Thread Ivan "Rambius" Ivanov via Python-list
On Mon, Mar 11, 2024 at 5:01 PM Chris Angelico via Python-list
 wrote:
>
> On Tue, 12 Mar 2024 at 07:54, 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:
> >
> > ...
> >
> > 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.
> >
>
> A Singleton is just a global variable. Why do this? Did someone tell
> you "global variables are bad, don't use them"?

I have bad experience with global variables because it is hard to
track what and when modifies them. I don't consider them bad, but if I
can I avoid them.

Regards
rambius

-- 
Tangra Mega Rock: http://www.radiotangra.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A Single Instance of an Object?

2024-03-11 Thread dn via Python-list

Good question Rambius!

On 12/03/24 09:53, Ivan "Rambius" Ivanov via Python-list wrote:

Hello,

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

import subprocess

CACHE = {}


First thought: don't reinvent-the-wheel, use lru_cache 
(https://docs.python.org/3/library/functools.html)




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 = {}



Change "cache" to be a class-attribute (it is currently an instance.

Then, code AFTER the definition of Lookup can refer to Lookup.cache, 
regardless of instantiation, and code within Lookup can refer to 
self.cache as-is...



--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: A Single Instance of an Object?

2024-03-11 Thread Chris Angelico via Python-list
On Tue, 12 Mar 2024 at 07:54, 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:
>
> ...
>
> 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.
>

A Singleton is just a global variable. Why do this? Did someone tell
you "global variables are bad, don't use them"?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


A Single Instance of an Object?

2024-03-11 Thread Ivan "Rambius" Ivanov via Python-list
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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error in Module

2024-03-11 Thread Sanskar Mukeshbhai Joshi via Python-list
Thank you for the information.

On Mon, Mar 11, 2024, 22:36  wrote:

> Sanskar Mukeshbhai Joshi wrote at 2024-3-10 18:08 +:
> >I had made my project in BCA in Python. When I had complete my project
> and run the program, at that time I got the error in runnig my project. The
> error was ModuleNotFoundError: No module named 'flask'.
>
> `flask` is not part of the Python library; it has to be installed
> separately.
>
> Apparently, you project is a `flask` project (a Web application platform).
> In this case, you must run your program in a special `flask`
> environment.
>
> I suggest you contact your project mentor to get help.
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error in Module

2024-03-11 Thread Dieter Maurer via Python-list
Sanskar Mukeshbhai Joshi wrote at 2024-3-10 18:08 +:
>I had made my project in BCA in Python. When I had complete my project and run 
>the program, at that time I got the error in runnig my project. The error was 
>ModuleNotFoundError: No module named 'flask'.

`flask` is not part of the Python library; it has to be installed separately.

Apparently, you project is a `flask` project (a Web application platform).
In this case, you must run your program in a special `flask`
environment.

I suggest you contact your project mentor to get help.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error in Module

2024-03-11 Thread Alan Gauld via Python-list
On 10/03/2024 18:08, Sanskar Mukeshbhai Joshi via Python-list wrote:

> I had made my project in BCA in Python. When I had complete my 
> project and run the program, at that time I got the error in 
> runnig my project. The error was ModuleNotFoundError: No module named 'flask'.

Flask is a third party package that you need to install separately
from Python. It does not come as standard. Have you installed Flask
on the computer where you are running your project? If so, how did you
download/install it?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list


Error in Module

2024-03-11 Thread Sanskar Mukeshbhai Joshi via Python-list
Respected Sir/Ma'am

I had made my project in BCA in Python. When I had complete my project and run 
the program, at that time I got the error in runnig my project. The error was 
ModuleNotFoundError: No module named 'flask'.

I request you to check this problem and resolve it or guide me to solve this 
Error.
-- 
https://mail.python.org/mailman/listinfo/python-list