def humannum(data, unit=None, include=None, exclude=None):
if isinstance(data, types.DictType):
data = data.copy()
keys = set(data.keys())
if include is not None:
keys = keys & set(include)
if exclude is not None:
keys = keys - set(exclude)
for k in keys:
data[k] = humannum(data[k])
return data
elif isinstance(data, types.BooleanType):
# We have to deal with bool because for historical reason bool is
# subclass of int.
# When bool is introduced into python 2.2 it is represented with int,
# similar to C.
return data
elif isinstance(data, types.ListType):
return [humannum(x) for x in data]
elif isinstance(data, types.StringTypes):
return data
elif isinstance(data, integer_types):
return humannum_int(data, unit=unit)
elif isinstance(data, types.FloatType):
if data > 999:
return humannum_int(int(data), unit=unit)
elif abs(data) < 0.0000000001:
return '0'
else:
return '%.2f' % (data)
else:
return data
评论列表
文章目录