I have a file utilities.pyfor my python project. It contains only usage functions, for example is_float(string), is_empty(file)etc.
Now I want to have a function is_valid(number)that should:
- read from a file
valid.txtthat contains all valid numbers and load them onto a card / set. - check the card for availability
numberand return True or False.
This function is called frequently, and the runtime should be as short as possible. I do not want to read openly and read valid.txtevery time a function is called. The only solution I came across is to use a global variable valid_dictthat loads once from valid.txtwhen it is imported utilities.py. The download code is recorded as the main one in utilities.py.
My question is: how do I do this without using a global variable, as this is considered bad practice? What is a good design pattern to accomplish such a task without using global variables? Also note that this is a utility file, so ideally there should not be a main goal, just a function.
source
share