Incorporate pat suggestions

This commit is contained in:
Neale Pickett 2017-02-02 11:34:57 -07:00
parent 5d06b24afa
commit dfb6f54d78
2 changed files with 23 additions and 16 deletions

View File

@ -151,7 +151,7 @@ you are a fool.
# List all point values in a category # List all point values in a category
title = "Puzzles in category `{}`".format(parts[2]) title = "Puzzles in category `{}`".format(parts[2])
body.write("<ul>") body.write("<ul>")
for points in cat.pointvals: for points in cat.pointvals():
body.write('<li><a href="/puzzles/{cat}/{points}/">puzzles/{cat}/{points}/</a></li>'.format(cat=parts[2], points=points)) body.write('<li><a href="/puzzles/{cat}/{points}/">puzzles/{cat}/{points}/</a></li>'.format(cat=parts[2], points=points))
body.write("</ul>") body.write("</ul>")
elif len(parts) == 4: elif len(parts) == 4:

View File

@ -29,6 +29,13 @@ def pushd(newdir):
finally: finally:
os.chdir(curdir) os.chdir(curdir)
def loadmod(name, path):
abspath = os.path.abspath(path)
loader = importlib.machinery.SourceFileLoader(name, abspath)
return loader.load_module()
# Get a big list of clean words for our answer file. # Get a big list of clean words for our answer file.
ANSWER_WORDS = [w.strip() for w in open(os.path.join(os.path.dirname(__file__), ANSWER_WORDS = [w.strip() for w in open(os.path.join(os.path.dirname(__file__),
'answer_words.txt'))] 'answer_words.txt'))]
@ -112,9 +119,7 @@ class Puzzle:
def read_directory(self, path): def read_directory(self, path):
try: try:
fn = os.path.abspath(os.path.join(path, "puzzle.py")) puzzle_mod = loadmod("puzzle", os.path.join(path, "puzzle.py"))
loader = importlib.machinery.SourceFileLoader('puzzle_mod', fn)
puzzle_mod = loader.load_module()
except FileNotFoundError: except FileNotFoundError:
puzzle_mod = None puzzle_mod = None
@ -255,22 +260,24 @@ class Category:
def __init__(self, path, seed): def __init__(self, path, seed):
self.path = path self.path = path
self.seed = seed self.seed = seed
self.pointvals = []
self.catmod = None self.catmod = None
if os.path.exists(os.path.join(path, 'category.py')): try:
with pushd(path): self.catmod = loadmod('category', os.path.join(path, 'category.py'))
fn = os.path.abspath('category.py') except FileNotFoundError:
loader = importlib.machinery.SourceFileLoader('category', fn) self.catmod = None
self.catmod = loader.load_module()
self.pointvals.extend(self.catmod.pointvals()) def pointvals(self):
if self.catmod:
with pushd(self.path):
pointvals = self.catmod.pointvals()
else: else:
for fpath in glob.glob(os.path.join(path, "[0-9]*")): pointvals = []
for fpath in glob.glob(os.path.join(self.path, "[0-9]*")):
pn = os.path.basename(fpath) pn = os.path.basename(fpath)
points = int(pn) points = int(pn)
self.pointvals.append(points) pointvals.append(points)
return sorted(pointvals)
self.pointvals.sort()
def puzzle(self, points): def puzzle(self, points):
puzzle = Puzzle(self.seed, points) puzzle = Puzzle(self.seed, points)
@ -283,5 +290,5 @@ class Category:
return puzzle return puzzle
def __iter__(self): def __iter__(self):
for points in self.pointvals: for points in self.pointvals():
yield self.puzzle(points) yield self.puzzle(points)