mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Make the Rational constructor accept '3.' and '.2' as well as '3.2'.
This commit is contained in:
parent
5a6cfee632
commit
1dabdb25f8
2 changed files with 18 additions and 3 deletions
|
@ -25,9 +25,18 @@ def gcd(a, b):
|
|||
return a
|
||||
|
||||
|
||||
_RATIONAL_FORMAT = re.compile(
|
||||
r'^\s*(?P<sign>[-+]?)(?P<num>\d+)'
|
||||
r'(?:/(?P<denom>\d+)|\.(?P<decimal>\d+))?\s*$')
|
||||
_RATIONAL_FORMAT = re.compile(r"""
|
||||
\A\s* # optional whitespace at the start, then
|
||||
(?P<sign>[-+]?) # an optional sign, then
|
||||
(?=\d|\.\d) # lookahead for digit or .digit
|
||||
(?P<num>\d*) # numerator (possibly empty)
|
||||
(?: # followed by an optional
|
||||
/(?P<denom>\d+) # / and denominator
|
||||
| # or
|
||||
\.(?P<decimal>\d*) # decimal point and fractional part
|
||||
)?
|
||||
\s*\Z # and optional whitespace to finish
|
||||
""", re.VERBOSE)
|
||||
|
||||
|
||||
class Rational(RationalAbc):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue