Skip to content

Commit

Permalink
[Utility] add env_float utility into ray._private.ray_constants (r…
Browse files Browse the repository at this point in the history
…ay-project#47117)

The current codebase includes `env_bool` and `env_integer` functions
that directly convert environment variable strings into their respective
types. To extend this functionality, we also need an `env_float`
function to safely convert strings representing floating-point numbers
into the `float` type."

Signed-off-by: Hongpeng Guo <hpguo@anyscale.com>
  • Loading branch information
hongpeng-guo authored Aug 13, 2024
1 parent 92d5807 commit bb127a8
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/ray/_private/ray_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ def env_integer(key, default):
return default


def env_float(key, default):
if key in os.environ:
value = os.environ[key]
try:
return float(value)
except ValueError:
logger.debug(
f"Found {key} in environment, but value must "
f"be a float. Got: {value}. Returning "
f"provided default {default}."
)
return default
return default


def env_bool(key, default):
if key in os.environ:
return (
Expand Down

0 comments on commit bb127a8

Please sign in to comment.