-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitstring_options.py
More file actions
41 lines (34 loc) · 1.32 KB
/
bitstring_options.py
File metadata and controls
41 lines (34 loc) · 1.32 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
from __future__ import annotations
import bitstring
import os
class Options:
"""Internal class to create singleton module options instance."""
_instance = None
def __init__(self):
self._lsb0 = False
self._bytealigned = False
self.mxfp_overflow = 'saturate'
self.no_color = False
no_color = os.getenv('NO_COLOR')
self.no_color = True if no_color else False
def set_lsb0(self, value: bool) -> None:
"""Set whether bit numbering starts at 0 (True) or 1 (False) for the least significant bit."""
self._lsb0 = bool(value)
def __repr__(self) -> str:
attributes = {attr: getattr(self, attr) for attr in dir(self) if not attr.startswith('_') and (not callable(getattr(self, attr)))}
return '\n'.join((f'{attr}: {value!r}' for attr, value in attributes.items()))
def __new__(cls):
if cls._instance is None:
cls._instance = super(Options, cls).__new__(cls)
return cls._instance
class Colour:
def __new__(cls, use_colour: bool) -> Colour:
x = super().__new__(cls)
if use_colour:
cls.blue = '\x1b[34m'
cls.purple = '\x1b[35m'
cls.green = '\x1b[32m'
cls.off = '\x1b[0m'
else:
cls.blue = cls.purple = cls.green = cls.off = ''
return x