# Copyright 2016 Peter Brittain # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ This module is just a collection of simple helper functions. """ from datetime import date, datetime def readable_mem(mem): """ :param mem: An integer number of bytes to convert to human-readable form. :return: A human-readable string representation of the number. """ for suffix in ["", "K", "M", "G", "T"]: if mem < 10000: return "{}{}".format(int(mem), suffix) mem /= 1024 return "{}P".format(int(mem)) def readable_timestamp(stamp): """ :param stamp: A floating point number representing the POSIX file timestamp. :return: A short human-readable string representation of the timestamp. """ if date.fromtimestamp(stamp) == date.today(): return str(datetime.fromtimestamp(stamp).strftime("%I:%M:%S%p")) else: return str(date.fromtimestamp(stamp)) class _DotDict(dict): """ Modified dictionary to allow dot notation access. This can be used for quick and easy structures. See https://stackoverflow.com/q/2352181/4994021 """ __getattr__ = dict.get __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__