-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert.py
executable file
·85 lines (68 loc) · 2.48 KB
/
convert.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
#!/usr/bin/env python
import sys
import argparse
import os
def ascii_to_binary(args):
# Open input and output files
input = open(args.input_file, "r")
output = open(args.output_file, "wb")
# Read in lines of input
input_rows = input.readlines()
# Read in strings representing integer dimensions, save as bytes to output
dims = [int(s) for s in input_rows[0].split()]
for d in dims:
output.write(d.to_bytes(4, "little", signed=True))
# Read in each row of the matrix
for row in input_rows[1:]:
# Read in float elements of each row, output as bytes to output
int_row = [int(s) for s in row.split()]
for num in int_row:
output.write(num.to_bytes(4, "little", signed=True))
input.close()
output.close()
def binary_to_ascii(args):
# Open input and output files
input = open(args.input_file, "rb")
output = open(args.output_file, "w")
# Read in bytes representing integer dimensions, save as strngs to output
rows = int.from_bytes(input.read(4), "little", signed=True)
columns = int.from_bytes(input.read(4), "little", signed=True)
output.write("{} {}{}".format(rows, columns, os.linesep))
# Read in one row's worth of elements at a time
for i in range(rows):
curr_row = []
# For each row, read in bytes representing floats, then save the row as a string to output
for j in range(columns):
val = int.from_bytes(input.read(4), "little", signed=True)
curr_row.append(str(val))
output.write(" ".join(curr_row) + os.linesep)
input.close()
output.close()
def main():
parser = argparse.ArgumentParser(
description="Converts between ascii and binary files representing integer matrices"
)
parser.add_argument("input_file", help="file to read from")
parser.add_argument("output_file", help="file to write to")
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--to-binary",
action="store_true",
default=False,
help="convert from ascii to binary",
)
group.add_argument(
"--to-ascii",
action="store_true",
default=False,
help="convert from binary to ascii",
)
args = parser.parse_args()
if args.to_binary:
ascii_to_binary(args)
elif args.to_ascii:
binary_to_ascii(args)
else:
parser.error("Either --to-binary or --to-ascii must be specified")
if __name__ == "__main__":
main()