Skip to content

Instantly share code, notes, and snippets.

@Aloxaf Aloxaf/__init__.py
Created Aug 9, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.