Best Python code snippet using toolium_python
logger.py
Source:logger.py  
1#!/usr/bin/python2# -*- coding: utf-8 -*-3#4# --- BEGIN_HEADER ---5#6# logger - logging helpers7# Copyright (C) 2003-2015  The MiG Project lead by Brian Vinter8#9# This file is part of MiG.10#11# MiG is free software: you can redistribute it and/or modify12# it under the terms of the GNU General Public License as published by13# the Free Software Foundation; either version 2 of the License, or14# (at your option) any later version.15#16# MiG is distributed in the hope that it will be useful,17# but WITHOUT ANY WARRANTY; without even the implied warranty of18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the19# GNU General Public License for more details.20#21# You should have received a copy of the GNU General Public License22# along with this program; if not, write to the Free Software23# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.24#25# -- END_HEADER ---26#27"""Logging helpers"""28import logging29_default_level = "info"30_default_format = "%(asctime)s %(levelname)s %(message)s"31_debug_format = "%(asctime)s %(module)s:%(funcName)s:%(lineno)s %(levelname)s %(message)s"32def _name_to_level(name):33    """Translate log level name to internal logging value"""34    levels = {"debug": logging.DEBUG, "info": logging.INFO,35              "warning": logging.WARNING, "error": logging.ERROR,36              "critical": logging.CRITICAL}37    name = name.lower()38    if not name in levels:39        print 'Unknown logging level %s, using %s!' % (name, _default_level)40        name = _default_level41    return levels[name]42def _name_to_format(name):43    formats = {"debug": _debug_format, "info": _default_format, 44              "warning": _default_format, "error": _default_format,45              "critical": _default_format}46    name = name.lower()47    if not name in formats:48        print 'Unknown logging format %s, using %s!' % (name, _default_format)49        name = _default_format50    return formats[name]51class Logger:52    """ MiG code should use an instance of this class to handle53    writing to log-files.54    """55    logger = None56    logginglevel = None57    hdlr = None58    logfile = None59    loggingformat = None60    def __init__(self, logfile, level, app='mig_main_logger'):61        self.logfile = logfile62        self.logger = logging.getLogger(app)63        # Repeated import of Configuration in cgi's would cause echo64        # in log files if a second handler is added to the existing65        # logger!66        self.logginglevel = _name_to_level(level)67        self.loggingformat = _name_to_format(level)68        if not self.logger.handlers:69            self.init_handler()70        else:71            self.hdlr = self.logger.handlers[0]72        self.logger.setLevel(self.logginglevel)73    def init_handler(self, stderr=False):74        """Init handler"""75        formatter = logging.Formatter(self.loggingformat)76        if stderr:77            # Add stderr handler78            self.console = logging.StreamHandler()79            self.console.setFormatter(formatter)80            self.logger.addHandler(self.console)81        else:82            # Add file handler83            self.hdlr = logging.FileHandler(self.logfile)84            self.hdlr.setFormatter(formatter)85            self.logger.addHandler(self.hdlr)86    def remove_handler(self, stderr=False):87        """Remove handler"""88        if stderr:89            # Remove stderr handler90            self.logger.removeHandler(self.console)91        else:92            # Remove file handler93            self.logger.removeHandler(self.hdlr)94    def loglevel(self):95        """Return active log level"""96        return logging.getLevelName(self.logginglevel)97    def hangup(self):98        """Reopen log file handlers to catch log rotation"""99        # We can not allow all handlers to be removed since it causes100        # a race. Thus we temporarily introduce a stderr handler while101        # reloading the file handler.102        self.init_handler(stderr=True)103        self.remove_handler(stderr=False)104        self.init_handler(stderr=False)105        self.remove_handler(stderr=True)106    def shutdown(self):107        """Flush all open files and disable logging to prepare for a108        clean shutdown.109        """110        logging.shutdown()111def daemon_logger(name, path=None, level="INFO", log_format=None):112    """Simple logger for daemons to get separate logging in standard format"""113    log_level = _name_to_level(level)114    if not log_format:115        log_format = _name_to_format(level)116    formatter = logging.Formatter(log_format)117    if path:118        handler = logging.FileHandler(path)119    else:120        handler = logging.StreamHandler()121    handler.setLevel(log_level)122    handler.setFormatter(formatter)123    # Make sure root logger does not filter us124    logging.getLogger().setLevel(log_level)125    logger = logging.getLogger(name)126    logger.addHandler(handler)...formatter.py
Source:formatter.py  
...3class Formatter:4    """Build the output document from an :class:`Element`"""5    name = None6    @staticmethod7    def _default_format(element) -> str:8        return element9    module_name_format = _default_format10    module_desc_format = _default_format11    class_name_format = _default_format12    class_desc_format = _default_format13    func_name_format = _default_format14    func_desc_format = _default_format15    marker_format = _default_format16    marker_prefix = "Markers"17    def create_document(self, doc_tree: Element) -> str:18        """Iterates over the elements and generates an hierarchical document structure"""19        out = []20        for module in doc_tree:21            out += self._doc_element(...binopdesc.py
Source:binopdesc.py  
1"""Binary operation description"""2import ast3_DEFAULT_FORMAT = "{left} {operation} {right}"4class BinaryOperationDesc:5    """Binary operation description"""6    OPERATION = {7        ast.Add: {8            "value": "+",9            "format": _DEFAULT_FORMAT,10        },11        ast.Sub: {12            "value": "-",13            "format": _DEFAULT_FORMAT,14        },15        ast.Mult: {16            "value": "*",17            "format": _DEFAULT_FORMAT,18        },19        ast.Div: {20            "value": "/",21            "format": _DEFAULT_FORMAT,22        },23        ast.Mod: {24            "value": "",25            "format": "math.fmod({left}, {right})",26        },27        ast.Pow: {28            "value": "",29            "format": "math.pow({left}, {right})",30        },31        ast.FloorDiv: {32            "value": "/",33            "format": "math.floor({left} {operation} {right})",34        },35        ast.LShift: {36            "value": "",37            "format": "bit32.lshift({left}, {right})",38        },39        ast.RShift: {40            "value": "",41            "format": "bit32.rshift({left}, {right})",42        },43        ast.BitOr: {44            "value": "",45            "format": "bit32.bor({left}, {right})",46        },47        ast.BitAnd: {48            "value": "",49            "format": "bit32.band({left}, {right})",50        },51        ast.BitXor: {52            "value": "",53            "format": "bit32.bxor({left}, {right})",54        },...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!!
