Created
July 18, 2012 20:24
-
-
Save shiroyuki/3138640 to your computer and use it in GitHub Desktop.
Example for Imagination's type validator
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
| # This example works with the developmental build of Imagination | |
| from imagination.decorator.validator import restrict_type | |
| # One sad old way of validation | |
| def old_say(context): | |
| assert isinstance(context, unicode) or isinstance(context, str) | |
| print context | |
| # Example on a function | |
| @restrict_type(unicode) | |
| def new_say(context): | |
| print context | |
| class Person(object): | |
| # Example on a constructor | |
| @restrict_type(unicode, int) | |
| def __init__(self, name, age): | |
| self.name = name | |
| self.age = age | |
| self.__friends = [] | |
| # Example on an instance method | |
| @restrict_type(Person) | |
| def add_friend(self, person): | |
| self.__friends.append(person) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment