twoolie/NBT — pull request #31 by macfreek (2012-03-13 15:45:29+00:00)
Title: (3/5) Pretty print of NBT objects

This pull request is **branched after pull request #30**; please examine that request first. (_The comments below are only about commits a7c12afc to 505760ca_. Let me know if this is problem and you want me to rebase this.)

This pull request is purely artificial: I did not like the non-informative format for NBT:

>>> f = nbt.NBTFile("bigtest.nbt")
>>> print(f)
11 Entries
>>> print(repr(f))
11 Entries

After this patch, the output is:

>>> f = nbt.NBTFile("bigtest.nbt")
>>> print(f)
{TAG_Long(u'longTest'): 9223372036854775807, TAG_Short(u'shortTest'): 32767, TAG_String(u'stringTest'): HELLO WORLD THIS IS A TEST STRING ÅÄÖ!, TAG_Float(u'floatTest'): 0.498231470585, TAG_Int(u'intTest'): 2147483647, TAG_Compound(u'nested compound test'): {2 Entries}, TAG_List(u'listTest (long)'): [5 TAG_Long(s)], TAG_List(u'listTest (compound)'): [2 TAG_Compound(s)], TAG_Byte(u'byteTest'): 127, TAG_Byte_Array(u'byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))'): [1000 byte(s)], TAG_Double(u'doubleTest'): 0.493128713218}
>>> print(repr(f))

The result is not fully recursive, but one level deep. This is contrary to the recommendation by Python, but I find this easiest to read. Also, I later found out that the output is not exactly the same as in NBT.txt. If you like it to be exactly the same, let me know and I'll change the patch.

(Note that there are some Unicode issues with this version; see pull request 5/5 for a fix for that)