Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
types.py
Go to the documentation of this file.
1 import json
2 from webiopi.utils import logger
3 
4 M_PLAIN = "text/plain"
5 M_JSON = "application/json"
6 
7 def jsonDumps(obj):
8  if logger.debugEnabled():
9  return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
10  else:
11  return json.dumps(obj)
12 
13 def str2bool(value):
14  return (value == "1") or (value == "true") or (value == "True") or (value == "yes") or (value == "Yes")
15 
16 def toint(value):
17  if isinstance(value, str):
18  if value.startswith("0b"):
19  return int(value, 2)
20  elif value.startswith("0x"):
21  return int(value, 16)
22  else:
23  return int(value)
24  return value
25 
26 
27 def signInteger(value, bitcount):
28  if value & (1<<(bitcount-1)):
29  return value - (1<<bitcount)
30  return value