forked from Full-House-UW/section
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.py
executable file
·123 lines (96 loc) · 3.4 KB
/
sort.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/local/bin/python
"""
This script serves as the entry point for a simple sorting program
"""
import argparse
import sys
from algorithms import sort_fns
from validation import url_validation_fns
from validation.canonicalizer import canonicalize
def main():
parser = argparse.ArgumentParser(description='Simple sort program')
parser.add_argument('-i', metavar='in-file', required=True,
type=argparse.FileType('r'),
help='input file of strings to sort, one per line')
parser.add_argument('-o', metavar='out-file', required=True,
type=argparse.FileType('w'),
help='output file for sorted strings, one per line')
# Create URL validation options
# Get available functions to use for url validation choices
# from the list of functions defined in __init__.py
#
url_validation_choices = tuple(url_validation_fns.keys())
# Set a default URL validation function
#
if 'all' in url_validation_fns:
default_url_validation_choice = 'all'
else:
default_url_validation_choice = url_validation_choices[0]
# Add command line argument options to parser
parser.add_argument('--validate-fn', dest='validate_fn',
choices=url_validation_choices,
default=default_url_validation_choice,
help='enable url validation type.')
# Get available sort functions to use for argument choices
choices = tuple(sort_fns.keys())
if 'selectionsort' in sort_fns:
default = 'selectionsort'
else:
default = choices[0]
parser.add_argument('--sort-fn', dest='sort_fn', choices=choices,
default=default,
help='sort function to use')
# Read parsed arguments
results = parser.parse_args()
infile = results.i
outfile = results.o
sort_fn = results.sort_fn
url_validate_fn = results.validate_fn
# read lines
try:
lines = infile.readlines()
except IOError, e:
parser.error(str(e))
finally:
infile.close()
infile = None
# argument parser guarantees a valid choice for url_validate_fn
lines = _validate(lines, url_validation_fns[url_validate_fn])
# argument parser guarantees a valid choice for sort_fn
lines = _sort(lines, sort_fns[sort_fn])
# write output
try:
for line in lines:
outfile.write(line)
except IOError, e:
parser.error(str(e))
finally:
outfile.close()
outfile = None
sys.exit(0)
def _validate(url_list, url_validation_fn):
"""Returns a validated copy of URLs list"""
validated_url = list(url_list)
validated_url = url_validation_fn(validated_url)
## See url_validation.py for adding implementations for
## various validation functions
return validated_url
def _sort(str_list, sort_fn):
"""Return a sorted copy of str_list, using functionsort_fn"""
copy = list(str_list)
sort_fn(copy)
return copy
# Define equals to be equality of the canonical form.
# Only defined for valid URLs.
def _equals(url1, url2):
return canonicalize(url1) == canonicalize(url2)
# Define less than to be less than of the canonical form.
# Only defined for valid URLs.
def _less(url1, url2):
return canonicalize(url1) < canonicalize(url2)
# Define greater than to be greater than of the canonical form.
# Only defined for valid URLs.
def _greater(url1, url2):
return canonicalize(url1) > canonicalize(url2)
if __name__ == '__main__':
main()