Browse Source

update: add type utils

dev
dnomd343 2 years ago
parent
commit
bec1027a52
  1. 33
      Basis/Functions.py
  2. 37
      Utils/Common/Type.py

33
Basis/Functions.py

@ -61,36 +61,3 @@ def genUUID() -> str: # generate uuid v5
))
def toInt(raw) -> int:
try:
return int(raw)
except:
raise RuntimeError('Unable convert to int')
def toStr(raw) -> str:
if raw is 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) -> str:
return toStr(raw).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')

37
Utils/Common/Type.py

@ -0,0 +1,37 @@
#!/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')
Loading…
Cancel
Save