Package gmisclib :: Module gpk_writer
[frames] | no frames]

Source Code for Module gmisclib.gpk_writer

1 -class writer(object):
2 - def comments(self, comments):
3 """Add comments to the data file. 4 Comments can appear anywhere. 5 """ 6 for comment in comments: 7 self.comment(comment)
8
9 - def comment(self, comment):
10 raise RuntimeError, "Virtual Function"
11
12 - def header(self, k, v):
13 raise RuntimeError, "Virtual Function"
14
15 - def headers(self, h):
16 hdritems = h.items() 17 hdritems.sort() 18 for (k, v) in hdritems: 19 self.header(k, v)
20 21
22 - def __init__(self, fd):
23 self.fd = fd
24
25 - def data(self, dataset):
26 """Write a series of lines to the output file.""" 27 for datum in dataset: 28 self.datum(datum)
29
30 - def extend(self, d):
31 self.data(d)
32
33 - def append(self, d):
34 self.datum(d)
35
36 - def datum(self, data_item):
37 raise RuntimeError, "Virtual Function"
38 39
40 - def flush(self):
41 if hasattr(self.fd, 'flush'): 42 self.fd.flush()
43
44 - def close(self):
45 # Don't explicitly close it, as self.fd 46 # could be sys.stdout. We just de-refererence 47 # it, so that it automatically closes if nothing 48 # else keeps it open. 49 if self.fd: 50 self.flush() 51 self.fd = None
52 53 54
55 -class null_writer(writer):
56 - def comments(self, comments):
57 pass
58
59 - def comment(self, comment):
60 pass
61
62 - def header(self, k, v):
63 pass
64
65 - def headers(self, h):
66 pass
67
68 - def __init__(self):
69 pass
70
71 - def data(self, dataset):
72 pass
73
74 - def datum(self, data_item):
75 pass
76 77 # def __del__(self): 78 # self.close() 79
80 - def flush(self):
81 pass
82
83 - def close(self):
84 pass
85