def test_block_sequential_literals(self): sequential_types = (List, Vector) item_specs = ((int, IntegerAtom, (1, 2, 3)), (lambda s: s.strip('"'), StringAtom, ('"Garnet"', '"Amethyst"', '"Pearl"'))) for sequential_type in sequential_types: for item_purifier, item_class, item_sequence in item_specs: with self.subTest(sequential_type=sequential_type, item_sequence=item_sequence): source = """ {}…{}— {} {} {} """.format(sequential_type.open_delimiter_character, sequential_type.close_delimiter_character, *item_sequence) parsed = list(parse(lex(source))) annotated = list(annotate(parsed)) try: sequential, = annotated except: from pudb import set_trace as debug debug() self.assertEqual(sequential_type, sequential.__class__) self.assertEqual( [item_class(item_purifier(i)) for i in item_sequence], sequential.elements)
def test_block_sequential_literals(self): sequential_types = (List, Vector) item_specs = ((int, IntegerAtom, (1, 2, 3)), (lambda s: s.strip('"'), StringAtom, ('"Garnet"', '"Amethyst"', '"Pearl"'))) for sequential_type in sequential_types: for item_purifier, item_class, item_sequence in item_specs: with self.subTest(sequential_type=sequential_type, item_sequence=item_sequence): source = """ {}…{}— {} {} {} """.format(sequential_type.open_delimiter_character, sequential_type.close_delimiter_character, *item_sequence) parsed = list(parse(lex(source))) annotated = list(annotate(parsed)) try: sequential, = annotated except: from pudb import set_trace as debug; debug() self.assertEqual(sequential_type, sequential.__class__) self.assertEqual( [item_class(item_purifier(i)) for i in item_sequence], sequential.elements )
def eval_pset(spec, grades): spec = json.loads(PSET_1_SPEC) base_dir = spec["base_dir"] pset_files = spec["files"] for student in os.listdir(base_dir): user_dir = os.path.join(base_dir, student) for pset_file in pset_files: pset_file_loc = os.path.join(user_dir, pset_file) try: code = Code(filename=pset_file_loc) annotations = annotate(code) num_errors = len(annotations) try: grade = grades[student] try: STATS[int(grade)].append(num_errors) except ValueError: # grade is "NULL" or not a number pass except KeyError: # student doesn't have this grade pass except IOError: #missing assignment file pass
def test_dictionary_literal_annotated_with_definition(self): source = """ := dee {"rah" 1; "hey" 2;}""" annotated = list(annotate(parse(lex(source)))) def_dee, = annotated dictionary_literal_node = def_dee.identified self.assertEqual(IdentifierAtom("dee"), dictionary_literal_node.identifier)
def render(filename): code = Code(filename) annotations = annotate(code) template = Template(filename="templates/annotated.txt") buf = StringIO() ctx = Context(buf, lines=code.lines, annotations=annotations) template.render_context(ctx) return buf.getvalue()
def execute(self, input_data, input_directory, output_directory): if not input_data['isvideo']: return {} # Open output video stream if this is first frame. if input_data['frame'] == 0: # The output directory structure should match input directory structure. relpath_of_input_file = os.path.relpath(input_data['file'], input_directory) relparent_of_input_file = os.path.dirname(relpath_of_input_file) inp_filename, inp_extension = os.path.splitext( os.path.basename(relpath_of_input_file)) output_filedir = os.path.join(output_directory, relparent_of_input_file) if not os.path.exists(output_filedir): os.makedirs(output_filedir) self.output_filepath = os.path.join( output_filedir, inp_filename + '-annotated.' + self.cfg['params']['format']) self.output_video = imageio.get_writer(self.output_filepath, 'ffmpeg') img = input_data['img'].copy() for comp in self.cfg['inputs']: comp_outputs = input_data.get(comp) comp_reports = comp_outputs['reports'] if not comp_reports: print( "Warning: pipeline file specifies {} as input for {} but {} is not outputting any location reports" .format(comp, self.name, comp)) continue annotate(img, comp_reports) final_img = cv2.resize(img, (self.cfg['params']['size']['width'], self.cfg['params']['size']['height'])) self.output_video.append_data(final_img) return {'file': self.output_filepath}
def execute(self, input_data, input_directory, output_directory): if not input_data['isphoto']: return {} img = input_data['img'].copy() for comp in self.cfg['inputs']: comp_outputs = input_data.get(comp) comp_reports = comp_outputs['reports'] if not comp_reports: print( "Warning: pipeline file specifies {} as input for {} but {} is not outputting any location reports" .format(comp, self.name, comp)) continue annotate(img, comp_reports) # The output directory structure should match input directory structure. relpath_of_input_file = os.path.relpath(input_data['file'], input_directory) relparent_of_input_file = os.path.dirname(relpath_of_input_file) inp_filename, inp_extension = os.path.splitext( os.path.basename(relpath_of_input_file)) output_filedir = os.path.join(output_directory, relparent_of_input_file) if not os.path.exists(output_filedir): os.makedirs(output_filedir) output_filepath = os.path.join( output_filedir, inp_filename + '-annotated.' + self.cfg['params']['format']) if self.cfg['params'].get('size'): final_img = cv2.resize(img, (self.cfg['params']['size']['width'], self.cfg['params']['size']['height'])) else: final_img = img print(output_filepath) imageio.imwrite(output_filepath, final_img) return {'file': output_filepath}
def test_definition_sets_subsequent_global_environment(self): source = """ := a [1 2 3] for |i a|— (println a) """ annotated = list(annotate(parse(lex(source)))) def_a, for_i_in_a = annotated self.assertIsNone(def_a.global_environment.get('a')) self.assertEqual( List([IntegerAtom(1), IntegerAtom(2), IntegerAtom(3)]), for_i_in_a.global_environment['a'])
def process(): if "code" in request.form and request.form["code"]: code_input = request.form["code"] code = Code(text=code_input) annotations = annotate(code) template = Template(filename="templates/annotated.txt", default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf8') return template.render(lines=code.lines, annotations=annotations) else: return "make sure you pasted code"
def test_definition_sets_subsequent_global_environment(self): source = """ := a [1 2 3] for |i a|— (println a) """ annotated = list(annotate(parse(lex(source)))) def_a, for_i_in_a = annotated self.assertIsNone(def_a.global_environment.get('a')) self.assertEqual( List([IntegerAtom(1), IntegerAtom(2), IntegerAtom(3)]), for_i_in_a.global_environment['a'] )
def test_named_function_definition(self): source = """ :=λ first_plus_square_of_second |a ^int b ^int| → ^int (+ a (⋅ b b)) := we_assert "a and b were in the fn body's env., but not here" """ defn_first_plus, def_we_assert = annotate(parse(lex(source))) # The local environment has our arguments, self.assertEqual( Argument(IdentifierAtom('a'), TypeSpecifierAtom("^int")), defn_first_plus.expressions[0].local_environment['a']) # and they don't leak. self.assertIsNone(def_we_assert.local_environment.get('a')) # And we can see the defined function from the environment of # the subsequent expression. self.assertEqual( type(def_we_assert.environment['first_plus_square_of_second']), NamedFunctionDefinition)
def test_named_function_definition(self): source = """ :=λ first_plus_square_of_second |a ^int b ^int| → ^int (+ a (⋅ b b)) := we_assert "a and b were in the fn body's env., but not here" """ defn_first_plus, def_we_assert = annotate(parse(lex(source))) # The local environment has our arguments, self.assertEqual( Argument(IdentifierAtom('a'), TypeSpecifierAtom("^int")), defn_first_plus.expressions[0].local_environment['a'] ) # and they don't leak. self.assertIsNone(def_we_assert.local_environment.get('a')) # And we can see the defined function from the environment of # the subsequent expression. self.assertEqual( type(def_we_assert.environment['first_plus_square_of_second']), NamedFunctionDefinition )