Coverage for MC6809/utils/humanize.py: 63%
35 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-06 19:50 +0100
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-06 19:50 +0100
1#!/usr/bin/env python
3"""
4 DragonPy - Humanize
5 ===================
7 :copyleft: 2013-2014 by the MC6809 team, see AUTHORS for more details.
8 :license: GNU GPL v3 or above, see LICENSE for more details.
9"""
12import locale
13import platform
14import sys
17def locale_format_number(val):
18 """
19 Depend on users local, so no active doctest here ;)
21 > locale_format_number(1234567.89)
22 '1.234.567.890'
23 """
24 try:
25 return locale.format('%d', val, 1)
26 except UnicodeDecodeError:
27 # For PyPy3, see: https://bitbucket.org/pypy/pypy/issue/1858/pypy3-localeformat-d-val-1
28 # return '{:n}'.format(val) # makes 1234567890.1234 to 1,23457e+09 :(
29 return f'{int(val):,}'
32def byte2bit_string(data):
33 """
34 >>> byte2bit_string(0x1b)
35 '00011011'
36 """
37 return f'{data:08b}'
40def nice_hex(v):
41 """
42 >>> nice_hex(0x1)
43 '$01'
44 >>> nice_hex(0x123)
45 '$0123'
46 """
47 if v < 0x100:
48 return f"${v:02x}"
49 if v < 0x10000: 49 ↛ 51line 49 didn't jump to line 51, because the condition on line 49 was never false
50 return f"${v:04x}"
51 return f"${v:x}"
54def hex_repr(d):
55 """
56 >>> hex_repr({"A":0x1,"B":0xabc})
57 'A=$01 B=$0abc'
58 """
59 txt = []
60 for k, v in sorted(d.items()):
61 if isinstance(v, int): 61 ↛ 64line 61 didn't jump to line 64, because the condition on line 61 was never false
62 txt.append(f"{k}={nice_hex(v)}")
63 else:
64 txt.append(f"{k}={v}")
65 return " ".join(txt)
68def cc_value2txt(status):
69 """
70 >>> cc_value2txt(0x50)
71 '.F.I....'
72 >>> cc_value2txt(0x54)
73 '.F.I.Z..'
74 >>> cc_value2txt(0x59)
75 '.F.IN..C'
76 """
77 return "".join(
78 "." if status & x == 0 else char
79 for char, x in zip("EFHINZVC", (128, 64, 32, 16, 8, 4, 2, 1))
80 )
83def get_python_info():
84 implementation = platform.python_implementation()
85 if implementation == "CPython":
86 return f"{implementation} v{platform.python_version()} [{platform.python_compiler()}]"
87 # e.g.: CPython v2.7.6 [GCC 4.8.2]
88 elif implementation == "PyPy":
89 ver_info = sys.version.split("(", 1)[0]
90 ver_info += sys.version.split("\n")[-1]
91 return f"Python {ver_info}"
92 # e.g.: Python 2.7.6 [PyPy 2.3.1 with GCC 4.8.2]
93 else:
94 return "{} {}".format(
95 sys.executable,
96 sys.version.replace("\n", " ")
97 )
100if __name__ == "__main__":
101 import doctest
102 print(doctest.testmod(verbose=0))