Beispiel #1
0
def write_registered_archers(data, filename='', show_bales=True):
    r"""Writes the list of registered archers out to an html file."""
    # check that a non-empty filename was specified
    if len(filename) == 0:
        raise ValueError('A filename must be specified.')
    # get the title from the filename
    (_, title) = os.path.split(filename)
    # get the html prequel text
    html = generic_html_start()
    # replace the generic "Title" with this filename
    html = html.replace(r'<title>Title</title>',
                        r'<title>' + title.split('.')[0] + r'</title>')
    # determine which columns to write out
    cols = [COL_LASTNAME, COL_FIRSTNAME, COL_GENDER, COL_SCHOOL, COL_DIVISION]
    if show_bales:
        cols.append(COL_BALE)
    # write the dataframe to an html text equivalent
    html_table = data.to_html(columns=cols,
                              index_names=False,
                              bold_rows=False,
                              na_rep='')
    # combine all the html together
    html = html + html_table + generic_html_end()
    # write out to the specified file
    write_text_file(filename, html)
Beispiel #2
0
 def setUp(self):
     self.fold1 = dcs.get_tests_dir()
     self.file1 = os.path.join(self.fold1, 'temp_file.pyc')
     self.fold2 = os.path.join(self.fold1, 'temp_sub_dir')
     self.file2 = os.path.join(self.fold2, 'temp_file2.pyc')
     dcs.write_text_file(self.file1, 'Text.')
     os.makedirs(self.fold2)
     dcs.write_text_file(self.file2, 'More text.')
Beispiel #3
0
 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))
Beispiel #4
0
 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))
Beispiel #5
0
 def test_bad_writing(self):
     if platform.system() != 'Windows':
         return
     with dcs.capture_output() as out:
         try:
             dcs.write_text_file(self.badpath, self.contents)
         except:
             self.assertTrue(sys.exc_info()[0] in [OSError, IOError, FileNotFoundError])
     output = out.getvalue().strip()
     out.close()
     self.assertEqual(output, r'Unable to open file "AA:\non_existent_path\bad_file.txt" for writing.')
Beispiel #6
0
 def setUp(self):
     self.folder = get_tests_dir()
     self.file_old1 = os.path.join(self.folder, 'temp image A.jpg')
     self.file_old2 = os.path.join(self.folder, 'temp xtra image B.jpg')
     self.file_new1 = os.path.join(self.folder, 'Photo 01.jpg')
     self.file_new2 = os.path.join(self.folder, 'Photo 02.jpg')
     self.prefix = 'Photo '
     self.start = 1
     self.digits = 2
     write_text_file(self.file_old1, '')
     write_text_file(self.file_old2, '')
Beispiel #7
0
 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))
Beispiel #8
0
 def setUpClass(cls):
     cls.folder = get_tests_dir()
     cls.folder_exclusions = [os.path.join(cls.folder, 'temp_dir'), \
         os.path.join(cls.folder, 'coverage_html_report')]
     file1 = os.path.join(cls.folder, 'temp image 01.jpg')
     file2 = os.path.join(cls.folder, 'temp image 02.jpg')
     file3 = os.path.join(cls.folder, 'temp image 04.jpg')
     file4 = os.path.join(cls.folder, 'temp image 006.jpg')
     file5 = os.path.join(cls.folder, 'temp something else 1.jpg')
     file6 = os.path.join(cls.folder, 'Picasa.ini')
     file7 = os.path.join(cls.folder, 'temp image 10 10.jpg')
     if not os.path.isdir(cls.folder_exclusions[0]):
         os.mkdir(cls.folder_exclusions[0])
     file8 = os.path.join(cls.folder_exclusions[0], 'temp image 01.jpg')
     file9 = os.path.join(cls.folder_exclusions[0],
                          'temp longimagename.jpg')
     cls.files = [
         file1, file2, file3, file4, file5, file6, file7, file8, file9
     ]
     for this_file in cls.files:
         write_text_file(this_file, '')
Beispiel #9
0
def write_indiv_results(data, filename=''):
    r"""Writes the individual archer results out to an html file."""
    # check that a non-empty filename was specified
    if len(filename) == 0:
        raise ValueError('A filename must be specified.')
    # get the title from the filename
    (_, title) = os.path.split(filename)
    # get the html prequel text
    html = generic_html_start()
    # replace the generic "Title" with this filename
    html = html.replace(r'<title>Title</title>',
                        r'<title>' + title.split('.')[0] + r'</title>')
    # determine which columns to write out
    cols = [COL_LASTNAME, COL_FIRSTNAME, COL_GENDER, COL_SCHOOL, COL_DIVISION, COL_BALE, \
            COL_SCORE1, COL_X_COUNT1, COL_SCORE2, COL_X_COUNT2, COL_SCORE_TOT, COL_X_COUNT_TOT, COL_SEED]
    # write the dataframe to an html text equivalent
    html_table = data.to_html(columns=cols,
                              index_names=False,
                              bold_rows=False,
                              na_rep='')
    # combine all the html together
    html = html + html_table + generic_html_end()
    # write out to the specified file
    write_text_file(filename, html)
Beispiel #10
0
 def setUpClass(cls):
     cls.folder = dcs.get_tests_dir()
     file1 = os.path.join(cls.folder, 'temp_code_01.py')
     file2 = os.path.join(cls.folder, 'temp_code_02.py')
     file3 = os.path.join(cls.folder, 'temp_code_03.m')
     cont1 = 'Line 1\n\nAnother line\n    Line with leading spaces\n'
     cont2 = '\n\n    Start line\nNo Bad tab lines\n    Start and end line    \nAnother line\n\n'
     cont3 = '\n\n    Start line\n\tBad tab line\n    Start and end line    \nAnother line\n\n'
     cls.files = [file1, file2, file3]
     dcs.write_text_file(file1, cont1)
     dcs.write_text_file(file2, cont2)
     dcs.write_text_file(file3, cont3)
     cls.bad1 = "    Line 004: '\\tBad tab line\\n'"
     cls.bad2 = "    Line 005: '    Start and end line    \\n'"
Beispiel #11
0
 def setUp(self):
     self.folder   = dcs.get_tests_dir()
     self.old_name = 'test1'
     self.new_name = 'test2'
     self.print_status = True
     self.old_dir    = os.path.join(self.folder, self.old_name)
     self.new_dir    = os.path.join(self.folder, self.new_name)
     self.git_dir    = os.path.join(self.old_dir, '.git')
     self.files      = ['__init__.py', '__init__.pyc', '__init__.misc']
     # make some files
     if not os.path.isdir(self.old_dir):
         os.mkdir(self.old_dir)
     if not os.path.isdir(self.git_dir):
         os.mkdir(self.git_dir)
     dcs.write_text_file(os.path.join(self.old_dir, '__init__.py'),'# Init file for "temp1".\n')
     dcs.write_text_file(os.path.join(self.old_dir, '__init__.pyc'),'')
     dcs.write_text_file(os.path.join(self.old_dir, '__init__.misc'),'# Misc file for "temp1".\n')
Beispiel #12
0
 def test_bad_writing(self):
     with dcs.capture_output() as (out, err):
         with self.assertRaises(OSError):
             dcs.write_text_file(self.badpath, self.contents)
     output = out.getvalue().strip()
     self.assertEqual(output, r'Unable to open file "AA:\non_existent_path\bad_file.txt" for writing.')
Beispiel #13
0
 def test_writing(self):
     dcs.write_text_file(self.filepath, self.contents)
     with open(self.filepath, 'rt') as file:
         text = file.read()
     self.assertEqual(text, self.contents)
Beispiel #14
0
 def setUp(self):
     self.folder = get_tests_dir()
     self.file_old = os.path.join(self.folder, 'temp image 01.JPG')
     self.file_new = os.path.join(self.folder, 'temp image 01.jpg')
     write_text_file(self.file_old, '')
Beispiel #15
0
 def setUp(self):
     self.folder = get_tests_dir()
     self.file_old = os.path.join(self.folder, 'Picasa.ini')
     self.file_new = os.path.join(self.folder, '.picasa.ini')
     write_text_file(self.file_old, '')
Beispiel #16
0
def write_brackets(data, filename='', team='indiv', display=True):
    r"""Writes out the brackets based on scores from earlier competition."""
    # initializ the bracket list
    brackets = []
    # loop through all the divisions
    for div in DIVISIONS:
        # loop through genders
        for sex in GENDERS:
            # get the index for this specific gender/division combination
            ix = data[(data[COL_GENDER].str.startswith(sex[0]))
                      & (data[COL_DIVISION] == div)].index
            # find out how many exist, and skip if this has zero length
            num = len(ix)
            if num > 0:
                # calculate the number of waves needed for this number of people
                waves = int(math.ceil(math.log(num, 2)))
                # throw an error if this is above 5 (1/16 round)
                if waves > MAX_WAVES:
                    raise ValueError(
                        'Code currently assumes a 1/{} round as the highest round'
                        .format(2**(MAX_WAVES - 1)))
                # get the initial seed order for wave 1
                seeds = build_seed_order(waves)
                # determine winners for the rest of the matches that have been completed
                all_seeds = determine_seed_winners(data, seeds, waves)
                # preallocate the text info for the bracket output file
                all_texts = [None] * len(all_seeds)
                # Build the pairings for all the waves
                for (this_wave, this_seed) in enumerate(all_seeds[:-1]):
                    # get a reverse index for the text for the html file
                    ix_rev = waves - this_wave
                    # initialize this text
                    all_texts[ix_rev] = []
                    # display some info
                    if display:
                        print('Wave {0}: {1} {2}'.format(
                            this_wave + 1, sex, div))
                    # loop through pairs
                    for this_pair in this_seed:
                        # get the names of the pairing
                        name1 = get_name_by_index(
                            data, this_pair[0] -
                            1) if this_pair[0] - 1 < num else 'Bye'
                        name2 = get_name_by_index(
                            data, this_pair[1] -
                            1) if this_pair[1] - 1 < num else 'Bye'
                        # get the combined text and save for later
                        text1 = ' #{0} {1}'.format(this_pair[0], name1)
                        text2 = ' #{0} {1}'.format(this_pair[1], name2)
                        all_texts[ix_rev].append(text1)
                        all_texts[ix_rev].append(text2)
                        # display info
                        if display:
                            print(text1 + ' vs. ' + text2)
                # handle the final results
                if display:
                    print('Final Results for {} {}:'.format(sex, div))
                text = []
                places = ['1st', '2nd', '3rd', '4th']
                for [ix, this_seed] in enumerate(all_seeds[-1]):
                    text.append(' {} Place: {}'.format(
                        places[ix],
                        get_name_by_index(data, this_seed -
                                          1) if this_seed - 1 < num else ''))
                all_texts[0] = text
                if display:
                    print('\n'.join(text) + '\n')
                # write to html file
                title = 'Individual Brackets ' + sex + ' ' + div
                html = build_bracket_html_template(waves)
                html = replace_bracket_tokens(html, title, all_texts)
                brackets.append(html)
                if filename[sex + ' ' + div]:
                    # write to file
                    write_text_file(filename[sex + ' ' + div], html)
    return brackets