From ad8e1cbf62db2f6d3b45e520dcb8a6c6b8933b3f Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 26 Jun 2017 20:49:41 -0700 Subject: [PATCH 1/3] convert environment vars to int if digits --- sanic/config.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sanic/config.py b/sanic/config.py index e3563bc1..b51f4d0c 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -201,4 +201,7 @@ class Config(dict): for k, v in os.environ.items(): if k.startswith(SANIC_PREFIX): _, config_key = k.split(SANIC_PREFIX, 1) - self[config_key] = v + if v.isdigit(): + self[config_key] = int(v) + else: + self[config_key] = v From 4379a4b0670c9172a3a6af63aa7d0132142c1989 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 26 Jun 2017 20:58:31 -0700 Subject: [PATCH 2/3] float logic --- sanic/config.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sanic/config.py b/sanic/config.py index b51f4d0c..ec4f9bf3 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -201,7 +201,11 @@ class Config(dict): for k, v in os.environ.items(): if k.startswith(SANIC_PREFIX): _, config_key = k.split(SANIC_PREFIX, 1) - if v.isdigit(): - self[config_key] = int(v) + # This is a float or an int + if v.replace('.', '').isdigit(): + if '.' in v: + self[config_key] = float(v) + else: + self[config_key] = int(v) else: self[config_key] = v From 395d85a12f9b2be24dce112a6a4b0e873374de7f Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 26 Jun 2017 21:26:34 -0700 Subject: [PATCH 3/3] use try/except --- sanic/config.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sanic/config.py b/sanic/config.py index ec4f9bf3..f5649cfe 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -201,11 +201,10 @@ class Config(dict): for k, v in os.environ.items(): if k.startswith(SANIC_PREFIX): _, config_key = k.split(SANIC_PREFIX, 1) - # This is a float or an int - if v.replace('.', '').isdigit(): - if '.' in v: + try: + self[config_key] = int(v) + except ValueError: + try: self[config_key] = float(v) - else: - self[config_key] = int(v) - else: - self[config_key] = v + except ValueError: + self[config_key] = v