Hex editor display - can this be more pythonic?

CC crobc at BOGUS.sbcglobal.net
Sun Jul 29 21:27:25 EDT 2007


Marc 'BlackJack' Rintsch wrote:
> On Sun, 29 Jul 2007 12:24:56 -0700, CC wrote:
>>The next step consists of printing out the ASCII printable characters. 
>>I have devised the following silliness:
>>
>>printable = ' 
>>1!2 at 3#4$5%6^7&8*9(0)aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ\
>>`~-_=+\\|[{]};:\'",<.>/?'
> 
> I'd use `string.printable` and remove the "invisible" characters like '\n'
> or '\t'.

What is `string.printable` ?  There is no printable method to strings, 
though I had hoped there would be.  I don't yet know how to make one.

>>for c in ln:
>>     if c in printable: sys.stdout.write(c)
>>     else: sys.stdout.write('.')

> The translation table can be created once and should be faster.

I suppose the way I'm doing it requires a search through `printable` for 
each c, right?  Whereas the translation would just be a lookup 
operation?  If so then perhaps the translation would be better.

>>I'd like to display the non-printable characters differently, since they 
>>can't be distinguished from genuine period '.' characters.  Thus, I may 
>>use ANSI escape sequences like:
>>
>>for c in ln:
>>     if c in printable: sys.stdout.write(c)
>>     else:
>>         sys.stdout.write('\x1B[31m.')
>>         sys.stdout.write('\x1B[0m')
>>
>>print
> 
> `re.sub()` might be an option here.

Yeah, that is an interesting option.  Since I don't wish to modify the 
block of data unless the user specifically edits it, so I might prefer 
the simple display operation.

> For escaping:
> 
> In [90]: '\n'.encode('string-escape')
> Out[90]: '\\n'

Hmm, I see there's an encoder that can do my hex display too.

Thanks for the input!

-- 
_____________________
Christopher R. Carlen
crobc at bogus-remove-me.sbcglobal.net
SuSE 9.1 Linux 2.6.5



More information about the Python-list mailing list