def my_safe_repr(obj, context, maxlevels, level, sort_dicts=True): if type(obj) == unicode: obj = obj.encode("utf-8") # Python 3.8 changed the call signature of pprint._safe_repr. # by adding sort_dicts. if PY38MIN: return pprint._safe_repr(obj, context, maxlevels, level, sort_dicts) else: return pprint._safe_repr(obj, context, maxlevels, level)
def _non_unicode_repr(objekt, context, maxlevels, level): """ Used to override the pprint format method to get rid of unicode prefixes. E.g.: 'John' instead of u'John'. """ if sys.version_info[0] == 3 and sys.version_info[1] >= 8: repr_string, isreadable, isrecursive = pprint._safe_repr( objekt, context, maxlevels, level, sort_dicts=None) else: repr_string, isreadable, isrecursive = pprint._safe_repr( objekt, context, maxlevels, level) if repr_string.startswith('u"') or repr_string.startswith("u'"): repr_string = repr_string[1:] return repr_string, isreadable, isrecursive
def my_safe_repr(object, context, maxlevels, level): typ = pprint._type(object) if typ is unicode: r = 'u"%s"' % (object.encode("utf8").replace('"', r'\"')) return (r, True, False) else: return pprint._safe_repr(object, context, maxlevels, level)
def format(self, object, context, maxlevels, level): if isinstance(object, StringType): return py3_repr_str(object), True, False elif isinstance(object, BytesType): return py3_repr_bytes(object), True, False return pprint._safe_repr(object, context, maxlevels, level)
def my_safe_repr(object, context, maxlevels, level): typ = pprint._type(object) if typ is unicode: r = 'u"%s"' % (object.encode("utf8").replace('"', r'\"')) return (r, True, False) else: return pprint._safe_repr(object, context, maxlevels, level)
def ascii_format(object_, context, maxlevels, level): typ = pprint._type(object_) if typ is unicode: object_ = str(object_).replace('"', '\\"') # replace " to ' rep = "\"%s\"" % object_ # repr(object_) return rep, (rep and not rep.startswith('<')), False return pprint._safe_repr(object_, context, maxlevels, level)
def ascii_format(object_, context, maxlevels, level): typ = pprint._type(object_) if typ is unicode: object_ = str(object_).replace('"', '\\"') # replace " to ' rep = "\"%s\"" % object_ # repr(object_) return rep, (rep and not rep.startswith('<')), False return pprint._safe_repr(object_, context, maxlevels, level)
def my_safe_repr(obj, context, maxlevels, level): """pretty print supressing unicode prefix http://stackoverflow.com/questions/16888409/ suppress-unicode-prefix-on-strings-when-using-pprint """ typ = type(obj) if six.PY2 and typ is six.text_type: obj = str(obj) return pprint._safe_repr(obj, context, maxlevels, level)
def my_safe_repr(obj, context, maxlevels, level): """pretty print supressing unicode prefix http://stackoverflow.com/questions/16888409/ suppress-unicode-prefix-on-strings-when-using-pprint """ typ = type(obj) if six.PY2 and typ is six.text_type: obj = str(obj) return pprint._safe_repr(obj, context, maxlevels, level)
def non_unicode_repr(objekt, context, maxlevels, level): """ Used to override the pprint format method to get rid of unicode prefixes. E.g.: 'John' instead of u'John'. """ repr_string, isreadable, isrecursive = pprint._safe_repr(objekt, context, maxlevels, level) if repr_string.startswith('u"') or repr_string.startswith("u'"): repr_string = repr_string[1:] return repr_string, isreadable, isrecursive
def _non_unicode_repr(objekt, context, maxlevels, level): """ Used to override the pprint format method to get rid of unicode prefixes. E.g.: 'John' instead of u'John'. """ repr_string, isreadable, isrecursive = pprint._safe_repr( objekt, context, maxlevels, level) if repr_string.startswith('u"') or repr_string.startswith("u'"): repr_string = repr_string[1:] return repr_string, isreadable, isrecursive
def format(_obj, context, maxlevels, level): if isinstance(_obj, bytes): #-# return (repr(_obj.encode('utf8')) or "''", False, False) return (("'" + _obj.decode('utf8') + "'") or "''", False, False) if isinstance(_obj, str): if unquote(_obj) == _obj: return (repr(_obj) or "''", False, False) else: #-# return (repr(unquote(_obj).decode('unicode-escape').encode('utf8')) or "''", False, False) #-# return (("'" + unquote(_obj).decode('unicode-escape').encode('utf8') + "'") or "''", False, False) return (("'" + unquote(_obj) + "'") or "''", False, False) return pprint._safe_repr(_obj, context, maxlevels, level)
def dumps(obj): """Return serialized python object as a string :param obj: The python object to be serialized :return: String with serialized object WARNING/TODO: ``dumps`` just checks the object is not recursive via ``pprint.isreadable``, but does not impose any other limits """ repr_string, isreadable, isrecursive = \ _safe_repr(obj, {}, None, 0) if not isreadable: raise TypeError("'{}' is not readable".format(obj)) return repr_string
def format(self, object, context, maxlevels, level): typ = type(object) if typ in string_types: object = object.decode("utf-8") elif typ is _dt.datetime: return "DT( %s )"%(format_dt(object),),True,False elif typ is not unicode: return _safe_repr(object, context, maxlevels, level) if "'" in object and '"' not in object: closure = '"' quotes = {'"': '\\"'} else: closure = "'" quotes = {"'": "\\'"} qget = quotes.get sio = _StringIO() write = sio.write for char in object: if char.isalpha(): write(char) else: write(qget(char, repr(char)[2:-1])) return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
def format(self, object, context, maxlevels, level): typ = type(object) if typ in string_types: object = object.decode("utf-8") elif typ is _dt.datetime: return "DT( %s )" % (format_dt(object), ), True, False elif typ is not unicode: return _safe_repr(object, context, maxlevels, level) if "'" in object and '"' not in object: closure = '"' quotes = {'"': '\\"'} else: closure = "'" quotes = {"'": "\\'"} qget = quotes.get sio = _StringIO() write = sio.write for char in object: if char.isalpha(): write(char) else: write(qget(char, repr(char)[2:-1])) return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
def my_safe_repr(object, context, maxlevels, level): if type(object) == unicode: object = object.encode("utf-8") return pprint._safe_repr(object, context, maxlevels, level)
def pprintFormatter(thing, context, maxlevels, level): typ = pprint._type(thing) if typ is unicode: thing = str(thing) return pprint._safe_repr(thing, context, maxlevels, level)
def format_print(object, context, maxlevels, level): typ = pprint._type(object) if typ is unicode: object = str(object) return pprint._safe_repr(object, context, maxlevels, level)
def my_safe_repr(object, context, maxlevels, level): typ = pprint._type(object) if typ is unicode: object = str(object) return pprint._safe_repr(object, context, maxlevels, level)
def de_unicode(object, context, maxlevels, level): if pprint._type(object) is unicode: object = str(object) return pprint._safe_repr(object, context, maxlevels, level)
def format(self, object, context, maxlevels, level): """Format un object.""" unused = self if type(object) is str: return "'" + str(object) + "'", True, False return pprint._safe_repr(object, context, maxlevels, level)
def format(self, object, context, maxlevels, level): """Format un object.""" if isinstance(object, text_type): return "'" + str(object) + "'", True, False return pprint._safe_repr(object, context, maxlevels, level)
def my_safe_repr(object, context, maxlevels, level): typ = pprint._type(object) if typ is unicode: object = str(object.encode('utf-8')) return pprint._safe_repr(object, context, maxlevels, level)
def suppress_unicode_repr(object, context, maxlevels, level): typ = pprint._type(object) if typ is unicode: object = str(object) return pprint._safe_repr(object, context, maxlevels, level)
def update_event(self, inp=-1): self.set_output_val( 0, pprint._safe_repr(self.input(0), self.input(1), self.input(2), self.input(3), self.input(4)))
def no_unicode(object, context, maxlevels, level): """ change unicode u'foo' to string 'foo' when pretty printing""" if pprint._type(object) is unicode: object = str(object) return pprint._safe_repr(object, context, maxlevels, level)
def no_unicode(object, context, maxlevels, level): """ change unicode u'foo' to string 'foo' when pretty printing""" if pprint._type(object) is unicode: object = str(object) return pprint._safe_repr(object, context, maxlevels, level)
def format(self, object, context, maxlevels, level): """Format un object.""" unused = self if type(object) is str: return "'" + str(object) + "'", True, False return pprint._safe_repr(object, context, maxlevels, level)
def concat(object, context, maxlevels, level): """ Concatenate any string which pprint displays. This is recursive, so it also should apply to dictionary keys and values, for example.""" if pprint._type(object) is (unicode or str): if len(object) > pprint_concatenation_length: object = object[:pprint_concatenation_length] + '...' return pprint._safe_repr(object, context, maxlevels, level)
def nice_repr(object, context, maxlevels, level): if sys.version_info.major < 3: typ = type(object) if typ is unicode: object = object.encode("utf-8") return pprint._safe_repr(object, context, maxlevels, level)
def format(self, object, context, maxlevels, level): """Format un object.""" if isinstance(object, text_type): return "'" + str(object) + "'", True, False return pprint._safe_repr(object, context, maxlevels, level)