forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode_format.py
More file actions
76 lines (62 loc) · 2.05 KB
/
unicode_format.py
File metadata and controls
76 lines (62 loc) · 2.05 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Handles the "Unicode" unit format.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from . import console
if TYPE_CHECKING:
from typing import ClassVar
from astropy.units import NamedUnit
from astropy.units.typing import Real
class Unicode(console.Console):
"""
Output-only format to display pretty formatting at the console
using Unicode characters.
For example::
>>> import astropy.units as u
>>> print(u.bar.decompose().to_string('unicode'))
100000 kg m⁻¹ s⁻²
>>> print(u.bar.decompose().to_string('unicode', fraction='multiline'))
kg
100000 ────
m s²
>>> print(u.bar.decompose().to_string('unicode', fraction='inline'))
100000 kg / (m s²)
"""
_times: ClassVar[str] = "×"
_line: ClassVar[str] = "─"
@classmethod
def _format_mantissa(cls, m: str) -> str:
return m.replace("-", "−")
@classmethod
def _format_unit_power(cls, unit: NamedUnit, power: Real = 1) -> str:
name = unit._get_format_name(cls.name)
# Check for superscript units
if power != 1:
if name in ("°", "e⁻", "″", "′", "ʰ"):
name = unit.short_names[0]
name += cls._format_power(power)
return name
@classmethod
def _format_superscript(cls, number: str) -> str:
mapping = str.maketrans(
{
"0": "⁰",
"1": "¹",
"2": "²",
"3": "³",
"4": "⁴",
"5": "⁵",
"6": "⁶",
"7": "⁷",
"8": "⁸",
"9": "⁹",
"-": "⁻",
"−": "⁻",
# This is actually a "raised omission bracket", but it's
# the closest thing I could find to a superscript solidus.
"/": "⸍",
}
)
return number.translate(mapping)