Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove global variable definitions (the ALL_CAPS variables) #105

Open
jeffwright13 opened this issue Jun 2, 2023 · 0 comments
Open

Remove global variable definitions (the ALL_CAPS variables) #105

jeffwright13 opened this issue Jun 2, 2023 · 0 comments
Assignees

Comments

@jeffwright13
Copy link
Owner

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.

@jeffwright13 jeffwright13 self-assigned this Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant