########################################################################### # # Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file utils.py # # @author (last) Peter Lucia # @date (last) 21-Oct-2020 # @author (original) Peter Lucia # @date (original) 09-Jul-2020 # ############################################################################ import time import struct def SRSUI(vSRSUI = ""): return "SRSUI " + "{}".format(vSRSUI).rjust(3, '0') def waitForGUI(vDelay = 2): """ a global 2 seconds default wait mehod which is used to wait for GUI animations mostly to make sure the object is avaialable. :param vDelay: (int) amout of second for delay :return: none """ time.sleep(vDelay) def toUXX(vValue, vByteCount, vDelimiter): """ converts the value vValue to hex :param vValue: (int) the value :param vByteCount: (int) number of bytes the value will take eg. U32=4, ... :param vDelimiter: (str) the output delimiter :return: hex formated conversion of the vValue """ x = '{0:0{1}X}'.format(int(vValue) & (2**(4*vByteCount)-1), vByteCount, 'x') bytes = partition(x, 2) return vDelimiter.join(bytes) def toI32(vValue, vDelimiter = ""): """ a convenient method for toUXX to convert the value vValue to integer 4 bytes :param vValue: (int) the value :param vDelimiter: (str) the output delimiter :return: hex formated conversion of the vValue to int 4 bytes """ return toUXX(vValue, 8, vDelimiter) def toI16(vValue, vDelimiter = ""): """ a convenient method for toUXX to convert the value vValue to integer 2 bytes :param vValue: (int) the value :param vDelimiter: (str) the output delimiter :return: hex formated conversion of the vValue to int 2 bytes """ return toUXX(vValue, 4, vDelimiter) def toI08(vValue, vDelimiter = ""): """ a convenient method for toUXX to convert the value vValue to integer 1 byte :param vValue: (int) the value :param vDelimiter: (str) the output delimiter :return: hex formated conversion of the vValue to int 1 bytes """ return toUXX(vValue, 2, vDelimiter) def toF32(vValue): """ converts value vValue to floating point 4 bytes. :param vValue: (float) the value :return: hex formated conversion of the vValue to float 4 bytes """ return '{:08X}'.format(struct.unpack('f', vValue))[0],'X') def partition(vString, vPart, vRightDirection=True): """ splits the given string into sections of vPart long and puts the sections from left to right if vRightDirection is False and puts the sections from right to left if vRightDirection is True after the split is done :param vString: (str) the given string :param vPart: (int) length of the section :param vRightDirection: (bool) order of sections in the output list :return: (list) the section of the string vString in list """ return [vString[i: i + vPart] for i in range(0, len(vString), vPart)][::-1 if vRightDirection else 1] def padding(vString, vLen): """ added zero at the right side of the string to be of length of vLen :param vString: (str) the string to add trainling zero to :param vLen: (int) the entire length of the string :return: (str) padded string """ lStr = len(vString) lPad = int(lStr / vLen) * vLen + ( vLen * (1 if lStr % vLen else 0) ) return vString.ljust(lPad, "0") def tstStart(vTestName): """ test case start print out with time :param vTestName: (str) name of the test case :return: none - prints out on the console """ print(time.strftime("%H:%M:%S Start", time.localtime()) + " - " + vTestName) def tstDone(): """ test case end print out with time :return: none - prints out on the console """ print(time.strftime("%H:%M:%S Done ", time.localtime())) def l2ml(vValue): """ converts liter to mili liter :param (int) vValue: the value in liter. :return: (int) vValue converted to miliiliter. """ return int(round (vValue, 3) * 1000) def ml2l(vValue): """ converts mili liter to liter :param (int) vValue: the value in miliiliter. :return: (int) vValue converted to liter. """ return vValue / 1000 def dict_update(obj: dict, key: str, value): """ Adds a key and value to a dictionary object without updating the original dictionary @param obj: (dict) the object to update @param key: (str) the key name @param value: the new value of the field @return: (dict) the updated dictionary object """ obj = obj.copy() obj[key] = value return obj