Remove global variable definitions (the ALL_CAPS variables)
Currently, utils.py defines a bunch of globals at the module level. These should be removed/relocated so they don't pollute the namespace when the module is imported.
ChatGpt4.0 has some info on how to mitigate this:
It seems like the primary concern is the potential name clashes when importing your module. Python provides ways to encapsulate your code to avoid such issues. Below are two main strategies you could use:
- Encapsulation via Object-Oriented Programming (OOP): Instead of defining your variables at the module level, you could encapsulate them in classes. This has the added benefit of encapsulating related functionality together, making your code more readable and maintainable.
Here is an example of how you might encapsulate your html_gen.py code into a class:
# In "html_gen.py":
from pathlib import Path
class HTMLGen:
def __init__(self):
self.CSS_FILE = Path(__file__).parent / "resources" / "styles.css"
self.JS_FILE = Path(__file__).parent / "resources" / "scripts.js"
#... define the rest of your variables here
# Later in your code, when you need to access these, you would do:
html_gen = HTMLGen()
print(html_gen.CSS_FILE)
You could do something similar with the code in utils.py.
-
Use of
__all__special variable: In Python, you can define a list named__all__in your module that explicitly exports certain public names and omits the rest. This can be useful to hide implementation details and prevent name clashes. When usingfrom module import *, only the names in__all__are imported. However, if__all__is not defined, the statementfrom module import *imports all names that do not begin with an underscore.
For example:
# In "html_gen.py":
__all__ = ['TAB_ABOUT', 'TAB_ABOUT_COLOR', 'TABS_RESULTS', 'TABS_RESULTS_COLORS']
# Then your variables...
But do note that explicit importing like from html_gen import TAB_ABOUT will still be able to access TAB_ABOUT even if it's not in __all__.
These techniques should help in keeping the namespaces clean and preventing potential name clashes.