k4ever/k4ever/api2/helper.py

25 lines
615 B
Python

from decimal import Decimal, InvalidOperation
def getInt(d, key, default):
""" Helper for dict.get to return the default on error. """
try:
return int(d.get(key, default))
except ValueError:
return default
def getDecimal(d, key, default):
""" Helper for dict.get to return the default on error or a Decimal
with a precision of 2. """
try:
return Decimal("%.2f" % float(d.get(key, default)), 2)
except InvalidOperation:
return default
except ValueError:
return default
def getError(err, msg):
""" Get an error, write a message on it an return it. """
err.write("\n%s\n" % (msg,))
return err