# Project: PiView
# Filename: Utils.py
# Location: ./piview
# Author: Adrian Gould <adrian.gould@nmtafe.wa.edu.au>
# Created: 10/04/21
#
# This file provides the following features, methods and associated
# supporting code:
# - draw a line using a provided string
# - format a number (bytes) into Bytes, KB, MB, GB,...
# - random number (percentage) between max and min percentages
import random
[docs]class Utils:
@staticmethod
[docs] def draw_line(characters="-=-", length=40):
"""
Draw a line of characters using a given character and length
:param characters: string, the character(s) to draw with
:param length: integer), the length of the line
:rtype: string
:return: A string of exactly length characters
"""
characters_length = len(characters)
repeats = length // characters_length
extras = length % characters_length
line = characters * repeats
for count in range(extras):
next_character = characters[count]
line = f"{line}{next_character}"
return line
# Original found in:
# https://stackoverflow.com/questions/12523586/python-format-size
# -application
# -converting-b-to-kb-mb-gb-tb/37423778
@staticmethod
@staticmethod
[docs] def random_percentage(min_percentage=0, max_percentage=100):
"""
This function returns a random percentage.
Useful for simulations when developing monitoring dashboards
:param min_percentage: float, minimum value to return, default 0.0
:param max_percentage: float, maximum to return, default 100.0
:rtype: float
:return: A random CPU load value between 0% and 100% to 1DP
"""
load = random.gauss(55, 10)
if load < min_percentage:
return 0.0
elif load > max_percentage:
return 100.0
else:
return round(load, 1)