Skip to content

Instantly share code, notes, and snippets.

@cdunklau
Last active June 4, 2017 09:00
Show Gist options
  • Save cdunklau/3e01037119ad468d05ac to your computer and use it in GitHub Desktop.
Save cdunklau/3e01037119ad468d05ac to your computer and use it in GitHub Desktop.
One use case for staticmethod
class _Transform:
transform = None
transargs = ()
def __call__(self, image):
"""
Run the transformation and return the tranformed image data
"""
args = [getattr(self, arg) for arg in self.transargs]
return self.transform(image, *args)
@attr.s
class GaussianBlur(_Transform):
transform = staticmethod(cv2.GaussianBlur)
transargs = ('ksize', 'sigma_x', 'sigma_y')
ksize = attr.ib()
@ksize.validator
def _oddvalued_2tuple(self, attribute, value):
if len(value) != 2:
raise ValueError('Must be a sequence of length two')
a, b = value
if a % 2 != 1 or b % 2 != 1:
raise ValueError('Both elements must be odd')
sigma_x = attr.ib()
sigma_y = attr.ib(default=0)
class ValidatorTestCase(unittest.TestCase):
validator = None
def assertValid(self, value):
self.validator(value)
def assertInvalid(self, value):
self.assertRaises(ValidationError, self.validator, value)
class FooValidatorTestCase(ValidatorTestCase):
validator = staticmethod(foovalidator)
def test_foo_valid(self):
self.assertValid('foo')
def test_notfoo_invalid(self):
self.assertInvalid('notfoo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment