Conditions | 6 |
Total Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | |||
31 | def annotation(value): |
||
32 | """ |
||
33 | Returns value either transformed with one of the function in args, or |
||
34 | casted to one of types in args, or the value itself if it is in the |
||
35 | args. |
||
36 | |||
37 | :raises ValueError: Raises when cannot transform value in any one of |
||
38 | specified ways. |
||
39 | """ |
||
40 | for arg in args: |
||
41 | if value == arg: |
||
42 | return value |
||
43 | if isinstance(arg, type) and isinstance(value, arg): |
||
44 | return value |
||
45 | try: |
||
46 | return arg(value) |
||
47 | except (ValueError, TypeError): |
||
48 | pass |
||
49 | raise ValueError( |
||
50 | "Couldn't convert value {value} to any specified type " |
||
51 | "or find it in specified values.".format( |
||
52 | value=repr(value))) |
||
53 | return annotation |
||
54 |