Skip to content

Instantly share code, notes, and snippets.

@Spindel
Created October 28, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spindel/4f4fc69b87a9683222ec to your computer and use it in GitHub Desktop.
Save Spindel/4f4fc69b87a9683222ec to your computer and use it in GitHub Desktop.
rewrite of a script I came across
#!/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