python - __repr__ for (large) composite objects -


i have informative representations composite objects (i.e., objects composed of other (potentially composite) objects). however, because code fundamentally deals high-precision numbers (please don't ask me why don't use doubles), end representations see here: http://pastebin.com/jplgafxc. better stick default __repr__?

whether have verbose repr depends on want accomplish. complex or composite objects, know i'd prefer of following:

point(x=1.12, y=2.2, z=-1.9) <__main__.point object @ 0x103011890> 

they both tell me type object is, first clear of (relevant) values involved, , avoids low-level information relevant on rarest of occasions.

i see real values. but, yours special case, given values frightfully humongous:

72401317106217603290426741268390656010621951704689382948334809645 87850348552960901165648762842931879347325584704068956434195098288 38279057775096090002410493665682226331178331461681861612403032369 73237863637784679012984303024949059416189689048527978878840119376 5152408961823197987224502419157858495179687559851 

that cannot useful development or debugging purposes. i'm sure there times need full serialization--to send , files, example. have rare, no? can't imagine remember 309 digits, or can determine if above number same 1 below on visual inspection:

72401317106217603290426741268390656010621951704689382948334809645 87850348552960901165648762842931879347325584704068956434195098288 38279057775096090002410493665682226331178331461681861612403032369 73327863637784679012984303024949059416189689048527978878840119376 5152408961823197987224502419157858495179687559851 

they're not same. unless you're spock or terminator, wouldn't know quick glance. (and actually, i've made easier here, length-wrapping avoid having horizontally scroll.)

so recommend (massively) shortening representation, make output more tractable. printing out entire chapter text every time want print chapter object. overkill.

instead, try shorter , easier work with. truncation and/or ellipsis useful. e.g.

72401...59851 7240131710...  

you can use object id well. if high-precision type hp, then:

hp(0x103011890) 

at least able tell them apart. 1 ugliness of using object ids, however, objects can logically equivalent, if create multiple objects same logical value, they'd have different ids, appear different when not. can around creating own short hash function. there's bit of art hashing, reprs, simple work. e.g.:

import binascii, struct  def shorthash(s):     """     given python value, produce short alphanumeric hash     helps identify debugging purposes. riff on      http://stackoverflow.com/a/2511059/240490     enhanced remove trailing boilerplate, , work     on either python 2 or python 3.     """     hashbytes = binascii.b2a_base64(struct.pack('l', hash(s)))     return hashbytes.decode('utf-8').rstrip().rstrip("=") 

then define repr in high-precision class:

def __repr__(self):     clsname = self.__class__.__name__     return '{0}({1}).format(clsname, shorthash(self.value)) 

where self.value whatever local attribute, property, or method creates multi-hundred-digit value. if you're subclassing int, self.

this gets to:

hp(tea+5my0wwa) 

the 2 massive, identical numbers above? using scheme, render out to:

hp(xhkg0358fx4) hp(27cdig5elhq) 

which different. can combine bit of value representation. e.g. few alternatives:

hp(~7.24013e308 @ xhkg0358fx4) hp(dig='72401...59851', ndigits=309, hash='xhkg0358fx4') 

you'll find these shorter values more useful in debugging contexts. can, of course, keep around method or property (e.g. .value, .digits, or .alldigits) case in need every last bit, define common case more consumed.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -