Monday, 15 September 2014

python - How can I configure ipython to display integers in hex format? -



python - How can I configure ipython to display integers in hex format? -

here's default behavior:

in [21]: 255 out[21]: 255

and here's like:

in [21]: 255 out[21]: ff

can setup ipython that?

you can registering special display formatter ints:

in [1]: formatter = get_ipython().display_formatter.formatters['text/plain'] in [2]: formatter.for_type(int, lambda n, p, cycle: p.text("%x" % n)) out[2]: <function ipython.lib.pretty._repr_pprint> in [3]: 1 out[3]: 1 in [4]: 100 out[4]: 64 in [5]: 255 out[5]: ff

if want always-on, can create file in $(ipython locate profile)/startup/hexints.py first 2 lines (or 1 avoid assignments):

get_ipython().display_formatter.formatters['text/plain'].for_type(int, lambda n, p, cycle: p.text("%x" % n))

which executed every time start ipython.

python ipython

No comments:

Post a Comment