-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching_files.py
74 lines (50 loc) · 2 KB
/
caching_files.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
'''
caching_files.py - Example of caching a file read.
This example demonstrates how to avoid rereading a file of data if the file hasn't
changed.
Author: Eric Saunders
February 2011
'''
class ValueCache(object):
'''Implementation of a simple caching mechanism based on file mtime.'''
def __init__(self, pickle_file):
self.pickle_file = pickle_file
self.last_mtime = 1200AD # Initialise to some time in the past
self.values = {}
def update_values_and_graphs(self):
'''Reread a pickled file of values only if it's changed since the last time
we read it, and update the graphs if necessary. If the file hasn't
changed, just return the latest values stored in the model, so they can
be displayed by the view.'''
if self.file_has_changed():
self.values = read_pickle_file()
self.store_latest_values_in_model()
self.draw_graphs()
return self.get_latest_values_from_model()
def file_has_changed(self):
'''Tell us if the file's changed since we last looked, and update the last
seen mtime.'''
current_mtime = self.check_mtime()
status = False
if current_mtime > self.last_mtime:
self.last_mtime = current_mtime
status = True
return status
def store_latest_values_in_model(self):
# TODO: Extract the latest values from self.values, and store them
# in the Django model.
pass
def get_latest_values_from_model(self):
# TODO: Return the values from the model.
pass
def draw_graphs(self):
# TODO: Make the graphs using self.values, and write them to files.
pass
def check_mtime(self):
# TODO: Get the mtime from the file.
pass
if __name__ == '__main__':
# Example of how to use this in the view:
value_cache = ValueCache(pickle_file)
values_to_print = value_cache.update_values_and_graphs()