forked from daleathan/ProgrammingInPython3-MarkSummerfield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryRecordFile_ans.py
More file actions
executable file
·176 lines (141 loc) · 5.12 KB
/
BinaryRecordFile_ans.py
File metadata and controls
executable file
·176 lines (141 loc) · 5.12 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
"""
>>> import shutil
>>> import sys
>>> S = struct.Struct("<15s")
>>> fileA = os.path.join(tempfile.gettempdir(), "fileA.dat")
>>> fileB = os.path.join(tempfile.gettempdir(), "fileB.dat")
>>> for name in (fileA, fileB):
... try:
... os.remove(name)
... except EnvironmentError:
... pass
>>> brf = BinaryRecordFile(fileA, S.size)
>>> for text in ("Alpha", "Bravo", "Charlie", "Delta",
... "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet",
... "Kilo", "Lima", "Mike", "November", "Oscar", "Papa",
... "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor",
... "Whisky", "X-Ray", "Yankee", "Zulu"):
... brf.append(S.pack(text.encode("utf8")))
>>> assert len(brf) == 26
>>> brf.append(S.pack(b"Extra at the end"))
>>> assert len(brf) == 27
>>> shutil.copy(fileA, fileB)
>>> del brf[12]
>>> del brf[0]
>>> del brf[24]
>>> assert len(brf) == 24, len(brf)
>>> brf.close()
>>> if ((os.path.getsize(fileA) + (3 * S.size)) !=
... os.path.getsize(fileB)):
... print("FAIL#1: expected file sizes are wrong")
... sys.exit()
>>> shutil.copy(fileB, fileA)
>>> if os.path.getsize(fileA) != os.path.getsize(fileB):
... print("FAIL#2: expected file sizes differ")
... sys.exit()
>>> for name in (fileA, fileB):
... try:
... os.remove(name)
... except EnvironmentError:
... pass
"""
import os
import struct
import tempfile
class BinaryRecordFile:
def __init__(self, filename, record_size, auto_flush=True):
"""A random access binary file that behaves rather like a list
with each item a bytes or bytesarray object of record_size.
"""
self.__record_size = record_size
mode = "w+b" if not os.path.exists(filename) else "r+b"
self.__fh = open(filename, mode)
self.auto_flush = auto_flush
@property
def record_size(self):
"The size of each item"
return self.__record_size
@property
def name(self):
"The name of the file"
return self.__fh.name
def flush(self):
"""Flush writes to disk
Done automatically if auto_flush is True
"""
self.__fh.flush()
def close(self):
self.__fh.close()
def append(self, record):
"""Adds a new record"""
assert isinstance(record, (bytes, bytearray)), \
"binary data required"
assert len(record) == self.record_size, (
"record must be exactly {0} bytes".format(
self.record_size))
self.__fh.seek(0, os.SEEK_END)
self.__fh.write(record)
if self.auto_flush:
self.__fh.flush()
def __setitem__(self, index, record):
"""Sets the item at position index to be the given record
The index position can be beyond the current end of the file.
"""
assert isinstance(record, (bytes, bytearray)), \
"binary data required"
assert len(record) == self.record_size, (
"record must be exactly {0} bytes".format(
self.record_size))
self.__seek_to_index(index)
self.__fh.write(record)
if self.auto_flush:
self.__fh.flush()
def __getitem__(self, index):
"""Returns the item at the given index position
If there is no item at the given position, raises an
IndexError exception.
If the item at the given position has been deleted returns
None.
"""
self.__seek_to_index(index)
return self.__fh.read(self.record_size)
def __seek_to_index(self, index):
if self.auto_flush:
self.__fh.flush()
self.__fh.seek(0, os.SEEK_END)
end = self.__fh.tell()
offset = index * self.record_size
if offset >= end:
raise IndexError("no record at index position {0}".format(
index))
self.__fh.seek(offset)
def __delitem__(self, index):
"""Deletes the item at the given index position and moves the
following records up.
"""
length = len(self)
for following in range(index + 1, length):
self[index] = self[following]
index += 1
self.__fh.truncate((length - 1) * self.record_size)
self.__fh.flush()
def __len__(self):
"""The number number of records."""
if self.auto_flush:
self.__fh.flush()
self.__fh.seek(0, os.SEEK_END)
end = self.__fh.tell()
return end // self.record_size
if __name__ == "__main__":
import doctest
doctest.testmod()