]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/black/numerics.py
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_complex_number(text
: str) -> str:
29 """Formats a complex string like `10j`"""
32 return f
"{format_float_or_int_string(number)}{suffix}"
35 def format_float_or_int_string(text
: str) -> str:
36 """Formats a float string like "1.0"."""
40 before
, after
= text
.split(".")
41 return f
"{before or 0}.{after or 0}"
44 def normalize_numeric_literal(leaf
: Leaf
) -> None:
45 """Normalizes numeric (float, int, and complex) literals.
47 All letters used in the representation are normalized to lowercase."""
48 text
= leaf
.value
.lower()
49 if text
.startswith(("0o", "0b")):
50 # Leave octal and binary literals alone.
52 elif text
.startswith("0x"):
53 text
= format_hex(text
)
55 text
= format_scientific_notation(text
)
56 elif text
.endswith("j"):
57 text
= format_complex_number(text
)
59 text
= format_float_or_int_string(text
)