You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

37 lines
1007 B

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def toInt(raw) -> int:
try:
return int(raw)
except:
raise RuntimeError('Unable convert to int')
def toStr(raw, allowNone: bool = False) -> str:
if raw is None:
if allowNone: # None => 'none'
return 'none'
raise RuntimeError('None could not convert to str')
if isinstance(raw, bytes): # bytes -> str
return str(raw, encoding = 'utf-8')
try:
return str(raw)
except:
raise RuntimeError('Unable convert to str')
def toStrTidy(raw, allowNone: bool = False) -> str:
return toStr(raw, allowNone).strip().lower() # with trim and lower
def toBool(raw) -> bool:
if isinstance(raw, (bool, int, float)):
return bool(raw)
try:
raw = toStr(raw).strip().lower()
if raw in ['true', 'false']:
return True if raw == 'true' else False
return int(raw) != 0
except:
raise RuntimeError('Unable convert to bool')