-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_files_up_to_size.py
75 lines (62 loc) · 3.1 KB
/
find_files_up_to_size.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import sys
sys.path.append('..')
sys.path.append('../..')
import argparse
import utils
from student_utils_sp18 import *
"""
======================================================================
No need to change any code below this line
======================================================================
"""
def analyze_from_file(input_file, input_filename, output_directory, params=[]):
# print('Processing', input_file)
basename, filename = os.path.split(input_file)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
input_data = utils.read_file(input_file)
number_of_kingdoms, list_of_kingdom_names, starting_kingdom, adjacency_matrix = data_parser(input_data)
lower_bound = 0
upper_bound = 752
if len(params) == 1:
upper_bound = int(float(params[0]))
if number_of_kingdoms <= n:
output_filename = "inputs_size_{}".format(upper_bound)
output_file = f'{output_directory}/{output_filename}'
utils.write_to_file(output_file, input_filename + "\n", append=True)
elif len(params) == 2:
lower_bound = int(float(params[0]))
upper_bound = int(float(params[1]))
if number_of_kingdoms <= upper_bound and number_of_kingdoms >= lower_bound:
output_filename = "inputs_size_{}_to_{}".format(lower_bound, upper_bound)
output_file = f'{output_directory}/{output_filename}'
utils.write_to_file(output_file, input_filename + "\n", append=True)
elif len(params) == 3:
# python3 find_files_up_to_size.py inputs files_by_id 726 752 count_by_id
# params[2] signifies to count by filename
lower_bound = int(float(params[0]))
upper_bound = int(float(params[1]))
if int(float(filename[:-3])) <= upper_bound and int(float(filename[:-3])) >= lower_bound:
output_filename = "inputs_size_{}_to_{}".format(lower_bound, upper_bound)
output_file = f'{output_directory}/{output_filename}'
utils.write_to_file(output_file, input_filename + "\n", append=True)
def analyze_all(input_directory, output_directory, params=[]):
# input_files = utils.get_files_with_extension(input_directory, 'in')
files = []
filenames = []
for name in os.listdir(input_directory):
if name.endswith("in"):
files.append(f'{input_directory}/{name}')
filenames.append(name)
for i in range(len(files)):
analyze_from_file(files[i], filenames[i], output_directory, params=params)
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Parsing arguments')
parser.add_argument('input', type=str, help='The path to the input file directory')
parser.add_argument('output_directory', type=str, nargs='?', default='.', help='The path to the directory where the output should be written')
parser.add_argument('params', nargs=argparse.REMAINDER, help='Extra arguments passed in')
args = parser.parse_args()
output_directory = args.output_directory
input_directory = args.input
analyze_all(input_directory, output_directory, params=args.params)