Last active
June 4, 2017 09:00
-
-
Save cdunklau/3e01037119ad468d05ac to your computer and use it in GitHub Desktop.
One use case for staticmethod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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