Last active
December 16, 2015 04:19
-
-
Save Zren/5376385 to your computer and use it in GitHub Desktop.
Code Jam Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code Jam Utils | |
| # Can be found on Github's Gist at: | |
| # https://gist.github.com/Zren/5376385 | |
| from itertools import repeat | |
| import logging | |
| import json | |
| import functools | |
| import os | |
| ### ------------------------------------------------------------- | |
| LOGGING_FORMAT = '[%(asctime)s] %(message)s' | |
| from logging import INFO, DEBUG | |
| ### ------------------------------------------------------------- | |
| ### Util: Logging | |
| def log(a, b=None, level=DEBUG): | |
| if b is None: | |
| msg = a | |
| else: | |
| msg = '{0}: \t{1}'.format(a, b) | |
| logging.log(level, msg) | |
| def info(a, b=None): | |
| log(a, b, INFO) | |
| def logdict(d, level=DEBUG): | |
| logging.log(level, '\n' + json.dumps(d, indent=4)) | |
| ### ------------------------------------------------------------- | |
| ### Util: Common recipies | |
| # http://wiki.python.org/moin/PythonDecoratorLibrary#Alternate_memoize_as_nested_functions | |
| # Does NOT memoize kwargs | |
| def memoize(obj): | |
| cache = obj.cache = {} | |
| @functools.wraps(obj) | |
| def memoizer(*args, **kwargs): | |
| if args not in cache: | |
| cache[args] = obj(*args, **kwargs) | |
| return cache[args] | |
| return memoizer | |
| ### ------------------------------------------------------------- | |
| ### Types: | |
| class Problem(object): | |
| def __init__(self, filename, directory='data', log_level=INFO): | |
| self.num = 0 # Called as case.num | |
| self.filename = filename | |
| self.directory = directory | |
| self.log_level = log_level | |
| ### ------------------------------------------------------------- | |
| @property | |
| def input_filename(self): | |
| return os.path.join(self.directory, self.filename + '.in') | |
| @property | |
| def output_filename(self): | |
| return os.path.join(self.directory, self.filename + '.out') | |
| ### ------------------------------------------------------------- | |
| ### IO: Basics | |
| def writeline(self, line): | |
| log("Write", line) | |
| self.fout.write(line + '\n') | |
| def readline(self): | |
| line = self.fin.readline().rstrip('\n') | |
| log("Read", line) | |
| return line | |
| ### ------------------------------------------------------------- | |
| ### IO: Input parsing | |
| def readint(self): | |
| return int(self.readline()) | |
| def readints(self): | |
| return map(int, self.readline().split()) | |
| def readfloat(self): | |
| return float(self.readline()) | |
| def readfloats(self): | |
| return map(float, self.readline().split()) | |
| ### ------------------------------------------------------------- | |
| ### IO: Common operations | |
| def read_num_cases(self): | |
| self.num_cases = self.readint() | |
| def writecase(self, s): | |
| self.writeline("Case #{0}: {1}".format(self.num, s)) | |
| self.fout.flush() | |
| ### ------------------------------------------------------------- | |
| ### Magic Methods | |
| def __enter__(self): | |
| logging.basicConfig(level=self.log_level, format=LOGGING_FORMAT) | |
| info("=== Opening Streams ===") | |
| log("In", self.input_filename) | |
| log("Out", self.output_filename) | |
| self.fin = open(self.input_filename, 'r') | |
| self.fout = open(self.output_filename, 'w') | |
| self.read_num_cases() | |
| info("# of Cases", self.num_cases) | |
| return self | |
| def __exit__(self, type, value, traceback): | |
| self.fin.close() | |
| self.fout.close() | |
| def __iter__(self): | |
| self.num = 0 | |
| for i in range(self.num_cases): | |
| self.num += 1 | |
| yield self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code Jam Utils | |
| # Can be found on Github's Gist at: | |
| # https://gist.github.com/Zren/5376385 | |
| from codejamutils import * | |
| ### ------------------------------------------------------------- | |
| problem_id = 'A' | |
| opt_args = { | |
| 'log_level': DEBUG, # INFO / DEBUG | |
| # 'filename': '', | |
| 'filename': problem_id + '-sample', | |
| # 'filename': problem_id + '-small', | |
| # 'filename': problem_id + '-large', | |
| # 'filename': problem_id + '-small-attempt0', | |
| # 'filename': problem_id + '-large-attempt0', | |
| } | |
| ### ------------------------------------------------------------- | |
| ### Problem: | |
| ### ------------------------------------------------------------- | |
| with Problem(**opt_args) as p: | |
| for case in p: | |
| info('Case', case.num) | |
| case.writecase('') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment