pep8; save category_seed within Puzzle instance

This commit is contained in:
J. Patrick Avery, Jr 2017-02-07 13:31:59 -07:00
parent 05505272ac
commit 8716a0cadd
1 changed files with 26 additions and 19 deletions

View File

@ -1,9 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import argparse
import contextlib import contextlib
import glob import glob
import hashlib
import io import io
import importlib.machinery import importlib.machinery
import mistune import mistune
@ -14,12 +12,14 @@ import tempfile
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def djb2hash(buf): def djb2hash(buf):
h = 5381 h = 5381
for c in buf: for c in buf:
h = ((h * 33) + c) & 0xffffffff h = ((h * 33) + c) & 0xffffffff
return h return h
@contextlib.contextmanager @contextlib.contextmanager
def pushd(newdir): def pushd(newdir):
curdir = os.getcwd() curdir = os.getcwd()
@ -40,17 +40,19 @@ def loadmod(name, path):
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'))]
class PuzzleFile: class PuzzleFile:
"""A file associated with a puzzle. """A file associated with a puzzle.
path: The path to the original input file. May be None (when this is created from a file handle path: The path to the original input file. May be None (when this is
and there is no original input. created from a file handle and there is no original input.
handle: A File-like object set to read the file from. You should be able to read straight handle: A File-like object set to read the file from. You should be able to
from it without having to seek to the beginning of the file. read straight from it without having to seek to the beginning of the
file.
name: The name of the output file. name: The name of the output file.
visible: A boolean indicating whether this file should visible to the user. If False, visible: A boolean indicating whether this file should visible to the user.
the file is still expected to be accessible, but it's path must be known If False, the file is still expected to be accessible, but it's path
(or figured out) to retrieve it.""" must be known (or figured out) to retrieve it."""
def __init__(self, stream, name, visible=True): def __init__(self, stream, name, visible=True):
self.stream = stream self.stream = stream
@ -62,8 +64,8 @@ class Puzzle:
def __init__(self, category_seed, points): def __init__(self, category_seed, points):
"""A MOTH Puzzle. """A MOTH Puzzle.
:param category_seed: A byte string to use as a seed for random numbers for this puzzle. :param category_seed: A byte string to use as a seed for random numbers
It is combined with the puzzle points. for this puzzle. It is combined with the puzzle points.
:param points: The point value of the puzzle. :param points: The point value of the puzzle.
""" """
@ -76,7 +78,8 @@ class Puzzle:
self.files = {} self.files = {}
self.body = io.StringIO() self.body = io.StringIO()
self.logs = [] self.logs = []
self.randseed = category_seed * self.points self.category_seed = category_seed
self.randseed = self.category_seed * self.points
self.rand = random.Random(self.randseed) self.rand = random.Random(self.randseed)
def log(self, *vals): def log(self, *vals):
@ -132,14 +135,15 @@ class Puzzle:
def random_hash(self): def random_hash(self):
"""Create a file basename (no extension) with our number generator.""" """Create a file basename (no extension) with our number generator."""
return ''.join(self.rand.choice(string.ascii_lowercase) for i in range(8)) return ''.join(
self.rand.choice(string.ascii_lowercase) for i in range(8))
def make_temp_file(self, name=None, visible=True): def make_temp_file(self, name=None, visible=True):
"""Get a file object for adding dynamically generated data to the puzzle. When you're """Get a file object for adding dynamically generated data to the
done with this file, flush it, but don't close it. puzzle. When you're done with this file, flush it, but don't close it.
:param name: The name of the file for links within the puzzle. If this is None, a name :param name: The name of the file for links within the puzzle. If this
will be generated for you. is None, a name will be generated for you.
:param visible: Whether or not the file will be visible to the user. :param visible: Whether or not the file will be visible to the user.
:return: A file object for writing :return: A file object for writing
""" """
@ -164,7 +168,9 @@ class Puzzle:
return self.rand.choice(ANSWER_WORDS) return self.rand.choice(ANSWER_WORDS)
def make_answer(self, word_count=4, sep=' '): def make_answer(self, word_count=4, sep=' '):
"""Generate and return a new answer. It's automatically added to the puzzle answer list. """Generate and return a new answer. It's automatically added to the
puzzle answer list.
:param int word_count: The number of words to include in the answer. :param int word_count: The number of words to include in the answer.
:param str|bytes sep: The word separator. :param str|bytes sep: The word separator.
:returns: The answer string :returns: The answer string
@ -263,7 +269,8 @@ class Category:
self.catmod = None self.catmod = None
try: try:
self.catmod = loadmod('category', os.path.join(path, 'category.py')) self.catmod = loadmod(
'category', os.path.join(path, 'category.py'))
except FileNotFoundError: except FileNotFoundError:
self.catmod = None self.catmod = None