Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8cf8d31
Add '.f' formatting for Fraction objects
mdickinson Dec 3, 2022
e9db697
Add support for % and F format specifiers
mdickinson Dec 3, 2022
6491b04
Add support for 'z' flag
mdickinson Dec 4, 2022
4551468
Tidy up of business logic
mdickinson Dec 4, 2022
43d34fc
Add support for 'e' presentation type
mdickinson Dec 4, 2022
f8dfcb9
Add support for 'g' presentation type; tidy
mdickinson Dec 4, 2022
6bfbc6c
Tidying
mdickinson Dec 10, 2022
48629b7
Add news entry
mdickinson Dec 10, 2022
3d21af2
Add documentation
mdickinson Dec 10, 2022
c86a57e
Add what's new entry
mdickinson Dec 10, 2022
b521efb
Fix backticks:
mdickinson Dec 10, 2022
aac576e
Fix more missing backticks
mdickinson Dec 10, 2022
b9ee0ff
Fix indentation
mdickinson Dec 10, 2022
1c8b8a9
Wordsmithing for consistency with other method definitions
mdickinson Dec 10, 2022
9dbde3b
Add link to the format specification mini-language
mdickinson Dec 10, 2022
2dd48bb
Fix: not true that thousands separators cannot have an effect for the…
mdickinson Dec 10, 2022
983726f
Fix typo in comment
mdickinson Dec 10, 2022
b7e5129
Tweak docstring and comments for _round_to_exponent
mdickinson Dec 21, 2022
cb5e234
Second pass on docstring and comments for _round_to_figures
mdickinson Dec 21, 2022
907487e
Add tests for the corner case of zero minimum width + alignment
mdickinson Dec 21, 2022
aba35f3
Tests for the case of zero padding _and_ a zero minimum width
mdickinson Dec 21, 2022
fc4d3b5
Cleanup of __format__ method
mdickinson Dec 21, 2022
4ccdf94
Add test cases from original issue and discussion thread
mdickinson Dec 21, 2022
b358b37
Merge branch 'main' into fraction-format
mdickinson Dec 21, 2022
67e020c
Tighten up the regex - extra leading zeros not permitted
mdickinson Dec 21, 2022
e240c70
Merge remote-tracking branch 'mdickinson/fraction-format' into fracti…
mdickinson Dec 21, 2022
111c41f
Add tests for a few more fill character corner cases
mdickinson Dec 21, 2022
d8cc3d6
Merge remote-tracking branch 'upstream/main' into fraction-format
mdickinson Jan 22, 2023
fff3751
Add testcases for no presentation type with an integral fraction
mdickinson Jan 22, 2023
0b8bfa6
Rename the regex to allow for future API expansion
mdickinson Jan 22, 2023
54ed402
Tweak comment
mdickinson Jan 22, 2023
e3a5fd2
Tweak algorithm comments
mdickinson Jan 22, 2023
098d34c
Fix incorrect acceptance of Z instead of z
mdickinson Jan 22, 2023
2c476a2
Use consistent quote style in tests
mdickinson Jan 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tidy up of business logic
  • Loading branch information
mdickinson committed Dec 4, 2022
commit 455146856395dc0925d7b206ca3cd8fd0cc1bd66
36 changes: 15 additions & 21 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def __format__(self, format_spec, /):
(?P<minimumwidth>\d+)?
(?P<thousands_sep>[,_])?
(?:\.(?P<precision>\d+))?
(?P<specifier_type>[f%])
(?P<presentation_type>[f%])
""", re.DOTALL | re.IGNORECASE | re.VERBOSE).fullmatch

# Validate and parse the format specifier.
Expand All @@ -357,11 +357,11 @@ def __format__(self, format_spec, /):
minimumwidth = int(match["minimumwidth"] or "0")
thousands_sep = match["thousands_sep"]
precision = int(match["precision"] or "6")
specifier_type = match["specifier_type"]
presentation_type = match["presentation_type"]

# Get sign and output digits for the target number
negative = self < 0
shift = precision + 2 if specifier_type == "%" else precision
shift = precision + 2 if presentation_type == "%" else precision
significand = round(abs(self) * 10**shift)

# Assemble the output: before padding, it has the form
Expand All @@ -372,7 +372,7 @@ def __format__(self, format_spec, /):
dot_pos = len(digits) - precision
sign = "-" if negative and (significand or neg_zero_ok) else pos_sign
separator = "." if precision or alternate_form else ""
percent = "%" if specifier_type == "%" else ""
percent = "%" if presentation_type == "%" else ""
trailing = separator + digits[dot_pos:] + percent
leading = digits[:dot_pos]

Expand All @@ -395,23 +395,17 @@ def __format__(self, format_spec, /):

after_sign = leading + trailing

# Pad if a minimum width was given and we haven't already zero padded.
if zeropad or minimumwidth is None:
result = sign + after_sign
else:
padding = fill * (minimumwidth - len(sign) - len(after_sign))
if align == ">":
result = padding + sign + after_sign
elif align == "<":
result = sign + after_sign + padding
elif align == "=":
result = sign + padding + after_sign
else:
# Centered, with a leftwards bias when padding length is odd.
assert align == "^"
half = len(padding)//2
result = padding[:half] + sign + after_sign + padding[half:]
return result
# Pad if necessary and return.
padding = fill * (minimumwidth - len(sign) - len(after_sign))
if align == ">":
return padding + sign + after_sign
elif align == "<":
return sign + after_sign + padding
elif align == "^":
half = len(padding)//2
return padding[:half] + sign + after_sign + padding[half:]
else: # align == "="
return sign + padding + after_sign

def _operator_fallbacks(monomorphic_operator, fallback_operator):
"""Generates forward and reverse operators given a purely-rational
Expand Down