-
Notifications
You must be signed in to change notification settings - Fork 3
env variable support #9
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
Open
KennySwayzee93
wants to merge
1
commit into
main
Choose a base branch
from
parallelism-via-env
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """Utility class for environment variable management.""" | ||
|
|
||
| import os | ||
| from enum import Enum | ||
| from typing import Any, cast | ||
|
|
||
|
|
||
| class EnvVarType(Enum): | ||
| """Types of environment variables.""" | ||
| INT = "int" | ||
| STRING = "str" | ||
| BOOL = "bool" | ||
|
|
||
|
|
||
| class EnvironmentVariables(Enum): | ||
| """ | ||
| Enum of known environment variables used in the application. | ||
|
|
||
| Each enum value is a tuple of (env_var_name, default_value, type). | ||
| """ | ||
| PARALLELISM_DIVISOR = ("PARALLELISM_DIVISOR", 2, EnvVarType.INT) | ||
|
|
||
| def __init__(self, env_name: str, default_value: Any, var_type: EnvVarType): | ||
| self.env_name = env_name | ||
| self.default_value = default_value | ||
| self.var_type = var_type | ||
|
|
||
|
|
||
| class EnvironmentManager: | ||
| """Static utility class for environment variable management.""" | ||
|
|
||
| @staticmethod | ||
| def get_value(env_var: EnvironmentVariables, override_default: Any = None) -> Any: | ||
| """ | ||
| Get a value from an environment variable with appropriate type conversion. | ||
|
|
||
| Args: | ||
| env_var: The environment variable to retrieve | ||
| override_default: Optional value to override the default defined in the enum | ||
|
|
||
| Returns: | ||
| The value of the environment variable or the default with appropriate type | ||
| """ | ||
| # Use override_default if provided, otherwise use the enum's default | ||
| default = override_default if override_default is not None else env_var.default_value | ||
|
|
||
| # Get the value from environment | ||
| value = os.environ.get(env_var.env_name) | ||
| if value is None: | ||
| return default | ||
|
|
||
| # Convert to appropriate type | ||
| if env_var.var_type == EnvVarType.INT: | ||
| try: | ||
| return int(value) | ||
| except ValueError: | ||
| # Log warning here if needed | ||
| return default | ||
| elif env_var.var_type == EnvVarType.BOOL: | ||
| return value.lower() in ('true', 'yes', '1', 'y') | ||
| else: # STRING or any other type | ||
| return value | ||
|
|
||
| @staticmethod | ||
| def get_int(env_var: EnvironmentVariables, default = None) -> int: | ||
| """ | ||
| Get an integer value from an environment variable. | ||
|
|
||
| Args: | ||
| env_var: The environment variable to retrieve | ||
| default: Optional value to override the default defined in the enum | ||
|
|
||
| Returns: | ||
| int: The value of the environment variable or the default | ||
| """ | ||
| return cast(int, EnvironmentManager.get_value(env_var, default)) | ||
|
|
||
| @staticmethod | ||
| def get_string(env_var: EnvironmentVariables, default = None) -> str: | ||
| """ | ||
| Get a string value from an environment variable. | ||
|
|
||
| Args: | ||
| env_var: The environment variable to retrieve | ||
| default: Optional value to override the default defined in the enum | ||
|
|
||
| Returns: | ||
| str: The value of the environment variable or the default | ||
| """ | ||
| return cast(str, EnvironmentManager.get_value(env_var, default)) | ||
|
|
||
| @staticmethod | ||
| def get_bool(env_var: EnvironmentVariables, default = None) -> bool: | ||
| """ | ||
| Get a boolean value from an environment variable. | ||
|
|
||
| Args: | ||
| env_var: The environment variable to retrieve | ||
| default: Optional value to override the default defined in the enum | ||
|
|
||
| Returns: | ||
| bool: The value of the environment variable or the default | ||
| """ | ||
| return cast(bool, EnvironmentManager.get_value(env_var, default)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| """Utility modules for the puzzle generator.""" | ||
|
|
||
| from .SystemSpecs import SystemSpecs | ||
| from .EnvironmentManager import EnvironmentManager, EnvironmentVariables | ||
|
|
||
| __all__ = ["SystemSpecs"] | ||
| __all__ = ["SystemSpecs", "EnvironmentManager", "EnvironmentVariables"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
edit this in your .env file to set amount of parallelism
1 = as parallelized as possible
2 = half
3 = one third
so on ..