Advertisement
piguy123

fib.py

Mar 26th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #/usr/bin/python
  2. # fib.py
  3. """
  4. Number generator
  5.  
  6. Uses cPickle to save and load previously calculated numbers. Could be improved to use databases or
  7. other mathods which are faster.
  8. """
  9.  
  10. import os
  11. from cPickle import dump, load, HIGHEST_PROTOCOL
  12.  
  13. class LoadableDict(dict):
  14.  
  15.     def __init__(self, filename="loadeddict.dict", *args, **kw):
  16.         dict.__init__(self, *args, **kw)
  17.         self.filename = filename
  18.         try: self.load()
  19.         except: os.remove(filename)
  20.  
  21.     def load(self, filename=None):
  22.         filename = filename or self.filename
  23.         if not os.path.exists(filename):
  24.             return
  25.         with open(filename, "rb") as f:
  26.             d = load(f)
  27.             assert isinstance(d, dict)
  28.             self.loaddict(d)
  29.  
  30.     def loaddict(self, d):
  31.         for key, value in d.iteritems():
  32.             self[key] = value
  33.        
  34.     def save(self, filename=None):
  35.         filename = filename or self.filename
  36.         with open(filename, "wb") as f:
  37.             dump(self, f, HIGHEST_PROTOCOL)
  38.  
  39.     def __del__(self):
  40.         try: self.save()
  41.         except: pass
  42.  
  43. try:
  44.     _mem = LoadableDict("fib.dict", {1: 0, 2: 1})
  45. except MemoryError:
  46.     os.remove("fib.dict")
  47.     _mem = LoadableDict("fib.dict", {1: 0, 2: 1})
  48.  
  49. def fib(n):
  50.     n = int(n)
  51.     if n in _mem:
  52.         return _mem[n]
  53.     for i in xrange(1, n+1):
  54.         if i in _mem:
  55.             continue
  56.         else:
  57.             _mem[i] = _mem[i-1] + _mem[i-2]
  58.     return _mem[n]
  59.  
  60. def fibrange(start, stop=None, step=1):
  61.     if stop is None:
  62.         stop, start = start, 0
  63.     return (fib(n+1) for n in xrange(start, stop, step))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement