You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
+32-19Lines changed: 32 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,24 +22,46 @@
22
22
A `VType` is a combination of :
23
23
24
24
- a type **name**, for example `'PositiveInt'`
25
-
- one or several **base types**: for example `int`.
25
+
- one or several **base types**: for example `int`. When several are provided, they all should match ("*and*" combination).
26
26
- one or several **validators**: for example `lambda x: x >= 0`
27
27
28
-
For example we can create a positive int:
28
+
A `VType` is constructed using `vtype(name, type, validators, ...)`. For example we can create a positive int:
29
29
30
30
```python
31
31
from vtypes import vtype
32
-
33
32
PositiveInt = vtype('PositiveInt', int, {'should be positive': lambdax: x >=0})
34
33
```
35
34
36
35
A `VType`'s main purpose is to behave like a type (therefore to be compliant with `isinstance`) and to validate both type and values at the same time when `isinstance` is called:
37
36
38
37
```python
39
38
assertisinstance(1, PositiveInt)
40
-
assertnotisinstance(-1, PositiveInt)
39
+
assertnotisinstance(-1, PositiveInt)# an int, but not >= 0
41
40
```
42
41
42
+
An alternate way to define a `VType` is to define a python class inheriting from `VType`.
43
+
44
+
-**base types** can be either provided as superclass, or in the `__type__` class attribute,
45
+
46
+
-**validators** can be provided in the `__validators__` class attribute.
47
+
48
+
49
+
So the following two classes are equivalent to our previous `PositiveInt` example:
50
+
51
+
```python
52
+
from vtypes import VType
53
+
54
+
# type is provided in the ancestors
55
+
classPositiveInt(int, VType):
56
+
__validators__ = {'should be positive': lambdax: x >=0}
57
+
58
+
# type is provided in __type__
59
+
classPositiveInt2(VType):
60
+
__type__ =int
61
+
__validators__ = {'should be positive': lambdax: x >=0}
62
+
```
63
+
64
+
43
65
### b - goodies
44
66
45
67
In addition to this primary feature, a `VType` provides a few handy methods:
@@ -95,7 +117,9 @@ If you wish to create even more compact callables, you may wish to look at [`min
95
117
96
118
### d - composition
97
119
98
-
You can combine types, for example a nonempty string can be obtained by mixing `NonEmpty`and`str`:
120
+
You can combine types, for example a nonempty string can be obtained by mixing `NonEmpty`and`str`.
121
+
122
+
-*with the compact style*: simply put the classes to combine in a tuplein the second `vtype` argument:
99
123
100
124
```python
101
125
NonEmpty= vtype('NonEmpty', (), {'should be non empty': lambdax: len(x) >0})
An alternate way to define `VType`s is to define a python class inheriting from`VType`.
112
-
113
-
- the validators can be provided as a class member named `__validators__`
114
-
115
-
- the base type(s) can be either provided as superclass(es), oras a class member named `__type__`.
116
-
117
-
This provides an alternate style that developers might find handy in particular for entering docstrings andfor making `VTypes` composition appear "just like normal python inheritance".
132
+
-*with the class style*: either use inheritance, or fill the `__type__`class attribute with a tuple
118
133
119
134
```python
120
-
from vtypes import VType
121
-
122
135
class NonEmpty(VType):
123
136
"""A VType describing non-empty containers, with strictly positive length."""
124
137
__validators__= {'should be non empty': lambdax: len(x) >0}
125
138
126
139
class NonEmptyStr(NonEmpty, str):
127
140
"""A VType for non-empty strings"""
128
141
129
-
classAlternateNonEmptyStr(VType):
142
+
classNonEmptyStr2(VType):
130
143
"""A VType for non-empty strings - alternate style"""
0 commit comments