Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/macfreek/python3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Woolford committed Mar 19, 2012
2 parents c509588 + 0ba7890 commit 7ce9f43
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 89 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Usage:
>>> nbtfile["nested compound test"].tag_info()
TAG_Compound("nested compound test"): 2 Entries
>>> for tag in nbtfile["nested compound test"]["ham"].tags:
... print tag.tag_info()
... print(tag.tag_info())
...
TAG_String("name"): Hampus
TAG_Float("value"): 0.75
Expand Down Expand Up @@ -73,7 +73,7 @@ Usage:
>>> mylist.tags.append(TAG_Long(100))
>>> mylist.tags.extend([TAG_Long(120),TAG_Long(320),TAG_Long(19)])
>>> nbtfile.tags.append(mylist)
>>> print nbtfile.pretty_tree()
>>> print(nbtfile.pretty_tree())
TAG_Compound("My Top Level Tag"): 2 Entries
{
TAG_Float("My Float Name"): 3.15298759395
Expand All @@ -86,7 +86,7 @@ Usage:
}
}
>>> nbtfile["TestList"].tags.sort(key = lambda tag: tag.value)
>>> print nbtfile.pretty_tree()
>>> print(nbtfile.pretty_tree())
TAG_Compound("My Top Level Tag"): 2 Entries
{
TAG_Float("My FloatName"): 3.15298759395
Expand Down
6 changes: 3 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Usage:
>>> nbtfile["nested compound test"].tag_info()
TAG_Compound("nested compound test"): 2 Entries
>>> for tag in nbtfile["nested compound test"]["ham"].tags:
... print tag.tag_info()
... print(tag.tag_info())
...
TAG_String("name"): Hampus
TAG_Float("value"): 0.75
Expand Down Expand Up @@ -73,7 +73,7 @@ Usage:
>>> mylist.tags.append(TAG_Long(100))
>>> mylist.tags.extend([TAG_Long(120),TAG_Long(320),TAG_Long(19)])
>>> nbtfile.tags.append(mylist)
>>> print nbtfile.pretty_tree()
>>> print(nbtfile.pretty_tree())
TAG_Compound("My Top Level Tag"): 2 Entries
{
TAG_Float("My Float Name"): 3.15298759395
Expand All @@ -86,7 +86,7 @@ Usage:
}
}
>>> nbtfile["TestList"].tags.sort(key = lambda tag: tag.value)
>>> print nbtfile.pretty_tree()
>>> print(nbtfile.pretty_tree())
TAG_Compound("My Top Level Tag"): 2 Entries
{
TAG_Float("My FloatName"): 3.15298759395
Expand Down
56 changes: 28 additions & 28 deletions examples/block_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ def bounded_stats_per_chunk(chunk, block_data_totals, start, stop):
world_z = z + chunk_z*16
if ( (start != None and world_z < int(start[2])) or (stop != None and world_z > int(stop[2])) ):
# Outside the bounding box; skip to next iteration
#print "Z break:",world_z,start[2],stop[2]
#print("Z break: %d,%d,%d" % (world_z,start[2],stop[2]))
break
for x in range(16):
world_x = x + chunk_x*16
if ( (start != None and world_x < int(start[0])) or (stop != None and world_x > int(stop[0])) ):
# Outside the bounding box; skip to next iteration
#print "X break:",world_x,start[0],stop[0]
#print("X break: %d,%d,%d" % (world_x,start[0],stop[0]))
break
for y in range(128):
if ( (start != None and y < int(start[1])) or (stop != None and y > int(stop[1])) ):
# Outside the bounding box; skip to next iteration
#print "Y break:",y,start[1],stop[1]
#print("Y break: %d,%d,%d" % (y,start[1],stop[1]))
break

#print "Chunk:",c['x'], c['z'],"Coord:",x,y,z
#print("Chunk: %d,%d Coord: %d,%d,%d" % (c['x'], c['z'],x,y,z))
block_id,block_data = chunk.blocks.get_block_and_data(x,y,z)
block_data_totals[block_id][block_data] += 1

Expand All @@ -53,7 +53,7 @@ def process_region_file(filename, start, stop):
rx = int(pieces[1])
rz = int(pieces[2])

block_data_totals = [[0]*16 for i in xrange(256)] # up to 16 data numbers in 256 block IDs
block_data_totals = [[0]*16 for i in range(256)] # up to 16 data numbers in 256 block IDs

# Does the region overlap the bounding box at all?
if (start != None):
Expand All @@ -67,7 +67,7 @@ def process_region_file(filename, start, stop):

# Get all chunks
chunks = file.get_chunks()
print "Parsing",os.path.basename(filename),"...",len(chunks),"chunks"
print("Parsing %s... %d chunks" % (os.path.basename(filename),len(chunks)))
for c in chunks:
# Does the chunk overlap the bounding box at all?
if (start != None):
Expand All @@ -79,7 +79,7 @@ def process_region_file(filename, start, stop):

chunk = Chunk(file.get_chunk(c['x'], c['z']))
assert chunk.get_coords() == (c['x'] + rx*32, c['z'] + rz*32)
#print "Parsing chunk ("+str(c['x'])+", "+str(c['z'])+")"
#print("Parsing chunk ("+str(c['x'])+", "+str(c['z'])+")")
# Parse the blocks

# Fast code if no start or stop coordinates are specified
Expand All @@ -100,43 +100,43 @@ def print_results(block_data_totals):
for block_id,data in enumerate(block_data_totals):
if sum(data) > 0:
datastr = ", ".join([locale.format_string("%d: %d", (i,c), grouping=True) for (i,c) in enumerate(data) if c > 0])
print locale.format_string("block id %3d: %12d (data id %s)", (block_id,sum(data),datastr), grouping=True)
print(locale.format_string("block id %3d: %12d (data id %s)", (block_id,sum(data),datastr), grouping=True))
block_totals = [sum(data_totals) for data_totals in block_data_totals]

total_blocks = sum(block_totals)
solid_blocks = total_blocks - block_totals[0]
solid_ratio = (solid_blocks+0.0)/total_blocks if (total_blocks > 0) else 0
print locale.format("%d", total_blocks, grouping=True),'total blocks in region,',locale.format("%d", solid_blocks, grouping=True),"are solid ({0:0.4%})".format(solid_ratio)
print(locale.format_string("%d total blocks in region, %d are solid (%0.4f", (total_blocks, solid_blocks, 100.0*solid_ratio), grouping=True)+"%)")

# Find valuable blocks
print 'Diamond Ore:', locale.format("%d", block_totals[56], grouping=True)
print 'Gold Ore:', locale.format("%d", block_totals[14], grouping=True)
print 'Redstone Ore:', locale.format("%d", block_totals[73], grouping=True)
print 'Iron Ore:', locale.format("%d", block_totals[15], grouping=True)
print 'Coal Ore:', locale.format("%d", block_totals[16], grouping=True)
print 'Lapis Lazuli Ore:', locale.format("%d", block_totals[21], grouping=True)
print 'Dungeons:', locale.format("%d", block_totals[52], grouping=True)
print(locale.format_string("Diamond Ore: %8d", block_totals[56], grouping=True))
print(locale.format_string("Gold Ore: %8d", block_totals[14], grouping=True))
print(locale.format_string("Redstone Ore: %8d", block_totals[73], grouping=True))
print(locale.format_string("Iron Ore: %8d", block_totals[15], grouping=True))
print(locale.format_string("Coal Ore: %8d", block_totals[16], grouping=True))
print(locale.format_string("Lapis Lazuli Ore: %8d", block_totals[21], grouping=True))
print(locale.format_string("Dungeons: %8d", block_totals[52], grouping=True))

print 'Clay:', locale.format("%d", block_totals[82], grouping=True)
print 'Sugar Cane:', locale.format("%d", block_totals[83], grouping=True)
print 'Cacti:', locale.format("%d", block_totals[81], grouping=True)
print 'Pumpkin:', locale.format("%d", block_totals[86], grouping=True)
print 'Dandelion:', locale.format("%d", block_totals[37], grouping=True)
print 'Rose:', locale.format("%d", block_totals[38], grouping=True)
print 'Brown Mushroom:', locale.format("%d", block_totals[39], grouping=True)
print 'Red Mushroom:', locale.format("%d", block_totals[40], grouping=True)
print 'Lava Springs:', locale.format("%d", block_totals[11], grouping=True)
print(locale.format_string("Clay: %8d", block_totals[82], grouping=True))
print(locale.format_string("Sugar Cane: %8d", block_totals[83], grouping=True))
print(locale.format_string("Cacti: %8d", block_totals[81], grouping=True))
print(locale.format_string("Pumpkin: %8d", block_totals[86], grouping=True))
print(locale.format_string("Dandelion: %8d", block_totals[37], grouping=True))
print(locale.format_string("Rose: %8d", block_totals[38], grouping=True))
print(locale.format_string("Brown Mushroom: %8d", block_totals[39], grouping=True))
print(locale.format_string("Red Mushroom: %8d", block_totals[40], grouping=True))
print(locale.format_string("Lava Springs: %8d", block_totals[11], grouping=True))



def main(world_folder, start=None, stop=None):
if (not os.path.exists(world_folder)):
print "No such folder as "+filename
print("No such folder as "+filename)
return 2 # ENOENT

regions = glob.glob(os.path.join(world_folder,'region','*.mcr'))

block_data_totals = [[0]*16 for i in xrange(256)] # up to 16 data numbers in 256 block IDs
block_data_totals = [[0]*16 for i in range(256)] # up to 16 data numbers in 256 block IDs
try:
for filename in regions:
region_totals = process_region_file(os.path.join(world_folder,'region',filename), start, stop)
Expand All @@ -154,7 +154,7 @@ def main(world_folder, start=None, stop=None):

if __name__ == '__main__':
if (len(sys.argv) == 1):
print "No world folder specified!"
print("No world folder specified!")
sys.exit(22) # EINVAL
world_folder = sys.argv[1]
start,stop = None,None
Expand Down
13 changes: 6 additions & 7 deletions examples/chest_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def items_from_nbt(nbtlist):
def chests_per_chunk(chunk):
"""Given a chunk, increment the block types with the number of blocks found"""
# if (len(chunk['Entities']) > 0) or (len(chunk['TileEntities']) > 0):
# print "Chunk ", chunk["xPos"],chunk["zPos"]
# print("Chunk %d,%d" % (chunk["xPos"],chunk["zPos"]))
entities = []
for entity in chunk['Entities']:
if entity["id"].value == "Minecart" and entity["type"].value == 1:
Expand All @@ -63,7 +63,6 @@ def process_region_file(filename):

# Get all chunks
chunks = file.get_chunks()
print "Parsing",os.path.basename(filename),"...",len(chunks),"chunks"
for cc in chunks:
chunk = file.get_chunk(cc['x'], cc['z'])
leveldata = chunk['Level']
Expand All @@ -76,14 +75,14 @@ def print_results(chests):
locale.setlocale(locale.LC_ALL, 'en_US')
for chest in chests:
itemcount = sum(chest.items.values())
print "%s at %s,%s,%s with %d items:" % \
print("%s at %s,%s,%s with %d items:" % \
(chest.type,\
locale.format("%0.1f",chest.pos.x,grouping=True),\
locale.format("%0.1f",chest.pos.y,grouping=True),\
locale.format("%0.1f",chest.pos.z,grouping=True),\
itemcount)
itemcount))
for blockid,count in chest.items.items():
print " %3dx Item %d" % (count, blockid)
print(" %3dx Item %d" % (count, blockid))


def main(world_folder):
Expand All @@ -102,11 +101,11 @@ def main(world_folder):

if __name__ == '__main__':
if (len(sys.argv) == 1):
print "No world folder specified!"
print("No world folder specified!")
sys.exit(22) # EINVAL
world_folder = sys.argv[1]
if (not os.path.exists(world_folder)):
print "No such folder as "+filename
print("No such folder as "+filename)
sys.exit(2) # ENOENT

sys.exit(main(world_folder))
2 changes: 1 addition & 1 deletion examples/generate_level_dat.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@
player.tags.append(inventory)
level.tags.append(player)

print level.pretty_tree()
print(level.pretty_tree())
#level.write_file("level.dat")
12 changes: 6 additions & 6 deletions examples/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def hsl_slide(hsl1, hsl2, ratio):
avg_l = hsl1['l'] + ratio*(hsl2['l']-hsl1['l'])
avg_h = math.degrees(avg_h)

#print 'tint:',tint, 'base:',final_color, 'avg:',avg_h,avg_s,avg_l
#print('tint: %s base: %s avg: %s %s %s' % (tint,final_color,avg_h,avg_s,avg_l))
return {'h':avg_h, 's':avg_s, 'l':avg_l}


Expand Down Expand Up @@ -193,23 +193,23 @@ def main(world_folder):
chunkmap = get_map(chunk)
x,z = chunk.get_coords()
map.paste(chunkmap, (16*(x-bb.minx),16*(z-bb.minz)))
print " done\n"
print(" done\n")
filename = os.path.basename(world_folder)+".png"
map.save(filename,"PNG")
print "Saved map as %s" % filename
print("Saved map as %s" % filename)
except KeyboardInterrupt:
print " aborted\n"
print(" aborted\n")
map.show()
return 0 # NOERR


if __name__ == '__main__':
if (len(sys.argv) == 1):
print "No world folder specified!"
print("No world folder specified!")
sys.exit(64) # EX_USAGE
world_folder = sys.argv[1]
if (not os.path.exists(world_folder)):
print "No such folder as "+filename
print("No such folder as "+filename)
sys.exit(72) # EX_IOERR

sys.exit(main(world_folder))
10 changes: 5 additions & 5 deletions examples/mob_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def process_region_file(filename):

# Get all chunks
chunks = file.get_chunks()
print "Parsing",os.path.basename(filename),"...",len(chunks),"chunks"
print("Parsing %s... %d chunks" % (os.path.basename(filename),len(chunks)))
entities = []
for cc in chunks:
chunk = file.get_chunk(cc['x'], cc['z'])
Expand All @@ -58,11 +58,11 @@ def process_region_file(filename):
def print_results(entities):
locale.setlocale(locale.LC_ALL, 'en_US')
for entity in entities:
print "%s at %s,%s,%s" % \
print("%s at %s,%s,%s" % \
(entity.type,\
locale.format("%0.1f",entity.pos.x,grouping=True),\
locale.format("%0.1f",entity.pos.y,grouping=True),\
locale.format("%0.1f",entity.pos.z,grouping=True))
locale.format("%0.1f",entity.pos.z,grouping=True)))


def main(world_folder):
Expand All @@ -80,11 +80,11 @@ def main(world_folder):

if __name__ == '__main__':
if (len(sys.argv) == 1):
print "No world folder specified!"
print("No world folder specified!")
sys.exit(22) # EINVAL
world_folder = sys.argv[1]
if (not os.path.exists(world_folder)):
print "No such folder as "+filename
print("No such folder as "+filename)
sys.exit(2) # ENOENT

sys.exit(main(world_folder))
6 changes: 3 additions & 3 deletions examples/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
def main(world_folder):
filename = os.path.join(world_folder,'level.dat')
level = NBTFile(filename)
print level["Data"]["RandomSeed"]
print(level["Data"]["RandomSeed"])
return 0 # NOERR


if __name__ == '__main__':
if (len(sys.argv) == 1):
print "No world folder specified!"
print("No world folder specified!")
sys.exit(22) # EINVAL
world_folder = sys.argv[1]
if (not os.path.exists(world_folder)):
print "No such folder as "+filename
print("No such folder as "+filename)
sys.exit(2) # ENOENT

sys.exit(main(world_folder))
Loading

0 comments on commit 7ce9f43

Please sign in to comment.