-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuf.py
More file actions
78 lines (59 loc) · 1.97 KB
/
shuf.py
File metadata and controls
78 lines (59 loc) · 1.97 KB
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
#!/usr/local/cs/bin/python3
import random, sys, argparse
"""Python 3 Script that Implements the GNU shuf command with same arguments"""
def main():
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'), nargs='?', default=sys.stdin)
parser.add_argument('--echo','-e', type=str, nargs='+')
parser.add_argument('--input-range', '-i', type=str)
parser.add_argument('--head-count', '-n', type=int)
parser.add_argument('--repeat', '-r', action='store_true')
args = parser.parse_args()
if(args.echo):
lines = args.echo
numCount = -1
if(args.head_count):
numCount = args.head_count
if(args.input_range):
if(args.echo):
sys.stderr.write("ERROR: Echo and Input-Range options cannot be used together\n")
return
beforeDash = True
firstNum = ""
secNum = ""
for element in args.input_range:
if(element == '-'):
beforeDash = False
continue
if(beforeDash):
firstNum += element
else:
secNum += element
firstNum = int(firstNum)
secNum = int(secNum)
if(firstNum > secNum):
sys.stderr.write("ERROR: Range Invalid\n")
return
lines = list(range(firstNum, secNum+1))
if(not args.echo and not args.input_range):
try:
lines = args.file.read().split()
except:
sys.stderr.write("ERROR: File Invalid")
random.shuffle(lines)
if(args.repeat):
x = 0
while x != numCount:
print(random.choice(lines))
x += 1
else:
if(args.head_count):
i = 0
while i < numCount and i < len(lines):
print(lines[i])
i += 1
else:
for element in lines:
print(element)
if __name__ == "__main__":
main()