def around(iterable, tol): if isinstance(iterable, float): return round(iterable, tol) from klepto.tools import isiterable if not isiterable(iterable): return iterable itype = type(iterable) _iterable = list(iterable) for i,j in enumerate(iterable): if isinstance(j, float): _iterable[i] = round(j, tol) return itype(_iterable)
def deep_round(*args, **kwds): argstype = type(args) _args = list(args) _kwds = kwds.copy() for i,j in enumerate(args): if isinstance(j, float): _args[i] = round(j, tol) # don't round int elif isinstance(j, (str, unicode, type(BaseException()))): continue elif isinstance(j, dict): _args[i] = deep_round(**j)[1] elif isiterable(j): #XXX: fails on the above, so don't iterate them jtype = type(j) _args[i] = jtype(deep_round(*j)[0]) for i,j in kwds.items(): if isinstance(j, float): _kwds[i] = round(j, tol) elif isinstance(j, (str, unicode, type(BaseException()))): continue elif isinstance(j, dict): _kwds[i] = deep_round(**j)[1] elif isiterable(j): #XXX: fails on the above, so don't iterate them jtype = type(j) _kwds[i] = jtype(deep_round(*j)[0]) return argstype(_args), _kwds
def around(iterable, tol): if not isiterable(iterable): return round(iterable, tol) itype = type(iterable) return itype( round(i, tol) for i in iterable )
def around(iterable, tol): if not isiterable(iterable): return round(iterable, tol) itype = type(iterable) return itype(round(i, tol) for i in iterable)