| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import numpy as np |
| 10 |
|
| 11 |
import h5py |
| 12 |
from datetime import datetime, timezone |
| 13 |
from uuid import uuid4, UUID |
| 14 |
from ..exceptions import exceptions |
| 15 |
from . import names |
| 16 |
|
| 17 |
|
| 18 |
vlen_str_dtype = h5py.string_dtype(encoding='utf-8', length=None) |
| 19 |
|
| 20 |
|
| 21 |
def create_id(): |
| 22 |
""" |
| 23 |
Creates an ID as used for nix entities. |
| 24 |
|
| 25 |
:returns: The ID. |
| 26 |
:rtype: str |
| 27 |
""" |
| 28 |
return str(uuid4()) |
| 29 |
|
| 30 |
|
| 31 |
def is_uuid(id_str): |
| 32 |
try: |
| 33 |
UUID(str(id_str)) |
| 34 |
return True |
| 35 |
except ValueError: |
| 36 |
return False |
| 37 |
|
| 38 |
|
| 39 |
def check_entity_name_and_type(name, type_): |
| 40 |
check_entity_name(name) |
| 41 |
check_entity_type(type_) |
| 42 |
|
| 43 |
|
| 44 |
def check_entity_type(type_): |
| 45 |
if not type_: |
| 46 |
raise ValueError("String provided for entity type is empty!") |
| 47 |
|
| 48 |
|
| 49 |
def check_entity_name(name): |
| 50 |
if not name: |
| 51 |
raise ValueError("String provided for entity name is empty!") |
| 52 |
if not names.check(name): |
| 53 |
raise ValueError("String provided for entity name is invalid!") |
| 54 |
|
| 55 |
|
| 56 |
def check_entity_id(id_): |
| 57 |
if not is_uuid(id_): |
| 58 |
raise ValueError("String provided for id is not a valid UUID") |
| 59 |
|
| 60 |
|
| 61 |
def check_empty_str(string, field_name): |
| 62 |
if not string: |
| 63 |
raise ValueError("String provided is empty! {}".format(field_name)) |
| 64 |
|
| 65 |
|
| 66 |
def check_name_or_id(name_or_id): |
| 67 |
if not name_or_id: |
| 68 |
raise ValueError("String provided for entity name is empty!") |
| 69 |
|
| 70 |
|
| 71 |
def check_entity_input(entity, raise_exception=True): |
| 72 |
if entity is not None: |
| 73 |
return True |
| 74 |
if raise_exception: |
| 75 |
raise exceptions.UninitializedEntity() |
| 76 |
return False |
| 77 |
|
| 78 |
|
| 79 |
def now_int(): |
| 80 |
""" |
| 81 |
Returns the current POSIX time as an integer. |
| 82 |
|
| 83 |
:return: integer POSIX time |
| 84 |
""" |
| 85 |
now = datetime.now() - datetime(1970, 1, 1) |
| 86 |
return int(now.total_seconds()) |
| 87 |
|
| 88 |
|
| 89 |
def time_to_str(time): |
| 90 |
""" |
| 91 |
Returns the time represented by the parameter in the format of Boost's |
| 92 |
posix time `to_iso_string` function. |
| 93 |
|
| 94 |
:param time: integer POSIX time |
| 95 |
:type time: int |
| 96 |
|
| 97 |
:return: string in the form "YYYYMMDDTHHMMSS", where T is the date-time separator |
| 98 |
:rtype: str |
| 99 |
""" |
| 100 |
dt = datetime.fromtimestamp(time, timezone.utc) |
| 101 |
return dt.strftime("%Y%m%dT%H%M%S").encode("utf-8") |
| 102 |
|
| 103 |
|
| 104 |
def str_to_time(time_str): |
| 105 |
""" |
| 106 |
Returns the POSIX time represented by the given string as an integer. |
| 107 |
|
| 108 |
:param time_str: string in the form "YYYYMMDDTHHMMSS", where T is the date-time separator |
| 109 |
:type time_str: str |
| 110 |
|
| 111 |
:return: integer POSIX time |
| 112 |
:rtype: int |
| 113 |
""" |
| 114 |
if isinstance(time_str, bytes): |
| 115 |
time_str = time_str.decode() |
| 116 |
dt = datetime.strptime(time_str, "%Y%m%dT%H%M%S") - datetime(1970, 1, 1) |
| 117 |
return int(dt.total_seconds()) |
| 118 |
|
| 119 |
|
| 120 |
def check_attr_type(value, type_): |
| 121 |
""" |
| 122 |
Checks if a value is of a given type and raises an exception if the check |
| 123 |
fails. The check does not fail if value is None. |
| 124 |
Specifying `str` for type checks against all string types |
| 125 |
(str, bytes, basestring). |
| 126 |
|
| 127 |
|
| 128 |
:param value: the value to check |
| 129 |
:param type_: the type to check against |
| 130 |
""" |
| 131 |
if value is not None and not isinstance(value, type_): |
| 132 |
raise exceptions.InvalidAttrType(type_, value) |
| 133 |
|
| 134 |
|
| 135 |
def apply_polynomial(coefficients, origin, data): |
| 136 |
data[:] = data[:] - origin |
| 137 |
if coefficients: |
| 138 |
data[:] = np.polynomial.polynomial.polyval(data, coefficients) |