Created
August 9, 2018 10:05
-
-
Save Aloxaf/de3de8e7c0b8913335847afd3ff76cc7 to your computer and use it in GitHub Desktop.
use hex instead of ascii when display bytes in ipython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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