PerfHash.py 900 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. """
  3. This example shows how to use the class PerfHash.
  4. This class is designed for creating perfect hash tables at run time,
  5. which should be avoided, in particulat inserting new keys is
  6. prohibitively expensive since a new perfect hash table needs to be
  7. constructed. However, this class can be usefull for testing.
  8. For practical programming purposes in Python the class PerfHash
  9. should never be used because Python's built-in dictionary is very
  10. efficient and always faster than PerfHash.
  11. """
  12. import sys
  13. sys.path.append('..')
  14. from perfect_hash import PerfHash
  15. month = dict(zip('jan feb mar apr may jun jul aug sep oct mov dec'.split(),
  16. range(1, 13)))
  17. d = PerfHash(month)
  18. for m in month:
  19. assert month[m] == d[m]
  20. d[True] = False
  21. assert d[True] == False
  22. for i in xrange(10): # very expensive
  23. d[i] = 2*i*i + 3*i -7
  24. assert d[4] == 37
  25. print 'OK'