Module bioiain.utilities.logging
Functions
def change_std_out(target_file, mode='w')-
Expand source code
def change_std_out(target_file, mode="w"): global std_out sys.std_out = open(target, mode) std_out = sys.stdout def eprint(*strings, style='^', **kwargs)-
Expand source code
def eprint(*strings, style = "^", **kwargs): # Print end of section tprint(*strings, style=style, end="\n\n", print_timer=True, **kwargs) def log(level: int | str = 1, *args, **kwargs)-
Expand source code
def log(level:int|str=1, *args, **kwargs): """ Log a message and display it according to the given level if higher than environment variable "BI_VERBOSE". Builtin prints are always displayed. If unset BI_VERBOSE is set to 10. BI_VERBOSE == 0 displays only ERROR, WARNING and DEBUG messages. BI_VERBOSE == -1 display only ERROR. BI_VERBOSE == -1 display nothing. :param level: Verbose level: ERROR | WARNING | DEBUG | TITLE | HEADER | int :param args: args for print function :param kwargs: kwargs for print function """ v = int(os.environ.get("BI_VERBOSE", 10)) if type(level) is str: level = level.lower() if v > -2: if level == "error": if isinstance(kwargs.get("error", None), Exception): raise kwargs.get("error") elif kwargs.get("raise_exception", False): raise Exception(" ".join([str(a) for a in args])) else: print("\033[91m") print("ERROR: ", end="") print(*args, **kwargs) print("\033[0m") elif v > -1: if level == "warning": print("\033[93m",end="") print("WARNING: ", end="") print(*args, **kwargs) print("\033[0m",end="") elif level == "debug": print(*args, **kwargs) elif level == "title": print("\033]0;",end="") print(*args, **kwargs) print("\a",end="") elif v > 0: if level == 0 or level is None: print(*args, **kwargs) elif level == "start": tprint(*args, **kwargs) elif level == "header": sprint(*args, **kwargs) elif level == "end": eprint(*args, **kwargs) elif type(level) is int: if v >= level: print1(*args, space=2*level, **kwargs) else: print1("...", space=2 * level, **kwargs) else: print("Unknown log level: {}".format(repr(level))) print(*args, **kwargs)Log a message and display it according to the given level if higher than environment variable "BI_VERBOSE". Builtin prints are always displayed. If unset BI_VERBOSE is set to 10. BI_VERBOSE == 0 displays only ERROR, WARNING and DEBUG messages. BI_VERBOSE == -1 display only ERROR. BI_VERBOSE == -1 display nothing. :param level: Verbose level: ERROR | WARNING | DEBUG | TITLE | HEADER | int :param args: args for print function :param kwargs: kwargs for print function
def print1(*strings: str, space: int = 2, **kwargs)-
Expand source code
def print1(*strings:str, space:int=2, **kwargs): # Print with 1 indent str_strings = [] for string in strings: if type(string) == list or type(string) == tuple: for string2 in string: str_strings.append(str(string2)) else: str_strings.append(str(string)) #str_strings = map(str, strings) out = "{}> {}".format(" " * space, " ".join(str_strings)) print(out, **kwargs) def print_children(d)-
Expand source code
def print_children(d): if type(d) == list: d = d[0] print("(list)[0]") print("strings:") [print(k, v) for k, v in d.items() if type(v) == str] print("other:") [print(k, type(v), len(v)) for k, v in d.items() if type(v) != str and v is not None] def restore_std_out()-
Expand source code
def restore_std_out(): global std_out sys.stdout = original_std_out std_out = sys.stdout def resume_logging()-
Expand source code
def resume_logging(): global std_out sys.stdout = std_out def send_tensorboard_run(host, folder, run, file, key, epoch=0, protocol='https')-
Expand source code
def send_tensorboard_run(host, folder, run, file, key, epoch=0, protocol="https"): url = f"{protocol}://{host}/runs/" fname = os.path.basename(file) fname = fname.replace(".0", f".{epoch}") log("header", "Uploading run to:", url) assert key is not None with open(file, "rb") as f: resp = requests.put( url, headers={ "Content-Type": "application/x-www-form-urlencoded", "key":key, "folder":folder, "run":run, "fname":fname }, data=f.read(), timeout=3000 ) print(resp.text) if resp.status_code != 200: print(resp) raise Exception(f"Error [{resp.status_code}] uploading file to: {url}") return resp def sprint(*strings: str, **kwargs)-
Expand source code
def sprint(*strings:str, **kwargs): # Print Subtitle str_strings = map(str, strings) prefix = "\n" out = " * "+ " ".join(str_strings) print(prefix+out,**kwargs) def stop_logging()-
Expand source code
def stop_logging(): sys.std_out = open(os.devnull, "w") def tprint(*strings: str,
head: int = 10,
style: str = '#',
end: str = '\n',
sep: str = ' ',
reset_timer=True,
print_timer=False)-
Expand source code
def tprint(*strings:str, head:int=10, style:str="#", end:str="\n", sep:str=" ", reset_timer=True, print_timer=False): # Print section title global start_time width = shutil.get_terminal_size()[0] -2 string = " ".join([str(s) for s in strings]) timer = "" if print_timer and start_time is not None: timer = "{}{}{}".format(sep, datetime.timedelta(seconds =time.time() - start_time), sep) tail2 = 3 * style tail1_len = width - head - len(string) - len(timer) - len(tail2) if tail1_len < 0: tail1_len = 0 tail2="" tail1 = style*tail1_len out = "\n{}{}{}{}{}{}{}".format(style*head, sep, string, sep, tail1, timer, tail2 ) print(out, end=end) if reset_timer: start_time = time.time() def tracemalloc_start()-
Expand source code
def tracemalloc_start(): tracemalloc.start() def tracemalloc_top(top=15)-
Expand source code
def tracemalloc_top(top=15): snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') log("header", f"Tracemalloc top {top}") for stat in top_stats[:top]: log(1, stat)
Classes
class Log-
Expand source code
class Log(object): def __init__(self): self.stdout = None self.stderr = None self.folder = "./logs" self.files = {"default": os.path.join(SUBDIR_NAME,"default.log"), "debug": os.path.join(SUBDIR_NAME, "debug.log")} self.logging = True self.terminal = True def __repr__(self): return f"<bi.Log: default: {self.files['default']}>" def list(self): return [l for l in self.files.values()] def dict(self): return self.files def __add__(self, s): pass def __call__(self): pass def log(self): pass def error(self): pass def warning(self): pass def title(self): pass def start(self): pass def end(self): pass def pause(self): pass def resume(self): pass def set_stdout(self, filepath): pass def set_stderr(self, filepath): pass def set_log_file(self, filepath, log_name="default"): pass def add_timestamp(self, log_name=None): pass def disable(self): self.terminal = False self.logging = FalseMethods
def add_timestamp(self, log_name=None)-
Expand source code
def add_timestamp(self, log_name=None): pass def dict(self)-
Expand source code
def dict(self): return self.files def disable(self)-
Expand source code
def disable(self): self.terminal = False self.logging = False def end(self)-
Expand source code
def end(self): pass def error(self)-
Expand source code
def error(self): pass def list(self)-
Expand source code
def list(self): return [l for l in self.files.values()] def log(self)-
Expand source code
def log(self): pass def pause(self)-
Expand source code
def pause(self): pass def resume(self)-
Expand source code
def resume(self): pass def set_log_file(self, filepath, log_name='default')-
Expand source code
def set_log_file(self, filepath, log_name="default"): pass def set_stderr(self, filepath)-
Expand source code
def set_stderr(self, filepath): pass def set_stdout(self, filepath)-
Expand source code
def set_stdout(self, filepath): pass def start(self)-
Expand source code
def start(self): pass def title(self)-
Expand source code
def title(self): pass def warning(self)-
Expand source code
def warning(self): pass