def toString(val): if not _Duration: _late_import() if val == None: return "" elif isinstance(val, (Mapping, list, set)): return _json_encoder(val, pretty=True) elif hasattr(val, "__json__"): return val.__json__() elif isinstance(val, _Duration): return _unicode(round(val.seconds, places=4)) + " seconds" elif isinstance(val, timedelta): duration = val.total_seconds() return _unicode(round(duration, 3)) + " seconds" elif isinstance(val, _unicode): return val elif isinstance(val, str): try: return val.decode('utf8') except Exception, _: pass try: return val.decode('latin1') except Exception as e: if not _Log: _late_import() _Log.error(unicode(type(val)) + " type can not be converted to unicode", cause=e)
def tab(value): if isinstance(value, Mapping): h, d = zip(*wrap(value).leaves()) return \ "\t".join(map(value2json, h)) + \ "\n" + \ "\t".join(map(value2json, d)) else: _unicode(value)
def tab(value): if not _convert: _late_import() if isinstance(value, Mapping): h, d = zip(*wrap(value).leaves()) return \ "\t".join(map(_convert.value2json, h)) + \ "\n" + \ "\t".join(map(_convert.value2json, d)) else: _unicode(value)
def utf82unicode(value): """ WITH EXPLANATION FOR FAILURE """ try: return value.decode("utf8") except Exception as e: if not _Log: _late_import() if not isinstance(value, basestring): _Log.error( "Can not convert {{type}} to unicode because it's not a string", type=type(value).__name__) e = _Except.wrap(e) for i, c in enumerate(value): try: c.decode("utf8") except Exception, f: _Log.error( "Can not convert charcode {{c}} in string index {{i}}", i=i, c=ord(c), cause=[e, _Except.wrap(f)]) try: latin1 = _unicode(value.decode("latin1")) _Log.error( "Can not explain conversion failure, but seems to be latin1", e) except Exception: pass try: a = _unicode(value.decode("iso-8859-1")) _Log.error( "Can not explain conversion failure, but seems to be iso-8859-1", e) except Exception: pass _Log.error( "Can not explain conversion failure of " + type(value).__name__ + "!", e)
def right_align(value, length): if length <= 0: return u"" value = _unicode(value) if len(value) < length: return (" " * (length - len(value))) + value else: return value[-length:]
def left_align(value, length): if length <= 0: return u"" value = _unicode(value) if len(value) < length: return value + (" " * (length - len(value))) else: return value[:length]
def indent(value, prefix=u"\t", indent=None): if indent != None: prefix = prefix * indent value = toString(value) try: content = value.rstrip() suffix = value[len(content):] lines = content.splitlines() return prefix + (u"\n" + prefix).join(lines) + suffix except Exception, e: raise Exception(u"Problem with indent of value (" + e.message + u")\n" + _unicode(toString(value)))
def comma(value): """ FORMAT WITH THOUSANDS COMMA (,) SEPARATOR """ try: if float(value) == __builtin__.round(float(value), 0): output = "{:,}".format(int(value)) else: output = "{:,}".format(float(value)) except Exception: output = _unicode(value) return output
def indent(value, prefix=u"\t", indent=None): if indent != None: prefix = prefix * indent value = toString(value) try: content = value.rstrip() suffix = value[len(content):] lines = content.splitlines() return prefix + (u"\n" + prefix).join(lines) + suffix except Exception as e: raise Exception(u"Problem with indent of value (" + e.message + u")\n" + _unicode(toString(value)))
def percent(value, decimal=None, digits=None, places=None): value = float(value) if value == 0.0: return "0%" digits = coalesce(digits, places) if digits != None: left_of_decimal = int(math.ceil(math.log10(abs(value)))) + 2 decimal = digits - left_of_decimal decimal = coalesce(decimal, 0) right_of_decimal = max(decimal, 0) format = "{:." + _unicode(right_of_decimal) + "%}" return format.format(__builtin__.round(value, decimal + 2))
def toString(val): if not _convert: _late_import() if val == None: return "" elif isinstance(val, (Mapping, list, set)): return _json_encoder(val, pretty=True) elif hasattr(val, "__json__"): return val.__json__() elif isinstance(val, _Duration): return _unicode(round(val.seconds, places=4)) + " seconds" elif isinstance(val, timedelta): duration = val.total_seconds() return _unicode(round(duration, 3)) + " seconds" try: return _unicode(val) except Exception, e: if not _Log: _late_import() _Log.error(str(type(val)) + " type can not be converted to unicode", e)
def round(value, decimal=None, digits=None, places=None): """ :param value: THE VALUE TO ROUND :param decimal: NUMBER OF DECIMAL PLACES TO ROUND (NEGATIVE IS LEFT-OF-DECIMAL) :param digits: ROUND TO SIGNIFICANT NUMBER OF digits :param places: SAME AS digits :return: """ value = float(value) if value == 0.0: return "0" digits = coalesce(digits, places) if digits != None: left_of_decimal = int(math.ceil(math.log10(abs(value)))) decimal = digits - left_of_decimal right_of_decimal = max(decimal, 0) format = "{:." + _unicode(right_of_decimal) + "f}" return format.format(__builtin__.round(value, decimal))
def unicode(value): if value == None: return "" return _unicode(value)
except Exception, e: if not _Log: _late_import() if not isinstance(value, basestring): _Log.error("Can not _convert {{type}} to unicode because it's not a string", type= type(value).__name__) e = _Except.wrap(e) for i, c in enumerate(value): try: c.decode("utf8") except Exception, f: _Log.error("Can not _convert charcode {{c}} in string index {{i}}", i=i, c=ord(c), cause=[e, _Except.wrap(f)]) try: latin1 = _unicode(value.decode("latin1")) _Log.error("Can not explain conversion failure, but seems to be latin1", e) except Exception: pass try: a = _unicode(value.decode("iso-8859-1")) _Log.error("Can not explain conversion failure, but seems to be iso-8859-1", e) except Exception: pass _Log.error("Can not explain conversion failure of " + type(value).__name__ + "!", e) def wordify(value): return [w for w in re.split(r"[\W_]", value) if strip(w)]
return val.decode('utf8') except Exception, _: pass try: return val.decode('latin1') except Exception as e: if not _Log: _late_import() _Log.error(unicode(type(val)) + " type can not be converted to unicode", cause=e) else: try: return _unicode(val) except Exception as e: if not _Log: _late_import() _Log.error(unicode(type(val)) + " type can not be converted to unicode", cause=e) def edit_distance(s1, s2): """ FROM http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance# Python LICENCE http://creativecommons.org/licenses/by-sa/3.0/ """ if len(s1) < len(s2):
"Can not _convert {{type}} to unicode because it's not a string", type=type(value).__name__) e = _Except.wrap(e) for i, c in enumerate(value): try: c.decode("utf8") except Exception, f: _Log.error( "Can not _convert charcode {{c}} in string index {{i}}", i=i, c=ord(c), cause=[e, _Except.wrap(f)]) try: latin1 = _unicode(value.decode("latin1")) _Log.error( "Can not explain conversion failure, but seems to be latin1", e) except Exception: pass try: a = _unicode(value.decode("iso-8859-1")) _Log.error( "Can not explain conversion failure, but seems to be iso-8859-1", e) except Exception: pass _Log.error(