[ python-Feature Requests-1351692 ] Switch to make pprint.pprint display ints and longs in hex

SourceForge.net noreply at sourceforge.net
Fri Nov 11 18:47:44 CET 2005


Feature Requests item #1351692, was opened at 2005-11-08 13:29
Message generated for change (Comment added) made by markhirota
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1351692&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Mark Hirota (markhirota)
Assigned to: Nobody/Anonymous (nobody)
Summary: Switch to make pprint.pprint display ints and longs in hex

Initial Comment:
It would be nice to have some sort of switch or hook to 
allow 'pretty-printing' of integers and long integers in 
hexidecimal. So, for example:

>>> import pprint
>>> pprint.pprint(range(10)) # instead of this:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> pprint.hexint = True
>>> pprint.pprint(range(10)) # you would get this:
[0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]
>>> pprint.pprint(range(0x100000000,0x100000010)) # 
and this:
[0x100000000L,
 0x100000001L,
 0x100000002L,
 0x100000003L,
 0x100000004L,
 0x100000005L,
 0x100000006L,
 0x100000007L,
 0x100000008L,
 0x100000009L,
 0x10000000AL,
 0x10000000BL,
 0x10000000CL,
 0x10000000DL,
 0x10000000EL,
 0x10000000FL]
>>>

Thanks,
--MH

----------------------------------------------------------------------

>Comment By: Mark Hirota (markhirota)
Date: 2005-11-11 09:47

Message:
Logged In: YES 
user_id=1375527

Is this bypassing considered a limitation or a bug?  I am, 
however, able to workaround the issue by setting the 
width=1: "mpp = MyPrettyPrinter(1,1)" -- it just means that 
instead of:

>>> mpp.pprint(range(10))
[0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]

I get instead:

>>> mpp.pprint(range(10))
[0x0,
 0x1,
 0x2,
 0x3,
 0x4,
 0x5,
 0x6,
 0x7,
 0x8,
 0x9]

...which is OK for my uses.  Thanks!

----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2005-11-10 14:56

Message:
Logged In: YES 
user_id=89016

In theory this should be possible by subclassing
pprint.PrettyPrinter and overwritting the format method:

import pprint

class MyPrettyPrinter(pprint.PrettyPrinter):
  def format(self, object, context, maxlevels, level):
    if isinstance(object, int):
      return hex(object), True, False
    else:
      return pprint.PrettyPrinter.format(self, object,
context, maxlevels, level)

mpp = MyPrettyPrinter()
mpp.pprint(range(50))

This doesn't work reliable though: When the string is short
enough, format() seems to be bypassed and repr() is called
directly.


----------------------------------------------------------------------

Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-11-09 13:45

Message:
Logged In: YES 
user_id=1188172

Moving to Feature Requests.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1351692&group_id=5470


More information about the Python-bugs-list mailing list