Skip to content

Commit fac8d40

Browse files
committed
New feature to convert to plain txt
1 parent ddefe61 commit fac8d40

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

Pilot1/Uno/topN_to_uno.py

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def parse_arguments():
2626
help='output filename')
2727
parser.add_argument('--show', action='store_true', help='Simply show the plan node')
2828
parser.add_argument('--raw', action='store_true', help='With --show, also show raw JSON')
29+
parser.add_argument('--convert', help='Convert JSON to text format')
2930

3031
args, unparsed = parser.parse_known_args()
3132
return args, unparsed
@@ -286,17 +287,17 @@ def build_dataframe(args):
286287
store.close()
287288

288289

289-
def print_line(line):
290+
def print_line(line, fp_out):
290291
"""line: list of str"""
291292
if len(line) == 0:
292293
return
293294
# Indent
294-
print(" ", end="")
295+
print(" ", end="", file=fp_out)
295296
text = " ".join(line)
296-
print(text)
297+
print(text, file=fp_out)
297298

298299

299-
def show_list(L):
300+
def show_list(L, fp_out):
300301
"""
301302
Show list entries in indented 70-character lines,
302303
ending on blank line
@@ -312,47 +313,82 @@ def show_list(L):
312313
n = len(s) + 1
313314
c += n
314315
if c > limit:
315-
print_line(line)
316+
print_line(line, fp_out)
316317
line.clear()
317318
c = len(s)
318319
line.append(s)
319320

320-
print_line(line)
321-
print("")
321+
print_line(line, fp_out)
322+
print("", file=fp_out)
322323

323324

324-
def show_node(subtree):
325+
def show_node(subtree, fp_out):
325326
"""Write out the given plan subtree"""
326327
for partition in ["val", "train"]:
327328
partition_sets = len(subtree[partition])
328-
print("%s_sets: %i" % (partition, partition_sets))
329+
print("%s_sets: %i" % (partition, partition_sets), file=fp_out)
329330
for index in range(0, len(subtree[partition])):
330-
print("index: %i" % index)
331+
print("index: %i" % index, file=fp_out)
331332
dataset = subtree[partition][index]
332333
for key in ["CELL", "DRUG"]:
333334
if key not in dataset:
334335
continue
335336
partition_keys = dataset[key]
336337
print("%s %ss: count: %i" %
337-
(partition, key, len(partition_keys)))
338-
show_list(partition_keys)
338+
(partition, key, len(partition_keys)),
339+
file=fp_out)
340+
show_list(partition_keys, fp_out)
339341

340342

341-
def show(args):
343+
def show(args, fp_out):
342344
"""Simply show the entry for this node"""
343345
if args.node is None:
344346
print("Provide a node to show!")
345347
exit(1)
346348
# Get the plan subtree for the given node
347349
subtree = read_plan(args.plan, args.node)
348350
if args.raw:
349-
print(str(subtree))
350-
print("node: " + args.node)
351-
show_node(subtree)
351+
print(str(subtree), file=fp_out)
352+
print("node: " + args.node, file=fp_out)
353+
show_node(subtree, fp_out)
354+
355+
356+
def show_metadata(node, fp_out):
357+
print("metadata:", file=fp_out)
358+
for key in node:
359+
print("%s: %s" % (key, str(node[key])), file=fp_out)
360+
print("", file=fp_out)
361+
362+
363+
def convert_all(tree, fp_out):
364+
for node in tree.keys():
365+
if node == "1":
366+
show_metadata(tree[node], fp_out)
367+
continue
368+
print("node: " + node, file=fp_out)
369+
show_node(tree[node], fp_out)
370+
371+
372+
def convert(args):
373+
output = args.convert
374+
if output == "-":
375+
print("converting to: stdout")
376+
fp_out = sys.stdout
377+
else:
378+
print("converting to: " + output)
379+
fp_out = open(output, "w")
380+
# Get the full tree:
381+
tree = read_plan(args.plan, None)
382+
convert_all(tree, fp_out)
383+
if output != "-":
384+
fp_out.close()
385+
352386

353387
if __name__ == '__main__':
354388
parsed, unparsed = parse_arguments()
355389
if parsed.show:
356-
show(parsed)
390+
show(parsed, sys.stdout)
391+
elif parsed.convert is not None:
392+
convert(parsed)
357393
else:
358394
build_dataframe(parsed)

0 commit comments

Comments
 (0)