Top

grizzled.text module

The grizzled.text package contains text-related classes and modules.

Functions

def hexdump(

source, out, width=16, start=0, limit=None, show_repeats=False)

Produce a "standard" hexdump of the specified string or file-like object. The output consists of a series of lines like this::

000000: 72 22 22 22 4f  53 20 72 6f 75  r'''OS rou
00000a: 74 69 6e 65 73  20 66 6f 72 20  tines for
000014: 4d 61 63 2c 20  4e 54 2c 20 6f  Mac, NT, o
00001e: 72 20 50 6f 73  69 78 20 64 65  r Posix de
000028: 70 65 6e 64 69  6e 67 20 6f 6e  pending on
000032: 20 77 68 61 74  20 73 79 73 74   what syst
00003c: 65 6d 20 77 65  27 72 65 20 6f  em we're o
000046: 6e 2e 0a 0a 54  68 69 73 20 65  n...This e

The output width (i.e., the number of decoded characters shown on a line) can be controlled with the width parameter.

Adjacent repeated lines are collapsed by default. For example::

000000: 00 00 00 00 00  00 00 00 00 00  ..........
*** Repeated 203 times
0007f8: 72 22 22 22 4f  53 20 72 6f 75  r'''OS rou

This behavior can be disabled via the show_repeats parameter.

Parameters

  • source (str or file-like): The object whose contents are to be dumped in hex. The object can be a string or a file-like object.
  • out (file-like): Where to dump the hex output
  • width (int): The number of dumped characters per line
  • start (int): Offset within input where reading should begin
  • limit (int): Total number of bytes to dump. Defaults to everything from start to the end.
  • show_repeats: (bool): False to collapse repeated output lines, True to dump all lines, even if they're repeats.

def strip_margin(

s, margin_char='|')

Akin to Scala's stripMargin() method on string, this function takes a multiline string and strips leading white space up to a margin character. It allows you to express multiline strings like this:

s = '''|line 1
       |line 2
       |line 3
    '''

Then, calling strip_margin on the string results in:

'''line 1
line 2
line 3
'''

Parameters

  • s (str): the multiline string
  • margin_char (str): the margin character, defaulting to '|'. Must be a single characters

Returns:

the stripped string