Best Python code snippet using fMBT_python
fmbtlogger.py
Source:fmbtlogger.py  
...51        "args": ", ".join(arglist),52        "kwargs": ", ".join([""] + _formatKwArgs(kwargs))53        }54    return fmt % values55def _formatRetunValue(fmt, retval):56    if type(retval) == str: values={'value': repr(retval)}57    else: values={'value': str(retval)}58    return fmt % values59def _formatException(fmt):60    s = traceback.format_exc()61    exc, msg, _ = sys.exc_info()62    values = {63        "tb": s,64        "exc": exc.__name__,65        "msg": msg66        }67    return fmt % values68class FileToLogFunc(object):69    def __init__(self, fileObj, timeFormat="%s.%f "):70        self._fileObj = fileObj71        self._timeFormat = timeFormat72    def __call__(self, msg):73        self._fileObj.write("%s%s\n" % (74                datetime.datetime.now().strftime(self._timeFormat), msg))75class LogWriter(object):76    """77    LogWriter interface has the following methods:78      start(actionName)79              called after the execution of next action in fMBT model80              has started.81      end(actionName)82              called after the execution of previously executed action83              has ended.84      call(func, args, kwargs)85              called before func(args, kwargs) is called in logged86              interface.87      ret(returnValue)88              called after function returned returnValue.89      exc()90              called after function raised exception. The exception91              can be inspected using sys.exc_info().92    """93    defaultFormats = {94        "start": "%(action)s",95        "end": "",96        "call": "%(func)s(%(args)s%(kwargs)s)",97        "ret": "= %(value)s",98        "exc": "! %(exc)s (%(msg)s)"99        }100class CSVLogWriter(LogWriter):101    # TODO: fmbtandroid should add here some checks include screenshots102    # to the log where appropriate103    def __init__(self, logFunc, separator=";", formats=None):104        if formats == None:105            self.formats = CSVLogWriter.defaultFormats106        else:107            self.formats = {}108            for key in CSVLogWriter.defaultFormats:109                self.formats = formats.get(key, CSVLogWriter.defaultFormats[key])110        self.logFunc = logFunc111        self.depth = 0112        self.separator = separator113    def _log(self, msg):114        if len(msg.strip()) > 0:115            prefix = (self.separator * self.depth)116            self.logFunc(prefix + msg)117    def start(self, actionName):118        msg = _formatAction(self.formats["start"], actionName)119        self._log(msg)120        self.depth += 1121    def end(self, actionName):122        msg = _formatAction(self.formats["end"], actionName)123        self._log(msg)124        self.depth -= 1125    def call(self, func, args, kwargs):126        msg = _formatCall(self.formats["call"], func, args, kwargs)127        self._log(msg)128        self.depth += 1129    def ret(self, returnValue):130        self.depth -= 1131        msg = _formatRetunValue(self.formats["ret"], returnValue)132        self._log(msg)133    def exc(self):134        self.depth -= 1135        msg = _formatException(self.formats["exc"])136        self._log(msg)137def csv(obj, logTarget, csvSeparator=";", formats=None, logDepth=1):138    if type(logTarget) == types.FunctionType:139        logWriter = CSVLogWriter(logTarget, separator=csvSeparator)140    elif hasattr(logTarget, "write"):141        logWriter = CSVLogWriter(FileToLogFunc(logTarget), separator=csvSeparator, formats=formats)142    else:143        raise TypeError("logTarget must be a function or a writable file(-like) object")144    return raw(obj, logWriter, logDepth)145def text(obj, logTarget, indentDepth=4, formats=None, logDepth=1):...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!!
