File size: 11,653 Bytes
28bf99c |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
from typing import Any, Dict, List, Optional
from mbstrdecoder import MultiByteStrDecoder
from typepy import Integer, StrictLevel, Typecode, TypeConversionError
from ._align import Align
from ._align_getter import align_getter
from ._base import DataPeropertyBase
from ._common import DefaultValue
from ._container import ListContainer, MinMaxContainer
from ._dataproperty import DataProperty
from ._function import calc_ascii_char_width
from .typing import FloatType
class ColumnDataProperty(DataPeropertyBase):
__slots__ = (
"__header_ascii_char_width",
"__body_ascii_char_width",
"__column_index",
"__dp_list",
"__float_type",
"__format_map",
"__is_calculate",
"__max_precision",
"__minmax_integer_digits",
"__minmax_decimal_places",
"__minmax_additional_format_len",
"__typecode_bitmap",
)
@property
def align(self) -> Align:
return align_getter.get_align_from_typecode(self.typecode)
@property
def bit_length(self) -> Optional[int]:
if self.typecode != Typecode.INTEGER:
return None
bit_length = 0
for value_dp in self.__dp_list:
try:
bit_length = max(bit_length, int.bit_length(value_dp.data))
except TypeError:
pass
return bit_length
@property
def column_index(self) -> int:
return self.__column_index
@property
def decimal_places(self) -> Optional[int]:
return self._decimal_places
@property
def ascii_char_width(self) -> int:
return max(self.__header_ascii_char_width, self.__body_ascii_char_width)
@property
def minmax_integer_digits(self) -> MinMaxContainer:
return self.__minmax_integer_digits
@property
def minmax_decimal_places(self) -> ListContainer:
return self.__minmax_decimal_places
@property
def minmax_additional_format_len(self) -> MinMaxContainer:
return self.__minmax_additional_format_len
def __init__(
self,
column_index: int,
float_type: Optional[FloatType],
min_width: int = 0,
format_flags: Optional[int] = None,
is_formatting_float: bool = True,
datetime_format_str: str = DefaultValue.DATETIME_FORMAT,
east_asian_ambiguous_width: int = 1,
max_precision: int = DefaultValue.MAX_PRECISION,
) -> None:
super().__init__(
format_flags=format_flags,
is_formatting_float=is_formatting_float,
datetime_format_str=datetime_format_str,
east_asian_ambiguous_width=east_asian_ambiguous_width,
)
self.__header_ascii_char_width = 0
self.__body_ascii_char_width = min_width
self.__column_index = column_index
self.__float_type = float_type
self.__is_calculate = True
self.__dp_list: List[DataProperty] = []
self.__minmax_integer_digits = MinMaxContainer()
self.__minmax_decimal_places = ListContainer()
self.__minmax_additional_format_len = MinMaxContainer()
self.__max_precision = max_precision
self.__typecode_bitmap = Typecode.NONE.value
self.__calc_typecode_from_bitmap()
self.__format_map: Dict[Typecode, str] = self._formatter.make_format_map(
decimal_places=self._decimal_places
)
def __repr__(self) -> str:
element_list = []
if self.column_index is not None:
element_list.append(f"column={self.column_index}")
element_list.extend(
[
f"type={self.typename}",
f"align={self.align.align_string}",
f"ascii_width={self.ascii_char_width}",
]
)
if Integer(self.bit_length).is_type():
element_list.append(f"bit_len={self.bit_length}")
if self.minmax_integer_digits.has_value():
if self.minmax_integer_digits.is_same_value():
value = f"int_digits={self.minmax_integer_digits.min_value}"
else:
value = f"int_digits=({self.minmax_integer_digits})"
element_list.append(value)
if self.minmax_decimal_places.has_value():
if self.minmax_decimal_places.is_same_value():
value = f"decimal_places={self.minmax_decimal_places.min_value}"
else:
value = f"decimal_places=({self.minmax_decimal_places})"
element_list.append(value)
if not self.minmax_additional_format_len.is_zero():
if self.minmax_additional_format_len.is_same_value():
value = f"extra_len={self.minmax_additional_format_len.min_value}"
else:
value = f"extra_len=({self.minmax_additional_format_len})"
element_list.append(value)
return ", ".join(element_list)
def dp_to_str(self, value_dp: DataProperty) -> str:
if value_dp.typecode == Typecode.STRING:
return str(value_dp.data)
try:
value = self.__preprocess_value_before_tostring(value_dp)
except TypeConversionError:
return self.__format_map.get(value_dp.typecode, "{:s}").format(value_dp.data)
to_string_format_str = self.__get_tostring_format(value_dp)
try:
return to_string_format_str.format(value)
except (ValueError, TypeError):
pass
try:
return MultiByteStrDecoder(value).unicode_str
except ValueError:
pass
return str(value)
def extend_width(self, ascii_char_width: int) -> None:
self.extend_header_width(ascii_char_width)
self.extend_body_width(ascii_char_width)
def extend_header_width(self, ascii_char_width: int) -> None:
self.__header_ascii_char_width += ascii_char_width
def extend_body_width(self, ascii_char_width: int) -> None:
self.__body_ascii_char_width += ascii_char_width
def update_header(self, header_db: DataProperty) -> None:
self.__header_ascii_char_width = header_db.ascii_char_width
def update_body(self, value_dp: DataProperty) -> None:
if value_dp.is_include_ansi_escape:
assert value_dp.no_ansi_escape_dp
value_dp = value_dp.no_ansi_escape_dp
self.__typecode_bitmap |= value_dp.typecode.value
self.__calc_typecode_from_bitmap()
if value_dp.typecode in (Typecode.REAL_NUMBER, Typecode.INTEGER):
self.__minmax_integer_digits.update(value_dp.integer_digits)
self.__minmax_decimal_places.update(value_dp.decimal_places)
self.__update_decimal_places()
self.__minmax_additional_format_len.update(value_dp.additional_format_len)
self.__dp_list.append(value_dp)
self.__update_ascii_char_width()
def merge(self, column_dp: "ColumnDataProperty") -> None:
self.__typecode_bitmap |= column_dp.typecode.value
self.__calc_typecode_from_bitmap()
self.__minmax_integer_digits.merge(column_dp.minmax_integer_digits)
self.__minmax_decimal_places.merge(column_dp.minmax_decimal_places)
self.__update_decimal_places()
self.__minmax_additional_format_len.merge(column_dp.minmax_additional_format_len)
self.__body_ascii_char_width = max(self.__body_ascii_char_width, column_dp.ascii_char_width)
self.__update_ascii_char_width()
def begin_update(self) -> None:
self.__is_calculate = False
def end_update(self) -> None:
self.__is_calculate = True
self.__calc_typecode_from_bitmap()
self.__update_decimal_places()
self.__update_ascii_char_width()
def __is_not_single_typecode(self, typecode_bitmap: int) -> bool:
return bool(
self.__typecode_bitmap & typecode_bitmap and self.__typecode_bitmap & ~typecode_bitmap
)
def __is_float_typecode(self) -> bool:
FLOAT_TYPECODE_BMP = (
Typecode.REAL_NUMBER.value | Typecode.INFINITY.value | Typecode.NAN.value
)
NUMBER_TYPECODE_BMP = FLOAT_TYPECODE_BMP | Typecode.INTEGER.value
if self.__is_not_single_typecode(NUMBER_TYPECODE_BMP | Typecode.NULL_STRING.value):
return False
if (
bin(self.__typecode_bitmap & (FLOAT_TYPECODE_BMP | Typecode.NULL_STRING.value)).count(
"1"
)
>= 2
):
return True
if bin(self.__typecode_bitmap & NUMBER_TYPECODE_BMP).count("1") >= 2:
return True
return False
def __calc_body_ascii_char_width(self) -> int:
width_list = [self.__body_ascii_char_width]
for value_dp in self.__dp_list:
if value_dp.is_include_ansi_escape:
assert value_dp.no_ansi_escape_dp
value_dp = value_dp.no_ansi_escape_dp
width_list.append(
calc_ascii_char_width(self.dp_to_str(value_dp), self._east_asian_ambiguous_width)
)
return max(width_list)
def __calc_decimal_places(self) -> Optional[int]:
if self.minmax_decimal_places.max_value is None:
return None
return min(self.__max_precision, int(self.minmax_decimal_places.max_value))
def __get_tostring_format(self, value_dp: DataProperty) -> str:
if self.typecode == Typecode.STRING:
return self.__format_map.get(value_dp.typecode, "{:s}")
return self.__format_map.get(self.typecode, "{:s}")
def __get_typecode_from_bitmap(self) -> Typecode:
if self.__is_float_typecode():
return Typecode.REAL_NUMBER
if any(
[
self.__is_not_single_typecode(Typecode.BOOL.value),
self.__is_not_single_typecode(Typecode.DATETIME.value),
]
):
return Typecode.STRING
typecode_list = [
Typecode.STRING,
Typecode.REAL_NUMBER,
Typecode.INTEGER,
Typecode.DATETIME,
Typecode.DICTIONARY,
Typecode.IP_ADDRESS,
Typecode.LIST,
Typecode.BOOL,
Typecode.INFINITY,
Typecode.NAN,
Typecode.NULL_STRING,
]
for typecode in typecode_list:
if self.__typecode_bitmap & typecode.value:
return typecode
if self.__typecode_bitmap == Typecode.NONE.value:
return Typecode.NONE
return Typecode.STRING
def __update_ascii_char_width(self) -> None:
if not self.__is_calculate:
return
self.__body_ascii_char_width = self.__calc_body_ascii_char_width()
def __update_decimal_places(self) -> None:
if not self.__is_calculate:
return
self._decimal_places = self.__calc_decimal_places()
self.__format_map = self._formatter.make_format_map(decimal_places=self._decimal_places)
def __calc_typecode_from_bitmap(self) -> None:
if not self.__is_calculate:
return
self._typecode = self.__get_typecode_from_bitmap()
def __preprocess_value_before_tostring(self, value_dp: DataProperty) -> Any:
if self.typecode == value_dp.typecode or self.typecode in [
Typecode.STRING,
Typecode.BOOL,
Typecode.DATETIME,
]:
return value_dp.data
return self.type_class(
value_dp.data,
strict_level=StrictLevel.MIN,
float_type=self.__float_type,
strip_ansi_escape=False,
).convert()
|