File: checkpointing.py

package info (click to toggle)
python-cogent 1.4.1-1.2
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 13,260 kB
  • ctags: 20,087
  • sloc: python: 116,163; ansic: 732; makefile: 74; sh: 9
file content (44 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
import os, time, cPickle

__author__ = "Peter Maxwell"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__credits__ = ["Peter Maxwell"]
__license__ = "GPL"
__version__ = "1.4.1"
__maintainer__ = "Peter Maxwell"
__email__ = "pm67nz@gmail.com"
__status__ = "Production"

class Checkpointer(object):
    def __init__(self, filename, interval=600, noisy=True):
        self.filename = filename
        self.interval = interval
        self.last_time = time.time()
        self.noisy = noisy
    
    def available(self):
        return self.filename is not None and os.path.exists(self.filename)
    
    def load(self):
        assert self.filename is not None, 'check .available() first'
        print "RESUMING from file '%s'" % self.filename
        f = open(self.filename)
        obj = cPickle.load(f)
        self.last_time = time.time()
        return obj
    
    def record(self, obj, msg=None, always=False):
        if self.filename is None:
            return
        now = time.time()
        elapsed = now - self.last_time
        if always or elapsed > self.interval:
            if self.noisy:
                print "CHECKPOINTING to file '%s'" % self.filename
                if msg is not None:
                    print msg
            f = open(self.filename, 'w')
            cPickle.dump(obj, f)
            self.last_time = now