Skip to content

Instantly share code, notes, and snippets.

@Aloxaf
Created August 9, 2018 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aloxaf/de3de8e7c0b8913335847afd3ff76cc7 to your computer and use it in GitHub Desktop.
Save Aloxaf/de3de8e7c0b8913335847afd3ff76cc7 to your computer and use it in GitHub Desktop.
use hex instead of ascii when display bytes in ipython
'''
to load allhex any where
copy allhex.py and __init__.py to folder "~/.local/lib/python3.7/site-packages/allhex/"
(3.7 is your python version)
'''
from .allhex import *
# allhex.py
'''
use "%load_ext allhex" to load it
use "%unload_ext allhex" to unload it
to load it automatically, add
`get_ipython().run_line_magic('load_ext', 'allhex')`
to your ~/.ipython/profile_default/startup/load_extensions.py
'''
def load_ipython_extension(ipython):
formatter = ipython.display_formatter.formatters['text/plain']
def _repr(obj, p, cycle):
s = "b'" + (''.join(f'\\x{c:02x}' for c in obj)) + "'"
p.text(s)
formatter.for_type(bytes, _repr)
def unload_ipython_extension(ipython):
formatter = ipython.display_formatter.formatters['text/plain']
formatter.for_type(bytes, lambda obj, p, cycle: p.text(str(obj)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment