layrz-sdk 4.0.13__py3-none-any.whl → 4.0.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of layrz-sdk might be problematic. Click here for more details.

@@ -0,0 +1,5 @@
1
+ """decorators"""
2
+
3
+ from .func_timing import func_timing
4
+
5
+ __all__ = ['func_timing']
@@ -0,0 +1,96 @@
1
+ """Timing decorator"""
2
+
3
+ import asyncio
4
+ import logging
5
+ import os
6
+ from collections.abc import Callable
7
+ from functools import wraps
8
+ from typing import Any, ParamSpec, TypeVar, overload
9
+
10
+ T = TypeVar('T')
11
+ P = ParamSpec('P')
12
+
13
+ log = logging.getLogger(__name__)
14
+
15
+ SHOULD_DISPLAY = os.environ.get('LAYRZ_SDK_DISPLAY_TIMING', '1') == '1'
16
+
17
+
18
+ @overload
19
+ def func_timing(func: Callable[P, T]) -> Callable[P, T]: ...
20
+
21
+
22
+ @overload
23
+ def func_timing(*, depth: int) -> Callable[[Callable[P, T]], Callable[P, T]]: ...
24
+
25
+
26
+ def func_timing(
27
+ func: Callable[P, T] | None = None,
28
+ *,
29
+ depth: int = 0,
30
+ ) -> Any:
31
+ """
32
+ Decorator to time a function execution.
33
+
34
+ :param depth: The depth of the function call for logging indentation.
35
+ :return: The wrapped function with timing functionality.
36
+ """
37
+
38
+ def decorator(func: Callable[P, T]) -> Callable[P, T]:
39
+ """Decorator to time a function"""
40
+ import time
41
+
42
+ prefix = '\t' * depth
43
+
44
+ @wraps(func)
45
+ async def async_wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
46
+ start_time = time.perf_counter_ns()
47
+ result: T = await func(*args, **kwargs) # type: ignore
48
+ diff = time.perf_counter_ns() - start_time
49
+
50
+ if SHOULD_DISPLAY:
51
+ log.info(f'{prefix}{func.__name__}() took {_readable_time(diff)}')
52
+
53
+ return result
54
+
55
+ @wraps(func)
56
+ def sync_wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
57
+ start_time = time.perf_counter_ns()
58
+ result = func(*args, **kwargs)
59
+ diff = time.perf_counter_ns() - start_time
60
+
61
+ if SHOULD_DISPLAY:
62
+ log.info(f'{prefix}{func.__name__}() took {_readable_time(diff)}')
63
+
64
+ return result
65
+
66
+ if asyncio.iscoroutinefunction(func):
67
+ return async_wrapper # type: ignore
68
+ return sync_wrapper
69
+
70
+ if func is None:
71
+ return decorator
72
+ return decorator(func)
73
+
74
+
75
+ def _readable_time(diff: int) -> str:
76
+ """Convert nanoseconds to a readable format"""
77
+ if diff < 1_000:
78
+ return f'{diff} ns'
79
+
80
+ diff = diff // 1_000
81
+ if diff < 1_000:
82
+ return f'{diff} μs'
83
+
84
+ diff = diff // 1_000
85
+ if diff < 1_000:
86
+ return f'{diff} ms'
87
+
88
+ diff = diff // 1_000
89
+ if diff < 60:
90
+ return f'{diff} s'
91
+
92
+ diff = diff // 60
93
+ if diff < 60:
94
+ return f'{diff} m'
95
+
96
+ return f'{diff // 60} h'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: layrz-sdk
3
- Version: 4.0.13
3
+ Version: 4.0.14
4
4
  Summary: Layrz SDK for Python
5
5
  Author-email: "Golden M, Inc." <software@goldenm.com>
6
6
  Maintainer-email: Kenny Mochizuki <kenny@goldenm.com>, Luis Reyes <lreyes@goldenm.com>, Kasen Li <kli@goldenm.com>, Miguel Zauzich <miguel@goldenm.com>, Angel Prieto <aprieto@goldenm.com>
@@ -1,6 +1,8 @@
1
1
  layrz_sdk/__init__.py,sha256=OutylN0QazaeDVIA5NRDVyzwfYnZkAwVQzT-2F6iX2M,28
2
2
  layrz_sdk/constants.py,sha256=guXfIsVAcex76OEMv6DAJy1km1A_WUfWJuUO2Lo3kXE,344
3
3
  layrz_sdk/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ layrz_sdk/decorators/__init__.py,sha256=btoa1egRuYFWggsnfXprvCnxBN9Oq8pbRGcRUR2lm10,82
5
+ layrz_sdk/decorators/func_timing.py,sha256=xRZrWv13ppS8wDy8DeMSNwH21soXGg-i1lOkSzsJ0c8,2196
4
6
  layrz_sdk/entities/__init__.py,sha256=7v3V9s_k2vf2WmHtgf9bMrEqm5EOPRR8xgZmg2B0eJ8,6079
5
7
  layrz_sdk/entities/action.py,sha256=cbyrfeBsUpdv5ipMtIDvFNJj-FE6whPt7I22BnEZ2sU,2020
6
8
  layrz_sdk/entities/action_geofence_ownership.py,sha256=xP_JG2qY7ogJtbCv-jE1xWAdc2oA0icoRIOAI6acicA,590
@@ -109,8 +111,8 @@ layrz_sdk/helpers/__init__.py,sha256=5iW3z2m3jrYhvTfxX-p-QTkR9X9oTKfEsbtVOg9jFFY
109
111
  layrz_sdk/helpers/color.py,sha256=dlpMafbM-4Wd9D9hMbbnZJf4ALkpie_ZmBR2Vz_YCmM,1203
110
112
  layrz_sdk/lcl/__init__.py,sha256=U967AWANkL3u_YVxMNAYlh8jkZ6hqHfStacz7yz6sOA,89
111
113
  layrz_sdk/lcl/core.py,sha256=_3uK05I0iwJl63cVWKxt-qFkq0DWggCj5M680GHH5uQ,25161
112
- layrz_sdk-4.0.13.dist-info/licenses/LICENSE,sha256=d5ZrU--lIPER7QByXDKcrtOTOMk1JvN_9FdYDuoWi7Y,1057
113
- layrz_sdk-4.0.13.dist-info/METADATA,sha256=PxiG_OYeHxjx597LSQXHSVoWddh2shySIvfbHMK8gdg,2045
114
- layrz_sdk-4.0.13.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
115
- layrz_sdk-4.0.13.dist-info/top_level.txt,sha256=yUTMMzfdZ0HDWQH5TaSlFM4xtwmP1fSGxmlL1dmu4l4,10
116
- layrz_sdk-4.0.13.dist-info/RECORD,,
114
+ layrz_sdk-4.0.14.dist-info/licenses/LICENSE,sha256=d5ZrU--lIPER7QByXDKcrtOTOMk1JvN_9FdYDuoWi7Y,1057
115
+ layrz_sdk-4.0.14.dist-info/METADATA,sha256=9pfKdzeUqrLsbNTAmRPo7U_YZ-3ghxpyAu_K4yCWCck,2045
116
+ layrz_sdk-4.0.14.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
117
+ layrz_sdk-4.0.14.dist-info/top_level.txt,sha256=yUTMMzfdZ0HDWQH5TaSlFM4xtwmP1fSGxmlL1dmu4l4,10
118
+ layrz_sdk-4.0.14.dist-info/RECORD,,