我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用collections.OrderedDict.__repr__()。
def __repr__(self): return 'OrderedDefaultDict(%s, %s)' % (self.default_factory, OrderedDict.__repr__(self))
def pprint_for_ordereddict(): """ Context manager that causes pprint() to print OrderedDict objects as nicely as standard Python dictionary objects. """ od_saved = OrderedDict.__repr__ try: OrderedDict.__repr__ = dict.__repr__ yield finally: OrderedDict.__repr__ = od_saved
def __repr__(self): return 'DefaultOrderedDict(%s, %s)' % (self.default_factory, OrderedDict.__repr__(self))
def __repr__(self, *args, **kwargs): return self.o.__repr__(*args, **kwargs)
def __repr__(self): return 'OrderedDefaultDict(%s, %s)' % (self.default_factory, OrderedDict.__repr__(self)) # From requests (Apache 2 License)
def __repr__(self): return str(dict(self.items())) # From requests (Apache 2 License)
def __repr__(self): return '<lookup \'%s\'>' % (self.name)
def _format(self, obj, stream, indent=4, level=1): _repr = getattr(type(obj), '__repr__', None) write = stream.write if ( (isinstance(obj, dict) and _repr is dict.__repr__) or (isinstance(obj, OrderedDict) and _repr == OrderedDict.__repr__) ): write('{\n') num = len(obj) if num > 0: for i, (key, value) in enumerate(obj.items()): write(' ' * (indent * level)) write("'%s'" % key) write(': ') self._format(value, stream, level=level + 1) if i < num - 1: write(',') write('\n') write(' ' * (indent * (level - 1))) write('}') elif isinstance(obj, list) and _repr is list.__repr__: write('[') num = len(obj) if num > 0: write('\n') for i, value in enumerate(obj): write(' ' * (indent * level)) self._format(value, stream, level=level + 1) if i < num - 1: write(',') write('\n') write(' ' * (indent * (level - 1))) write(']') else: value = repr(obj) if '\n' in value: lines = value.split('\n') num = len(lines) for i, line in enumerate(lines): if i > 0: write(' ' * (indent * (level - 1))) write(line) if i < num - 1: write('\n') else: write(value)