def overlay(self, image: T, alpha: float, beta: float, x: int, y: int) -> T: if x + image.width() > self.width( ) or x < 0 or y + image.height() > self.height() or y < 0: logger = logging.getLogger("logger") logger.warning( "{0} is overlaid on {1} out of frame. " "Dimensions of {1} are ({2}, {3}), {0} was placed at ({4}, {5})" .format(image.name, self.name, self.width(), self.height(), x, y)) img = np.copy(self.img) x_start = max(0, x) x_stop = min(x + image.width(), self.width()) y_start = max(0, y) y_stop = min(y + image.height(), self.height()) for x_i in range(x_start, x_stop): for y_i in range(y_start, y_stop): img[y_i, x_i] = alpha * img[y_i, x_i] + beta * image.img[y_i - y, x_i - x] return ImageFromMatrix( img, "overlay_{0}_on_{1}_at_{2}_{3}".format(image.name, self.name, x, y))
def save_list_on_entity(entity: typing.T, list: [any]): if len(list) is 0: list.set([]) for item in list: list.add(item) entity.save()
def inner(obj: t.T) -> t.Any: if isinstance(obj, Map): return type(obj)({k: inner(v) for k, v in obj.items()}) elif _seq and isinstance(obj, (t.List, t.Set)): return type(obj)(inner(v) for v in obj) else: return function(obj)
def test_bind(self): self.assertNotIsInstance(42, T) # Baseline. with T.bind(int): self.assertIsInstance(42, T) self.assertNotIsInstance(3.14, T) self.assertTrue(issubclass(int, T)) self.assertFalse(issubclass(T, int)) self.assertFalse(issubclass(float, T)) self.assertNotIsInstance(42, T) # Baseline restored.
def inner(obj: t.T) -> t.Any: if isinstance(obj, Map): for v in obj.values(): inner(v) elif _seq and isinstance(obj, (t.List, t.Set)): for v in obj: inner(v) else: return function(obj)
def serialize_marshmallow_model_to_dict(data: T) -> dict: '''Uses marshmallow.Schema.dumps to serialize `data`. Assumes that data makes it's schema available in __marshmallow__ attrbute Args: data (T): A marshmallow model Returns: str: string representation of data ''' return data.__marshmallow__().dump(data)
def wrapper(klass: t.T = None) -> t.Union[t.T, partial]: """ :param klass: type/object to decorate, defaults to None :type klass: t.T, optional :returns: a decorated type/object, or a partially evaluated `decorate_methods` :rtype: {t.Union[t.T, partial]} :raises: TypeError """ if klass is None: spec = (decorator is None, methods is None) if spec == (False, False): return partial(decorate_methods, decorator=decorator, methods=methods) elif spec == (False, True): return partial(decorate_methods, decorator=decorator) elif spec == (True, False): return partial(decorate_methods, methods=methods) else: return decorate_methods if not issubclass(type(decorator), t.Callable): raise TypeError("'decorator' must be a t.Callable type") if isinstance(klass, type): _ = klass.__name__ class klass(klass): pass klass.__name__ = _ available_methods = [ m for m in type(klass).__dir__(klass) if issubclass(type(getattr(klass, m, None)), t.Callable) and m not in ["__class__", "__new__"] ] if methods is not None: if not issubclass(type(methods), t.List): raise TypeError("'methods' needs to be a list") to_decorate = filter(lambda x: x in methods, available_methods) else: to_decorate = available_methods for method in to_decorate: m = getattr(klass, method, None) if m is not None: decorated = decorator(m) setattr(klass, method, decorated) return klass
def test_bind_fail(self): # This essentially tests what happens when __enter__() raises # an exception. __exit__() won't be called, but the # VarBinding and the TypeVar are still in consistent states. bv = T.bind(int) with mock.patch('typing.TypeVar._bind', side_effect=RuntimeError): with self.assertRaises(RuntimeError): with bv: self.assertFalse("Should not get here") self.assertNotIsInstance(42, T) with bv: self.assertIsInstance(42, T) self.assertNotIsInstance(42, T)
def test_bind_reuse(self): self.assertNotIsInstance(42, T) # Baseline. bv = T.bind(int) with bv: self.assertIsInstance(42, T) # Bound. self.assertNotIsInstance(3.14, T) self.assertNotIsInstance(42, T) # Baseline restored. # Reusing bv will work. with bv: self.assertIsInstance(42, T) # Bound. self.assertNotIsInstance(3.14, T) # Reusing bv recursively won't work. with self.assertRaises(TypeError): with bv: self.assertFalse("Should not get here") # Rebinding T explicitly will work. with T.bind(float): self.assertIsInstance(3.14, T) self.assertNotIsInstance(42, T) # Now the previous binding should be restored. self.assertIsInstance(42, T) self.assertNotIsInstance(3.14, T) self.assertNotIsInstance(42, T) # Baseline restored.
def user_object_from_credentials(self, ue: str, up: str) -> T('User'): """Returns the User instance based on his email and password. """ # ue: user_email # up: user_password if ue is None or type(ue) != str\ or up is None or type(up) != str: return None try: users = User.search({'email': ue}) if users: for user in users: if user.is_valid_password(up): return user except BaseException: return None return None
def resize_to_match(self, image: T, height=False, width=False): new_height = image.height() if height else self.height() new_width = image.width if width else self.width() self.img = cv.resize(self.img, (new_width, new_height))