Utils¶
Timers
Timer decorators
- luna_backport4.crutches_on_wheels.cow.utils.timer.printLogger(self, func, duration)[source]¶
Print timing to log
- Return type:
None
- Parameters:
self – class instance with logger
func – measured function
duration – calling duration of function in secs
- luna_backport4.crutches_on_wheels.cow.utils.timer.timer(func)[source]¶
Decorator for function work time estimation.
- Return type:
Callable
- Parameters:
func – decorating function
- Returns:
decorated function.
Common regexps
- luna_backport4.crutches_on_wheels.cow.utils.regexps.REQUEST_ID_REGEXP¶
compiled regexp for request id string
Loggers
Loggers module.
- class luna_backport4.crutches_on_wheels.cow.utils.log.Logger(template='', requestId=None)[source]¶
Subclass to add support for requestIdCtx.
- property requestId: str¶
Get current request id
- Returns:
request id from __init__ 2) from requestIdCtx 3) default
- Return type:
request id. priority
- luna_backport4.crutches_on_wheels.cow.utils.log.logger = <Logger (DEBUG)>¶
disable-msg=C0103
- Type:
pylint
Module realizes getting and checking request id
- luna_backport4.crutches_on_wheels.cow.utils.rid.checkRequestId(requestId)[source]¶
Validate request id str
- Return type:
bool
- Parameters:
requestId – str for checking
- Returns:
True if requestId match with REQUEST_ID_REGEXP otherwise False
- luna_backport4.crutches_on_wheels.cow.utils.rid.generateRequestId(timeIsLocal)[source]¶
Generate correct request id.
- Return type:
str
- Parameters:
timeIsLocal – timestamp generate in localtime or not
- Returns:
standard request id string.
Module realizes useful function.
- async luna_backport4.crutches_on_wheels.cow.utils.functions.addCallBack(callableFunction, sleepTime=0)[source]¶
Function creates infinity loop, where call function
- Return type:
None
- Parameters:
callableFunction – function to call
sleepTime – time in secods if callback is periodic, in seconds
- luna_backport4.crutches_on_wheels.cow.utils.functions.convertDateTimeToCurrentFormatDateTime(dateTimeString, currentTimeFormat)[source]¶
Convert datetime string from UTC or LOCAL to datetime
- Return type:
datetime
- Parameters:
dateTimeString – str with datetime
currentTimeFormat – LOCAL or UTC
- Returns:
datetime with/without timezone depends on currentTimeFormat
>>> t1 = convertDateTimeToCurrentFormatDateTime('2018-12-25 12:21:46.652495Z', 'LOCAL') >>> print(type(t1), t1) <class 'datetime.datetime'> 2018-12-25 12:21:46.652495 >>> t2 = convertDateTimeToCurrentFormatDateTime('2018-12-25 15:21:46.652495+03:00', 'LOCAL') >>> print(type(t2), t2) <class 'datetime.datetime'> 2018-12-25 12:21:46.652495 >>> t1 = convertDateTimeToCurrentFormatDateTime('2018-12-25 12:21:46.652495Z', 'UTC') >>> print(type(t1), t1) <class 'datetime.datetime'> 2018-12-25 12:21:46.652495+00:00 >>> t2 = convertDateTimeToCurrentFormatDateTime('2018-12-25 15:21:46.652495+03:00', 'UTC') >>> print(type(t2), t2) <class 'datetime.datetime'> 2018-12-25 15:21:46.652495+03:00
- luna_backport4.crutches_on_wheels.cow.utils.functions.convertDateTimeToCurrentFormatStr(dateTimeString, currentTimeFormat)[source]¶
Convert datetime string from UTC or LOCAL to string, considering current STORAGE_TIME
- Return type:
str
- Parameters:
dateTimeString – str with datetime
currentTimeFormat – LOCAL or UTC
- Returns:
str with datetime in current format
- luna_backport4.crutches_on_wheels.cow.utils.functions.convertEnumToSnakeDict(enum)[source]¶
Convert enum with camelCase name to dict with snake_case keys
- Return type:
dict
- Parameters:
enum – enum
- Returns:
dict
- luna_backport4.crutches_on_wheels.cow.utils.functions.currentDateTime(currentTimeFormat)[source]¶
Function get current date-time in ISO format.
- Return type:
str
- Parameters:
currentTimeFormat – LOCAL or UTC
- Returns:
20:30.45+01:00)
- Return type:
date-time in rfc3339 format (e.g. 1997-07-16T19
- luna_backport4.crutches_on_wheels.cow.utils.functions.currentTimestamp(logTime='LOCAL', dbType='postgres')[source]¶
Function get time (local or UTC, depends on logTime). The function is needed to correctly fill in the fields in different databases
- Return type:
Union
[str
,datetime
]- Parameters:
logTime – LOCAL or UTC, LOCAL for default
dbType – db type (oracle/postgres), postgres for default
- Returns:
%M:%S.%fZ’
- Return type:
Timestamp if format ‘%Y-%m-%dT%H
- async luna_backport4.crutches_on_wheels.cow.utils.functions.downloadImage(url, logger, timeout, accountId=None)[source]¶
Download image. Add Luna-Account-Id header for request. If request to lis, apply account id to request.
- Return type:
tuple
[bytes
,str
]- Parameters:
url – image url
logger – logger
timeout – client timeout
accountId – account id
- Returns:
tuple with image bytes and its content type
- Raises:
VLException(Error.FailDownloadImage.format(url), 400, isCriticalError=False) if failed to download image –
- luna_backport4.crutches_on_wheels.cow.utils.functions.getAnnotationWithNullable(value)[source]¶
Get python annotation with allowed nullable value
- Return type:
type
[Literal
[]]
Notes
annotation contains 2 parts: - first part is tuple(…) + None - required for good validation error messages - second part | None - required to allow None as value
- Parameters:
value – incoming value
- Returns:
annotation