Skip to content

Commit 62781b9

Browse files
committed
Change examples to REPL syntax and fix errors in them
1 parent 76a3e4b commit 62781b9

14 files changed

Lines changed: 94 additions & 74 deletions

File tree

‎README.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ schema.
1010

1111
.. code-block:: python
1212
13-
import validators
13+
>>> import validators
1414
1515
>>> validators.email('someone@example.com')
1616
True

‎docs/index.rst‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ is an email. With ``validators`` this use case becomes as easy as::
2020

2121
>>> import validators
2222

23-
2423
>>> validators.email('someone@example.com')
2524
True
2625

@@ -46,20 +45,18 @@ validate and possibly some additional key-value arguments. Each function returns
4645
:class:`~validators.utils.ValidationFailure` class implements ``__bool__``
4746
method so you can easily check if validation failed::
4847

49-
if not email('some_bogus_email@@@'):
50-
# Do something here
51-
pass
48+
>>> if not validators.email('some_bogus_email@@@'):
49+
... # Do something here
50+
... pass
5251

5352
:class:`~validators.utils.ValidationFailure` object also holds all the arguments
5453
passed to original function::
5554

56-
result = number_range(3, min=5)
57-
if not result:
58-
result.value
59-
# 3
60-
61-
result.min
62-
# 5
55+
>>> result = validators.between(3, min=5)
56+
>>> result.value
57+
3
58+
>>> result.min
59+
5
6360

6461

6562
between

‎validators/between.py‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ def between(value, min=None, max=None):
1717
1818
Examples::
1919
20-
>>> import validators
21-
22-
>>> assert validators.between(5, min=2)
20+
>>> from datetime import datetime
2321
24-
>>> assert validators.between(13.2, min=13, max=14)
22+
>>> between(5, min=2)
23+
True
2524
26-
>>> assert not validators.between(500, max=400)
25+
>>> between(13.2, min=13, max=14)
26+
True
2727
28-
>>> from datetime import datetime
28+
>>> between(500, max=400)
29+
ValidationFailure(func=between, args=...)
2930
30-
>>> assert validator.between(
31+
>>> between(
3132
... datetime(2000, 11, 11),
3233
... min=datetime(1999, 11, 11)
3334
... )
34-
35+
True
3536
3637
:param min:
3738
The minimum required value of the number. If not provided, minimum
@@ -50,7 +51,11 @@ def between(value, min=None, max=None):
5051
min = Min
5152
if max is None:
5253
max = Max
53-
if min > max:
54+
try:
55+
min_gt_max = min > max
56+
except TypeError:
57+
min_gt_max = max < min
58+
if min_gt_max:
5459
raise AssertionError('`min` cannot be more than `max`.')
5560

56-
return min <= value <= max
61+
return min <= value and max >= value

‎validators/email.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ def email(value, whitelist=None):
3333
3434
Examples::
3535
36-
>>> assert validators.email('someone@example.com')
37-
38-
>>> assert not validators.email('bogus@@')
36+
>>> email('someone@example.com')
37+
True
3938
39+
>>> email('bogus@@')
40+
ValidationFailure(func=email, ...)
4041
4142
.. _Django's email validator:
4243
https://github.com/django/django/blob/master/django/core/validators.py

‎validators/extremes.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ class Min(object):
1414
1515
Examples::
1616
17-
>>> import validators
1817
>>> import sys
1918
20-
>>> validators.Min < -sys.maxint
19+
>>> Min < -sys.maxint
2120
True
22-
>>> validators.Min < None
21+
22+
>>> Min < None
2323
True
24-
>>> validators.Min < ''
24+
25+
>>> Min < ''
2526
True
2627
2728
.. versionadded:: 0.2
@@ -41,14 +42,15 @@ class Max(object):
4142
4243
Examples::
4344
44-
>>> import validators
4545
>>> import sys
4646
47-
>>> validators.Max > validators.Min
47+
>>> Max > Min
4848
True
49-
>>> validators.Max > sys.maxint
49+
50+
>>> Max > sys.maxint
5051
True
51-
>>> validators.Max > 99999999999999999
52+
53+
>>> Max > 99999999999999999
5254
True
5355
5456
.. versionadded:: 0.2

‎validators/i18n/fi.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ def fi_business_id(business_id):
2929
3030
Examples::
3131
32-
>>> assert fi_business_id('0112038-9') # Fast Monkeys Ltd
32+
>>> fi_business_id('0112038-9') # Fast Monkeys Ltd
33+
True
3334
34-
>>> assert not fi_business_id('1234567-8') # Bogus ID
35+
>>> fi_business_id('1234567-8') # Bogus ID
36+
ValidationFailure(func=fi_business_id, ...)
3537
3638
.. versionadded:: 0.4
3739
.. versionchanged:: 0.5
@@ -61,9 +63,11 @@ def fi_ssn(ssn):
6163
6264
Examples::
6365
64-
>>> assert fi_ssn('010101-0101')
66+
>>> fi_ssn('010101-0101')
67+
True
6568
66-
>>> assert not fi_ssn('101010-0102')
69+
>>> fi_ssn('101010-0102')
70+
ValidationFailure(func=fi_ssn, args={'ssn': '101010-0102'})
6771
6872
.. versionadded:: 0.5
6973

‎validators/ip_address.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ def ipv4(value):
1313
1414
Examples::
1515
16-
>>> import validators
17-
18-
>>> validators.ipv4('123.0.0.7')
16+
>>> ipv4('123.0.0.7')
1917
True
20-
>>> validators.ipv4('900.80.70.11')
21-
False
18+
19+
>>> ipv4('900.80.70.11')
20+
ValidationFailure(func=ipv4, args={'value': '900.80.70.11'})
2221
2322
.. versionadded:: 0.2
2423
@@ -43,9 +42,11 @@ def ipv6(value):
4342
4443
Examples::
4544
46-
>>> assert validators.ipv6('abcd:ef::42:1')
45+
>>> ipv6('abcd:ef::42:1')
46+
True
4747
48-
>>> assert not validators.ipv6('abc.0.0.1')
48+
>>> ipv6('abc.0.0.1')
49+
ValidationFailure(func=ipv6, args={'value': 'abc.0.0.1'})
4950
5051
.. versionadded:: 0.2
5152

‎validators/length.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ def length(value, min=None, max=None):
1010
1111
Examples::
1212
13-
>>> assert length('something', min=2)
13+
>>> length('something', min=2)
14+
True
1415
15-
>>> assert length('something', min=9, max=9)
16+
>>> length('something', min=9, max=9)
17+
True
1618
17-
>>> assert not length('something', max=5)
19+
>>> length('something', max=5)
20+
ValidationFailure(func=length, ...)
1821
1922
:param value:
2023
The string to validate.

‎validators/mac_address.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def mac_address(value):
2020
2121
Examples::
2222
23-
>>> assert validators.mac_address('01:23:45:67:ab:CD')
23+
>>> mac_address('01:23:45:67:ab:CD')
24+
True
2425
25-
>>> assert not validators.mac_address('00:00:00:00:00')
26+
>>> mac_address('00:00:00:00:00')
27+
ValidationFailure(func=mac_address, args={'value': '00:00:00:00:00'})
2628
2729
.. versionadded:: 0.2
2830

‎validators/slug.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ def slug(value):
1515
1616
Examples::
1717
18-
>>> assert not slug('my.slug')
18+
>>> slug('my.slug')
19+
ValidationFailure(func=slug, args={'value': 'my.slug'})
1920
20-
>>> assert slug('my-slug-2134')
21+
>>> slug('my-slug-2134')
22+
True
2123
2224
.. versionadded:: 0.6
2325

0 commit comments

Comments
 (0)