From 1e00ee0dacff180d31e2fe60c46de61e32b45226 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Sat, 14 Apr 2012 14:34:34 +0200 Subject: Add str2bool helper Signed-off-by: Michael Buesch --- libtoprammer/util.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libtoprammer/util.py b/libtoprammer/util.py index a546999..b9bcd14 100644 --- a/libtoprammer/util.py +++ b/libtoprammer/util.py @@ -67,6 +67,18 @@ def bytes2ascii(bindata): return "" return "".join(map(byte2ascii, bindata)) +def str2bool(string): + string = str(string).lower().strip() + if string in ("false", "off", "no"): + return False + if string in ("true", "on", "yes"): + return True + try: + return bool(int(string, 10)) + except (ValueError), e: + pass + return None + def genRandomBlob(size): blob = map(lambda x: int2byte(random.randint(0, 0xFF)), range(0, size)) -- cgit v1.2.3