-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomize.py
More file actions
34 lines (25 loc) · 755 Bytes
/
randomize.py
File metadata and controls
34 lines (25 loc) · 755 Bytes
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
import json
import random
import sys
def randdict(d, size):
samples = random.sample(d.items(), size)
result = {k: v for (k, v) in samples}
return result
def main():
if len(sys.argv) < 3:
print('Missing arguments!\n'
f'Usage: python {sys.argv[0]} <json file> <output size>'
' [<output file>]\n'
'Optional arguments are enclosed with "[]"')
exit(0)
with open(sys.argv[1], 'r') as f:
d = json.load(f)
size = int(sys.argv[2])
result = randdict(d, size)
if len(sys.argv) > 3:
with open(sys.argv[3], 'w') as o:
json.dump(result, o, indent=4)
else:
print(json.dumps(result, indent=4))
if __name__ == '__main__':
main()