3
Y'íc2 ã @ sP d dl T d dlmZmZmZ ddlmZ G dd dZdd eej D Z d S )
é )Ú*)Údelimited_listÚany_open_tagÚ
any_close_tagé )Údatetimec @ sö e Zd ZdZeeZeeZe e
jdjeZ
e ejdjeedZedjdjeZe jed e je jdZejd d
eeeedj e B jdZeje ed
jdjeZedjdjeZeeB eB jdj ZedjdjeZe eejdZ edjdZ!edjdZ"e"de" d jdZ#ee"de" dY d ee"de" dZ jdZ$e$j%d d
d!e! jd"Z&e'e#e&B e$B jd#jd#Z(ed$jd%Z)e*d[e+d'd(d)Z,e*d\e+d'd+d,Z-ed-jd.Z.ed/jd0Z/ed1jd2Z0e1j e2j B Z3e*e+ee4d3d4d5Z5e'e6e7d6 e8 e e9d6d7 ee:d8e;e8 d6B j jd9Z<e=ee>j? e<B d:d;jd<Z@e*ed=d
ZAe*ed>d
ZBedtjdWZCeZDeZEe,ZFe-ZGe5ZHeAZIeBZJdXS )uÚpyparsing_commona" Here are some common low-level expressions that may be useful in
jump-starting parser development:
- numeric forms (:class:`integers<integer>`, :class:`reals<real>`,
:class:`scientific notation<sci_real>`)
- common :class:`programming identifiers<identifier>`
- network addresses (:class:`MAC<mac_address>`,
:class:`IPv4<ipv4_address>`, :class:`IPv6<ipv6_address>`)
- ISO8601 :class:`dates<iso8601_date>` and
:class:`datetime<iso8601_datetime>`
- :class:`UUID<uuid>`
- :class:`comma-separated list<comma_separated_list>`
- :class:`url`
Parse actions:
- :class:`convertToInteger`
- :class:`convertToFloat`
- :class:`convertToDate`
- :class:`convertToDatetime`
- :class:`stripHTMLTags`
- :class:`upcaseTokens`
- :class:`downcaseTokens`
Example::
pyparsing_common.number.runTests('''
# any int or real number, returned as the appropriate type
100
-100
+100
3.14159
6.02e23
1e-12
''')
pyparsing_common.fnumber.runTests('''
# any int or real number, returned as float
100
-100
+100
3.14159
6.02e23
1e-12
''')
pyparsing_common.hex_integer.runTests('''
# hex numbers
100
FF
''')
pyparsing_common.fraction.runTests('''
# fractions
1/2
-3/4
''')
pyparsing_common.mixed_integer.runTests('''
# mixed fractions
1
1/2
-3/4
1-3/4
''')
import uuid
pyparsing_common.uuid.setParseAction(tokenMap(uuid.UUID))
pyparsing_common.uuid.runTests('''
# uuid
12345678-1234-5678-1234-567812345678
''')
prints::
# any int or real number, returned as the appropriate type
100
[100]
-100
[-100]
+100
[100]
3.14159
[3.14159]
6.02e23
[6.02e+23]
1e-12
[1e-12]
# any int or real number, returned as float
100
[100.0]
-100
[-100.0]
+100
[100.0]
3.14159
[3.14159]
6.02e23
[6.02e+23]
1e-12
[1e-12]
# hex numbers
100
[256]
FF
[255]
# fractions
1/2
[0.5]
-3/4
[-0.75]
# mixed fractions
1
[1]
1/2
[0.5]
-3/4
[-0.75]
1-3/4
[1.75]
# uuid
12345678-1234-5678-1234-567812345678
[UUID('12345678-1234-5678-1234-567812345678')]
Úintegerzhex integeré z[+-]?\d+zsigned integerú/Úfractionc C s | d | d S )Nr r éÿÿÿÿ© )Úttr r ú5/tmp/pip-build-gk9425m9/pyparsing/pyparsing/common.pyÚ<lambda>¹ s zpyparsing_common.<lambda>ú-z"fraction or mixed integer-fractionz[+-]?(?:\d+\.\d*|\.\d+)zreal numberz@[+-]?(?:\d+(?:[eE][+-]?\d+)|(?:\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?)z$real number with scientific notationÚnumberz[+-]?\d+\.?\d*([eE][+-]?\d+)?ÚfnumberÚ
identifierzK(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}zIPv4 addressz[0-9a-fA-F]{1,4}Úhex_integerú:é zfull IPv6 addressr é z::zshort IPv6 addressc C s t dd | D dk S )Nc s s | ]}t jj|rd V qdS )r N)r Ú
_ipv6_partÚmatches)Ú.0r r r r ú <genexpr>í s z,pyparsing_common.<lambda>.<locals>.<genexpr>é )Úsum)Útr r r r í s z::ffff:zmixed IPv6 addresszIPv6 addressz:[0-9a-fA-F]{2}([:.-])[0-9a-fA-F]{2}(?:\1[0-9a-fA-F]{2}){4}zMAC addressú%Y-%m-%d)Úfmtc s fdd}|S )aÜ
Helper to create a parse action for converting parsed date string to Python datetime.date
Params -
- fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%d"``)
Example::
date_expr = pyparsing_common.iso8601_date.copy()
date_expr.setParseAction(pyparsing_common.convertToDate())
print(date_expr.parseString("1999-12-31"))
prints::
[datetime.date(1999, 12, 31)]
c sL yt j|d j S tk
rF } zt| |t|W Y d d }~X nX d S )Nr )r ÚstrptimeÚdateÚ
ValueErrorÚParseExceptionÚstr)ÚssZllr Úve)r"