imagination.decorator.validator¶
| Author: | Juti Noppornpitak |
|---|---|
| Availability: | 1.5 |
The module contains reusable input validators.
Note
Copyright (c) 2012 Juti Noppornpitak
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
imagination.decorator.validator.restrict_type(*restricted_list, **restricted_map)[source]¶ The method decorator to validate the type of inputs given to the method.
Parameters: - restricted_list – the list of types to restrict the type of each parameter in the same order as the parameter given to the method.
- restricted_map – the map of types to restrict the type of each parameter by name.
When the input fails the validation, an exception of type
TypeErroris throw.- There are a few exceptions:
- If the given type is
None, there will be no restriction. - If the given type is
long, the value ofintandfloatare also valid. - If the given type is
unicode, the valud ofstris also valid.
- If the given type is
Warning
In Imagination 1.6, types
unicodeandlongare no longer have fallback check in order to support Python 3.3.from imagination.decorator.validator import restrict_type # Example on a function @restrict_type(unicode) def 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)