Working Map class and test suite
I revised the Ocarina 4.0 map class so it will hopefully have better performance. I also began working on a new testing system so testing specific changes should be easier than with Ocarina 4.0
This commit is contained in:
parent
08d0b05467
commit
4d71ae824d
|
@ -0,0 +1,9 @@
|
||||||
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
|
__all__ = ["data", "map", "path"]
|
||||||
|
|
||||||
|
|
||||||
|
import map
|
||||||
|
|
||||||
|
# Runtime variables are not saved
|
||||||
|
vars = map.Map()
|
|
@ -0,0 +1,34 @@
|
||||||
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
|
from libsaria import path
|
||||||
|
import cPickle as pickle
|
||||||
|
|
||||||
|
PROTO = pickle.HIGHEST_PROTOCOL
|
||||||
|
|
||||||
|
def save(item, file, ext="pickle"):
|
||||||
|
file = "%s%s" % (path.join(path.sariadir(),file), ext)
|
||||||
|
savefile(item, file)
|
||||||
|
|
||||||
|
|
||||||
|
def savefile(item, file):
|
||||||
|
f = open(file, 'w')
|
||||||
|
p = pickle.Pickler(f, PROTO)
|
||||||
|
p.dump(item)
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
def load(file, ext="pickle"):
|
||||||
|
file = "%s%s" % (path.join(path.sariadir(),file),ext)
|
||||||
|
return loadfile(file)
|
||||||
|
|
||||||
|
|
||||||
|
def loadfile(file):
|
||||||
|
if path.exists(file) == False:
|
||||||
|
return
|
||||||
|
f = open(file)
|
||||||
|
p = pickle.Unpickler(f)
|
||||||
|
item = p.load()
|
||||||
|
f.close()
|
||||||
|
return item
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
|
data = None
|
||||||
|
|
||||||
|
class Map(dict):
|
||||||
|
savefile = None
|
||||||
|
def __init__(self, file=None):
|
||||||
|
dict.__init__(self)
|
||||||
|
self.savefile = file
|
||||||
|
self.reload()
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
dict.__setitem__(self, key, value)
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return dict.get(self, key, None)
|
||||||
|
|
||||||
|
def has(self,key):
|
||||||
|
return key in self
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
if self.savefile == None:
|
||||||
|
return
|
||||||
|
global data
|
||||||
|
if data == None:
|
||||||
|
import data
|
||||||
|
data.save(self, self.savefile)
|
||||||
|
|
||||||
|
def reload(self):
|
||||||
|
if self.savefile == None:
|
||||||
|
return
|
||||||
|
global data
|
||||||
|
if data == None:
|
||||||
|
import data
|
||||||
|
item = data.load(self.savefile)
|
||||||
|
if item == None:
|
||||||
|
return
|
||||||
|
for key in item:
|
||||||
|
dict.__setitem__(self, key, item[key])
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
exists = os.path.exists
|
||||||
|
expand = os.path.expanduser
|
||||||
|
join = os.path.join
|
||||||
|
mkdir = os.mkdir
|
||||||
|
rm = os.remove
|
||||||
|
|
||||||
|
dir
|
||||||
|
def sariadir():
|
||||||
|
global dir
|
||||||
|
dir = join(expand("~"), ".saria")
|
||||||
|
if exists(dir) == False:
|
||||||
|
mkdir(dir)
|
||||||
|
sariadir = get_sariadir()
|
||||||
|
return get_sariadir()
|
||||||
|
|
||||||
|
|
||||||
|
def get_sariadir():
|
||||||
|
global dir
|
||||||
|
return dir
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import datetime
|
||||||
|
now = datetime.datetime.now
|
||||||
|
|
||||||
|
print "Loading libsaria"
|
||||||
|
|
||||||
|
before = now()
|
||||||
|
import libsaria
|
||||||
|
after = now()
|
||||||
|
|
||||||
|
print "libsaria loaded (", after-before, ")"
|
||||||
|
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
print
|
||||||
|
print "Available tests:"
|
||||||
|
for file in os.listdir("tests"):
|
||||||
|
split = file.rsplit(".")
|
||||||
|
if split[1] == "pyc":
|
||||||
|
continue
|
||||||
|
if split[0] == "__init__":
|
||||||
|
continue
|
||||||
|
print " ", file
|
||||||
|
|
||||||
|
|
||||||
|
for file in sys.argv[1:]:
|
||||||
|
print
|
||||||
|
print "=====", file, "====="
|
||||||
|
print
|
||||||
|
begin = now()
|
||||||
|
__import__("tests."+file.rsplit(".")[0])
|
||||||
|
end = now()
|
||||||
|
print
|
||||||
|
print "=====", file, "(", after-before,") ====="
|
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
from libsaria import path
|
||||||
|
from libsaria.map import Map
|
||||||
|
key = "key"
|
||||||
|
|
||||||
|
# __init__
|
||||||
|
test = Map()
|
||||||
|
print "Testing basic functions (no persistance):"
|
||||||
|
# has
|
||||||
|
print " Has key <False>?", test.has(key)
|
||||||
|
# __getitem__
|
||||||
|
print " test[key] <None>?", test[key]
|
||||||
|
|
||||||
|
# __setitem__
|
||||||
|
print " Setting test[key] = True"
|
||||||
|
test[key] = True
|
||||||
|
|
||||||
|
# has
|
||||||
|
print " Has key <True>?", test.has(key)
|
||||||
|
# __getitem__
|
||||||
|
print " test[key] <True>?", test['key']
|
||||||
|
|
||||||
|
|
||||||
|
print
|
||||||
|
print "Testing persistance:"
|
||||||
|
file = path.join(path.join(path.sariadir(),".saria"),"tests.map.pickle")
|
||||||
|
try:
|
||||||
|
path.rm(file)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
del test
|
||||||
|
|
||||||
|
test = Map("tests.map")
|
||||||
|
test["a"] = 0
|
||||||
|
test["b"] = 0
|
||||||
|
print " print test"
|
||||||
|
print test
|
||||||
|
print " del test"
|
||||||
|
del test
|
||||||
|
print " test = Map(\"tests.map\")"
|
||||||
|
test = Map("tests.map")
|
||||||
|
print " print test"
|
||||||
|
print test
|
||||||
|
try:
|
||||||
|
path.rm(file)
|
||||||
|
except:
|
||||||
|
pass
|
Loading…
Reference in New Issue