Best Python code snippet using slash
log.py
Source:log.py
1# -*- coding: utf-8 -*-2"""3 terminal.log4 ~~~~~~~~~~~~5 Verbose logger support for terminal.6 :copyright: (c) 2013 by Hsiaoming Yang.7"""8import sys9import copy10class Logger(object):11 """12 The Logger interface.13 :param verbose: control if the log to show verbose log14 :param quiet: control if the log to show debug and info log15 :param indent: control the indent level, default is zero16 Play with :class:`Logger`, it supports nested logging::17 log = Logger()18 log.info('play with log')19 log.start('start a indent level')20 log.info('this log will indent two spaces')21 log.end('close a indent level')22 """23 def __init__(self, **kwargs):24 self._indent = 025 self._is_verbose = False26 self._enable_verbose = False27 self._enable_quiet = False28 self.warn_count = 029 self.error_count = 030 self.config(**kwargs)31 def config(self, **kwargs):32 """33 Config the behavior of :class:`Logger`.34 Control the output to show :class:`Logger.verbose` log::35 log.config(verbose=True)36 Control the output to show only the :class:`Logger.warn` and37 :class:`Logger.error` log::38 log.config(quiet=True)39 """40 if 'indent' in kwargs:41 self._indent = kwargs.get('indent', 0)42 if 'verbose' in kwargs:43 self._enable_verbose = kwargs.get('verbose', False)44 if 'quiet' in kwargs:45 self._enable_quiet = kwargs.get('quiet', False)46 return self47 def message(self, level, *args):48 """49 Format the message of the logger.50 You can rewrite this method to format your own message::51 class MyLogger(Logger):52 def message(self, level, *args):53 msg = ' '.join(args)54 if level == 'error':55 return terminal.red(msg)56 return msg57 """58 msg = ' '.join((str(o) for o in args))59 if level not in ('start', 'end', 'debug', 'info', 'warn', 'error'):60 return msg61 return '%s: %s' % (level, msg)62 def writeln(self, level='info', *args):63 if not self._enable_verbose and self._is_verbose:64 return self65 msg = self.message(level, *args)66 if self._indent:67 msg = '%s%s' % (' ' * self._indent, msg)68 if level == 'error':69 sys.stderr.write('%s\n' % msg)70 else:71 sys.stdout.write('%s\n' % msg)72 return self73 @property74 def verbose(self):75 """76 Make it the verbose log.77 A verbose log can be only shown when user want to see more logs.78 It works as::79 log.verbose.warn('this is a verbose warn')80 log.verbose.info('this is a verbose info')81 """82 log = copy.copy(self)83 log._is_verbose = True84 return log85 def start(self, *args):86 """87 Start a nested log.88 """89 if self._is_verbose:90 # verbose log has no start method91 return self92 self.writeln('start', *args)93 self._indent += 194 return self95 def end(self, *args):96 """97 End a nested log.98 """99 if self._is_verbose:100 # verbose log has no end method101 return self102 if not args:103 self._indent -= 1104 return self105 self.writeln('end', *args)106 self._indent -= 1107 return self108 def debug(self, *args):109 """110 The debug level log.111 """112 if self._enable_quiet:113 return self114 return self.writeln('debug', *args)115 def info(self, *args):116 """117 The info level log.118 """119 if self._enable_quiet:120 return self121 return self.writeln('info', *args)122 def warn(self, *args):123 """124 The warn level log.125 """126 self.warn_count += 1127 return self.writeln('warn', *args)128 def error(self, *args):129 """130 The error level log.131 """132 self.error_count += 1...
message_utils.py
Source:message_utils.py
1_is_verbose = False2def verbose_on():3 global _is_verbose4 _is_verbose = True5def verbose_off():6 global _is_verbose7 _is_verbose = False8def is_verbose():9 return _is_verbose10def make_message(error_items, tensor):11 if not len(error_items):12 return ""13 message = " ".join(error_items)14 if is_verbose():15 message += f"\nThe tensor is:\n{tensor}\n"...
printer.py
Source:printer.py
1from datetime import datetime2_is_verbose = False3# Flags the program to allow debugging print statements4def set_is_verbose():5 global _is_verbose6 _is_verbose = True7# When the program is verbose, prints the current timestamp8# and everything to be printed to standard output9def prnt(*args):10 if _is_verbose:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!