Skip to content

Commit

Permalink
Split long lines of code
Browse files Browse the repository at this point in the history
  • Loading branch information
brainix committed Jul 6, 2018
1 parent c173cfe commit aad17c6
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 29 deletions.
18 changes: 15 additions & 3 deletions pottery/deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,33 @@ def _populate(self, iterable=tuple()):
raise TypeError('an integer is required')
super()._populate(iterable)
if not iterable and self.maxlen is not None and len(self) > self.maxlen:
raise IndexError('persistent {} beyond its maximum size'.format(self.__class__.__name__))
raise IndexError(
'persistent {} beyond its maximum size'.format(
self.__class__.__name__,
),
)

@property
def maxlen(self):
return self._maxlen

@maxlen.setter
def maxlen(self, value):
raise AttributeError("attribute 'maxlen' of '{}' objects is not writable".format(self.__class__.__name__))
raise AttributeError(
"attribute 'maxlen' of '{}' objects is not writable".format(
self.__class__.__name__,
),
)

def insert(self, index, value):
'Insert an element into a RedisDeque before the given index. O(n)'
with self._watch():
if self.maxlen is not None and len(self) >= self.maxlen:
raise IndexError('{} already at its maximum size'.format(self.__class__.__name__))
raise IndexError(
'{} already at its maximum size'.format(
self.__class__.__name__,
),
)
else:
return super()._insert(index, value)

Expand Down
10 changes: 8 additions & 2 deletions pottery/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def pop(self, index=None):
getattr(self.redis, pop_method)(self.key)
encoded_value = self.redis.execute()[0]
if encoded_value is None:
raise IndexError('pop from an empty {}'.format(self.__class__.__name__))
raise IndexError(
'pop from an empty {}'.format(self.__class__.__name__),
)
else:
return self._decode(encoded_value)
else:
Expand All @@ -253,4 +255,8 @@ def remove(self, value):
self._delete(index)
break
else:
raise ValueError('{class_}.remove(x): x not in {class_}'.format(class_=self.__class__.__name__))
raise ValueError(
'{class_}.remove(x): x not in {class_}'.format(
class_=self.__class__.__name__,
),
)
3 changes: 2 additions & 1 deletion pottery/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def __eq__(self, other):
this method on to the Redis client so that two client instances are equal
if they're connected to the same Redis host, port, and database.
'''
equals = isinstance(other, Redis) and self._connection() == other._connection()
equals = isinstance(other, Redis) and \
self._connection() == other._connection()
return equals

def __ne__(self, other):
Expand Down
12 changes: 9 additions & 3 deletions pottery/nextid.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def _register_set_id_script(self):
return set_id_script

def _init_masters(self):
with concurrent.futures.ThreadPoolExecutor(max_workers=len(self.masters)) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=len(self.masters),
) as executor:
for master in self.masters:
executor.submit(master.setnx, self.key, 0)

Expand All @@ -112,7 +114,9 @@ def __repr__(self):
@property
def _current_id(self):
futures, current_id, num_masters_gotten = set(), 0, 0
with concurrent.futures.ThreadPoolExecutor(max_workers=len(self.masters)) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=len(self.masters),
) as executor:
for master in self.masters:
futures.add(executor.submit(master.get, self.key))
for future in concurrent.futures.as_completed(futures):
Expand All @@ -127,7 +131,9 @@ def _current_id(self):
@_current_id.setter
def _current_id(self, value):
futures, num_masters_set = set(), 0
with concurrent.futures.ThreadPoolExecutor(max_workers=len(self.masters)) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=len(self.masters),
) as executor:
for master in self.masters:
future = executor.submit(
self._set_id_script,
Expand Down
19 changes: 14 additions & 5 deletions pottery/redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,17 @@ def _acquire_masters(self):
self._value, self._extension_num = random.random(), 0
futures, num_masters_acquired = set(), 0
with ContextTimer() as timer, \
concurrent.futures.ThreadPoolExecutor(max_workers=len(self.masters)) as executor:
concurrent.futures.ThreadPoolExecutor(
max_workers=len(self.masters),
) as executor:
for master in self.masters:
futures.add(executor.submit(self._acquire_master, master))
for future in concurrent.futures.as_completed(futures):
with contextlib.suppress(TimeoutError, ConnectionError):
num_masters_acquired += future.result()
quorum = num_masters_acquired >= len(self.masters) // 2 + 1
validity_time = self.auto_release_time - timer.elapsed() - self._drift()
elapsed = timer.elapsed() - self._drift()
validity_time = self.auto_release_time - elapsed
if quorum and max(validity_time, 0):
return True
else:
Expand Down Expand Up @@ -308,7 +311,9 @@ def locked(self):
'''
futures, num_masters_acquired, ttls = set(), 0, []
with ContextTimer() as timer, \
concurrent.futures.ThreadPoolExecutor(max_workers=len(self.masters)) as executor:
concurrent.futures.ThreadPoolExecutor(
max_workers=len(self.masters),
) as executor:
for master in self.masters:
futures.add(executor.submit(self._acquired_master, master))
for future in concurrent.futures.as_completed(futures):
Expand Down Expand Up @@ -348,7 +353,9 @@ def extend(self):
raise TooManyExtensions(self.masters, self.key)
else:
futures, num_masters_extended = set(), 0
with concurrent.futures.ThreadPoolExecutor(max_workers=len(self.masters)) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=len(self.masters),
) as executor:
for master in self.masters:
futures.add(executor.submit(self._extend_master, master))
for future in concurrent.futures.as_completed(futures):
Expand All @@ -375,7 +382,9 @@ def release(self):
False
'''
futures, num_masters_released = set(), 0
with concurrent.futures.ThreadPoolExecutor(max_workers=len(self.masters)) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=len(self.masters),
) as executor:
for master in self.masters:
futures.add(executor.submit(self._release_master, master))
for future in concurrent.futures.as_completed(futures):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def setUp(self):
self.raj = RedisDict(key='pottery:raj', hobby='music', vegetarian=True)

self.redis.delete('pottery:nilika')
self.nilika = RedisDict(key='pottery:nilika', hobby='music', vegetarian=True)
self.nilika = RedisDict(
key='pottery:nilika',
hobby='music',
vegetarian=True,
)

self.redis.delete('luvh')
self.luvh = RedisDict(key='luvh', hobby='bullying', vegetarian=False)
Expand Down
13 changes: 10 additions & 3 deletions tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def test_basic_usage(self):
assert squares[-1] == 25
assert squares[-3:] == [9, 16, 25]
assert squares[:] == [1, 4, 9, 16, 25]
assert squares + [36, 49, 64, 81, 100] == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
assert squares + [36, 49, 64, 81, 100] == \
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

def test_mutability_and_append(self):
cubes = RedisList((1, 8, 27, 65, 125))
Expand Down Expand Up @@ -179,10 +180,16 @@ def test_pop_out_of_range(self):
squares.pop(len(squares))

def test_pop_index(self):
metasyntactic = RedisList(('foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply', 'waldo', 'fred', 'plugh', 'xyzzy', 'thud'))
metasyntactic = RedisList((
'foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply',
'waldo', 'fred', 'plugh', 'xyzzy', 'thud',
))
assert metasyntactic.pop(1) == 'bar'

def test_remove_nonexistent(self):
metasyntactic = RedisList(('foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply', 'waldo', 'fred', 'plugh', 'xyzzy', 'thud'))
metasyntactic = RedisList((
'foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply',
'waldo', 'fred', 'plugh', 'xyzzy', 'thud',
))
with self.assertRaises(ValueError):
metasyntactic.remove('raj')
10 changes: 8 additions & 2 deletions tests/test_nextid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ def test_iter(self):

def test_next(self):
with self.assertRaises(QuorumNotAchieved), \
unittest.mock.patch.object(next(iter(self.ids.masters)), 'get') as get:
unittest.mock.patch.object(
next(iter(self.ids.masters)),
'get',
) as get:
get.side_effect = TimeoutError
next(self.ids)

with self.assertRaises(QuorumNotAchieved), \
unittest.mock.patch.object(self.ids, '_set_id_script') as _set_id_script:
unittest.mock.patch.object(
self.ids,
'_set_id_script',
) as _set_id_script:
_set_id_script.side_effect = TimeoutError
next(self.ids)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,5 @@ def test_context_manager_release_before_exit(self):
assert not self.redis.exists(self.redlock.key)

def test_repr(self):
assert repr(self.redlock) == '<Redlock key=redlock:printer value=None timeout=0>'
assert repr(self.redlock) == \
'<Redlock key=redlock:printer value=None timeout=0>'
16 changes: 12 additions & 4 deletions tests/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ def test_basic_usage(self):
assert not 'crabgrass' in basket

def test_add(self):
basket = RedisSet({'apple', 'orange', 'apple', 'pear', 'orange', 'banana'})
basket = RedisSet({
'apple', 'orange', 'apple', 'pear', 'orange', 'banana',
})
basket.add('tomato')
assert basket == {'apple', 'orange', 'apple', 'pear', 'orange', 'banana', 'tomato'}
assert basket == {
'apple', 'orange', 'apple', 'pear', 'orange', 'banana', 'tomato',
}

def test_discard(self):
basket = RedisSet({'apple', 'orange', 'apple', 'pear', 'orange', 'banana', 'tomato'})
basket = RedisSet({
'apple', 'orange', 'apple', 'pear', 'orange', 'banana', 'tomato',
})
basket.discard('tomato')
assert basket == {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
assert basket == {
'apple', 'orange', 'apple', 'pear', 'orange', 'banana',
}

def test_repr(self):
basket = RedisSet({'apple'})
Expand Down
11 changes: 7 additions & 4 deletions tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ def tearDown(self):

def test_imports(self):
test_dir = os.path.dirname(__file__)
test_files = (f for f in os.listdir(test_dir, absolute=True)
if f.endswith('.py'))
test_files = (
f for f in os.listdir(test_dir, absolute=True) if f.endswith('.py')
)

source_dir = os.path.dirname(test_dir)
source_files = (f for f in os.listdir(source_dir, absolute=True)
if f.endswith('.py'))
source_files = (
f for f in os.listdir(source_dir, absolute=True)
if f.endswith('.py')
)

for f in itertools.chain(source_files, test_files):
with self.subTest(f=f):
Expand Down

0 comments on commit aad17c6

Please sign in to comment.