Skip to content

Instantly share code, notes, and snippets.

@dubslow
Last active October 30, 2017 09:19
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 dubslow/057407e71a8edda2bcb7541e73c0bb6e to your computer and use it in GitHub Desktop.
Save dubslow/057407e71a8edda2bcb7541e73c0bb6e to your computer and use it in GitHub Desktop.
Sort the OPN most wanted roadblocks file
#! /usr/bin/env python3
# indent width: 5 spaces
# default sort method to use:
sortkey = lambda line: line.snfs_difficulty()
###############################################################################
from math import log10
from abc import ABCMeta, abstractmethod
class OPNLine(metaclass=ABCMeta):
@abstractmethod
def __init__(self, line):
line = line.split()
for i, section in enumerate(line):
try:
line[i] = int(section)
except ValueError:
raise ValueError("corrupt line, got piece {}".format(section))
self.base, self.power, self.composite = line[:3]
self._line = line
def snfs_difficulty(self):
'''Zeroth order estimate'''
return log10(self.base) * self.power # log10 of (base^(power+1)-1)/(base-1)
def gnfs_difficulty(self):
return log10(self.composite)
@property
@abstractmethod
def _str_template(self):
pass
def __str__(self):
fmt, *args = self._str_template
return fmt.format(*args)
class MostWantedLine(OPNLine):
def __init__(self, line):
super().__init__(line)
self.weight = self._line[3]
@property
def _str_template(self):
return ("{:.1f} {:.1f} {} {} {}\n", self.snfs_difficulty(), self.gnfs_difficulty(), self.weight, self.base, self.power)
class TLine(OPNLine):
def __init__(self, line):
return super().__init__(line)
@property
def _str_template(self):
return ("{:.1f} {:.1f} {} {}\n", self.snfs_difficulty(), self.gnfs_difficulty(), self.base, self.power)
def determine_file_type(line):
if len(line.split()) > 3:
return MostWantedLine
else:
return TLine
def inner_main(file, _sortkey=None):
lines = []
if _sortkey is None: _sortkey = sortkey
with open(file) as f:
line = f.readline()
OPNClass = determine_file_type(line) # t file or mwrb file?
lines.append(OPNClass(line))
for line in f:
lines.append(OPNClass(line))
lines.sort(key=_sortkey)
with open(file+'.sort', 'w') as f:
f.writelines(str(line) for line in lines)
def main(*argv):
file = argv[1]
sortkey = None
if len(argv) > 2:
sortkey = eval("lambda line: "+argv[2])
inner_main(file, sortkey)
if __name__ == "__main__":
from sys import argv
main(*argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment