From 0de908b8e03ffdea62236abeb4a3b996c2386c6f Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 29 Jan 2021 12:25:09 +0800 Subject: [PATCH] cleanup fromStr --- meshtastic/__main__.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 50a0d69..c1c6a51 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -49,6 +49,9 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): print(f"Connection changed: {topic.getName()}") +trueTerms = {"t", "true", "yes"} +falseTerms = {"f", "false", "no"} + def fromStr(valstr): """try to parse as int, float or bool (and fallback to a string as last resort) @@ -59,6 +62,10 @@ def fromStr(valstr): """ if(valstr.startswith('0x')): val = bytes.fromhex(valstr[2:]) # if needed convert to string with asBytes.decode('utf-8') + elif valstr in trueTerms: + val = True + elif valstr in falseTerms: + val = False else: try: val = int(valstr) @@ -66,14 +73,8 @@ def fromStr(valstr): try: val = float(valstr) except ValueError: - trueTerms = {"t", "true", "yes"} - falseTerms = {"f", "false", "no"} - if valstr in trueTerms: - val = True - elif valstr in falseTerms: - val = False - else: - val = valstr # Try to treat the parameter as a string + val = valstr # Not a float or an int, assume string + return val