Skip to content

Commit

Permalink
Refactored by Sourcery
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcery-ai-bot committed Jul 4, 2020
1 parent 65ce858 commit 5bed24a
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 76 deletions.
6 changes: 1 addition & 5 deletions gaphas/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,11 +805,7 @@ def _normalize(self, items):
>>> e.handles()
[<Handle object on (0, 0)>, <Handle object on (9, 0)>, <Handle object on (9, 10)>, <Handle object on (0, 10)>]
"""
dirty_matrix_items = set()
for item in items:
if item.normalize():
dirty_matrix_items.add(item)

dirty_matrix_items = {item for item in items if item.normalize()}
return self.update_matrices(dirty_matrix_items)

def update_index(self):
Expand Down
5 changes: 1 addition & 4 deletions gaphas/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,7 @@ def __init__(self, band=None, v=None, balance=None):
def update_balance(self):
b1, b2 = self.band
w = b2 - b1
if w != 0:
self.balance = (self.v - b1) / w
else:
self.balance = 0
self.balance = (self.v - b1) / w if w != 0 else 0

def solve_for(self, var):
b1, b2 = self.band
Expand Down
5 changes: 1 addition & 4 deletions gaphas/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ def __init__(self, single=False, timeout=0, priority=GLib.PRIORITY_DEFAULT):

def source(self, func):
timeout = self.timeout
if timeout > 0:
s = GLib.Timeout(timeout)
else:
s = GLib.Idle()
s = GLib.Timeout(timeout) if timeout > 0 else GLib.Idle()
s.set_callback(func)
s.priority = self.priority
return s
Expand Down
34 changes: 17 additions & 17 deletions gaphas/quadtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(self, bounds=(0, 0, 0, 0), capacity=10):
self._bucket = QuadtreeBucket(bounds, capacity)

# Easy lookup item->(bounds, data, clipped bounds) mapping
self._ids = dict()
self._ids = {}

bounds = property(lambda s: s._bucket.bounds)

Expand Down Expand Up @@ -328,22 +328,22 @@ def find_bucket(self, bounds):
This method should be used to find a bucket that fits, before
add() or remove() is called.
"""
if self._buckets:
sx, sy, sw, sh = self.bounds
cx, cy = sx + sw / 2.0, sy + sh / 2.0
x, y, w, h = bounds
index = 0
if x >= cx:
index += 1
elif x + w > cx:
return self

if y >= cy:
index += 2
elif y + h > cy:
return self
return self._buckets[index].find_bucket(bounds)
return self
if not self._buckets:
return self
sx, sy, sw, sh = self.bounds
cx, cy = sx + sw / 2.0, sy + sh / 2.0
x, y, w, h = bounds
index = 0
if x >= cx:
index += 1
elif x + w > cx:
return self

if y >= cy:
index += 2
elif y + h > cy:
return self
return self._buckets[index].find_bucket(bounds)

def find(self, rect, method):
"""
Expand Down
2 changes: 1 addition & 1 deletion gaphas/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def dispatch(event, queue):
s(event)


_reverse = dict()
_reverse = {}


def reversible_function(func, reverse, bind={}):
Expand Down
4 changes: 1 addition & 3 deletions gaphas/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def __init__(self, columns, indexes):
self._indexes = tuple(fields[i] for i in indexes)

# create data structure, which acts as cache
index = {}
for n in fields:
index[n] = dict()
index = {n: {} for n in fields}
self._index = index

columns = property(lambda s: s._type)
Expand Down
46 changes: 22 additions & 24 deletions gaphas/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def __init__(self, view=None):
self.speed = 10

def on_button_press(self, event):
if not event.get_state()[1] & PAN_MASK == PAN_VALUE:
if event.get_state()[1] & PAN_MASK != PAN_VALUE:
return False
if event.get_button()[1] == 2:
self.x0, self.y0 = event.get_coords()[1:]
Expand All @@ -517,7 +517,7 @@ def on_motion_notify(self, event):

def on_scroll(self, event):
# Ensure no modifiers
if not event.get_state()[1] & PAN_MASK == PAN_VALUE:
if event.get_state()[1] & PAN_MASK != PAN_VALUE:
return False
view = self.view
direction = event.get_scroll_direction()[1]
Expand Down Expand Up @@ -572,34 +572,32 @@ def on_button_release(self, event):

def on_motion_notify(self, event):
if (
event.get_state()[1] & ZOOM_MASK == ZOOM_VALUE
and event.get_state()[1] & Gdk.ModifierType.BUTTON2_MASK
event.get_state()[1] & ZOOM_MASK != ZOOM_VALUE
or not event.get_state()[1] & Gdk.ModifierType.BUTTON2_MASK
):
view = self.view
pos = event.get_coords()[1:]
dy = pos[1] - self.y0
return

sx = view._matrix[0]
sy = view._matrix[3]
ox = (view._matrix[4] - self.x0) / sx
oy = (view._matrix[5] - self.y0) / sy
view = self.view
pos = event.get_coords()[1:]
dy = pos[1] - self.y0

if abs(dy - self.lastdiff) > 20:
if dy - self.lastdiff < 0:
factor = 1.0 / 0.9
else:
factor = 0.9
sx = view._matrix[0]
sy = view._matrix[3]
ox = (view._matrix[4] - self.x0) / sx
oy = (view._matrix[5] - self.y0) / sy

m = view.matrix
m.translate(-ox, -oy)
m.scale(factor, factor)
m.translate(+ox, +oy)
if abs(dy - self.lastdiff) > 20:
factor = 1.0 / 0.9 if dy - self.lastdiff < 0 else 0.9
m = view.matrix
m.translate(-ox, -oy)
m.scale(factor, factor)
m.translate(+ox, +oy)

# Make sure everything's updated
view.request_update((), view._canvas.get_all_items())
# Make sure everything's updated
view.request_update((), view._canvas.get_all_items())

self.lastdiff = dy
return True
self.lastdiff = dy
return True

def on_scroll(self, event):
if event.get_state()[1] & Gdk.ModifierType.CONTROL_MASK:
Expand Down
10 changes: 3 additions & 7 deletions gaphas/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _set_focused_item(self, item):
Set the focused item, this item is also added to the
selected_items set.
"""
if not item is self._focused_item:
if item is not self._focused_item:
self.queue_draw_item(self._focused_item, item)

if item:
Expand Down Expand Up @@ -629,11 +629,7 @@ def update_adjustments(self, allocation=None):
v = Rectangle(0, 0, aw, ah)

# union of these limits gives scrollbar limits
if v in c:
u = c
else:
u = c + v

u = c if v in c else c + v
if hadjustment is None:
self._hadjustment = Gtk.Adjustment.new(
value=v.x,
Expand Down Expand Up @@ -753,7 +749,7 @@ def update(self):

self.update_back_buffer()
finally:
self._dirty_items.clear()
dirty_items.clear()
self._dirty_matrix_items.clear()

def update_bounding_box(self, items):
Expand Down
8 changes: 2 additions & 6 deletions tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ def test_resize_se(cb):
assert h_se is cb.handles[SE]

count = getenv("GAPHAS_TEST_COUNT")
if count:
count = int(count)
else:
count = 1

for i in range(count):
count = int(count) if count else 1
for _ in range(count):
h_se.pos.x += 100 # h.se.{x,y} = 10, now
h_se.pos.y += 100
cb.box.request_update()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_guide_item_in_motion(win):
assert 0 == win.e3.matrix[5]

# Moved back to guided lines:
for d in range(0, 3):
for d in range(3):
guider.move((d, d))
assert 0 == win.e3.matrix[4]
assert 0 == win.e3.matrix[5]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_ports_after_split(simple_canvas):
# Split 1st segment again: 1st port should be deleted, but 2nd one should
# remain untouched
segment.split_segment(0)
assert not (old_ports[0] in simple_canvas.line.ports())
assert old_ports[0] not in simple_canvas.line.ports()
assert old_ports[1] == simple_canvas.line.ports()[2]


Expand Down Expand Up @@ -308,7 +308,7 @@ def test_constraints_after_merge(simple_canvas):
cinfo = simple_canvas.canvas.get_connection(head)
assert cinfo.constraint._line[0]._point == h1.pos
assert cinfo.constraint._line[1]._point == h2.pos
assert not (c1 == cinfo.constraint)
assert c1 != cinfo.constraint


def test_merge_multiple(simple_canvas):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class SList:
def __init__(self):
self.list = list()
self.list = []

def add(self, node, before=None):
if before:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_get_item_at_point(view_fixture):
view_fixture.box.width = 50
view_fixture.box.height = 50
assert len(view_fixture.view._qtree._ids) == 1
assert not view_fixture.view._qtree._bucket.bounds == (
assert view_fixture.view._qtree._bucket.bounds != (
0,
0,
0,
Expand Down

0 comments on commit 5bed24a

Please sign in to comment.