Created
October 28, 2015 12:38
-
-
Save Spindel/4f4fc69b87a9683222ec to your computer and use it in GitHub Desktop.
rewrite of a script I came across
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import shelve | |
def grabfile(filename): | |
result = [] | |
with open(filename, "rb") as f: | |
for line in f: | |
try: | |
val = int(line) | |
result.append(val) | |
except ValueError: | |
print("Error parsing line: %r" % line) | |
return result | |
def fixfile(): | |
canstart = grabfile("llnn-canstart.dump") | |
canend = grabfile("llnn-canend.dump") | |
# XXX: Check the data structure. Should probably be a dict instead. | |
words = [] | |
with open("llnn-words.dump", "rb") as outwords: | |
for line in outwords: | |
try: | |
# XXX: the below should probably be turned to a pattern | |
parts = line.split(',', 1) | |
wid = int(parts[0]) | |
word = parts[1].split('\n', 1)[0] | |
words.append([word, []]) | |
words[wid][0] = word | |
except: | |
print("Error parsing word line: %r" % line) | |
with open("llnn-links.dump", "rb") as outlinks: | |
for line in outlinks: | |
try: | |
parts = line.split(':', 1) | |
wid = int(parts[0]) | |
for link in parts[1].split(','): | |
words[wid][1].append(int(link)) | |
except: | |
print("Error parsing link line: %r" % line) | |
try: | |
dbase = shelve.open("llnn-restore.dat") | |
except: | |
print("Error opening input file") | |
try: | |
dbase['words'] = words | |
dbase['canstart'] = canstart | |
dbase['canend'] = canend | |
except: | |
print("Error writing restored data") | |
# XXX: We never close the dbase file | |
def main(): | |
fixfile() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment