Skip to content

Instantly share code, notes, and snippets.

@blackphreak
Last active July 8, 2018 11:15
Show Gist options
  • Select an option

  • Save blackphreak/bd4d6d58e0b120d69b5617cbc53b06ff to your computer and use it in GitHub Desktop.

Select an option

Save blackphreak/bd4d6d58e0b120d69b5617cbc53b06ff to your computer and use it in GitHub Desktop.
My first python program, just for fun :o
#!/usr/bin/python
# Colored OUTput
class cout:
import sys
from time import sleep
'''
LOG_LEVEL: defines what logs will be output-ed (works with binary)
(0xF) 15: ALL ([DEFAULT]. output all logs)
(0x0) 0: NONE (no logs -- this useless -3-)
(0x1) 1: INFO (Information)
(0x2) 2: WARN (Warning)
(0x4) 4: CRIT (Critical)
(0x8) 8: ERR (Error)
'''
NONE = 0x0
INFO = 0x1
WARN = 0x2
CRIT = 0x4
ERR = 0x8
NOR = 0xF
ALL = 0xF
# log level
LOG_LEVEL = 0xF
'''
LOG_MODE: defines what mode will be used for outupt
0: text mode (prefix= INFO / WARN / ...)
1: symbol mode (prefix= + / ! / * / -)
'''
LOG_MODE = 0
SYMBOLS = {NOR:"+", WARN:"!", CRIT:"*", ERR:"-", INFO:"X", NONE:""}
TEXTS = {NOR:"NOR_", WARN:"WARN", CRIT:"CRIT", ERR:"ERROR", INFO:"INFO", NONE:""}
# to create a colored message
@staticmethod
def msg(colorCode, msg):
return '\x1b[' + str(colorCode) + 'm' + msg + '\x1b[0m'
# colorCodes
black = '30'
red = '31'
green = '32'
yellow = '33'
blue = '34'
magenta = '35'
cyan = '36'
white = '37'
bblack = '90'
bred = '91'
bgreen = '92'
byellow = '93'
bblue = '94'
bmagenta = '95'
bcyan = '96'
bwhite = '97'
''' Usage:
log(logLevel, msg, inSameLine = False)
logLevel : defines this message is in what level of logging.
msg : the message you want to print.
inSameLine: print this message in the same line (update current line)
'''
@staticmethod
def log(logLevel, msg, inSameLine = False):
attr = ''
if logLevel != cout.NONE:
attr = cout.genPrefix(logLevel)
if cout.LOG_LEVEL & logLevel != logLevel:
return
string = "{prefix}{0}".format(msg, prefix=attr)
if inSameLine:
sys.stdout.write('\r' + string)
sys.stdout.flush()
else:
print string
'''
__init__ is an initializer for "progress" function.
To use "progress" function, you have to init. "cout" first by this.
msg : the message you want to show
maxProgress : the max value of the progress
updateRate : delay how long before the next update(print) [OPTIONAL]
Full example:
progress = cout("Test progressbar", 100) // init.
for i in range(101):
progress.progress(i) # updating the current progress
sleep(0.4)
'''
def __init__(self, msg, maxProgress, updateRate = 0.1):
import time
self.__progress = 0
self.__maxProgress = maxProgress
self.__prefixLoad = 0 #step 0
self.__updateRate = updateRate
self.__msg = msg
self.__time = time.time()
import threading
# create a Thread for updating the ProgressBar line
self.__progressThread = threading.Thread(target=self.__progress__,\
args=(self.__msg, self.__progress, self.__updateRate))
# used to update the current progress
# ->> pass "currentProgress = -1" to indicate the job is done. [OPTIONAL]
def progress(self, currentProgress):
if self.__progressThread.is_alive():
self.__progressThread.start()
import time
if time.time() - self.__time > self.__updateRate:
self.__progress__(self.__msg, currentProgress)
self.__time = time.time()
# pass "currentProgress = -1" to indicate the job is done. [OPTIONAL]
if currentProgress >= self.__maxProgress or currentProgress == -1:
self.__progress_done__(self.__msg)
# used to print out the current progress & msg
def __progress__(self, msg, currentProgress):
# prefix is doing the animation...ignore it is fine.
# ->> btw, there is no way to disable this beautiful animation which created by me =w=
prefix = ''
if self.__prefixLoad == 0 or self.__prefixLoad == 8:
prefix += '-....'
elif self.__prefixLoad == 1 or self.__prefixLoad == 7:
prefix += '.-...'
elif self.__prefixLoad == 2 or self.__prefixLoad == 6:
prefix += '..-..'
elif self.__prefixLoad == 3 or self.__prefixLoad == 5:
prefix += '...-.'
elif self.__prefixLoad == 4:
prefix += '....-'
self.__prefixLoad += 1
if self.__prefixLoad >= 8:
self.__prefixLoad = 0
import sys
sys.stdout.write('\r[' + cout.msg(cout.magenta, prefix) + '] ' + msg + ": [" + cout.msg(cout.byellow, str((float(currentProgress) / float(self.__maxProgress))*100)+ "%") + "]")
sys.stdout.flush()
# used when the progress is finished.
def __progress_done__(self, msg):
self.__progressThread = False # end the Thread
import sys
sys.stdout.write('\r[' + cout.msg(cout.magenta, '=====') + '] ' + msg + ": [" + cout.msg(cout.bgreen, "DONE") + "]" + (' '*20))
sys.stdout.flush()
print ""
@staticmethod
def genPrefix(logLevel):
prefix = '['
if logLevel == cout.INFO:
prefix += '\x1b[37m'
elif logLevel == cout.WARN:
prefix +='\x1b[93m'
elif logLevel == cout.CRIT:
prefix += '\x1b[94m'
elif logLevel == cout.ERR:
prefix += '\x1b[91m'
else:
prefix += '\x1b[92m'
return prefix + (cout.TEXTS[logLevel] if cout.LOG_MODE == 0 else cout.SYMBOLS[logLevel]) + '\x1b[0m] '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment