perfect_hash.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. #!/usr/bin/env python
  2. """
  3. Generate a minimal perfect hash function for the keys in a file,
  4. desired hash values may be specified within this file as well.
  5. A given code template is filled with parameters, such that the
  6. output is code which implements the hash function.
  7. Templates can easily be constructed for any programming language.
  8. The code is based on an a program A.M. Kuchling wrote:
  9. http://www.amk.ca/python/code/perfect-hash
  10. The algorithm the program uses is described in the paper
  11. 'Optimal algorithms for minimal perfect hashing',
  12. Z. J. Czech, G. Havas and B.S. Majewski.
  13. http://citeseer.ist.psu.edu/122364.html
  14. The algorithm works like this:
  15. 1. You have K keys, that you want to perfectly hash against some
  16. desired hash values.
  17. 2. Choose a number N larger than K. This is the number of
  18. vertices in a graph G, and also the size of the resulting table G.
  19. 3. Pick two random hash functions f1, f2, that return values from 0..N-1.
  20. 4. Now, for all keys, you draw an edge between vertices f1(key) and f2(key)
  21. of the graph G, and associate the desired hash value with that edge.
  22. 5. Check if G is acyclic, i.e. has no loops; if no, go back to step 2.
  23. 6. Assign values to each vertex such that, for each edge, you can add
  24. the values for the two vertices and get the desired (hash) value
  25. for that edge. This task is easy, because the graph is acyclic.
  26. This is done by picking a vertex, and assigning it a value of 0.
  27. Then do a depth-first search, assigning values to new vertices so that
  28. they sum up properly.
  29. 7. f1, f2, and vertex values of G now make up a perfect hash function.
  30. For simplicity, the implementation of the algorithm combines steps 5 and 6.
  31. That is, we check for loops in G and assign the vertex values in one procedure.
  32. If this procedure succeeds, G is acyclic and the vertex values are assigned.
  33. If the procedure fails, G is cyclic, and we go back to step 2, replacing G
  34. with a new graph, and thereby discarding the vertex values from the failed
  35. attempt.
  36. """
  37. __author__ = 'Ilan Schnell <ilanschnell@gmail.com>, 2008 (and AMK 2000)'
  38. __license__ = 'GNU GPL 2'
  39. __version__ = '0.1'
  40. import sys, random, string, cStringIO, StringIO
  41. verbose = False
  42. trails = 5
  43. class Graph:
  44. """
  45. Implements a graph with 'N' vertices. First, you connect the graph with
  46. edges, which have a desired value associated. Then the vertex values
  47. are assigned, which will fail if the graph is cyclic. The vertex values
  48. are assigned such that the two values corresponding to an edge add up to
  49. the desired edge value (mod N).
  50. Example:
  51. >>> G = Graph(3)
  52. >>> G.assign_vertex_values()
  53. True
  54. Now we make an edge between vertex 0 and 1 with desired edge value 2:
  55. >>> G.connect(0, 1, 2)
  56. Make another edge 1:2 with desired edge value 1:
  57. >>> G.connect(1, 2, 1)
  58. The graph is still acyclic, and assigning values works:
  59. >>> G.assign_vertex_values()
  60. True
  61. >>> G.vertex_values
  62. [0, 2, 2]
  63. What do these values mean?
  64. When you add the values for edge 0:1 you get 0 + 2 = 2, as desired.
  65. For edge 1:2 you add 2 + 2 = 4 = 1 (mod 3), as desired.
  66. Adding edge 0:2 produces a loop, so the graph is no longer acyclic.
  67. Assigning values fails.
  68. >>> G.connect(0, 2, 0)
  69. >>> G.assign_vertex_values()
  70. False
  71. """
  72. def __init__(self, N):
  73. self.N = N # number of vertices
  74. # maps a vertex number to the list of tuples (vertices, edge value)
  75. # to which it is connected by edges.
  76. self.adjacent = [[] for n in xrange(N)]
  77. def connect(self, vertex1, vertex2, edge_value):
  78. """
  79. Connect 'vertex1' and 'vertex2' with an edge, with associated
  80. value 'value'
  81. """
  82. # Add vertices to each other's adjacent list
  83. self.adjacent[vertex1].append( (vertex2, edge_value) )
  84. self.adjacent[vertex2].append( (vertex1, edge_value) )
  85. def assign_vertex_values(self):
  86. """
  87. Try to assign the vertex values, such that, for each edge, you can
  88. add the values for the two vertices involved and get the desired
  89. value for that edge, i.e. the desired hash key.
  90. This will fail when the graph is cyclic.
  91. This is done by a Depth-First Search of the graph. If the search
  92. finds a vertex that was visited before, there's a loop and False is
  93. returned immediately, i.e. the assignment is terminated.
  94. On success (when the graph is acyclic) True is returned.
  95. """
  96. self.vertex_values = self.N * [-1] # -1 means unassigned
  97. visited = self.N * [False]
  98. # Loop over all vertices, taking unvisited ones as roots.
  99. for root in xrange(self.N):
  100. if visited[root]:
  101. continue
  102. # explore tree starting at 'root'
  103. self.vertex_values[root] = 0 # set arbitrarily to zero
  104. # Stack of vertices to visit, a list of tuples (parent, vertex)
  105. tovisit = [ (None, root) ]
  106. while tovisit:
  107. parent, vertex = tovisit.pop()
  108. visited[vertex] = True
  109. # Loop over adjacent vertices, but skip the vertex we arrived
  110. # here from the first time it is encountered.
  111. skip = True
  112. for neighbor, edge_value in self.adjacent[vertex]:
  113. if skip and neighbor == parent:
  114. skip = False
  115. continue
  116. if visited[neighbor]:
  117. # We visited here before, so the graph is cyclic.
  118. return False
  119. tovisit.append( (vertex, neighbor) )
  120. # Set new vertex's value to the desired edge value,
  121. # minus the value of the vertex we came here from.
  122. self.vertex_values[neighbor] = \
  123. ( edge_value - self.vertex_values[vertex] ) % self.N
  124. # check if all vertices have a valid value
  125. for vertex in xrange(self.N):
  126. assert self.vertex_values[vertex] >= 0
  127. # We got though, so the graph is acyclic,
  128. # and all values are now assigned.
  129. return True
  130. def generate_hash(kdic, Hash):
  131. """
  132. Return hash functions f1 and f2, and G for a perfect minimal hash.
  133. Input is dictionary 'kdic' with the keys and desired hash values.
  134. 'Hash' is a random hash function generator, that means Hash(N) returns a
  135. returns a random hash function which returns hash values from 0..N-1.
  136. """
  137. # N is the number of vertices in the graph G
  138. N = 1 if not kdic else (max(kdic.values()) + 1)
  139. if verbose >= 2:
  140. sys.stderr.write('N = %i\n' % N)
  141. trail = 0 # Number of trial graphs so far
  142. while True:
  143. if (trail % trails) == 0: # trails failures, increase N slightly
  144. if trail > 0:
  145. N = max(N+1, int(1.05*N))
  146. if verbose:
  147. sys.stderr.write('\n')
  148. sys.stderr.write('Generating graphs N = %i ' % N)
  149. trail += 1
  150. if verbose:
  151. sys.stderr.write('.')
  152. sys.stderr.flush()
  153. G = Graph(N) # Create graph with N vertices
  154. f1 = Hash(N) # Create 2 random hash functions
  155. f2 = Hash(N)
  156. # Connect vertices given by the values of the two hash functions
  157. # for each key. Associate the desired hash value with each edge.
  158. for key, hashval in kdic.iteritems():
  159. G.connect(f1(key), f2(key), hashval)
  160. # Try to assign the vertex values. This will fail when the graph
  161. # is cyclic. But when the graph is acyclic it will succeed and we
  162. # break out, because we're done.
  163. if G.assign_vertex_values():
  164. break
  165. if verbose:
  166. sys.stderr.write('\nAcyclic graph found after %i trails.\n' % trail)
  167. if verbose >= 2:
  168. sys.stderr.write('N = %i\n' % N)
  169. if verbose:
  170. sys.stderr.write('Checking generated hash function... ')
  171. # Sanity check the result by actually verifying that all the keys
  172. # hash to the right value.
  173. for key, hashval in kdic.iteritems():
  174. assert hashval == ( G.vertex_values[f1(key)] +
  175. G.vertex_values[f2(key)] ) % N
  176. if verbose:
  177. sys.stderr.write('OK\n')
  178. return f1, f2, G.vertex_values
  179. class Hash1:
  180. """
  181. Random hash function generator.
  182. For simplicity and speed, this doesn't implement any byte-level hashing
  183. scheme. Instead, a random string is generated and prefixing to str(key),
  184. and then Python's hashing function is used.
  185. """
  186. def __init__(self, N):
  187. self.N = N
  188. self.salt = "".join(random.choice(string.letters + string.digits)
  189. for i in xrange(8))
  190. def __call__(self, key):
  191. return hash(self.salt + str(key)) % self.N
  192. template = """
  193. def perfect_hash(key):
  194. return (G[ hash('$S1' + str(key)) % $NG ] +
  195. G[ hash('$S2' + str(key)) % $NG ]) % $NG
  196. """
  197. class Hash2:
  198. """
  199. Random hash function generator.
  200. Simple byte level hashing, each byte is multiplied in sequence to a table
  201. containing random numbers modulo N, and then these products are summed up.
  202. The table with random numbers is dynamically expanded whenever
  203. a key longer than the current table size is encountered.
  204. """
  205. def __init__(self, N):
  206. self.N = N
  207. self.salt = []
  208. def __call__(self, key):
  209. skey = key
  210. while len(self.salt) < len(skey): # add more salt if necessary
  211. self.salt.append(random.randint(0, self.N-1))
  212. return sum(self.salt[i] * ord(c)
  213. for i, c in enumerate(skey)) % self.N
  214. template = """
  215. S1 = [$S1]
  216. S2 = [$S2]
  217. def hash_f(key, T):
  218. return sum(T[i % $NS] * ord(c) for i, c in enumerate(str(key))) % $NG
  219. def perfect_hash(key):
  220. return (G[hash_f(key, S1)] + G[hash_f(key, S2)]) % $NG
  221. """
  222. class PerfHash:
  223. """
  224. This class is designed for creating perfect hash tables at run time,
  225. which should be avoided, in particulat inserting new keys is
  226. prohibitively expensive since a new perfect hash table needs to be
  227. constructed. However, this class can be usefull for testing.
  228. >>> d = PerfHash({'foo':(429, 'bar'), 42:True, False:'baz'})
  229. >>> d['foo'], d[42], d[False]
  230. ((429, 'bar'), True, 'baz')
  231. >>> d[False] = (1, 2)
  232. >>> d[False]
  233. (1, 2)
  234. >>> d.has_key('foo')
  235. True
  236. >>> d.has_key(True)
  237. False
  238. """
  239. def __init__(self, dic):
  240. self.klst = []
  241. self.objs = []
  242. kdic = {}
  243. for hashval, (key, obj) in enumerate(dic.iteritems()):
  244. self.klst.append(key)
  245. self.objs.append(obj)
  246. kdic[key] = hashval
  247. self.N = len(dic)
  248. self.f1, self.f2, self.G = generate_hash(kdic, Hash1)
  249. def __setitem__(self, newkey, newobj):
  250. dic = {}
  251. for key in self.klst:
  252. dic[key] = self[key]
  253. dic[newkey] = newobj
  254. self.__init__(dic)
  255. def hashval(self, key):
  256. return ( self.G[self.f1(key)] + self.G[self.f2(key)] ) % len(self.G)
  257. def __getitem__(self, key):
  258. h = self.hashval(key)
  259. if h < self.N and key == self.klst[h]:
  260. return self.objs[h]
  261. else:
  262. raise IndexError
  263. def has_key(self, key):
  264. h = self.hashval(key)
  265. return h < self.N and key == self.klst[h]
  266. class Format:
  267. """
  268. >>> class o:
  269. ... pass
  270. >>> o.delimiter = ': '
  271. >>> o.width = 75
  272. >>> o.indent = 4
  273. >>> x = Format( o )
  274. >>> x( range(10) )
  275. '0: 1: 2: 3: 4: 5: 6: 7: 8: 9'
  276. >>> o.delimiter = '; '
  277. >>> x = Format( o )
  278. >>> x( range(5) )
  279. '0; 1; 2; 3; 4'
  280. >>> o.delimiter = ' '
  281. >>> x = Format( o )
  282. >>> x( range(5), quote = True )
  283. '"0" "1" "2" "3" "4"'
  284. >>> x(42)
  285. '42'
  286. >>> x('Hello')
  287. 'Hello'
  288. """
  289. def __init__(self, options):
  290. names = ['width', 'indent', 'delimiter']
  291. for name in names:
  292. setattr(self, name, getattr(options, name))
  293. if verbose >=2:
  294. sys.stderr.write("Format options:\n")
  295. for name in names:
  296. sys.stderr.write(' %s: %r\n' % (name, getattr(self, name)))
  297. def __call__(self, data, quote = False):
  298. if type(data) != type([]):
  299. return str(data)
  300. lendel = len(self.delimiter)
  301. aux = StringIO.StringIO()
  302. pos = 20
  303. for i, elt in enumerate(data):
  304. last = bool(i == len(data)-1)
  305. s = ('"%s"' if quote else '%s') % elt
  306. if pos + len(s) + lendel > self.width:
  307. aux.write('\n' + (self.indent * ' '))
  308. pos = self.indent
  309. aux.write(s)
  310. pos += len(s)
  311. if not last:
  312. aux.write(self.delimiter)
  313. pos += lendel
  314. return aux.getvalue()
  315. def keyDict(keys_hashes):
  316. """
  317. Checks a list with (key, hashvalue) tupels and returns dictionary.
  318. >>> d = keyDict([(1, 2), (3, 4), (5, 6)])
  319. >>> d[3]
  320. 4
  321. """
  322. K = len(keys_hashes) # number of keys
  323. if verbose >= 2:
  324. sys.stderr.write('K = %i\n' % K)
  325. kdic = dict(keys_hashes)
  326. if len(kdic) < K:
  327. sys.stderr.write('Warning: Input contains duplicate keys\n')
  328. if len(set(kdic.values())) < K:
  329. sys.stderr.write('Warning: Input contains duplicate hash values\n')
  330. return kdic
  331. def generate_code(keys_hashes, template, Hash, options, extra_subs):
  332. """
  333. Takes a list of key value pairs and inserts the generated parameter
  334. lists into the 'template' strinng. 'Hash' is the random hash function
  335. generator, and the optional keywords are formating options.
  336. The return value is the substituted code template.
  337. """
  338. f1, f2, G = generate_hash(keyDict(keys_hashes), Hash)
  339. assert f1.N == f2.N == len(G)
  340. assert len(f1.salt) == len(f2.salt)
  341. fmt = Format(options)
  342. return string.Template(template).substitute(
  343. NS = len(f1.salt),
  344. S1 = fmt(f1.salt),
  345. S2 = fmt(f2.salt),
  346. NG = len(G),
  347. G = fmt(G),
  348. NK = len(keys_hashes),
  349. K = fmt([key for key, hashval in keys_hashes], quote = True),
  350. H = fmt([hashval for key, hashval in keys_hashes]),
  351. **extra_subs)
  352. def read_table(filename, options):
  353. """
  354. Reads keys and desired hash value pairs from a file. If no column
  355. for the hash value is specified, a sequence of hash values is generated,
  356. from 0 to N-1, where N is the number of rows found in the file.
  357. """
  358. if verbose >= 2:
  359. sys.stderr.write("Reading table from file `%s' to extract keys.\n" %
  360. filename)
  361. try:
  362. f = file(filename)
  363. except IOError :
  364. exit("Error: Could not open `%s' for reading." % filename)
  365. keys_hashes = []
  366. hashval = -1
  367. if verbose >= 2:
  368. sys.stderr.write("Reader options:\n")
  369. for name in ['comment', 'splitby', 'keycol', 'hashcol']:
  370. sys.stderr.write(' %s: %r\n' %
  371. (name, getattr(options, name)))
  372. for n, line in enumerate(f):
  373. line = line.strip()
  374. if not line or line.startswith(options.comment):
  375. continue
  376. if line.count(options.comment): # strip content after comment
  377. line = line.split(options.comment)[0].strip()
  378. row = [col.strip() for col in line.split(options.splitby)]
  379. try:
  380. key = row[options.keycol-1]
  381. except IndexError :
  382. exit("%s:%i: Error: Cannot read key, not enough columns." %
  383. (filename, n+1))
  384. if options.hashcol:
  385. try:
  386. val = row[options.hashcol-1]
  387. except IndexError :
  388. exit("%s:%i: Error: Cannot read hash value, not enough columns."
  389. % (filename, n+1))
  390. try:
  391. hashval = int(val)
  392. except ValueError :
  393. exit("%s:%i: Error: Cannot convert `%s' to int." %
  394. (filename, n+1, row[options.hashcol-1]))
  395. else:
  396. hashval += 1
  397. keys_hashes.append( (key, hashval) )
  398. f.close()
  399. if not keys_hashes:
  400. exit("Error: no keys found in file `%s'." % filename)
  401. return keys_hashes
  402. def print_keys_hashes(keys_hashes):
  403. fmt = '%-20s %10s'
  404. head = fmt % ('Key', 'Hash value')
  405. sys.stderr.write('\n' + head + '\n')
  406. sys.stderr.write(len(head)*'-' + '\n')
  407. for tup in keys_hashes:
  408. sys.stderr.write(fmt % tup + '\n')
  409. sys.stderr.write('\n')
  410. def read_template(filename):
  411. if verbose >= 2:
  412. sys.stderr.write("Reading template from file `%s'.\n" % filename)
  413. try:
  414. f = file(filename)
  415. except IOError :
  416. fatal_error("Error: Could not open `%s' for reading." % filename)
  417. return f.read()
  418. def builtin_template(Hash):
  419. return """\
  420. # =======================================================================
  421. # ================= Python code for perfect hash function ===============
  422. # =======================================================================
  423. G = [$G]
  424. """ + Hash.template + """
  425. # ============================ Sanity check =============================
  426. K = [$K]
  427. H = [$H]
  428. assert len(K) == len(H) == $NK
  429. for k, h in zip(K, H):
  430. assert perfect_hash(k) == h
  431. """
  432. def print_code(code, name, width = 78):
  433. def center(s):
  434. v = (width - len(s))/2
  435. return '='*v + s + '='*v
  436. sys.stderr.write(center(' BEGIN %s ' % name) + '\n')
  437. sys.stderr.write(code + '\n')
  438. sys.stderr.write(center(' END %s ' % name) + '\n')
  439. def self_test(options):
  440. import doctest
  441. global verbose
  442. print 'Starting self tests ...'
  443. def random_word():
  444. return ''.join(random.choice(string.letters + string.digits)
  445. for i in xrange(random.randint(1, 20)))
  446. def flush_dot():
  447. sys.stdout.write('.')
  448. sys.stdout.flush()
  449. def run(K, Hash):
  450. flush_dot()
  451. keys = [chr(65+i) for i in xrange(K)]
  452. hashes = range(K)
  453. random.shuffle(keys)
  454. random.shuffle(hashes)
  455. code = generate_code(zip(keys, hashes),
  456. builtin_template(Hash),
  457. Hash,
  458. options)
  459. exec(code) in {}
  460. verbose = False
  461. for Hash in [Hash1, Hash2]:
  462. for K in xrange(0, 27):
  463. run(K, Hash)
  464. print
  465. verbose = options.verbose
  466. N = 250
  467. for Hash in [Hash1, Hash2]:
  468. if verbose:
  469. print 'Generating approximately %i key/hash pairs ...' % N
  470. kh = {}
  471. for i in xrange(N):
  472. kh[random_word()] = i
  473. if verbose:
  474. print 'Generating code for %i key/hash pairs ...' % len(kh)
  475. code = generate_code(kh.items(),
  476. builtin_template(Hash),
  477. Hash,
  478. options)
  479. if verbose:
  480. print 'Executing code ...'
  481. flush_dot()
  482. exec(code) in {}
  483. flush_dot()
  484. d = PerfHash(dict([(100-i, i*i) for i in xrange(500)]))
  485. for i in xrange(500):
  486. assert d[100-i] == i*i
  487. flush_dot()
  488. d[None] = True
  489. assert d[None] == True
  490. if verbose:
  491. print 'Running doctest ...'
  492. verbose = False
  493. failure_count, test_count = doctest.testmod(report = True, verbose = False)
  494. print
  495. if failure_count:
  496. sys.stderr.write('FAILED\n')
  497. sys.exit(2)
  498. else:
  499. sys.stderr.write('%i tests passed.\n' % test_count)
  500. sys.stderr.write('OK\n')
  501. sys.exit(0)
  502. if __name__ == '__main__':
  503. from optparse import OptionParser
  504. usage = "usage: %prog [options] KEYS_FILE [TMPL_FILE]"
  505. description = """\
  506. Generates code for perfect hash functions from
  507. a file with keywords and a code template.
  508. If no template file is provided, a small built-in Python template
  509. is processed and the output code is written to stdout.
  510. """
  511. parser = OptionParser(usage = usage,
  512. description = description,
  513. prog = sys.argv[0],
  514. version = "%prog 0.1")
  515. parser.add_option("--delimiter",
  516. action = "store",
  517. default = ", ",
  518. help = "Delimiter for list items used in output, "
  519. "the default delimiter is '%default'",
  520. metavar = "STR")
  521. parser.add_option("--indent",
  522. action = "store",
  523. default = 2,
  524. type = "int",
  525. help = "Make INT spaces at the beginning of a "
  526. "new line when generated list is wrapped. "
  527. "Default is %default",
  528. metavar = "INT")
  529. parser.add_option("--width",
  530. action = "store",
  531. default = 76,
  532. type = "int",
  533. help = "Maximal width of generated list when "
  534. "wrapped. Default width is %default",
  535. metavar = "INT")
  536. parser.add_option("--comment",
  537. action = "store",
  538. default = "#",
  539. help = "STR is the character, or sequence of "
  540. "characters, which marks the beginning "
  541. "of a comment (which runs till "
  542. "the end of the line), in the input "
  543. "KEYS_FILE. "
  544. "Default is '%default'",
  545. metavar = "STR")
  546. parser.add_option("--splitby",
  547. action = "store",
  548. default = ",",
  549. help = "STR is the character by which the columns "
  550. "in the input KEYS_FILE are split. "
  551. "Default is '%default'",
  552. metavar = "STR")
  553. parser.add_option("--keycol",
  554. action = "store",
  555. default = 1,
  556. type = "int",
  557. help = "Specifies the column INT in the input "
  558. "KEYS_FILE which contains the keys. "
  559. "Default is %default, i.e. the first column.",
  560. metavar = "INT")
  561. parser.add_option("--hashcol",
  562. action = "store",
  563. default = 0,
  564. type = "int",
  565. help = "Specifies the column INT in the input "
  566. "KEYS_FILE which contains the desired "
  567. "hash values. "
  568. "By default the hash values are given by the "
  569. "sequence 0..N-1.",
  570. metavar = "INT")
  571. parser.add_option("--trails",
  572. action = "store",
  573. default = 5,
  574. type = "int",
  575. help = "Specifies the number of trails before "
  576. "N is increased. A small INT will give "
  577. "compute faster, but the array G will be "
  578. "large. A large INT will take longer to "
  579. "compute but G will be smaller. "
  580. "Default is %default",
  581. metavar = "INT")
  582. parser.add_option("--hft",
  583. action = "store",
  584. default = 2,
  585. type = "int",
  586. help = "Hash function type INT (see documentation), "
  587. "The default is %default",
  588. metavar = "INT")
  589. parser.add_option("-e", "--execute",
  590. action = "store_true",
  591. help = "Execute the generated code within "
  592. "the Python interpreter.")
  593. parser.add_option("-o", "--output",
  594. action = "store",
  595. help = "Specify output FILE explicitly. "
  596. "`-o std' means standard output. "
  597. "`-o no' means no output. "
  598. "By default, the file name is obtained "
  599. "from the name of the template file by "
  600. "substituting `tmpl' to `code'.",
  601. metavar = "FILE")
  602. parser.add_option("--test",
  603. action = "store_true",
  604. help = "Perform self test")
  605. parser.add_option("-v", "--verbose",
  606. action = "count",
  607. help = "Be verbose, "
  608. "use -vv to be even more verbose")
  609. options, args = parser.parse_args()
  610. print type(options), '\n', repr(options)
  611. if options.trails > 0:
  612. trails = options.trails
  613. else:
  614. parser.error("trails before increasing N has to be larger than zero")
  615. verbose = options.verbose
  616. if options.test:
  617. self_test(options)
  618. if len(args) not in (1, 2):
  619. parser.error("incorrect number of arguments")
  620. if len(args) == 2 and not args[1].count('tmpl'):
  621. parser.error("template filename does not contain 'tmpl'")
  622. if options.hft == 1:
  623. Hash = Hash1
  624. elif options.hft == 2:
  625. Hash = Hash2
  626. else:
  627. parser.error("Hash function %i not implemented.")
  628. # --------------------- end parsing and checking --------------
  629. # ---------------- keys_file
  630. keys_file = args[0]
  631. if verbose:
  632. sys.stderr.write("keys_file = %r\n" % keys_file)
  633. # ---------------- keys_hashes
  634. keys_hashes = read_table(keys_file, options)
  635. if verbose >= 3:
  636. print_keys_hashes(keys_hashes)
  637. # ---------------- tmpl_file
  638. if len(args) == 2:
  639. tmpl_file = args[1]
  640. else:
  641. tmpl_file = None
  642. if verbose:
  643. sys.stderr.write("tmpl_file = %r\n" % tmpl_file)
  644. # ---------------- template
  645. if tmpl_file:
  646. template = read_template(tmpl_file)
  647. else:
  648. template = builtin_template(Hash)
  649. if verbose >= 3:
  650. print_code(template, 'TEMPLATE')
  651. # ---------------- outname
  652. if options.output:
  653. outname = options.output
  654. else:
  655. if tmpl_file:
  656. if tmpl_file.count('tmpl'):
  657. outname = tmpl_file.replace('tmpl', 'code')
  658. else:
  659. exit("Hmm, template filename does not contain 'tmpl'")
  660. else:
  661. outname = 'std'
  662. if verbose:
  663. sys.stderr.write("outname = %r\n" % outname)
  664. # ---------------- outstream
  665. if outname == 'std':
  666. outstream = sys.stdout
  667. elif outname == 'no':
  668. outstream = None
  669. else:
  670. try:
  671. outstream = open(outname, 'w')
  672. except IOError :
  673. exit("Error: Could not open `%s' for writing." % outname)
  674. # ---------------- generated code
  675. code = generate_code(keys_hashes, template, Hash, options)
  676. if verbose >= 3:
  677. print_code(code, 'GENERATED CODE')
  678. # ---------------- execute code
  679. if options.execute or template == builtin_template(Hash):
  680. if verbose:
  681. sys.stderr.write('Executing code...\n')
  682. exec(code)
  683. # ---------------- write code to output stream
  684. if outstream:
  685. outstream.write(code)
  686. if not outname == 'std':
  687. outstream.close()