def tearDownClass(cls): if os.path.isdir(cls.extra): os.rmdir(cls.extra) if os.path.isdir(cls.folder): with capture_output(): setup_dir(cls.folder) os.rmdir(cls.folder)
def test_clean_up_folder(self): with dcs.capture_output(): dcs.setup_dir(self.folder) dcs.write_text_file(self.filename, self.text) with dcs.capture_output() as (out, err): dcs.setup_dir(self.folder) output = out.getvalue().strip() self.assertEqual(output, 'Files/Sub-folders were removed from: "{}"'.format(self.folder))
def test_clean_up_recursively(self): with dcs.capture_output(): dcs.setup_dir(self.subdir) dcs.write_text_file(self.subfile, self.text) with dcs.capture_output() as (out, err): dcs.setup_dir(self.folder, rec=True) output = out.getvalue().strip() self.assertEqual(output, 'Files/Sub-folders were removed from: "{}"\n'.format(self.subdir) + \ 'Files/Sub-folders were removed from: "{}"'.format(self.folder))
def setUpClass(cls): cls.source = os.path.join(get_images_dir(), 'python.png') cls.name1 = 'image1.jpg' cls.name2 = 'image2.jpg' cls.name3 = 'image3.jpg' cls.name4 = 'image4.jpg' cls.name5 = 'image5.jpg' cls.name6 = 'image6.jpeg' cls.folder = os.path.join(get_tests_dir(), 'images') cls.extra = os.path.join(cls.folder, 'extra') cls.size1 = 128 cls.size2 = 96 cls.size3 = 32 cls.size4 = 128 * 2 cls.size5 = 96 * 2 cls.size6 = 4 cls.output = os.path.join(cls.folder, 'resized') with capture_output(): setup_dir(cls.folder) setup_dir(cls.extra) with open(cls.source, 'rb') as file: img = Image.open(file) img.load() img = img.convert( 'RGB' ) # remove transparency (alpha) channel to allow JPG saving new_img = img.resize((cls.size1, cls.size1), Image.ANTIALIAS) new_img.save(os.path.join(cls.folder, cls.name1)) new_img.save(os.path.join(cls.folder, cls.name6)) new_img = img.resize((cls.size2, cls.size1), Image.ANTIALIAS) new_img.save(os.path.join(cls.folder, cls.name2)) new_img = img.resize((cls.size1, cls.size2), Image.ANTIALIAS) new_img.save(os.path.join(cls.folder, cls.name3)) new_img = img.resize((cls.size1, cls.size6), Image.ANTIALIAS) new_img.save(os.path.join(cls.folder, cls.name4)) new_img = img.resize((cls.size6, cls.size1), Image.ANTIALIAS) new_img.save(os.path.join(cls.folder, cls.name5)) img.close() new_img.close()
def test_clean_up_partial(self): with dcs.capture_output(): dcs.setup_dir(self.folder) dcs.write_text_file(self.filename, '') dcs.setup_dir(self.subdir) dcs.write_text_file(self.subfile, '') with dcs.capture_output() as out: dcs.setup_dir(self.folder, rec=False) output = out.getvalue().strip() out.close() self.assertEqual(output, 'Files/Sub-folders were removed from: "{}"'.format(self.folder))
if run_tests: # Run docstring test test_docstrings() if make_soln: # Create and set Opts date = datetime.now() opts = Opts() opts.case_name = 'Brick' opts.save_path = os.path.join(get_root_dir(), 'results', date.strftime('%Y-%m-%d') + '_brick') opts.save_plot = True opts.show_plot = False if make_plots: setup_dir(opts.save_path, rec=True) # Solve for the center color soln[1, 1, 1] = solve_center(pieces) if make_plots: # plot the cube plot_cube(soln, title='Final Solution', opts=opts) # find all possible orientations of all pieces all_piece_combos = [] for this_piece in pieces: all_piece_combos.append(get_all_positions(this_piece)) # Print the total combinations before simplifying to the solution print_combos(all_piece_combos, 'total')
def test_nested_folder(self): with dcs.capture_output() as (out, err): dcs.setup_dir(self.subdir) output = out.getvalue().strip() self.assertEqual(output, 'Created directory: "{}"'.format(self.subdir))
def test_create_folder(self): with dcs.capture_output() as out: dcs.setup_dir(self.folder) output = out.getvalue().strip() out.close() self.assertEqual(output, 'Created directory: "{}"'.format(self.folder))
def test_empty_string(self): with dcs.capture_output() as out: dcs.setup_dir('') output = out.getvalue().strip() out.close() self.assertEqual(output, '')
# bracket lists # ex: Individual Brackets Female Bare Bow.htm file_list_indiv = {} file_list_teams = {} file_list_mixed = {} for div in arch.DIVISIONS: for sex in arch.GENDERS: file_list_indiv[sex + ' ' + div] = os.path.join(output_folder, 'Individual Brackets ' + sex + ' ' + div + '.htm') file_list_teams[sex + ' ' + div] = os.path.join(output_folder, 'Team Brackets ' + sex + ' ' + div + '.htm') if sex == arch.GENDERS[0]: file_list_mixed[div] = os.path.join(output_folder, 'Mixed Team Brackets ' + div + '.htm') #%% Output folder # create the output folder if it doesn't already exist if not os.path.isdir(output_folder): setup_dir(output_folder) #%% Process data # import data from excel data_indiv = arch.import_from_excel(test_file, sheet=arch.SHEET_NAME_INDIV) data_teams = arch.import_from_excel(test_file, sheet=arch.SHEET_NAME_TEAMS) data_mixed = arch.import_from_excel(test_file, sheet=arch.SHEET_NAME_MIXED) # display some information arch.display_info(data_indiv) # write out list of registered archers arch.write_registered_archers(data_indiv, filename=file_registered, show_bales=False) # assign to bales data_indiv = arch.assign_bales(data_indiv)
# make all the pieces pieces = make_all_pieces() # create all the possible permutations of all the pieces all_pieces = make_all_permutations(pieces) # Create and set Opts date = datetime.now() opts = Opts() opts.save_path = os.path.join(get_root_dir(), 'results', date.strftime('%Y-%m-%d') + '_fiver') opts.save_plot = True opts.show_plot = False # Save plots of the possible piece orientations if make_plots: setup_dir(opts.save_path, rec=True) for (ix, these_pieces) in enumerate(all_pieces): for ix2 in range(these_pieces.shape[0]): this_title = 'Piece {}, Permutation {}'.format(ix + 1, ix2 + 1) fig = plot_board(these_pieces[ix2], this_title, opts=opts) plt.close(fig) # print empty boards fig = plot_board(BOARD1[3:-3, 3:-3], 'Empty Board 1', opts=opts) plt.close(fig) fig = plot_board(BOARD2[3:-3, 3:-3], 'Empty Board 2', opts=opts) plt.close(fig) # solve the puzzle locations1 = find_all_valid_locations(BOARD1, all_pieces) locations2 = find_all_valid_locations(BOARD2, all_pieces) if make_soln:
def create_scoresheet(filename, scores, names, plotname='Score Distribution.png'): r""" Creates an HTML file scoresheet of the given information. Parameters ---------- filename : str Filename to read data from scores : list of list of int/str Scores for each round names : list of str List of names for the scoring rounds plotname : str, optional Name of the plot to include in the scoresheet Returns ------- html : str Resulting HTML mark-up code for htm file Notes ----- #. Written by David C. Stauffer in March 2015. Examples -------- >>> from dstauffman2.archery.scoring import create_scoresheet >>> filename = '' >>> scores = [10*['X', 10, 9], 10*[9, 9, 9]] >>> names = ['First Round', 'Second Round'] >>> html = create_scoresheet(filename, scores, names) >>> print(html[:56]) <!DOCTYPE html> <html> <head> <title>Score Sheet</title> """ # determine if making a file if filename == "": make_file = False else: make_file = True # TODO: want to move CSS information into a separate style sheet in the future. htm = """<!DOCTYPE html> <html> <head> <title>Score Sheet</title> <style type="text/css"> table.score_table { font-family: verdana,arial,sans-serif; font-size:11px; border-width: 1px; border-color: #000000; border-collapse: collapse; color: inherit; background-color: #ffffff } table.score_table th { border-width: 1px; padding: 8px; border-style: solid;; background-color: #dedede; } table.score_table td { border-width: 1px; padding: 8px; border-style: solid; text-align: center; } table.score_table td.Y { background-color: #ffff00; } table.score_table td.R { background-color: #ff0000; } table.score_table td.B { background-color: #0099FF; } table.score_table td.K { color: #ffffff; background-color: #000000; } table.score_table td.W { background-color: #ffffff; } table.score_table td.M { color: #ff0000 background-color: #ffffff; } span.red {color: #ff0000;} span.white {color: #ffffff;} </style> </head> <body> """ table1 = """<table class="score_table"> <tr> <th>Archer</th> <th colspan="4">End 1</th> <th colspan="4">End 2</th> <th colspan="4">End 3</th> <th colspan="4">End 4</th> <th colspan="4">End 5</th> <th colspan="4">End 6</th> <th colspan="4">End 7</th> <th colspan="4">End 8</th> <th colspan="4">End 9</th> <th colspan="4">End 10</th> <th>Total (NFAA)</th> <th>Total (USAA)</th> <th>Total (X's)</th> <th>Total (Hits)</th> </tr> """ table2 = """<table class="score_table"> <thead> <tr> <td colspan="13">Arrow Count</td> </tr> <tr> <td></td> <td class="Y">X</td> <td class="Y">10</td> <td class="Y">9</td> <td class="R">8</td> <td class="R">7</td> <td class="B">6</td> <td class="B">5</td> <td class="K"><span class="white">4</span></td> <td class="K"><span class="white">3</span></td> <td class="W">2</td> <td class="W">1</td> <td class="W"><span class="red">M</span></td> </tr> </thead> """ plot1 = """<p><img src=""" + plotname.replace( ' ', '%20' ) + """ alt="Normal Distribution plot" height="597" width="800"> </p> """ for i in range(0, len(scores)): table1 = table1 + ' <tr>\n <td rowspan="2">' + names[i] + '</td>\n' this_data = scores[i] this_data = [ x if isinstance(x, str) else str(int(x)) for x in this_data ] this_nums = [score_text_to_number(x) for x in this_data] this_cumsum = np.cumsum(this_nums) for j in range(0, len(this_data)): this_text = this_data[j] if this_text in {'X', 'x', '10', '9'}: c = 'Y' s = '' elif this_text in {'8', '7'}: c = 'R' s = '' elif this_text in {'6', '5'}: c = 'B' s = '' elif this_text in {'4', '3'}: c = 'K"><span class="white' s = '</span>' elif this_text in {'2', '1'}: c = 'W' s = '' elif this_text in {'M', 'm', '0'}: c = 'M"><span class="red' s = '</span>' else: raise ValueError( 'Unexpected Value for score "{}".'.format(this_text)) table1 = table1 + ' <td class="' + c + '">' + this_text + s + '</td>\n' if j % 3 == 2: table1 = table1 + ' <td>{}</td>\n'.format( np.sum(this_nums[j - 2:j + 1])) num_tens = np.count_nonzero([x == '10' for x in this_data]) table1 = table1 + ' <td rowspan="2">{}</td>\n'.format(this_cumsum[-1]) table1 = table1 + ' <td rowspan="2">{}</td>\n'.format( this_cumsum[-1] - num_tens) table1 = table1 + ' <td rowspan="2">{}</td>\n'.format( np.count_nonzero([x.lower() == 'x' for x in this_data])) table1 = table1 + ' <td rowspan="2">{}</td>\n'.format(num_tens) table1 = table1 + ' </tr>\n <tr>\n' for j in range(0, len(this_data)): if j % 3 == 2: table1 = table1 + ' <td colspan="4">' + '{}'.format( this_cumsum[j]) + '</td>\n' table1 = table1 + ' </tr>\n' table2 = table2 + '<tr>' table2 = table2 + '<td>' + names[i] + '</td>\n' table2 = table2 + '<td class="Y">{}</td>\n'.format( np.count_nonzero([x.lower() == 'x' for x in this_data])) table2 = table2 + '<td class="Y">{}</td>\n'.format( np.count_nonzero([x == '10' for x in this_data])) table2 = table2 + '<td class="Y">{}</td>\n'.format( np.count_nonzero([x == '9' for x in this_data])) table2 = table2 + '<td class="R">{}</td>\n'.format( np.count_nonzero([x == '8' for x in this_data])) table2 = table2 + '<td class="R">{}</td>\n'.format( np.count_nonzero([x == '7' for x in this_data])) table2 = table2 + '<td class="B">{}</td>\n'.format( np.count_nonzero([x == '6' for x in this_data])) table2 = table2 + '<td class="B">{}</td>\n'.format( np.count_nonzero([x == '5' for x in this_data])) table2 = table2 + '<td class="K"><span class="white">{}</span></td>\n'.format( np.count_nonzero([x == '4' for x in this_data])) table2 = table2 + '<td class="K"><span class="white">{}</span></td>\n'.format( np.count_nonzero([x == '3' for x in this_data])) table2 = table2 + '<td class="W">{}</td>\n'.format( np.count_nonzero([x == '2' for x in this_data])) table2 = table2 + '<td class="W">{}</td>\n'.format( np.count_nonzero([x == '1' for x in this_data])) table2 = table2 + '<td class="W"><span class="red">{}</span></td>\n'.format( np.count_nonzero([x.lower() == 'm' or x == '0' for x in this_data])) table2 = table2 + '</tr>\n' table1 = table1 + '</table>\n' table2 = table2 + '</table>\n' htm = htm + table1 + '<br /><br />\n' + table2 + '<br /><br />' + plot1 + '</body>\n\n</html>\n' # write text to file if make_file: folder = os.path.split(filename)[0] if not os.path.isdir(folder): setup_dir(folder) with open(filename, 'w') as file: file.write(htm) return htm