Permalink
Browse files

Minor cleanups as inspired by pylint.

Lots of little issues reported by pylint are fixed here.

BuildMaps.py: docstrings, tabbing, and argv
BuildRegion.py: argv
GetRegion.py: argv
PrepRegion.py: argv
bathy.py: docstring
clidt.py: unused variables, FIXME, docstrings, comma spacing
crust.py: unused variables
memoize.py: tabs and spaces
ore.py: comma spacing, unused variables
region.py: a little of everything
  • Loading branch information...
1 parent 9e72357 commit f3a4fafd738dd52a39ce300d57916c1434093c06 @mathuin committed Mar 6, 2012
Showing with 53 additions and 57 deletions.
  1. +5 −3 BuildMaps.py
  2. +2 −2 BuildRegion.py
  3. +2 −2 GetRegion.py
  4. +2 −2 PrepRegion.py
  5. +1 −0 bathy.py
  6. +8 −7 clidt.py
  7. +1 −1 crust.py
  8. +14 −15 memoize.py
  9. +5 −7 ore.py
  10. +13 −18 region.py
View
@@ -10,6 +10,7 @@
import argparse
def center(name):
+ """Returns the center chunk values for a given region."""
worlddir = os.path.join('Worlds', name, 'level.dat')
world = mclevel.fromFile(worlddir)
bounds = world.bounds
@@ -19,7 +20,8 @@ def center(name):
world = None
return centerx/16, centerz/16
-def main(argv):
+def main():
+ """The main routine."""
parser = argparse.ArgumentParser(description='Builds c10t maps for regions.')
parser.add_argument('--name', required=True, type=str, help='name of region to be mapped')
parser.add_argument('--gmaps', action='store_true', help='generate Google Maps')
@@ -30,14 +32,14 @@ def main(argv):
(centerx, centerz) = center(args.name)
if args.gmaps:
- cleanmkdir(os.path.join('Maps', args.name))
+ cleanmkdir(os.path.join('Maps', args.name))
command = 'C10T=../c10t/build/c10t ../c10t/scripts/google-api/google-api.sh -w Worlds/%s -o Maps/%s -O "-M 2048 -z --center %d,%d"' % (args.name, args.name, centerx, centerz)
else:
command = '../c10t/build/c10t -M 2048 -z -w Worlds/%s -o Maps/%s.png --center %d,%d' % (args.name, args.name, centerx, centerz)
os.system(command)
if __name__ == '__main__':
- sys.exit(main(sys.argv))
+ sys.exit(main())
View
@@ -26,7 +26,7 @@ def buildtile(args):
myTile = Tile(myRegion, tilex, tiley)
myTile()
-def main(argv):
+def main():
"""Builds a region."""
# example:
# ./BuildRegion.py --name BlockIsland
@@ -119,5 +119,5 @@ def main(argv):
setspawnandsave(world, peak)
if __name__ == '__main__':
- sys.exit(main(sys.argv))
+ sys.exit(main())
View
@@ -24,7 +24,7 @@ def checkLandcoverIDs(string):
else:
raise argparse.ArgumentTypeError, 'landcover IDs invalid: %s' % string
-def main(argv):
+def main():
"""Creates a specified region and downloads files from USGS."""
# example:
# ./GetRegion.py --name BlockIsland --ymax 41.2378 --ymin 41.1415 --xmin -71.6202 --xmax -71.5332
@@ -65,5 +65,5 @@ def main(argv):
myRegion.getfiles()
if __name__ == '__main__':
- sys.exit(main(sys.argv))
+ sys.exit(main())
View
@@ -7,7 +7,7 @@
import os
import yaml
-def main(argv):
+def main():
"""Rebuilds maps on broken regions."""
parser = argparse.ArgumentParser(description='Prepares downloaded regions for building.')
parser.add_argument('--name', required=True, type=str, help='name of region')
@@ -23,4 +23,4 @@ def main(argv):
myRegion.buildmap(args.doOCL)
if __name__ == '__main__':
- sys.exit(main(sys.argv))
+ sys.exit(main())
View
@@ -3,6 +3,7 @@
from osgeo import gdal
def getBathy(deptharray, maxdepth, geotrans, projection):
+ """Generate simulated bathymetry data based on distance from shore."""
# save deptharray as a dataset named depthds
(depthz, depthx) = deptharray.shape
drv = gdal.GetDriverByName('MEM')
View
@@ -58,9 +58,8 @@ def __init__(self, coords, values, base, wantCL=True, split=None, nnear=None, ma
self.coords = np.asarray(coords, dtype=np.int32)
self.values = np.asarray(values, dtype=np.int32)
self.base = np.asarray(base, dtype=np.int32)
- (lencoords, null) = self.coords.shape
- (lenvalues,) = self.values.shape
- (lenbase, null) = self.base.shape
+ lencoords = self.coords.shape[0]
+ lenvalues = self.values.shape[0]
assert lencoords == lenvalues, "lencoords does not equal lenvalues"
self.wantCL = wantCL
@@ -77,6 +76,7 @@ def __init__(self, coords, values, base, wantCL=True, split=None, nnear=None, ma
self.coordindices = self.genindices(self.coords)
self.baseindices = self.genindices(self.base)
self.canCL = True
+ # FIXME: specify an exception type
except:
print "warning: unable to use pyopencl, defaulting to Invdisttree"
self.canCL = False
@@ -89,8 +89,9 @@ def __init__(self, coords, values, base, wantCL=True, split=None, nnear=None, ma
self.usemajority = np.int32(1 if majority else 0)
def build(self, coords, values, base):
- (lenbase, null) = base.shape
- (lencoords, null) = coords.shape
+ """Use OpenCL to build the arrays."""
+ lenbase = base.shape[0]
+ lencoords = coords.shape[0]
coords_array = cla.to_device(self.queue, coords)
values_array = cla.to_device(self.queue, values)
base_array = cla.to_device(self.queue, base)
@@ -113,13 +114,13 @@ def build(self, coords, values, base):
def __call__(self):
# build output array
if self.wantCL and self.canCL:
- (lenbase, null) = self.base.shape
+ lenbase = self.base.shape[0]
retval = np.zeros((lenbase), dtype=np.int32)
for key, value in self.baseindices.items():
(a, b) = key
cindices = []
# currently grabs nine bins for each processed bin
- pairs = [(c, d) for c, d in product(xrange(a-1,a+2), xrange(b-1,b+2)) if (c,d) in self.coordindices.keys()]
+ pairs = [(c, d) for c, d in product(xrange(a-1, a+2), xrange(b-1, b+2)) if (c, d) in self.coordindices.keys()]
for pair in pairs:
cindices += self.coordindices[pair]
coords = self.coords[cindices]
View
@@ -17,7 +17,7 @@ def __init__(self, xsize, zsize, coverage=0.05, wantCL=True):
self.wantCL = wantCL
self.numcoords = int(self.xsize * self.zsize * self.coverage)
self.shape = (self.zsize, self.xsize)
- self.coords = [(randint(0, self.zsize-1), randint(0, self.xsize-1)) for elem in xrange(self.numcoords)]
+ self.coords = [(randint(0, self.zsize-1), randint(0, self.xsize-1)) for dummy in xrange(self.numcoords)]
self.values = [uniform(Crust.minwidth, Crust.maxwidth) for elem in xrange(self.numcoords)]
self.base = [(z, x) for z, x in product(xrange(self.zsize), xrange(self.xsize))]
self.clidt = CLIDT(self.coords, self.values, self.base, wantCL=self.wantCL, majority=False)
View
@@ -1,17 +1,16 @@
# http://askawizard.blogspot.com/2008/09/decorators-python-saga-part-2_28.html
-class memoize(object):
- def __init__(self, cache = None):
- self.cache = cache
- def __call__(self, function):
- return Memoized(function, self.cache)
-
-class Memoized(object):
- def __init__(self, function, cache = None):
- if cache is None: cache = {}
- self.function = function
- self.cache = cache
- def __call__(self, *args):
- if args not in self.cache:
- self.cache[args] = self.function(*args)
- return self.cache[args]
+class memoize:
+ def __init__(self, cache = None):
+ self.cache = cache
+ def __call__(self, function):
+ return Memoized(function, self.cache)
+class Memoized:
+ def __init__(self, function, cache = None):
+ if cache is None: cache = {}
+ self.function = function
+ self.cache = cache
+ def __call__(self, *args):
+ if args not in self.cache:
+ self.cache[args] = self.function(*args)
+ return self.cache[args]
View
12 ore.py
@@ -27,9 +27,9 @@ def __call__(self, coords):
# generate ellipsoid values based on parameters
(mcx, mcy, mcz) = coords
# start with random radius-like values
- x0 = randint(1,4)
- y0 = randint(1,4)
- z0 = randint(1,4)
+ x0 = randint(1, 4)
+ y0 = randint(1, 4)
+ z0 = randint(1, 4)
v0 = 4/3 * pi * x0 * y0 * z0
# scale to match volume and round up
scale = cbrt(self.size / v0)
@@ -60,10 +60,10 @@ def placeoreintile(tile):
for ore in oreobjs:
extent = cbrt(oreobjs[ore].size)*2
- maxy = pow(2,oreobjs[ore].depth)
+ maxy = pow(2, oreobjs[ore].depth)
numrounds = int(oreobjs[ore].rounds * (tile.size/16) * (tile.size/16))
oreID = materialNamed(oreobjs[ore].name)
- for oreround in xrange(numrounds):
+ for dummy in xrange(numrounds):
orex = randint(0, tile.size)
orey = randint(0, maxy)
orez = randint(0, tile.size)
@@ -97,5 +97,3 @@ def placeoreinregion(ores, oreobjs, world):
Ore('Diamond Ore', 4, 1, 7),
Ore('Redstone Ore', 4, 8, 7),
Ore('Lapis Lazuli Ore', 4, 3, 7) ]
-
-oreDQ = set([ore.name for ore in oreObjs] + ['Air', 'Water', 'Water (active)', 'Lava', 'Lava (active)', 'Bedrock'])
View
@@ -27,6 +27,9 @@
from clidt import CLIDT
class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
+ def __init__(self):
+ pass
+
"""stupid redirect handling craziness"""
def http_error_302(self, req, fp, code, msg, headers):
result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
@@ -88,7 +91,7 @@ class Region:
compressionTypes = { 'tgz': ['T'],
'zip': ['Z'] }
- def __init__(self, name, xmax, xmin, ymax, ymin, tilesize=None, scale=None, vscale=None, trim=None, sealevel=None, maxdepth=None, lcIDs=None, elIDs=None, debug=False, doOre=True, doSchematics=False):
+ def __init__(self, name, xmax, xmin, ymax, ymin, tilesize=None, scale=None, vscale=None, trim=None, sealevel=None, maxdepth=None, lcIDs=None, elIDs=None, doOre=True, doSchematics=False):
"""Create a region based on lat-longs and other parameters."""
# NB: smart people check names
self.name = name
@@ -233,10 +236,6 @@ def __init__(self, name, xmax, xmin, ymax, ymin, tilesize=None, scale=None, vsca
self.lclayer = self.checkavail(landcoverIDs, 'landcover')
self.ellayer = self.checkavail(elevationIDs, 'elevation')
- # write yaml file
- self.writeyaml()
-
- def writeyaml(self):
# write the values to the file
stream = file(os.path.join(self.regiondir, 'Region.yaml'), 'w')
yaml.dump(self, stream)
@@ -307,7 +306,7 @@ def checkavail(self, productlist, maptype):
for elem in rAatts.ArrayOfCustomAttributes:
for each in elem[0]:
if (each[0] == 'PRODUCTKEY' and each[1] in productlist):
- offered.append(each[1])
+ offered.append(each[1])
# this should extract the first
try:
productID = [ ID for ID in productlist if ID in offered ][0]
@@ -372,8 +371,6 @@ def requestvalidation(self, layerIDs):
def downloadfile(self, layerID, downloadURL):
"""Actually download the file at the URL."""
- # FIXME: extract try/expect around urlopen
- # FIXME: consider breaking apart further
(pType, iType, mType, cType) = self.decodeLayerID(layerID)
layerdir = os.path.join(self.mapsdir, layerID)
cleanmkdir(layerdir)
@@ -414,7 +411,7 @@ def downloadfile(self, layerID, downloadURL):
# parse out status code and status text
startPos = result.find("<ns:return>") + 11
endPos = result.find("</ns:return>")
- (code, status) = result[startPos:endPos].split(',',1)
+ (code, status) = result[startPos:endPos].split(',', 1)
print " status is %s" % status
if (int(code) == 400):
break
@@ -436,7 +433,7 @@ def downloadfile(self, layerID, downloadURL):
raise IOError
else:
print " downloading %s now!" % filename
- downloadFile = open(os.path.join(layerdir,filename), 'wb')
+ downloadFile = open(os.path.join(layerdir, filename), 'wb')
while True:
data = obj.read(8192)
if data == "":
@@ -496,10 +493,10 @@ def buildvrts(self):
cFile.extract(compimage, layerdir)
elif (cType == "zip"):
omfgcompimage = "\\".join([compbase, "%s.%s" % (compbase, iType)])
- os.mkdir(os.path.dirname(os.path.join(datasubdir,compimage)))
+ os.mkdir(os.path.dirname(os.path.join(datasubdir, compimage)))
cFile = zipfile.ZipFile(fullfile)
cFile.extract(omfgcompimage, datasubdir)
- os.rename(os.path.join(datasubdir,omfgcompimage),os.path.join(layerdir,compimage))
+ os.rename(os.path.join(datasubdir, omfgcompimage), os.path.join(layerdir, compimage))
cFile.close()
vrtfile = '%s.vrt' % layerID
buildvrtcmd = 'gdalbuildvrt %s */*.%s >/dev/null' % (vrtfile, iType)
@@ -629,8 +626,8 @@ def buildmap(self, wantCL=True):
# depth array is entire landcover region, landcover array is subset
deptharray = lcband.ReadAsArray(0, 0, lcds.RasterXSize, lcds.RasterYSize)
lcarray = deptharray[self.maxdepth:-1*self.maxdepth, self.maxdepth:-1*self.maxdepth]
- geotrans = [ lcextents['xmin'], self.scale, 0, lcextents['ymax'], 0, -1 * self.scale ]
- projection = srs.ExportToWkt()
+ geotrans = [ lcextents['xmin'], self.scale, 0, lcextents['ymax'], 0, -1 * self.scale ]
+ projection = srs.ExportToWkt()
bathyarray = getBathy(deptharray, self.maxdepth, geotrans, projection)
mapds.GetRasterBand(Region.rasters['bathy']).WriteArray(bathyarray)
# perform terrain translation
@@ -640,10 +637,8 @@ def buildmap(self, wantCL=True):
trans = Terrain.translate[lcpid]
for key in trans:
lcarray[lcarray == key] = 1000+trans[key]
- lcarray[lcarray>1000] -= 1000
- examples = numpy.unique(lcarray)
- examples = examples.flatten()
- for value in examples:
+ lcarray[lcarray > 1000] -= 1000
+ for value in numpy.unique(lcarray).flat:
if value not in Terrain.terdict:
print "bad value: ", value
mapds.GetRasterBand(Region.rasters['landcover']).WriteArray(lcarray)

0 comments on commit f3a4faf

Please sign in to comment.