| 1 |
.. _id.appendix.parse_builtin_types: |
| 2 |
|
| 3 |
Predefined Data Types in ``parse`` |
| 4 |
============================================================================== |
| 5 |
|
| 6 |
:pypi:`behave` uses the :pypi:`parse` module (inverse of Python `string.format`_) |
| 7 |
under the hoods to parse parameters in step definitions. |
| 8 |
This leads to rather simple and readable parse expressions for step parameters. |
| 9 |
|
| 10 |
.. code-block:: python |
| 11 |
|
| 12 |
# -- FILE: features/steps/type_transform_example_steps.py |
| 13 |
from behave import given |
| 14 |
|
| 15 |
@given('I have {number:d} friends') #< Convert 'number' into int type. |
| 16 |
def step_given_i_have_number_friends(context, number): |
| 17 |
assert number > 0 |
| 18 |
... |
| 19 |
|
| 20 |
Therefore, the following ``parse types`` are already supported |
| 21 |
in step definitions without registration of any *user-defined type*: |
| 22 |
|
| 23 |
|
| 24 |
===== =========================================== ============ |
| 25 |
Type Characters Matched Output Type |
| 26 |
===== =========================================== ============ |
| 27 |
w Letters and underscore str |
| 28 |
W Non-letter and underscore str |
| 29 |
s Whitespace str |
| 30 |
S Non-whitespace str |
| 31 |
d Digits (effectively integer numbers) int |
| 32 |
D Non-digit str |
| 33 |
n Numbers with thousands separators (, or .) int |
| 34 |
% Percentage (converted to value/100.0) float |
| 35 |
f Fixed-point numbers float |
| 36 |
e Floating-point numbers with exponent float |
| 37 |
e.g. 1.1e-10, NAN (all case insensitive) |
| 38 |
g General number format (either d, f or e) float |
| 39 |
b Binary numbers int |
| 40 |
o Octal numbers int |
| 41 |
x Hexadecimal numbers (lower and upper case) int |
| 42 |
ti ISO 8601 format date/time datetime |
| 43 |
e.g. 1972-01-20T10:21:36Z |
| 44 |
te RFC2822 e-mail format date/time datetime |
| 45 |
e.g. Mon, 20 Jan 1972 10:21:36 +1000 |
| 46 |
tg Global (day/month) format date/time datetime |
| 47 |
e.g. 20/1/1972 10:21:36 AM +1:00 |
| 48 |
ta US (month/day) format date/time datetime |
| 49 |
e.g. 1/20/1972 10:21:36 PM +10:30 |
| 50 |
tc ctime() format date/time datetime |
| 51 |
e.g. Sun Sep 16 01:03:52 1973 |
| 52 |
th HTTP log format date/time datetime |
| 53 |
e.g. 21/Nov/2011:00:07:11 +0000 |
| 54 |
tt Time time |
| 55 |
e.g. 10:21:36 PM -5:30 |
| 56 |
===== =========================================== ============ |
| 57 |
|
| 58 |
|
| 59 |
.. _string.format: https://docs.python.org/2/library/string.html#format-string-syntax |