All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 Formatting numeric literals.
4 from blib2to3.pytree import Leaf
7 def format_hex(text: str) -> str:
9 Formats a hexadecimal string like "0x12B3"
11 before, after = text[:2], text[2:]
12 return f"{before}{after.upper()}"
15 def format_scientific_notation(text: str) -> str:
16 """Formats a numeric string utilizing scentific notation"""
17 before, after = text.split("e")
19 if after.startswith("-"):
22 elif after.startswith("+"):
24 before = format_float_or_int_string(before)
25 return f"{before}e{sign}{after}"
28 def format_long_or_complex_number(text: str) -> str:
29 """Formats a long or complex string like `10L` or `10j`"""
32 # Capitalize in "2L" because "l" looks too similar to "1".
35 return f"{format_float_or_int_string(number)}{suffix}"
38 def format_float_or_int_string(text: str) -> str:
39 """Formats a float string like "1.0"."""
43 before, after = text.split(".")
44 return f"{before or 0}.{after or 0}"
47 def normalize_numeric_literal(leaf: Leaf) -> None:
48 """Normalizes numeric (float, int, and complex) literals.
50 All letters used in the representation are normalized to lowercase (except
51 in Python 2 long literals).
53 text = leaf.value.lower()
54 if text.startswith(("0o", "0b")):
55 # Leave octal and binary literals alone.
57 elif text.startswith("0x"):
58 text = format_hex(text)
60 text = format_scientific_notation(text)
61 elif text.endswith(("j", "l")):
62 text = format_long_or_complex_number(text)
64 text = format_float_or_int_string(text)