improved over.types.ndict.__repr__

This commit is contained in:
Martin 2017-10-13 14:28:15 +02:00
parent c92c0fb6e9
commit f8b9f983ad
2 changed files with 9 additions and 7 deletions

View file

@ -114,7 +114,7 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show
""" """
Writes a hex dump of `data` to `output`. Writes a hex dump of `data` to `output`.
The output text is indented with spaces and contains `offset` bytes per line. The output text is indented with spaces and contains `width` bytes per line.
If `show_header` is True, a single line with byte numbers preceeds all output. If `show_header` is True, a single line with byte numbers preceeds all output.
If `show_offsets` is True, each line is prefixed with the address of the first byte of that line. If `show_offsets` is True, each line is prefixed with the address of the first byte of that line.
If `show_ascii` is True, each line is suffixed with its ASCII representation. Unprintable characters If `show_ascii` is True, each line is suffixed with its ASCII representation. Unprintable characters
@ -145,11 +145,11 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show
if show_offsets: if show_offsets:
line.append("offset"[:offset_figures].ljust(offset_figures + 2)) line.append("offset"[:offset_figures].ljust(offset_figures + 2))
for i in range(offset): for i in range(width):
line.append("%2x" %(i)) line.append("%2x" %(i))
if show_ascii: if show_ascii:
line.append(" *{0}*".format("ASCII".center(offset, "-"))) line.append(" *{0}*".format("ASCII".center(width, "-")))
output_io.write(" ".join(line) + "\n") output_io.write(" ".join(line) + "\n")
@ -163,7 +163,7 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show
hex_bytes = [] hex_bytes = []
ascii_bytes = [] ascii_bytes = []
for local_i, i in enumerate(range(ptr, ptr+offset)): for local_i, i in enumerate(range(ptr, ptr+width)):
if i < len(data): if i < len(data):
c = data[i] c = data[i]
hex_bytes.append("%02x" %(c)) hex_bytes.append("%02x" %(c))
@ -173,7 +173,7 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show
else: else:
ascii_bytes.append(".") ascii_bytes.append(".")
elif i == len(data): elif i == len(data):
hex_bytes.extend([" "] * (offset - local_i)) hex_bytes.extend([" "] * (width - local_i))
if use_colors: output_io.write(text.render("<W>")) if use_colors: output_io.write(text.render("<W>"))
output_io.write(" ".join(hex_bytes)) output_io.write(" ".join(hex_bytes))
@ -186,7 +186,7 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show
output_io.write("\n") output_io.write("\n")
ptr += offset ptr += width
if not output: if not output:
output_io.seek(0) output_io.seek(0)

View file

@ -26,7 +26,9 @@ class ndict:
object.__setattr__(self, "d", OrderedDict(*args, **kwargs)) object.__setattr__(self, "d", OrderedDict(*args, **kwargs))
def __repr__(self): def __repr__(self):
return "|" + repr(self.d) atoms = ["(%s, %s)" %(repr(k), repr(v)) for k, v in self.items()]
return "ndict([" + ", ".join(atoms) + "])"
def __iter__(self): def __iter__(self):
return self.d.__iter__() return self.d.__iter__()