Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit 481ae5f

Browse files
committed
Make Point and Region accessible as a dict
1 parent 2770e04 commit 481ae5f

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

eyes_core/applitools/core/geometry.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,24 @@
1414
__all__ = ('Point', 'Region',)
1515

1616

17-
class Point(object):
17+
class DictAccessMixin(object):
18+
def __getitem__(self, item):
19+
if item not in self.__slots__:
20+
raise KeyError
21+
return getattr(self, item)
22+
23+
24+
class StateMixin(object):
25+
def __getstate__(self):
26+
return OrderedDict([(name, getattr(self, name)) for name in self.__slots__])
27+
28+
# Required is required in order for jsonpickle to work on this object.
29+
# noinspection PyMethodMayBeStatic
30+
def __setstate__(self, state):
31+
raise EyesError('Cannot create Point instance from dict!')
32+
33+
34+
class Point(DictAccessMixin, StateMixin):
1835
"""
1936
A point with the coordinates (x,y).
2037
"""
@@ -25,15 +42,6 @@ def __init__(self, x=0, y=0):
2542
self.x = int(round(x))
2643
self.y = int(round(y))
2744

28-
def __getstate__(self):
29-
return OrderedDict([("x", self.x),
30-
("y", self.y)])
31-
32-
# Required is required in order for jsonpickle to work on this object.
33-
# noinspection PyMethodMayBeStatic
34-
def __setstate__(self, state):
35-
raise EyesError('Cannot create Point instance from dict!')
36-
3745
def __add__(self, other):
3846
return Point(self.x + other.x, self.y + other.y)
3947

@@ -165,11 +173,10 @@ def rotate_about(self, p, theta):
165173
return result
166174

167175
def scale(self, scale_ratio):
168-
return Point(int(math.ceil(self.x * scale_ratio)),
169-
int(math.ceil(self.y * scale_ratio)))
176+
return Point(int(math.ceil(self.x * scale_ratio)), int(math.ceil(self.y * scale_ratio)))
170177

171178

172-
class Region(object):
179+
class Region(DictAccessMixin, StateMixin):
173180
"""
174181
A rectangle identified by left,top, width, height.
175182
"""
@@ -183,15 +190,6 @@ def __init__(self, left=0, top=0, width=0, height=0, coordinates_type=Coordinate
183190
self.height = int(round(height))
184191
self.coordinates_type = coordinates_type
185192

186-
def __getstate__(self):
187-
return OrderedDict([("top", self.top), ("left", self.left), ("width", self.width),
188-
("height", self.height), ("coordinatesType", self.coordinates_type)])
189-
190-
# Required is required in order for jsonpickle to work on this object.
191-
# noinspection PyMethodMayBeStatic
192-
def __setstate__(self, state):
193-
raise EyesError('Cannot create Region instance from dict!')
194-
195193
@classmethod
196194
def create_empty_region(cls):
197195
return cls(0, 0, 0, 0)
@@ -267,8 +265,9 @@ def is_same(self, other):
267265
:return: Whether or not the rectangles have same coordinates.
268266
:rtype: bool
269267
"""
270-
return (self.left == other.left and self.top == other.top and self.width == other.width
271-
and self.height == other.height)
268+
return (
269+
self.left == other.left and self.top == other.top and self.width == other.width and self.height ==
270+
other.height)
272271

273272
def is_same_size(self, other):
274273
# type: (Region) -> bool
@@ -327,8 +326,8 @@ def overlaps(self, other):
327326
"""
328327
Return true if a rectangle overlaps this rectangle.
329328
"""
330-
return ((self.left <= other.left <= self.right or other.left <= self.left <= other.right)
331-
and (self.top <= other.top <= self.bottom or other.top <= self.top <= other.bottom))
329+
return ((self.left <= other.left <= self.right or other.left <= self.left <= other.right) and (
330+
self.top <= other.top <= self.bottom or other.top <= self.top <= other.bottom))
332331

333332
def intersect(self, other):
334333
# type: (Region) -> None
@@ -366,8 +365,7 @@ def get_sub_regions(self, max_sub_region_size):
366365
current_height = current_bottom - current_top
367366
current_width = current_right - current_left
368367

369-
sub_regions.append(Region(current_left, current_top, current_width,
370-
current_height))
368+
sub_regions.append(Region(current_left, current_top, current_width, current_height))
371369

372370
current_left += max_sub_region_size["width"]
373371

@@ -382,17 +380,11 @@ def middle_offset(self):
382380

383381
def offset(self, dx, dy):
384382
location = self.location.offset(dx, dy)
385-
return Region(left=location.x, top=location.y,
386-
width=self.size['width'],
387-
height=self.size['height'])
383+
return Region(left=location.x, top=location.y, width=self.size['width'], height=self.size['height'])
388384

389385
def scale(self, scale_ratio):
390-
return Region(
391-
left=int(math.ceil(self.left * scale_ratio)),
392-
top=int(math.ceil(self.top * scale_ratio)),
393-
width=int(math.ceil(self.width * scale_ratio)),
394-
height=int(math.ceil(self.height * scale_ratio))
395-
)
386+
return Region(left=int(math.ceil(self.left * scale_ratio)), top=int(math.ceil(self.top * scale_ratio)),
387+
width=int(math.ceil(self.width * scale_ratio)), height=int(math.ceil(self.height * scale_ratio)))
396388

397389
def __str__(self):
398390
return "(%s, %s) %s x %s" % (self.left, self.top, self.width, self.height)

eyes_selenium/applitools/selenium/capture/eyes_webdriver_screenshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def get_base64(self):
134134
return self._screenshot64
135135

136136
def get_location_relative_to_frame_viewport(self, location):
137-
result = {'x': location.x, 'y': location.y}
137+
result = {'x': location['x'], 'y': location['y']}
138138
if self._frame_chain or self._is_viewport_screenshot:
139139
result['x'] -= self._scroll_position.x
140140
result['y'] -= self._scroll_position.y

0 commit comments

Comments
 (0)