1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """ ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Formatting numeric literals. ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | """ ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from blib2to3.pytree import Leaf ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | 0006 0005 0003 0000 0003 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def format_hex(text: str) -> str: 0006 0005 0003 0000 0003 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """ 0006 0005 0003 0000 0003 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Formats a hexadecimal string like "0x12B3" 0006 0005 0003 0000 0003 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """ 0006 0005 0003 0000 0003 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | before, after = text[:2], text[2:] 0006 0005 0003 0000 0003 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return f"{before}{after.upper()}" ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def format_scientific_notation(text: str) -> str: 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """Formats a numeric string utilizing scientific notation""" 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | before, after = text.split("e") 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | sign = "" 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | if after.startswith("-"): 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | after = after[1:] 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | sign = "-" 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | elif after.startswith("+"): 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | after = after[1:] 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | before = format_float_or_int_string(before) 0011 0013 0010 0000 0000 0000 0001 -- 03 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return f"{before}e{sign}{after}" ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | 0005 0006 0004 0000 0000 0000 0001 -- 01 001 001 002 002 002 004 0004.00 0004.00 0001.00 | def format_complex_number(text: str) -> str: 0005 0006 0004 0000 0000 0000 0001 -- 01 001 001 002 002 002 004 0004.00 0004.00 0001.00 | """Formats a complex string like `10j`""" 0005 0006 0004 0000 0000 0000 0001 -- 01 001 001 002 002 002 004 0004.00 0004.00 0001.00 | number = text[:-1] 0005 0006 0004 0000 0000 0000 0001 -- 01 001 001 002 002 002 004 0004.00 0004.00 0001.00 | suffix = text[-1] 0005 0006 0004 0000 0000 0000 0001 -- 01 001 001 002 002 002 004 0004.00 0004.00 0001.00 | return f"{format_float_or_int_string(number)}{suffix}" ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | 0007 0006 0005 0000 0000 0001 0001 -- 04 002 005 003 006 007 009 0025.27 0030.32 0001.20 | def format_float_or_int_string(text: str) -> str: 0007 0006 0005 0000 0000 0001 0001 -- 04 002 005 003 006 007 009 0025.27 0030.32 0001.20 | """Formats a float string like "1.0".""" 0007 0006 0005 0000 0000 0001 0001 -- 04 002 005 003 006 007 009 0025.27 0030.32 0001.20 | if "." not in text: 0007 0006 0005 0000 0000 0001 0001 -- 04 002 005 003 006 007 009 0025.27 0030.32 0001.20 | return text 0007 0006 0005 0000 0000 0001 0001 -- 04 002 005 003 006 007 009 0025.27 0030.32 0001.20 | 0007 0006 0005 0000 0000 0001 0001 -- 04 002 005 003 006 007 009 0025.27 0030.32 0001.20 | before, after = text.split(".") 0007 0006 0005 0000 0000 0001 0001 -- 04 002 005 003 006 007 009 0025.27 0030.32 0001.20 | return f"{before or 0}.{after or 0}" ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | def normalize_numeric_literal(leaf: Leaf) -> None: 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | """Normalizes numeric (float, int, and complex) literals. 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | All letters used in the representation are normalized to lowercase.""" 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | text = leaf.value.lower() 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | if text.startswith(("0o", "0b")): 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | # Leave octal and binary literals alone. 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | pass 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | elif text.startswith("0x"): 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | text = format_hex(text) 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | elif "e" in text: 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | text = format_scientific_notation(text) 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | elif text.endswith("j"): 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | text = format_complex_number(text) 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | else: 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | text = format_float_or_int_string(text) 0017 0014 0013 0001 0002 0001 0001 -- 05 001 002 001 002 003 003 0004.75 0002.38 0000.50 | leaf.value = text