-
Notifications
You must be signed in to change notification settings - Fork 131
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
Add SDK config #1232
Merged
Merged
Add SDK config #1232
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 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 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 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,87 @@ | ||
"""This module contains constants for tracking. | ||
|
||
We then override these by: | ||
1. Looking for a configuration file and taking the section under `SDK_CONSTANTS`. | ||
2. Via environment variables. They should be prefixed with `HAMILTON_`. | ||
3. Lastly folks can manually adjust these values directly by importing the module and changing the value. | ||
|
||
Note: This module cannot import other Hamilton modules. | ||
""" | ||
|
||
import configparser | ||
import logging | ||
import os | ||
from typing import Any | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# The following are the default values for the tracking client | ||
CAPTURE_DATA_STATISTICS = True | ||
MAX_LIST_LENGTH_CAPTURE = 50 | ||
MAX_DICT_LENGTH_CAPTURE = 100 | ||
|
||
# Check for configuration file | ||
# TODO -- add configuration file support | ||
DEFAULT_CONFIG_URI = os.environ.get("HAMILTON_CONFIG_URI", "~/.hamilton.conf") | ||
DEFAULT_CONFIG_LOCATION = os.path.expanduser(DEFAULT_CONFIG_URI) | ||
|
||
|
||
def _load_config(config_location: str) -> configparser.ConfigParser: | ||
"""Pulls config if it exists. | ||
|
||
:param config_location: location of the config file. | ||
""" | ||
config = configparser.ConfigParser() | ||
try: | ||
with open(config_location) as f: | ||
config.read_file(f) | ||
except Exception: | ||
pass | ||
|
||
return config | ||
|
||
|
||
_constant_values = globals() | ||
file_config = _load_config(DEFAULT_CONFIG_LOCATION) | ||
|
||
|
||
def _convert_to_type(val_: str) -> Any: | ||
skrawcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not isinstance(val_, str): # guard | ||
return val_ | ||
if val_.isdigit(): | ||
# convert to int | ||
val_ = int(val_) | ||
elif val_.lower() in {"true", "false"}: | ||
# convert to bool | ||
val_ = val_.lower() == "true" | ||
else: | ||
try: # check if float | ||
val_ = float(val_) | ||
except ValueError: | ||
pass | ||
return val_ | ||
|
||
|
||
# loads from config file and overwrites | ||
if "SDK_CONSTANTS" in file_config: | ||
for key, val in file_config["SDK_CONSTANTS"].items(): | ||
upper_key = key.upper() | ||
if upper_key not in _constant_values: | ||
continue | ||
# convert from string to appropriate type | ||
val = _convert_to_type(val) | ||
# overwrite value | ||
_constant_values[upper_key] = val | ||
skrawcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Check for environment variables & overwrites | ||
# TODO automate this by pulling anything in with a prefix and checking | ||
# globals here and updating them. | ||
CAPTURE_DATA_STATISTICS = os.getenv("HAMILTON_CAPTURE_DATA_STATISTICS", CAPTURE_DATA_STATISTICS) | ||
if isinstance(CAPTURE_DATA_STATISTICS, str): | ||
CAPTURE_DATA_STATISTICS = CAPTURE_DATA_STATISTICS.lower() == "true" | ||
MAX_LIST_LENGTH_CAPTURE = int( | ||
os.getenv("HAMILTON_MAX_LIST_LENGTH_CAPTURE", MAX_LIST_LENGTH_CAPTURE) | ||
) | ||
MAX_DICT_LENGTH_CAPTURE = int( | ||
os.getenv("HAMILTON_MAX_DICT_LENGTH_CAPTURE", MAX_DICT_LENGTH_CAPTURE) | ||
) |
This file contains 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 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 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 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,21 @@ | ||
import configparser | ||
from hamilton_sdk.tracking import constants | ||
|
||
|
||
def test__convert_to_type(): | ||
# using configparser to make it more realistic | ||
config = configparser.ConfigParser() | ||
config["SDK_CONSTANTS"] = { | ||
"CAPTURE_DATA_STATISTICS": "true", | ||
"MAX_LIST_LENGTH_CAPTURE": "5", | ||
"MAX_DICT_LENGTH_CAPTURE": "10", | ||
"SOMETHING_ELSE": "11.0", | ||
"Another_thing": "1asdfasdf", | ||
} | ||
assert constants._convert_to_type(config["SDK_CONSTANTS"]["CAPTURE_DATA_STATISTICS"]) is True | ||
assert constants._convert_to_type(config["SDK_CONSTANTS"]["MAX_LIST_LENGTH_CAPTURE"]) == 5 | ||
assert constants._convert_to_type(config["SDK_CONSTANTS"]["MAX_DICT_LENGTH_CAPTURE"]) == 10 | ||
assert constants._convert_to_type(config["SDK_CONSTANTS"]["SOMETHING_ELSE"]) == 11.0 | ||
assert constants._convert_to_type(config["SDK_CONSTANTS"]["Another_thing"]) == "1asdfasdf" | ||
o = object() | ||
assert constants._convert_to_type(o) == o |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
I feel like you should just hardcode this to a dict then we don't have to deal with globals. E.G.
constants_values={"CAPTURE_DATA_STATISTICS" : True, ...}
Then you just load up the file and modify that. Should simplify the code.
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.
No I don't like that because that would then encumber directly accessing the module values; or I could do something like this at the end to push them all into the module space... either way playing with globals.
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.
This has a single global versus multiple? Don't see why we want to directly access module values.