Skip to content

Remove global variable definitions (the ALL_CAPS variables) #105

@jeffwright13

Description

@jeffwright13

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:

  1. 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.

  1. 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 using from module import *, only the names in __all__ are imported. However, if __all__ is not defined, the statement from 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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions