Skip to content

Commit 1d502c0

Browse files
committed
factor out counting dict
1 parent 89c1ea3 commit 1d502c0

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

zarr/tests/test_attrs.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import absolute_import, print_function, division
33
import json
44
import unittest
5-
import collections
65

76

87
from nose.tools import eq_ as eq, assert_raises
@@ -11,34 +10,7 @@
1110
from zarr.attrs import Attributes
1211
from zarr.compat import binary_type, text_type
1312
from zarr.errors import PermissionError
14-
15-
16-
class CountingDict(collections.MutableMapping):
17-
18-
def __init__(self):
19-
self.wrapped = dict()
20-
self.counter = collections.Counter()
21-
22-
def __len__(self):
23-
return len(self.wrapped)
24-
25-
def __iter__(self):
26-
return iter(self.wrapped)
27-
28-
def __contains__(self, item):
29-
return item in self.wrapped
30-
31-
def __getitem__(self, item):
32-
self.counter['__getitem__', item] += 1
33-
return self.wrapped[item]
34-
35-
def __setitem__(self, key, value):
36-
self.counter['__setitem__', key] += 1
37-
self.wrapped[key] = value
38-
39-
def __delitem__(self, key):
40-
self.counter['__delitem__', key] += 1
41-
del self.wrapped[key]
13+
from zarr.tests.util import CountingDict
4214

4315

4416
class TestAttributes(unittest.TestCase):

zarr/tests/util.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import, print_function, division
3+
import collections
4+
5+
6+
class CountingDict(collections.MutableMapping):
7+
8+
def __init__(self):
9+
self.wrapped = dict()
10+
self.counter = collections.Counter()
11+
12+
def __len__(self):
13+
return len(self.wrapped)
14+
15+
def __iter__(self):
16+
return iter(self.wrapped)
17+
18+
def __contains__(self, item):
19+
return item in self.wrapped
20+
21+
def __getitem__(self, item):
22+
self.counter['__getitem__', item] += 1
23+
return self.wrapped[item]
24+
25+
def __setitem__(self, key, value):
26+
self.counter['__setitem__', key] += 1
27+
self.wrapped[key] = value
28+
29+
def __delitem__(self, key):
30+
self.counter['__delitem__', key] += 1
31+
del self.wrapped[key]

0 commit comments

Comments
 (0)