Best Python code snippet using Testify_python
simple_logger.py
Source:simple_logger.py  
...13        tuple[1] ... file handler associated to logger14        tuple[2] ... stream handler associated to logger15    16    If no paths argument, then logs only to shell.'''17    clear_logger()18    global logger19    if not paths:20        paths = []21    if isinstance(paths, str):22        paths = [paths]23    logger = logging.getLogger(__name__)24    logger.setLevel(logging.DEBUG)25    [logger.removeHandler(h) for h in logger.handlers]26    formatter = logging.Formatter(fmt='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S %z')27    formatter.converter = time.gmtime28    for p in paths:29        fh = logging.FileHandler(filename=p, mode='a', encoding='utf-8')30        fh.setFormatter(formatter)31        logger.addHandler(fh)32    sh = logging.StreamHandler()33    sh.setFormatter(formatter)34    logger.addHandler(sh)35    for path in paths:36        logger.info(f'Logging to {path}')37    return logger, fh, sh38def clear_logger():39    '''Mysteriously (to me) this doesn't seem to work on the second run of script in40    the same ipython/debugger session, but does seem to work on the third... Weird,41    not gonna bother trying to track it down --- just adds duplicate printouts.'''42    if 'logger' not in locals():43        return44    for h in logger.handlers:45        logger.removeHandler(h)46    47def assert2(test, message, show_quit_msg=True):48    '''Like an assert, but cleaner handling of logging.'''49    if not test:50        logger.error(message)51        if show_quit_msg:52            logger.warning('Now quitting, so user can check inputs.')...logger_rate_limiter.py
Source:logger_rate_limiter.py  
2    def __init__(self):3        self.queue = []4        self.curr_time = 05        self.items = set()6    def clear_logger(self): # We clear the logger with all7        topop = 08        for q in self.queue:9            if q[1]+10<=self.curr_time:10                topop += 111                self.items.remove(q[0]) # Removing items from hash12        13        while topop>0:14            self.queue.pop(0)15            topop -= 116    def should_print(self, timestamp, message):17        self.curr_time = timestamp18        self.clear_logger()19        if message in self.items: # This means we can't print20            return False 21        22        self.queue.append((message, timestamp))23        self.items.add(message)24        return True 25logger = Logger()26logger.should_print(1, "foo")27logger.should_print(2, "bar")28logger.should_print(3, "foo")29logger.should_print(8, "bar")30logger.should_print(10, "foo")31logger.should_print(11, "foo")32logger.should_print(11, "bar")...logger_rate_limiter_opt.py
Source:logger_rate_limiter_opt.py  
1class Logger:2    def __init__(self):3        self.hash_items = {}4        self.curr_time = 05    def clear_logger(self): # We clear the logger with all6        items = list(self.hash_items.keys())7        for i in items:8            if self.hash_items[i]+10<=self.curr_time:9                del self.hash_items[i]10    def should_print(self, timestamp, message):11        self.curr_time = timestamp12        self.clear_logger()13        if message in self.hash_items: # This means we can't print14            return False 15        16        self.hash_items[message] = timestamp17        return True 18logger = Logger()19logger.should_print(1, "foo")20logger.should_print(2, "bar")21logger.should_print(3, "foo")22logger.should_print(8, "bar")23logger.should_print(10, "foo")24logger.should_print(11, "foo")25logger.should_print(11, "bar")26logger.should_print(12, "bar")...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!!
