-
Notifications
You must be signed in to change notification settings - Fork 18
/
dataparser.py
52 lines (39 loc) · 1.19 KB
/
dataparser.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
import argparse
import json
from collections import *
from pathlib import Path
import util
def ni(itr):
return int(next(itr))
# parses the next string of itr as a list of integers
def nl(itr):
return [int(v) for v in next(itr).split()]
def parse(inp):
itr = (line for line in inp.split('\n'))
ns = argparse.Namespace()
# TODO: fill ns
return ns
def parse_cached(inp, tc, overwrite=False):
return util.getCachedObj('inputNameSpace', tc, lambda: parse(inp), overwrite)
class FlexibleEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, argparse.Namespace):
return vars(obj)
return json.JSONEncoder.default(self, obj)
def parse2json(inp):
ns = parse(inp)
return json.dumps(ns, cls=FlexibleEncoder)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('inp', nargs='?')
return parser.parse_args()
if __name__ == '__main__':
args = get_args()
if args.inp:
file_list = [args.inp]
else:
file_list = Path('in').glob('*.in')
for inp in file_list:
data = parse2json(inp.read_text())
with inp.with_suffix('.json').open('w') as f:
f.write(data)