def test_joined_attr(): f = models.Review( app="Annoyed Birds", stars=5, reviewer_email="*****@*****.**", reviewer_name="Some One", ) print(f.serialize()) assert ulid.from_str(f.serialize()["ulid"]["S"]) assert ulid.from_str(f.serialize()["pk"]["S"].split("#")[-1]) assert f.serialize()["__type"]["S"] == "Review" assert f.serialize()["gsi1_pk"]["S"] == "REVIEW#[email protected]"
def from_response_body( cls: Type[_T], body: Dict[str, Any], url_index: int, urls: LazyPage[Dict[str, str]], *, cache_path: str = "", ) -> _T: """Loads a :class:`Frame` object from a response body. Arguments: body: The response body which contains the information of a frame, whose format should be like:: { "frameId": <str>, "frame": [ { "sensorName": <str>, "remotePath": <str>, "timestamp": <float>, "url": <str>, "label": {...} }, ... ... ] } url_index: The index of the url. urls: A sequence of mappings which key is the sensor name and value is the url. cache_path: The path to store the cache. Returns: The loaded :class:`Frame` object. """ # noqa: DAR101 # https://github.com/terrencepreilly/darglint/issues/120 try: frame_id = from_str(body["frameId"]) except ValueError: # Legacy fusion dataset use uuid as frame ID # Keep this code here to make SDK compatible with uuid frame_id = from_uuid(UUID(body["frameId"])) if cls._logger_flag: cls._logger_flag = False logger.warning( "WARNING: This is a legacy fusion dataset which use uuid as frame ID, " "it should be updated to ulid.") frame = cls(frame_id) for data_contents in body["frame"]: sensor_name = data_contents["sensorName"] url = URL.from_getter( lambda s=sensor_name: urls.items[url_index].get()[s], urls.pull) frame[sensor_name] = RemoteData.from_response_body( data_contents, url=url, cache_path=cache_path, ) return frame
def get_id_timestamp(uid: str) -> datetime: """Get the datetime that an ID was created. Args: uid (str): A unique ID string. Returns: (datetime): The date when a unique lexicographic ID was generated. """ return ulid.from_str(uid).timestamp().datetime
def timestamp(self): """ Method to return the timestamp of a fulid. Returns: timestamp (datetime object): Timestamp of the fulid. """ return ulid.from_str(self.str).timestamp().datetime
def from_response_body(cls: Type[_T], body: Dict[str, Any], frame_index: int, urls: Sequence[Mapping[str, str]]) -> _T: """Loads a :class:`Frame` object from a response body. Arguments: body: The response body which contains the information of a frame, whose format should be like:: { "frameId": <str>, "frame": [ { "sensorName": <str>, "remotePath": <str>, "timestamp": <float>, "label": {...} }, ... ... ] } frame_index: The index of the frame. urls: A sequence of mappings which key is the sensor name and value is the url. Returns: The loaded :class:`Frame` object. """ # noqa: DAR101 # https://github.com/terrencepreilly/darglint/issues/120 try: frame_id = from_str(body["frameId"]) except ValueError: # Legacy fusion dataset use uuid as frame ID # Keep this code here to make SDK compatible with uuid frame_id = from_uuid(UUID(body["frameId"])) if cls._logger_flag: # pylint: disable=protected-access cls._logger_flag = False # pylint: disable=protected-access logger.warning( "WARNING: This is a legacy fusion dataset which use uuid as frame ID, " "it should be updated to ulid.") frame = cls(frame_id) for data_contents in body["frame"]: sensor_name = data_contents["sensorName"] frame[sensor_name] = RemoteData.from_response_body( data_contents, _url_getter=lambda _, s=sensor_name: urls[frame_index][ s], # type: ignore[misc] ) return frame
def clean_tmp(): if not os.path.exists('./tmp'): return None file_list = os.listdir('./tmp') # print(file_list) os.chdir('./tmp') for file_name in file_list: check_time = ulid.from_str(file_name).timestamp( ).datetime + datetime.timedelta(hours=9, minutes=15) now = datetime.datetime.now() if now >= check_time: os.remove(file_name) s_now = now.strftime('%Y/%m/%d %H:%M:%S') print('[{0}] remove tmp_file:{1}'.format(s_now, file_name))
def deserialize(self, value): return ulid.from_str(value)
def __ge__(self: _S, other: _S) -> bool: return ulid.from_str(self) >= ulid.from_str(other)
def __lt__(self: _S, other: _S) -> bool: return ulid.from_str(self) < ulid.from_str(other)
def __hash__(self: _S) -> int: return hash(ulid.from_str(self))
def __eq__(self: _S, other: _S) -> bool: return ulid.from_str(self) == ulid.from_str(other)
def __init__(self: _S, value: str) -> _S: # Validation ulid.from_str(value) str.__init__(value)
#!/usr/bin/env python3 # # Copyright 2021 Graviti. Licensed under MIT License. # import ulid from .. import Frame, RemoteData _FRAME_ID = ulid.from_str("01F29QVWASMNGNA2FZBMZCDEG1") _FRAME_DATA = { "frameId": _FRAME_ID.str, "frame": [ { "sensorName": "sensor1", "remotePath": "test1.png", "timestamp": 1614945883, "label": {}, }, { "sensorName": "sensor2", "remotePath": "test2.png", "timestamp": 1614945884, "label": {}, }, ], }
def test_foo(): f = models.FooModel(foo="hello") print(f.serialize()) assert ulid.from_str(f.serialize()["ulid"]["S"]) assert ulid.from_str(f.serialize()["pk"]["S"].split("#")[-1]) assert f.serialize()["__type"]["S"] == "Foo"
def test_str_instance_hyphens(self): ULIDModel.objects.create(field='550e8400-e29b-41d4-a716-446655440000') loaded = ULIDModel.objects.get() self.assertEqual(loaded.field, ulid.from_str('2N1T201RMV87AAE5J4CSAM8000'))