Skip to content

Commit 0bae3cd

Browse files
ferdnycptmcg
authored andcommitted
Doctests for pyparsing.common
1 parent 98ec165 commit 0bae3cd

File tree

1 file changed

+91
-45
lines changed

1 file changed

+91
-45
lines changed

pyparsing/common.py

Lines changed: 91 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class pyparsing_common:
3030
- :class:`upcase_tokens`
3131
- :class:`downcase_tokens`
3232
33-
Example::
33+
Examples:
34+
35+
.. testcode::
3436
3537
pyparsing_common.number.run_tests('''
3638
# any int or real number, returned as the appropriate type
@@ -42,44 +44,9 @@ class pyparsing_common:
4244
1e-12
4345
''')
4446
45-
pyparsing_common.fnumber.run_tests('''
46-
# any int or real number, returned as float
47-
100
48-
-100
49-
+100
50-
3.14159
51-
6.02e23
52-
1e-12
53-
''')
54-
55-
pyparsing_common.hex_integer.run_tests('''
56-
# hex numbers
57-
100
58-
FF
59-
''')
47+
.. testoutput::
48+
:options: +NORMALIZE_WHITESPACE
6049
61-
pyparsing_common.fraction.run_tests('''
62-
# fractions
63-
1/2
64-
-3/4
65-
''')
66-
67-
pyparsing_common.mixed_integer.run_tests('''
68-
# mixed fractions
69-
1
70-
1/2
71-
-3/4
72-
1-3/4
73-
''')
74-
75-
import uuid
76-
pyparsing_common.uuid.set_parse_action(token_map(uuid.UUID))
77-
pyparsing_common.uuid.run_tests('''
78-
# uuid
79-
12345678-1234-5678-1234-567812345678
80-
''')
81-
82-
prints::
8350
8451
# any int or real number, returned as the appropriate type
8552
100
@@ -100,6 +67,22 @@ class pyparsing_common:
10067
1e-12
10168
[1e-12]
10269
70+
.. testcode::
71+
72+
pyparsing_common.fnumber.run_tests('''
73+
# any int or real number, returned as float
74+
100
75+
-100
76+
+100
77+
3.14159
78+
6.02e23
79+
1e-12
80+
''')
81+
82+
.. testoutput::
83+
:options: +NORMALIZE_WHITESPACE
84+
85+
10386
# any int or real number, returned as float
10487
100
10588
[100.0]
@@ -119,20 +102,58 @@ class pyparsing_common:
119102
1e-12
120103
[1e-12]
121104
105+
.. testcode::
106+
107+
pyparsing_common.hex_integer.run_tests('''
108+
# hex numbers
109+
100
110+
FF
111+
''')
112+
113+
.. testoutput::
114+
:options: +NORMALIZE_WHITESPACE
115+
116+
122117
# hex numbers
123118
100
124119
[256]
125120
126121
FF
127122
[255]
128123
124+
.. testcode::
125+
126+
pyparsing_common.fraction.run_tests('''
127+
# fractions
128+
1/2
129+
-3/4
130+
''')
131+
132+
.. testoutput::
133+
:options: +NORMALIZE_WHITESPACE
134+
135+
129136
# fractions
130137
1/2
131138
[0.5]
132139
133140
-3/4
134141
[-0.75]
135142
143+
.. testcode::
144+
145+
pyparsing_common.mixed_integer.run_tests('''
146+
# mixed fractions
147+
1
148+
1/2
149+
-3/4
150+
1-3/4
151+
''')
152+
153+
.. testoutput::
154+
:options: +NORMALIZE_WHITESPACE
155+
156+
136157
# mixed fractions
137158
1
138159
[1]
@@ -145,6 +166,18 @@ class pyparsing_common:
145166
146167
1-3/4
147168
[1.75]
169+
.. testcode::
170+
171+
import uuid
172+
pyparsing_common.uuid.set_parse_action(token_map(uuid.UUID))
173+
pyparsing_common.uuid.run_tests('''
174+
# uuid
175+
12345678-1234-5678-1234-567812345678
176+
''')
177+
178+
.. testoutput::
179+
:options: +NORMALIZE_WHITESPACE
180+
148181
149182
# uuid
150183
12345678-1234-5678-1234-567812345678
@@ -264,13 +297,17 @@ def convert_to_date(fmt: str = "%Y-%m-%d"):
264297
Params -
265298
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%d"``)
266299
267-
Example::
300+
Example:
301+
302+
.. testcode::
268303
269304
date_expr = pyparsing_common.iso8601_date.copy()
270305
date_expr.set_parse_action(pyparsing_common.convert_to_date())
271306
print(date_expr.parse_string("1999-12-31"))
272307
273-
prints::
308+
prints:
309+
310+
.. testoutput::
274311
275312
[datetime.date(1999, 12, 31)]
276313
"""
@@ -291,13 +328,17 @@ def convert_to_datetime(fmt: str = "%Y-%m-%dT%H:%M:%S.%f"):
291328
Params -
292329
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%dT%H:%M:%S.%f"``)
293330
294-
Example::
331+
Example:
332+
333+
.. testcode::
295334
296335
dt_expr = pyparsing_common.iso8601_datetime.copy()
297336
dt_expr.set_parse_action(pyparsing_common.convert_to_datetime())
298337
print(dt_expr.parse_string("1999-12-31T23:59:59.999"))
299338
300-
prints::
339+
prints:
340+
341+
.. testoutput::
301342
302343
[datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
303344
"""
@@ -329,15 +370,20 @@ def cvt_fn(s, l, t):
329370
def strip_html_tags(s: str, l: int, tokens: ParseResults):
330371
"""Parse action to remove HTML tags from web page HTML source
331372
332-
Example::
373+
Example:
374+
375+
.. testcode::
333376
334377
# strip HTML links from normal text
335378
text = '<td>More info at the <a href="https://github.com/pyparsing/pyparsing/wiki">pyparsing</a> wiki page</td>'
336379
td, td_end = make_html_tags("TD")
337-
table_text = td + SkipTo(td_end).set_parse_action(pyparsing_common.strip_html_tags)("body") + td_end
380+
table_text = td + SkipTo(td_end).set_parse_action(
381+
pyparsing_common.strip_html_tags)("body") + td_end
338382
print(table_text.parse_string(text).body)
339383
340-
Prints::
384+
Prints:
385+
386+
.. testoutput::
341387
342388
More info at the pyparsing wiki page
343389
"""

0 commit comments

Comments
 (0)