f20080305d
Определение специфических рекомендаций (матрица профилей §8):
1. Ж/д слой (закрыт мёртвый railway ×2.5 у РАС):
- /api/v1/water/{case_id} отдаёт railway=rail как LineString
(без service/industrial/military веток), кэш общий v2;
- railway_warning «перекрыть/проверить немедленно» по профилям;
- SearchMap: Polyline слой ж/д (тёмно-красный), счётчики 💧/🚂.
2. cant_swim → профиль не_умеет_плавать (water ×3.0, без изменения
радиуса, critical_warning «обследовать водоёмы НЕМЕДЛЕННО»):
- раньше чекбокс влиял только на текст, в скоринге был пробел;
- derive в analyze._derive_profiles — работает и для closed_cases.
3. unmodeled_profiles: ДЦП/слабое зрение/слух — честная пометка
«вне поведенческой модели» с пояснением (vector_tasks B12:
профили без аналога не выдавать за учтённые); блок на фронте
в карточке здоровья.
Площадь воды: сферический эксцесс, проверен на квадрате 53° (744017 м²
vs 743272 точного). Тесты: 202 passed (новый test_cant_swim_profile).
139 lines
4.0 KiB
Python
139 lines
4.0 KiB
Python
"""
|
|
Common functions for providing cross-python version compatibility.
|
|
"""
|
|
import sys
|
|
import re
|
|
import binascii
|
|
from six import integer_types
|
|
|
|
|
|
def str_idx_as_int(string, index):
|
|
"""Take index'th byte from string, return as integer"""
|
|
val = string[index]
|
|
if isinstance(val, integer_types):
|
|
return val
|
|
return ord(val)
|
|
|
|
|
|
if sys.version_info < (3, 0): # pragma: no branch
|
|
import platform
|
|
|
|
def normalise_bytes(buffer_object):
|
|
"""Cast the input into array of bytes."""
|
|
# flake8 runs on py3 where `buffer` indeed doesn't exist...
|
|
return buffer(buffer_object) # noqa: F821
|
|
|
|
def hmac_compat(ret):
|
|
return ret
|
|
|
|
if (
|
|
sys.version_info < (2, 7)
|
|
or sys.version_info < (2, 7, 4)
|
|
or platform.system() == "Java"
|
|
): # pragma: no branch
|
|
|
|
def remove_whitespace(text):
|
|
"""Removes all whitespace from passed in string"""
|
|
return re.sub(r"\s+", "", text)
|
|
|
|
def compat26_str(val):
|
|
return str(val)
|
|
|
|
def bit_length(val):
|
|
if val == 0:
|
|
return 0
|
|
return len(bin(val)) - 2
|
|
|
|
else:
|
|
|
|
def remove_whitespace(text):
|
|
"""Removes all whitespace from passed in string"""
|
|
return re.sub(r"\s+", "", text, flags=re.UNICODE)
|
|
|
|
def compat26_str(val):
|
|
return val
|
|
|
|
def bit_length(val):
|
|
"""Return number of bits necessary to represent an integer."""
|
|
return val.bit_length()
|
|
|
|
def b2a_hex(val):
|
|
return binascii.b2a_hex(compat26_str(val))
|
|
|
|
def a2b_hex(val):
|
|
try:
|
|
return bytearray(binascii.a2b_hex(val))
|
|
except Exception as e:
|
|
raise ValueError("base16 error: %s" % e)
|
|
|
|
def bytes_to_int(val, byteorder):
|
|
"""Convert bytes to an int."""
|
|
if not val:
|
|
return 0
|
|
if byteorder == "big":
|
|
return int(b2a_hex(val), 16)
|
|
if byteorder == "little":
|
|
return int(b2a_hex(val[::-1]), 16)
|
|
raise ValueError("Only 'big' and 'little' endian supported")
|
|
|
|
def int_to_bytes(val, length=None, byteorder="big"):
|
|
"""Return number converted to bytes"""
|
|
if length is None:
|
|
length = byte_length(val)
|
|
if byteorder == "big":
|
|
return bytearray(
|
|
(val >> i) & 0xFF for i in reversed(range(0, length * 8, 8))
|
|
)
|
|
if byteorder == "little":
|
|
return bytearray(
|
|
(val >> i) & 0xFF for i in range(0, length * 8, 8)
|
|
)
|
|
raise ValueError("Only 'big' or 'little' endian supported")
|
|
|
|
else:
|
|
|
|
def hmac_compat(data):
|
|
return data
|
|
|
|
def normalise_bytes(buffer_object):
|
|
"""Cast the input into array of bytes."""
|
|
return memoryview(buffer_object).cast("B")
|
|
|
|
def compat26_str(val):
|
|
return val
|
|
|
|
def remove_whitespace(text):
|
|
"""Removes all whitespace from passed in string"""
|
|
return re.sub(r"\s+", "", text, flags=re.UNICODE)
|
|
|
|
def a2b_hex(val):
|
|
try:
|
|
return bytearray(binascii.a2b_hex(bytearray(val, "ascii")))
|
|
except Exception as e:
|
|
raise ValueError("base16 error: %s" % e)
|
|
|
|
# pylint: disable=invalid-name
|
|
# pylint is stupid here and doesn't notice it's a function, not
|
|
# constant
|
|
bytes_to_int = int.from_bytes
|
|
# pylint: enable=invalid-name
|
|
|
|
def bit_length(val):
|
|
"""Return number of bits necessary to represent an integer."""
|
|
return val.bit_length()
|
|
|
|
def int_to_bytes(val, length=None, byteorder="big"):
|
|
"""Convert integer to bytes."""
|
|
if length is None:
|
|
length = byte_length(val)
|
|
# for gmpy we need to convert back to native int
|
|
if not isinstance(val, int):
|
|
val = int(val)
|
|
return bytearray(val.to_bytes(length=length, byteorder=byteorder))
|
|
|
|
|
|
def byte_length(val):
|
|
"""Return number of bytes necessary to represent an integer."""
|
|
length = bit_length(val)
|
|
return (length + 7) // 8
|