Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fda3285

Browse files
committed
warnings fixes
1 parent 1af4b42 commit fda3285

File tree

3 files changed

+68
-57
lines changed

3 files changed

+68
-57
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@
5252
"Programming Language :: Python :: 3.4",
5353
],
5454
long_description=long_description,
55+
tests_require=('nose', 'nosexcover'),
56+
test_suite='nose.collector'
5557
)

smmap/test/test_buf.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import with_statement, print_function
2+
13
from .lib import TestBase, FileCreator
24

35
from smmap.mman import SlidingWindowMapManager, StaticWindowMapManager
@@ -17,65 +19,66 @@
1719
static_man = StaticWindowMapManager()
1820

1921
class TestBuf(TestBase):
20-
22+
2123
def test_basics(self):
2224
fc = FileCreator(self.k_window_test_size, "buffer_test")
23-
25+
2426
# invalid paths fail upon construction
2527
c = man_optimal.make_cursor(fc.path)
2628
self.assertRaises(ValueError, SlidingWindowMapBuffer, type(c)()) # invalid cursor
2729
self.assertRaises(ValueError, SlidingWindowMapBuffer, c, fc.size) # offset too large
28-
30+
2931
buf = SlidingWindowMapBuffer() # can create uninitailized buffers
3032
assert buf.cursor() is None
31-
33+
3234
# can call end access any time
3335
buf.end_access()
3436
buf.end_access()
3537
assert len(buf) == 0
36-
38+
3739
# begin access can revive it, if the offset is suitable
3840
offset = 100
3941
assert buf.begin_access(c, fc.size) == False
4042
assert buf.begin_access(c, offset) == True
4143
assert len(buf) == fc.size - offset
4244
assert buf.cursor().is_valid()
43-
45+
4446
# empty begin access keeps it valid on the same path, but alters the offset
4547
assert buf.begin_access() == True
4648
assert len(buf) == fc.size
4749
assert buf.cursor().is_valid()
48-
50+
4951
# simple access
50-
data = open(fc.path, 'rb').read()
52+
with open(fc.path, 'rb') as fp:
53+
data = fp.read()
5154
assert data[offset] == buf[0]
5255
assert data[offset:offset*2] == buf[0:offset]
53-
56+
5457
# negative indices, partial slices
5558
assert buf[-1] == buf[len(buf)-1]
5659
assert buf[-10:] == buf[len(buf)-10:len(buf)]
57-
60+
5861
# end access makes its cursor invalid
5962
buf.end_access()
6063
assert not buf.cursor().is_valid()
6164
assert buf.cursor().is_associated() # but it remains associated
62-
65+
6366
# an empty begin access fixes it up again
6467
assert buf.begin_access() == True and buf.cursor().is_valid()
6568
del(buf) # ends access automatically
6669
del(c)
67-
70+
6871
assert man_optimal.num_file_handles() == 1
69-
72+
7073
# PERFORMANCE
71-
# blast away with rnadom access and a full mapping - we don't want to
74+
# blast away with rnadom access and a full mapping - we don't want to
7275
# exagerate the manager's overhead, but measure the buffer overhead
73-
# We do it once with an optimal setting, and with a worse manager which
76+
# We do it once with an optimal setting, and with a worse manager which
7477
# will produce small mappings only !
7578
max_num_accesses = 100
7679
fd = os.open(fc.path, os.O_RDONLY)
7780
for item in (fc.path, fd):
78-
for manager, man_id in ( (man_optimal, 'optimal'),
81+
for manager, man_id in ( (man_optimal, 'optimal'),
7982
(man_worst_case, 'worst case'),
8083
(static_man, 'static optimal')):
8184
buf = SlidingWindowMapBuffer(manager.make_cursor(item))
@@ -84,7 +87,7 @@ def test_basics(self):
8487
num_accesses_left = max_num_accesses
8588
num_bytes = 0
8689
fsize = fc.size
87-
90+
8891
st = time()
8992
buf.begin_access()
9093
while num_accesses_left:
@@ -102,16 +105,17 @@ def test_basics(self):
102105
num_bytes += 1
103106
#END handle mode
104107
# END handle num accesses
105-
108+
106109
buf.end_access()
107110
assert manager.num_file_handles()
108111
assert manager.collect()
109112
assert manager.num_file_handles() == 0
110113
elapsed = max(time() - st, 0.001) # prevent zero division errors on windows
111114
mb = float(1000*1000)
112115
mode_str = (access_mode and "slice") or "single byte"
113-
sys.stderr.write("%s: Made %i random %s accesses to buffer created from %s reading a total of %f mb in %f s (%f mb/s)\n"
114-
% (man_id, max_num_accesses, mode_str, type(item), num_bytes/mb, elapsed, (num_bytes/mb)/elapsed))
116+
print("%s: Made %i random %s accesses to buffer created from %s reading a total of %f mb in %f s (%f mb/s)"
117+
% (man_id, max_num_accesses, mode_str, type(item), num_bytes/mb, elapsed, (num_bytes/mb)/elapsed),
118+
file=sys.stderr)
115119
# END handle access mode
116120
# END for each manager
117121
# END for each input

smmap/test/test_mman.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import with_statement, print_function
2+
13
from .lib import TestBase, FileCreator
24

35
from smmap.mman import *
@@ -12,43 +14,43 @@
1214
from copy import copy
1315

1416
class TestMMan(TestBase):
15-
17+
1618
def test_cursor(self):
1719
fc = FileCreator(self.k_window_test_size, "cursor_test")
18-
20+
1921
man = SlidingWindowMapManager()
2022
ci = WindowCursor(man) # invalid cursor
2123
assert not ci.is_valid()
2224
assert not ci.is_associated()
2325
assert ci.size() == 0 # this is cached, so we can query it in invalid state
24-
26+
2527
cv = man.make_cursor(fc.path)
2628
assert not cv.is_valid() # no region mapped yet
2729
assert cv.is_associated()# but it know where to map it from
2830
assert cv.file_size() == fc.size
2931
assert cv.path() == fc.path
30-
32+
3133
# copy module
3234
cio = copy(cv)
3335
assert not cio.is_valid() and cio.is_associated()
34-
36+
3537
# assign method
3638
assert not ci.is_associated()
3739
ci.assign(cv)
3840
assert not ci.is_valid() and ci.is_associated()
39-
41+
4042
# unuse non-existing region is fine
4143
cv.unuse_region()
4244
cv.unuse_region()
43-
45+
4446
# destruction is fine (even multiple times)
4547
cv._destroy()
4648
WindowCursor(man)._destroy()
47-
49+
4850
def test_memory_manager(self):
4951
slide_man = SlidingWindowMapManager()
5052
static_man = StaticWindowMapManager()
51-
53+
5254
for man in (static_man, slide_man):
5355
assert man.num_file_handles() == 0
5456
assert man.num_open_files() == 0
@@ -59,15 +61,15 @@ def test_memory_manager(self):
5961
assert man.window_size() > winsize_cmp_val
6062
assert man.mapped_memory_size() == 0
6163
assert man.max_mapped_memory_size() > 0
62-
64+
6365
# collection doesn't raise in 'any' mode
6466
man._collect_lru_region(0)
6567
# doesn't raise if we are within the limit
6668
man._collect_lru_region(10)
67-
68-
# doesn't fail if we overallocate
69+
70+
# doesn't fail if we overallocate
6971
assert man._collect_lru_region(sys.maxsize) == 0
70-
72+
7173
# use a region, verify most basic functionality
7274
fc = FileCreator(self.k_window_test_size, "manager_test")
7375
fd = os.open(fc.path, os.O_RDONLY)
@@ -77,8 +79,9 @@ def test_memory_manager(self):
7779
assert c.use_region(10, 10).is_valid()
7880
assert c.ofs_begin() == 10
7981
assert c.size() == 10
80-
assert c.buffer()[:] == open(fc.path, 'rb').read(20)[10:]
81-
82+
with open(fc.path, 'rb') as fp:
83+
assert c.buffer()[:] == fp.read(20)[10:]
84+
8285
if isinstance(item, int):
8386
self.assertRaises(ValueError, c.path)
8487
else:
@@ -87,38 +90,39 @@ def test_memory_manager(self):
8790
#END for each input
8891
os.close(fd)
8992
# END for each manager type
90-
93+
9194
def test_memman_operation(self):
9295
# test more access, force it to actually unmap regions
9396
fc = FileCreator(self.k_window_test_size, "manager_operation_test")
94-
data = open(fc.path, 'rb').read()
97+
with open(fc.path, 'rb') as fp:
98+
data = fp.read()
9599
fd = os.open(fc.path, os.O_RDONLY)
96100
max_num_handles = 15
97-
#small_size =
101+
#small_size =
98102
for mtype, args in ( (StaticWindowMapManager, (0, fc.size // 3, max_num_handles)),
99103
(SlidingWindowMapManager, (fc.size // 100, fc.size // 3, max_num_handles)),):
100104
for item in (fc.path, fd):
101105
assert len(data) == fc.size
102-
106+
103107
# small windows, a reasonable max memory. Not too many regions at once
104108
man = mtype(window_size=args[0], max_memory_size=args[1], max_open_handles=args[2])
105109
c = man.make_cursor(item)
106-
110+
107111
# still empty (more about that is tested in test_memory_manager()
108112
assert man.num_open_files() == 0
109113
assert man.mapped_memory_size() == 0
110-
114+
111115
base_offset = 5000
112116
# window size is 0 for static managers, hence size will be 0. We take that into consideration
113117
size = man.window_size() // 2
114118
assert c.use_region(base_offset, size).is_valid()
115119
rr = c.region_ref()
116120
assert rr().client_count() == 2 # the manager and the cursor and us
117-
121+
118122
assert man.num_open_files() == 1
119123
assert man.num_file_handles() == 1
120124
assert man.mapped_memory_size() == rr().size()
121-
125+
122126
#assert c.size() == size # the cursor may overallocate in its static version
123127
assert c.ofs_begin() == base_offset
124128
assert rr().ofs_begin() == 0 # it was aligned and expanded
@@ -127,9 +131,9 @@ def test_memman_operation(self):
127131
else:
128132
assert rr().size() == fc.size
129133
#END ignore static managers which dont use windows and are aligned to file boundaries
130-
131-
assert c.buffer()[:] == data[base_offset:base_offset+(size or c.size())]
132-
134+
135+
assert c.buffer()[:] == data[base_offset:base_offset+(size or c.size())]
136+
133137
# obtain second window, which spans the first part of the file - it is a still the same window
134138
nsize = (size or fc.size) - 10
135139
assert c.use_region(0, nsize).is_valid()
@@ -138,7 +142,7 @@ def test_memman_operation(self):
138142
assert c.size() == nsize
139143
assert c.ofs_begin() == 0
140144
assert c.buffer()[:] == data[:nsize]
141-
145+
142146
# map some part at the end, our requested size cannot be kept
143147
overshoot = 4000
144148
base_offset = fc.size - (size or c.size()) + overshoot
@@ -156,23 +160,23 @@ def test_memman_operation(self):
156160
assert rr().ofs_begin() < c.ofs_begin() # it should have extended itself to the left
157161
assert rr().ofs_end() <= fc.size # it cannot be larger than the file
158162
assert c.buffer()[:] == data[base_offset:base_offset+(size or c.size())]
159-
163+
160164
# unising a region makes the cursor invalid
161165
c.unuse_region()
162166
assert not c.is_valid()
163167
if man.window_size():
164-
# but doesn't change anything regarding the handle count - we cache it and only
168+
# but doesn't change anything regarding the handle count - we cache it and only
165169
# remove mapped regions if we have to
166170
assert man.num_file_handles() == 2
167171
#END ignore this for static managers
168-
172+
169173
# iterate through the windows, verify data contents
170174
# this will trigger map collection after a while
171175
max_random_accesses = 5000
172176
num_random_accesses = max_random_accesses
173177
memory_read = 0
174178
st = time()
175-
179+
176180
# cache everything to get some more performance
177181
includes_ofs = c.includes_ofs
178182
max_mapped_memory_size = man.max_mapped_memory_size()
@@ -182,7 +186,7 @@ def test_memman_operation(self):
182186
while num_random_accesses:
183187
num_random_accesses -= 1
184188
base_offset = randint(0, fc.size - 1)
185-
189+
186190
# precondition
187191
if man.window_size():
188192
assert max_mapped_memory_size >= mapped_memory_size()
@@ -192,19 +196,20 @@ def test_memman_operation(self):
192196
csize = c.size()
193197
assert c.buffer()[:] == data[base_offset:base_offset+csize]
194198
memory_read += csize
195-
199+
196200
assert includes_ofs(base_offset)
197201
assert includes_ofs(base_offset+csize-1)
198202
assert not includes_ofs(base_offset+csize)
199203
# END while we should do an access
200204
elapsed = max(time() - st, 0.001) # prevent zero divison errors on windows
201205
mb = float(1000 * 1000)
202-
sys.stderr.write("%s: Read %i mb of memory with %i random on cursor initialized with %s accesses in %fs (%f mb/s)\n"
203-
% (mtype, memory_read/mb, max_random_accesses, type(item), elapsed, (memory_read/mb)/elapsed))
204-
206+
print("%s: Read %i mb of memory with %i random on cursor initialized with %s accesses in %fs (%f mb/s)\n"
207+
% (mtype, memory_read/mb, max_random_accesses, type(item), elapsed, (memory_read/mb)/elapsed),
208+
file=sys.stderr)
209+
205210
# an offset as large as the size doesn't work !
206211
assert not c.use_region(fc.size, size).is_valid()
207-
212+
208213
# collection - it should be able to collect all
209214
assert man.num_file_handles()
210215
assert man.collect()

0 commit comments

Comments
 (0)