Modernize code to Python 3.6+ and some cleanup

This commit is contained in:
Miroslav Šedivý
2021-01-31 22:48:38 +01:00
parent 93bb4407fa
commit b69e0c2023
86 changed files with 153 additions and 189 deletions

View File

@@ -16,7 +16,7 @@ with open(sys.argv[1], encoding='utf-8') as fp:
for line_no, line in enumerate(fp, 1):
for match in WORD_RE.finditer(line):
word = match.group()
column_no = match.start()+1
column_no = match.start() + 1
location = (line_no, column_no)
index[word].append(location) # <2>

View File

@@ -41,15 +41,15 @@ def test(container_type, verbose):
size=size, verbose=verbose)
test = TEST.format(verbose=verbose)
tt = timeit.repeat(stmt=test, setup=setup, repeat=5, number=1)
print('|{:{}d}|{:f}'.format(size, MAX_EXPONENT + 1, min(tt)))
print(f'|{size:{MAX_EXPONENT + 1}d}|{min(tt):f}')
if __name__=='__main__':
if __name__ == '__main__':
if '-v' in sys.argv:
sys.argv.remove('-v')
verbose = True
else:
verbose = False
if len(sys.argv) != 2:
print('Usage: %s <container_type>' % sys.argv[0])
print(f'Usage: {sys.argv[0]} <container_type>')
else:
test(sys.argv[1], verbose)

View File

@@ -2,8 +2,8 @@
Generate data for container performance test
"""
import random
import array
import random
MAX_EXPONENT = 7
HAYSTACK_LEN = 10 ** MAX_EXPONENT
@@ -12,26 +12,26 @@ SAMPLE_LEN = HAYSTACK_LEN + NEEDLES_LEN // 2
needles = array.array('d')
sample = {1/random.random() for i in range(SAMPLE_LEN)}
print('initial sample: %d elements' % len(sample))
sample = {1 / random.random() for i in range(SAMPLE_LEN)}
print(f'initial sample: {len(sample)} elements')
# complete sample, in case duplicate random numbers were discarded
while len(sample) < SAMPLE_LEN:
sample.add(1/random.random())
sample.add(1 / random.random())
print('complete sample: %d elements' % len(sample))
print(f'complete sample: {len(sample)} elements')
sample = array.array('d', sample)
random.shuffle(sample)
not_selected = sample[:NEEDLES_LEN // 2]
print('not selected: %d samples' % len(not_selected))
print(f'not selected: {len(not_selected)} samples')
print(' writing not_selected.arr')
with open('not_selected.arr', 'wb') as fp:
not_selected.tofile(fp)
selected = sample[NEEDLES_LEN // 2:]
print('selected: %d samples' % len(selected))
print(f'selected: {len(selected)} samples')
print(' writing selected.arr')
with open('selected.arr', 'wb') as fp:
selected.tofile(fp)

View File

@@ -1,17 +1,17 @@
import sys
MAX_BITS = len(format(sys.maxsize, 'b'))
print('%s-bit Python build' % (MAX_BITS + 1))
print(f'{MAX_BITS + 1}-bit Python build')
def hash_diff(o1, o2):
h1 = '{:>0{}b}'.format(hash(o1), MAX_BITS)
h2 = '{:>0{}b}'.format(hash(o2), MAX_BITS)
h1 = f'{hash(o1):>0{MAX_BITS}b}'
h2 = f'{hash(o2):>0{MAX_BITS}b}'
diff = ''.join('!' if b1 != b2 else ' ' for b1, b2 in zip(h1, h2))
count = '!= {}'.format(diff.count('!'))
count = f'!= {diff.count("!")}'
width = max(len(repr(o1)), len(repr(o2)), 8)
sep = '-' * (width * 2 + MAX_BITS)
return '{!r:{width}} {}\n{:{width}} {} {}\n{!r:{width}} {}\n{}'.format(
o1, h1, ' ' * width, diff, count, o2, h2, sep, width=width)
return (f'{o1!r:{width}} {h1}\n{" ":{width}} {diff} {count}\n'
f'{o2!r:{width}} {h2}\n{sep}')
if __name__ == '__main__':
print(hash_diff(1, 1.0))

View File

@@ -17,7 +17,7 @@ _sentinel = object()
class TransformDict(MutableMapping):
'''Dictionary that calls a transformation function when looking
"""Dictionary that calls a transformation function when looking
up keys, but preserves the original keys.
>>> d = TransformDict(str.lower)
@@ -26,18 +26,18 @@ class TransformDict(MutableMapping):
True
>>> set(d.keys())
{'Foo'}
'''
"""
__slots__ = ('_transform', '_original', '_data')
def __init__(self, transform, init_dict=None, **kwargs):
'''Create a new TransformDict with the given *transform* function.
"""Create a new TransformDict with the given *transform* function.
*init_dict* and *kwargs* are optional initializers, as in the
dict constructor.
'''
"""
if not callable(transform):
msg = 'expected a callable, got %r'
raise TypeError(msg % transform.__class__)
raise TypeError(
f'expected a callable, got {transform.__class__!r}')
self._transform = transform
# transformed => original
self._original = {}
@@ -48,7 +48,7 @@ class TransformDict(MutableMapping):
self.update(kwargs)
def getitem(self, key):
'D.getitem(key) -> (stored key, value)'
"""D.getitem(key) -> (stored key, value)"""
transformed = self._transform(key)
original = self._original[transformed]
value = self._data[transformed]
@@ -56,7 +56,7 @@ class TransformDict(MutableMapping):
@property
def transform_func(self):
"This TransformDict's transformation function"
"""This TransformDict's transformation function"""
return self._transform
# Minimum set of methods required for MutableMapping
@@ -83,7 +83,7 @@ class TransformDict(MutableMapping):
# Methods overridden to mitigate the performance overhead.
def clear(self):
'D.clear() -> None. Remove all items from D.'
"""D.clear() -> None. Remove all items from D."""
self._data.clear()
self._original.clear()
@@ -91,14 +91,14 @@ class TransformDict(MutableMapping):
return self._transform(key) in self._data
def get(self, key, default=None):
'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
"""D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None."""
return self._data.get(self._transform(key), default)
def pop(self, key, default=_sentinel):
'''D.pop(k[,d]) -> v, remove key and return corresponding value.
"""D.pop(k[,d]) -> v, remove key and return corresponding value.
If key is not found, d is returned if given, otherwise
KeyError is raised.
'''
"""
transformed = self._transform(key)
if default is _sentinel:
del self._original[transformed]
@@ -108,16 +108,16 @@ class TransformDict(MutableMapping):
return self._data.pop(transformed, default)
def popitem(self):
'''D.popitem() -> (k, v), remove and return some (key, value) pair
"""D.popitem() -> (k, v), remove and return some (key, value) pair
as a 2-tuple; but raise KeyError if D is empty.
'''
"""
transformed, value = self._data.popitem()
return self._original.pop(transformed), value
# Other methods
def copy(self):
'D.copy() -> a shallow copy of D'
"""D.copy() -> a shallow copy of D"""
other = self.__class__(self._transform)
other._original = self._original.copy()
other._data = self._data.copy()
@@ -137,5 +137,4 @@ class TransformDict(MutableMapping):
except TypeError:
# Some keys are unhashable, fall back on .items()
equiv = list(self.items())
return '%s(%r, %s)' % (self.__class__.__name__,
self._transform, repr(equiv))
return f'{self.__class__.__name__}({self._transform!r}, {equiv!r})'