def test_to_number_grid(): assert Parser.from_lines(["123", "456"]).to_number_grid() == \ Grid.from_values([[1, 2, 3], [4, 5, 6]]) assert Parser.from_lines(["1 2 3", "4 5 6"]).to_number_grid(separator=None) == \ Grid.from_values([[1, 2, 3], [4, 5, 6]]) assert Parser.from_lines(["1, 2, 3", "4, 5, 6"]).to_number_grid(separator=",") == \ Grid.from_values([[1, 2, 3], [4, 5, 6]])
def test_to_regex_match(): assert Parser.from_lines([ "from here to there", "from anywhere to somewhere" ]).to_regex_match(f"from {WORD} to {WORD}") == [["here", "there"], ["anywhere", "somewhere"]] assert Parser.from_lines(["from here to 99", "from anywhere to 42"]).to_regex_match( f"from {WORD} to {NUMBER}", [str, int]) == [["here", 99], ["anywhere", 42]] assert Parser.from_lines(["from 1 to 99", "from 0 to 42" ]).to_regex_match(f"from {NUMBER} to {NUMBER}", int) == [[1, 99], [0, 42]] assert Parser.from_lines([ "alfanum 4lf4n1m", "alfanum 0a1b2c" ]).to_regex_match(f"alfanum {ALFANUM}") == [["4lf4n1m"], ["0a1b2c"]] with pytest.raises(ValueError): Parser.from_lines(["abc"]).to_regex_match(WORD, "no_type") with pytest.raises(ValueError): Parser.from_lines(["some line"]).to_regex_match("not that line") with pytest.raises(ValueError): Parser.from_lines(["abc"]).to_regex_match(NUMBER)
def change_skin(self, obj, object_name, mtl_directory, new_skin): """ Find line number(s) to be changed and change them. :param obj: Object whose skin will be changed :param mtl_directory: Directory of mtl file to be edited :param object_name: object whose skin will be randomized :param new_skin: New skin of object :return: None """ # Read change_skin from config file change_skin_dict = Parser().parse_long_term_configuration( pathlib.Path(configuration))['change_skin'] number = self.get_key(object_name, change_skin_dict) if number == 0: p = Parser().parse_long_term_configuration( pathlib.Path(configuration))['jazz'] chance = np.random.rand() if 1 - p < chance: rgba = self.random_color(chance) b.Blender().color_object(obj, rgba) # Change skins at specified line numbers elif number is not None: for i in range(int(number)): self.change_mtl(mtl_directory, new_skin, 12 + 11 * i)
class ParserTestCase(unittest.TestCase): def setUp(self): self.parser = Parser() def test_parse_args(self): parsed_args = self.parser.parse_args(['-m', 'HDPE', '-p', '100', '-c', '1', '-i', '1', '-b', 'background', '-o', 'images']) self.assertEqual(parsed_args.materials, ['HDPE']) def test_exception_1(self): self.assertRaises(OSError, lambda: self.parser.parse_args(['-m', 'HDPE', 'PET', '-p', '100', '-c', '1', '-i', '1', '-b', 'background', '-o', 'images'])) def test_exception_2(self): self.assertRaises(OSError, lambda: self.parser.parse_args(['-m', 'HDPE', '-p', '80', '-c', '1', '-i', '1', '-b', 'background', '-o', 'images'])) def test_yaml(self): data = self.parser.parse_long_term_configuration(pathlib.Path( src_dir + r"/test/util/test.yaml")) self.assertEqual(data, {'key1': {'key1.1': 'stringvalue'}, 'key2': {'key2.1': 1, 'key2.2': [1, 2, 3]}})
def test_to_number_lists(): assert Parser.from_lines(["1 2", "3 4"]).to_number_lists() == [[1, 2], [3, 4]] assert Parser.from_lines(["1, 2", "3, 4" ]).to_number_lists(separator=",") == [[1, 2], [3, 4]]
def test_from_file(): input_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_parser_input") assert Parser.from_file(input_file).to_lines() == ["123", "456"]
def main(args): # noqa: CFQ001 """ Main method of our code. Sets the paths of background and object. :param args: arguments used for parsing to values. :return: time_data: a dictionary with timestamps of the different stages of the pipeline """ total_start_time = time.time() parser = Parser() # Load the config file for camera and lights configuration = parser.parse_long_term_configuration(pathlib.Path( src_dir + r"/configuration.yaml")) # Parse arguments provided as input by user args = parser.parse_args(args) # Load objects if args.reuse_crushes: material_dirs = list(map(lambda x: list(pathlib.Path(src_dir + r'/Crushed Models/' + x).glob('**/*.obj')), args.materials)) else: material_dirs = list(map(lambda x: list(pathlib.Path(src_dir + r'/Models/' + x).glob( '**/*.obj')), args.materials)) # List of the models models = [list(map(lambda path: Object(str( path), 'random', 'random', 'random', None), material)) for material in material_dirs] # Calculate number of objects per material based on proportions number_of_objects = list(map(lambda x: int(round( args.objects_per_image * (x / 100))), args.proportions)) objects = make_object_selection(args, number_of_objects, models) if not args.only_crush: # Setup scene scene = Scene(configuration['camera']['location'], configuration['camera']['rotation'], configuration['light']['location'], configuration['light']['energy'], configuration['light']['type']) scene.add_background(args.background, None) # Render images image_object_bboxes = render(args, configuration['render'], scene, objects) # Write info file write_file(configuration['info_json'], (configuration['render']['res_width'], configuration['render']['res_height']), 'info', args.image_count, image_object_bboxes) time_data['total'] = time.time() - total_start_time # Print time data print('Total Time: ' + str(time_data['total'])) print('Object Creation Time: ' + str(time_data['object_creation_time'])) if not args.only_crush: print('Object Setup Time: ' + str(time_data['object_setup_time'])) for i in range(args.image_count): print('Image ' + str(i) + ' Time: ' + str(time_data['image_' + str(i)])) # To be displayed on the server page return time_data
def test_to_character_grid(): assert Parser.from_lines(["..#", "#.#"]).to_character_grid() == Grid.from_values( [[".", ".", "#"], ["#", ".", "#"]])
def test_to_string_graph(): number_graph = Parser.from_lines( ["Haarlem to Utrecht", "Utrecht to Driebergen"]).to_string_graph("to") assert number_graph.get_nodes() == ["Haarlem", "Utrecht", "Driebergen"] assert number_graph.get_edges() == [(("Haarlem", "Utrecht"), 1), (("Utrecht", "Driebergen"), 1)]
def test_to_coordinate_list(): assert Parser.from_lines(["1 2", "3 4"]).to_coordinate_list() == \ [Coordinate.from_point(1, 2), Coordinate.from_point(3, 4)] assert Parser.from_lines(["1, 2", "3, 4"]).to_coordinate_list(separator=",") == \ [Coordinate.from_point(1, 2), Coordinate.from_point(3, 4)]
def test_to_bitstring_list(): assert [ bit_string.to_int() for bit_string in Parser.from_lines( ["10101", "101"]).to_bitstring_list() ] == [21, 5]
def setUp(self): self.parser = Parser()
def test_to_number_list_from_multi_line(): assert Parser.from_lines( ["1", "2", "3", "4"]).to_number_list_from_multi_line() == [1, 2, 3, 4]
def test_to_word_lists(): assert Parser.from_lines(["word1 word2", "word3 word4"]).to_word_lists() == \ [["word1", "word2"], ["word3", "word4"]] assert Parser.from_lines(["word1, word2", "word3, word4"]).to_word_lists(separator=",") == \ [["word1", "word2"], ["word3", "word4"]]
def read_skins_from_library(self): """ Read skins from the config file. :return: Dict with skins """ return Parser().parse_long_term_configuration(pathlib.Path(configuration))['skins']
def test_to_sections(): assert Parser.from_string("123\n\n456").to_sections() == ["123", "456"]
def test_to_lines(): assert Parser.from_string("123\n456").to_lines() == ["123", "456"]
def test_to_string(): assert Parser.from_string("123\n456").to_string() == "123\n456"
def test_from_lines(): assert Parser.from_lines(["123", "456"]).to_lines() == ["123", "456"]
def test_to_number_graph(): number_graph = Parser.from_lines(["12 -> 34", "34 -> 56", "56 -> 78"]).to_number_graph("->") assert number_graph.get_nodes() == [12, 34, 56, 78] assert number_graph.get_edges() == [((12, 34), 1), ((34, 56), 1), ((56, 78), 1)]
import zipfile import os src_dir = "/workdir" sys.path.insert(1, src_dir) sys.path.append(os.getcwd()) from src.server.generate import check_generate # noqa: E402 from src.util.parser import Parser # noqa: E402 app = Flask(__name__, template_folder='../../templates', static_folder='../../static') app.secret_key = "don't tell anyone!!!" configuration = Parser().parse_long_term_configuration( Path("configuration.yaml"))['flask'] @app.route("/") def index(): """ Renders the index page. """ return render_template('index.html') @app.route('/download_images') def download(): """ Downloads the contents of the output folder :return: sends a zip file to the user.
def test_to_number_list_from_single_line(): assert Parser.from_lines( ["1 2 3 4"]).to_number_list_from_single_line() == [1, 2, 3, 4] assert Parser.from_lines([ "1, 2, 3, 4" ]).to_number_list_from_single_line(separator=",") == [1, 2, 3, 4]