| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
from __future__ import print_function, absolute_import, division, unicode_literals |
| 4 |
|
| 5 |
import sys |
| 6 |
from .compat import no_limit_int # NOQA |
| 7 |
from strictyaml.ruamel.anchor import Anchor |
| 8 |
|
| 9 |
if False: # MYPY |
| 10 |
from typing import Text, Any, Dict, List # NOQA |
| 11 |
|
| 12 |
__all__ = ["ScalarFloat", "ExponentialFloat", "ExponentialCapsFloat"] |
| 13 |
|
| 14 |
|
| 15 |
class ScalarFloat(float): |
| 16 |
def __new__(cls, *args, **kw): |
| 17 |
# type: (Any, Any, Any) -> Any |
| 18 |
width = kw.pop("width", None) # type: ignore |
| 19 |
prec = kw.pop("prec", None) # type: ignore |
| 20 |
m_sign = kw.pop("m_sign", None) # type: ignore |
| 21 |
m_lead0 = kw.pop("m_lead0", 0) # type: ignore |
| 22 |
exp = kw.pop("exp", None) # type: ignore |
| 23 |
e_width = kw.pop("e_width", None) # type: ignore |
| 24 |
e_sign = kw.pop("e_sign", None) # type: ignore |
| 25 |
underscore = kw.pop("underscore", None) # type: ignore |
| 26 |
anchor = kw.pop("anchor", None) # type: ignore |
| 27 |
v = float.__new__(cls, *args, **kw) # type: ignore |
| 28 |
v._width = width |
| 29 |
v._prec = prec |
| 30 |
v._m_sign = m_sign |
| 31 |
v._m_lead0 = m_lead0 |
| 32 |
v._exp = exp |
| 33 |
v._e_width = e_width |
| 34 |
v._e_sign = e_sign |
| 35 |
v._underscore = underscore |
| 36 |
if anchor is not None: |
| 37 |
v.yaml_set_anchor(anchor, always_dump=True) |
| 38 |
return v |
| 39 |
|
| 40 |
def __iadd__(self, a): # type: ignore |
| 41 |
# type: (Any) -> Any |
| 42 |
return float(self) + a |
| 43 |
x = type(self)(self + a) |
| 44 |
x._width = self._width |
| 45 |
x._underscore = ( |
| 46 |
self._underscore[:] if self._underscore is not None else None |
| 47 |
) # NOQA |
| 48 |
return x |
| 49 |
|
| 50 |
def __ifloordiv__(self, a): # type: ignore |
| 51 |
# type: (Any) -> Any |
| 52 |
return float(self) // a |
| 53 |
x = type(self)(self // a) |
| 54 |
x._width = self._width |
| 55 |
x._underscore = ( |
| 56 |
self._underscore[:] if self._underscore is not None else None |
| 57 |
) # NOQA |
| 58 |
return x |
| 59 |
|
| 60 |
def __imul__(self, a): # type: ignore |
| 61 |
# type: (Any) -> Any |
| 62 |
return float(self) * a |
| 63 |
x = type(self)(self * a) |
| 64 |
x._width = self._width |
| 65 |
x._underscore = ( |
| 66 |
self._underscore[:] if self._underscore is not None else None |
| 67 |
) # NOQA |
| 68 |
x._prec = self._prec # check for others |
| 69 |
return x |
| 70 |
|
| 71 |
def __ipow__(self, a): # type: ignore |
| 72 |
# type: (Any) -> Any |
| 73 |
return float(self) ** a |
| 74 |
x = type(self)(self ** a) |
| 75 |
x._width = self._width |
| 76 |
x._underscore = ( |
| 77 |
self._underscore[:] if self._underscore is not None else None |
| 78 |
) # NOQA |
| 79 |
return x |
| 80 |
|
| 81 |
def __isub__(self, a): # type: ignore |
| 82 |
# type: (Any) -> Any |
| 83 |
return float(self) - a |
| 84 |
x = type(self)(self - a) |
| 85 |
x._width = self._width |
| 86 |
x._underscore = ( |
| 87 |
self._underscore[:] if self._underscore is not None else None |
| 88 |
) # NOQA |
| 89 |
return x |
| 90 |
|
| 91 |
@property |
| 92 |
def anchor(self): |
| 93 |
# type: () -> Any |
| 94 |
if not hasattr(self, Anchor.attrib): |
| 95 |
setattr(self, Anchor.attrib, Anchor()) |
| 96 |
return getattr(self, Anchor.attrib) |
| 97 |
|
| 98 |
def yaml_anchor(self, any=False): |
| 99 |
# type: (bool) -> Any |
| 100 |
if not hasattr(self, Anchor.attrib): |
| 101 |
return None |
| 102 |
if any or self.anchor.always_dump: |
| 103 |
return self.anchor |
| 104 |
return None |
| 105 |
|
| 106 |
def yaml_set_anchor(self, value, always_dump=False): |
| 107 |
# type: (Any, bool) -> None |
| 108 |
self.anchor.value = value |
| 109 |
self.anchor.always_dump = always_dump |
| 110 |
|
| 111 |
def dump(self, out=sys.stdout): |
| 112 |
# type: (Any) -> Any |
| 113 |
out.write( |
| 114 |
"ScalarFloat({}| w:{}, p:{}, s:{}, lz:{}, _:{}|{}, w:{}, s:{})\n".format( |
| 115 |
self, |
| 116 |
self._width, # type: ignore |
| 117 |
self._prec, # type: ignore |
| 118 |
self._m_sign, # type: ignore |
| 119 |
self._m_lead0, # type: ignore |
| 120 |
self._underscore, # type: ignore |
| 121 |
self._exp, # type: ignore |
| 122 |
self._e_width, # type: ignore |
| 123 |
self._e_sign, # type: ignore |
| 124 |
) |
| 125 |
) |
| 126 |
|
| 127 |
|
| 128 |
class ExponentialFloat(ScalarFloat): |
| 129 |
def __new__(cls, value, width=None, underscore=None): |
| 130 |
# type: (Any, Any, Any) -> Any |
| 131 |
return ScalarFloat.__new__(cls, value, width=width, underscore=underscore) |
| 132 |
|
| 133 |
|
| 134 |
class ExponentialCapsFloat(ScalarFloat): |
| 135 |
def __new__(cls, value, width=None, underscore=None): |
| 136 |
# type: (Any, Any, Any) -> Any |
| 137 |
return ScalarFloat.__new__(cls, value, width=width, underscore=underscore) |