aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-01 11:54:22 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-08-01 11:54:22 -0500
commitc7922b08bf010375416138b2ca85fe88fd8edcef (patch)
tree1cbf65802f963f66f9fbe6ef6bae7f8d92bf1f59
parentLint files (diff)
downloadconfig-file-c7922b08bf010375416138b2ca85fe88fd8edcef.zip
Expand on docstrings for parse_value file
-rw-r--r--config_file/parsers/parse_value.py95
-rw-r--r--tests/test_parse_value.py1
2 files changed, 77 insertions, 19 deletions
diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py
index 28f1ccb..12032d6 100644
--- a/config_file/parsers/parse_value.py
+++ b/config_file/parsers/parse_value.py
@@ -1,7 +1,13 @@
-"""
-This module allows you to parse a string into its native type.
+"""This module allows you to parse a string into its native type."""
+import ast
+import re
+from distutils.util import strtobool
+from typing import Any
+
+
+def parse_value(value: Any) -> Any:
+ """Parse a value into native type.
-Example:
parse_value('5') -> 5 (int)
parse_value('-5') -> -5 (int)
@@ -13,20 +19,7 @@ Example:
parse_value('true') -> True (bool)
parse_value('FALSE') -> False (bool)
-
-"5".isdigit() -> True
-"-5".isdigit() -> False
-can_be_parsed_as_int("-5") -> True
-parse_value("-5") -> -5
-"""
-import ast
-import re
-from distutils.util import strtobool
-from typing import Any
-
-
-def parse_value(value: Any) -> Any:
- """Parse a value into native type.
+ parse_value(" [ [ [ [] ]] ] ") -> [[[[]]]] (list)
Args:
value: The value to parse.
@@ -74,13 +67,18 @@ def parse_value(value: Any) -> Any:
def can_be_parsed_as_int(value: Any) -> bool:
"""Check whether a value can be parsed as a integer.
+ can_be_parsed_as_int(5) -> True
+ can_be_parsed_as_int("5") -> True
+ can_be_parsed_as_int("-5") -> True
+ can_be_parsed_as_int(True) -> False
+
Args:
value: The value to check if it can be parsed as an integer.
Returns:
True if we can parse the value as a integer. False otherwise.
"""
- if isinstance(value, int) and not type(value) is bool:
+ if isinstance(value, int) and not isinstance(value, bool):
return True
if not isinstance(value, str):
@@ -95,6 +93,20 @@ def can_be_parsed_as_int(value: Any) -> bool:
def can_be_parsed_as_float(value: Any) -> bool:
+ """Checks whether a value can be parsed as a float.
+
+ can_be_parsed_as_float(45) -> False
+ can_be_parsed_as_float(1.1) -> True
+ can_be_parsed_as_float("1.1") -> True
+ can_be_parsed_as_float("-1.1") -> True
+ can_be_parsed_as_float(".1") -> True
+
+ Args:
+ value: The value to be parsed.
+
+ Returns:
+ True if the value can be parsed as a float. False otherwise.
+ """
FLOAT_REGEX = r"^\d*\.\d+$"
if isinstance(value, float):
@@ -112,6 +124,20 @@ def can_be_parsed_as_float(value: Any) -> bool:
def can_be_parsed_as_bool(value: Any) -> bool:
+ """Checks whether a value can be parsed as a boolean.
+
+ can_be_parsed_as_bool(True) -> True
+ can_be_parsed_as_bool("true") -> True
+ can_be_parsed_as_bool("false") -> True
+ can_be_parsed_as_bool("TRUE") -> True
+ can_be_parsed_as_bool(0) -> False
+
+ Args:
+ value: The value to be parsed.
+
+ Returns:
+ True if the value can be parsed as a boolean. False otherwise.
+ """
if isinstance(value, bool):
return True
@@ -124,6 +150,21 @@ def can_be_parsed_as_bool(value: Any) -> bool:
def can_be_parsed_as_dict(value: Any) -> bool:
+ """Checks whether a value can be parsed as a dictionary.
+
+ can_be_parsed_as_dict({}) -> True
+ can_be_parsed_as_dict("{}") -> True
+ can_be_parsed_as_dict(" { } ") -> True
+ can_be_parsed_as_dict("{ 'hello': 5 }") -> True
+ can_be_parsed_as_dict([]]) -> False
+ can_be_parsed_as_dict(" { invalid } ") -> False
+
+ Args:
+ value: The value to be parsed.
+
+ Returns:
+ True if the value can be parsed as a dictionary. False otherwise.
+ """
if isinstance(value, dict):
return True
@@ -142,7 +183,23 @@ def can_be_parsed_as_dict(value: Any) -> bool:
return False
-def can_be_parsed_as_list(value) -> bool:
+def can_be_parsed_as_list(value: Any) -> bool:
+ """Checks whether a value can be parsed as a list.
+
+ can_be_parsed_as_list([]) -> True
+ can_be_parsed_as_list([1, 2]) -> True
+ can_be_parsed_as_list("[]") -> True
+ can_be_parsed_as_list(" [] ") -> True
+ can_be_parsed_as_list("[1, 2]") -> True
+ can_be_parsed_as_list({}) -> False
+ can_be_parsed_as_list(" [ invalid ] ") -> False
+
+ Args:
+ value: The value to be parsed.
+
+ Returns:
+ True if the value can be parsed as a list. False otherwise.
+ """
if isinstance(value, list):
return True
diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py
index 735abc3..853537c 100644
--- a/tests/test_parse_value.py
+++ b/tests/test_parse_value.py
@@ -50,6 +50,7 @@ def test_parse_value_parses_ints_floats_and_bools(test_input, expected):
),
(" [1, 2, 3] ", [1, 2, 3]),
(" [True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]),
+ (" [ [ [ [] ]] ] ", [[[[]]]]),
],
)
def test_parse_value_parses_dicts_and_lists(test_input, expected):