diff --git a/Problem.py b/Problem.py index 827d3e0..49e7269 100644 --- a/Problem.py +++ b/Problem.py @@ -63,6 +63,20 @@ def %s: """ +## python class parameters ## +PYTHON_CLASS_TEMPLATE = """#!/usr/bin/python + +class %s(object): + \"\"\"docstring for %s\"\"\" + def __init__(self, arg=None): + super(%s, self).__init__() + self.arg = arg + + def %s: + pass + +""" + ## test icons ## CHECK_MARK = 'Y' # u'\u2713' CROSS_MARK = 'N' # u'\u2717' @@ -115,6 +129,22 @@ def _generate_mini_signature(self): signature += ")" return signature + def _generate_mini_instancemethod_signature(self): + """Returns the instance method signature for the problem, in the form + (self, , , ...) + e.g. MyFunc(self, A, B) + """ + signature = "%s(self, " % self['definition']['method'] + signature += ", ".join([str(x) for x in self['definition']['names']['input']]) + signature += ")" + return signature + + def _generate_mini_class_name(self): + """Returns the class name for the problem + """ + classname = "%s" % self['definition']['class'] + return classname + def _generate_filled_signature(self, inputs = None, output = None): """Returns the method signature for the problem with the given inputs and output, in the form @@ -268,13 +298,19 @@ def test_method(self, method): def to_python(self, template = PYTHON_TEMPLATE): """Returns a Python file, with the method header, according to the specified python template.""" - return PYTHON_TEMPLATE % self._generate_mini_signature() - - def to_python_file(self, filename, template = PYTHON_TEMPLATE): + if template == PYTHON_TEMPLATE: + return template % self._generate_mini_signature() + else: + classname = self._generate_mini_class_name() + method_signature = self._generate_mini_instancemethod_signature() + return template % (classname, classname, classname, method_signature) + + def to_python_file(self, filename, template_type = "method"): """Saves Python text to a file, with the method header, according to the specified python template.""" + template_map = {"method":PYTHON_TEMPLATE, "class":PYTHON_CLASS_TEMPLATE} python_file = open(filename, 'w') - python_file.write(self.to_python()) + python_file.write(self.to_python(template_map[template_type])) python_file.close() # json output # diff --git a/ProblemFolder.py b/ProblemFolder.py index d2ea2ad..96fe52b 100644 --- a/ProblemFolder.py +++ b/ProblemFolder.py @@ -113,7 +113,7 @@ def add_problem(self, problem, force = False): # check if python file exists: if it doesn't, create it if force or not os.access(python_path, os.F_OK): - problem.to_python_file(python_path) + problem.to_python_file(python_path, "class") # create init file, if it doesn't exist if force or not os.access(init_path, os.F_OK): @@ -136,7 +136,7 @@ def del_problem(self, problem): return len(problems_to_delete) - def test_problems(self, problems): + def test_problems(self, problems, template_type="method"): """Given a list of problem tuples (rel_path, number, name), runs the tests for each problem. Uses the method provided in the Python file in the problem directory.""" @@ -154,7 +154,11 @@ def test_problems(self, problems): python_file.close() # get method - method = ns[problem[P_PROBLEM_DEFINITION]['method']] + if template_type == "class": + cls = ns[problem[P_PROBLEM_DEFINITION]['class']] + method = getattr(cls(), problem[P_PROBLEM_DEFINITION]['method']) + elif template_type == "method": + method = ns[problem[P_PROBLEM_DEFINITION]['method']] # run tests problem.test_method(method) diff --git a/scraper.py b/scraper.py index 8fd1bb6..f9c5b83 100644 --- a/scraper.py +++ b/scraper.py @@ -77,7 +77,7 @@ def get_topcoder_problem_ids(opener, n, end = None): if args.test: # test specified problems for n in problem_input: - folder.test_problems(folder.find_problem(number = n)) + folder.test_problems(folder.find_problem(number = n), template_type="class") else: # connect to TopCoder