def diff_in_minutes(cls, datetime_start: datetime, datetime_end: datetime, default_value=-1, round_result=True): if not datetime_start or not datetime_end: return default_value try: diff = datetime_end - datetime_start total_minutes = diff.total_seconds() / 60 if round_result: total_minutes = int(round(total_minutes, 0)) return total_minutes except Exception as ex: log_exception(ex) return default_value
def from_string(cls, the_str: str, encoding='uft-8', errors='replace', default_value=None): """ Convert string into bytes :param the_str: the string :param encoding: encoding of the string :param errors: other possible values are 'ignore', 'strict', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error() :param default_value: if can't convert use this value to return :return: """ try: return the_str.encode(encoding=encoding, errors=errors) except Exception as ex: log_exception(ex) return default_value
def diff_in_days(cls, datetime_start: datetime, datetime_end: datetime, default_value=-1): """ Days of (end - start) :param datetime_start: :param datetime_end: :param default_value: :return: """ if not datetime_start or not datetime_end: return default_value try: diff = datetime_end - datetime_start return diff.days except Exception as ex: log_exception(ex) return default_value
def to_string(cls, byte_array, encoding='uft-8', errors='replace', default_value=None): """ Convert an array of bytes into string. :type byte_array: bytes|bytearray :param byte_array: the source :param encoding: the encoded encoding of the binary :param errors: other possible values are 'ignore', 'strict', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error() :param default_value: if can't convert use this value to return :rtype: str """ try: return byte_array.decode(encoding=encoding, errors=errors) except Exception as ex: log_exception(ex) return default_value
def get_unique(cls, the_list, default_value=None, unique_attr=None): """ Get a list of unique values in the list, default_value is [] if default_value is set to None. :param the_list: :param default_value: if none value is [] :param unique_attr: select your own unique attribute (in case when the object is unhashable or you want your own attr) :rtype list """ if default_value is None: default_value = [] if not the_list: return default_value try: # Src: http://stackoverflow.com/questions/480214 # /how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order # Src: http://www.peterbe.com/plog/uniqifiers-benchmark if unique_attr is None: added_list = set() add_to_added_list = added_list.add # this static ref for performance reason return [ x for x in the_list if not (x in added_list or add_to_added_list(x)) ] result = [] existed_item = { } # dict is much faster than list when checking existence of a key for itm in the_list: key = Obj.getattr(itm, unique_attr) if key not in existed_item: result.append(itm) existed_item[key] = None return result except Exception as ex: log_exception(ex) return default_value