Best Python code snippet using lisa_python
progressbar.py
Source:progressbar.py
1import threading2import time3import sys4class Progressbar:5 def __init__(self, label=None, bar_length=40, refresh_time_in_seconds=1):6 self.label = label7 self._refresh_timeout = refresh_time_in_seconds8 self._bar_length = bar_length9 self._start_time = None10 self._progress = 0.011 self._background_thread = None12 def _timer_handle(self):13 self._update()14 self._background_thread = threading.Timer(self._refresh_timeout, self._timer_handle)15 self._background_thread.start()16 def _update(self, message=None):17 elapsed = time.time() - self._start_time18 filled_len = int(self._bar_length * self._progress)19 percents = round(100.0 * self._progress, 1)20 prefix = self.label + '... ' if self.label is not None else ''21 bar = '=' * filled_len + ' ' * (self._bar_length - filled_len)22 percents_text = ('%.1f%%' % percents).rjust(6)23 elapsed_text = '%02d:%02d' % (int(elapsed / 60), elapsed % 60)24 sys.stdout.write('%s[%s] %s %s ' % (prefix, bar, percents_text, elapsed_text))25 if message is not None:26 sys.stdout.write(message)27 sys.stdout.write('\r')28 sys.stdout.flush()29 def start(self):30 self._start_time = time.time()31 self._timer_handle()32 def set_progress(self, progress):33 self._progress = progress34 def cancel(self):35 self._background_thread.cancel()36 def complete(self):37 self._background_thread.cancel()38 self._progress = 1.039 self._update()40 sys.stdout.write('\n')41 def abort(self, error=None):42 self._background_thread.cancel()43 self._update(message=None if error is None else (' ERROR: %s' % error))44 sys.stdout.write('\n')45class UndefinedProgressbar(Progressbar):46 def __init__(self, label=None, bar_length=40, paddle_length=15, refresh_time_in_seconds=0.05):47 Progressbar.__init__(self, label, bar_length, refresh_time_in_seconds)48 self._paddle_length = paddle_length49 def _update(self, newline=False, complete=False, error=False):50 elapsed = time.time() - self._start_time51 self._progress = int((self._progress+1) % self._bar_length)52 prefix = self.label + '... ' if self.label is not None else ''53 if error:54 bar = " " * self._bar_length55 elif complete:56 bar = "=" * self._bar_length57 else:58 bar = list(' ' * self._bar_length)59 for i in range(self._progress, self._progress+self._paddle_length):60 bar[i % self._bar_length] = "="61 bar = "".join(bar)62 elapsed_text = '%02d:%02d' % (int(elapsed / 60), elapsed % 60)63 sys.stdout.write('%s[%s] %s\r' % (prefix, bar, elapsed_text))64 if newline:65 sys.stdout.write('\n')66 sys.stdout.flush()67 def cancel(self):68 self._update(newline=True, complete=False, error=True)69 Progressbar.cancel(self)70 def complete(self):71 self._background_thread.cancel()72 self._progress = 1.0...
timer.py
Source:timer.py
1# Based on https://realpython.com/python-timer/2import time3from logging import info, error,debug4class TimerException(Exception):5 """A custom exception used to report errors in use of Timer class"""6class Timer:7 def __init__(self, elapsed_text="Elapsed time: {:0.2f} seconds"):8 self._start_time = None9 self.elapsed_text = elapsed_text10 self.completed=False11 12 def start(self):13 """Start a new timer"""14 if self._start_time is not None:15 raise TimerException(f"Timer is running. Use .stop() to stop it")16 self._start_time = time.perf_counter()17 def stop(self):18 """Stop the timer, and report the elapsed time"""19 if self._start_time is None:20 raise TimerException(f"Timer is not running. Use .start() to start it")21 elapsed_time = time.perf_counter() - self._start_time22 self._start_time = None23 if self.op == '':24 info(self.elapsed_text.format(elapsed_time))25 else:26 pass27class Op:28 def __init__(self, op):29 self._start_time = 0.030 self.op = op31 self.completed_text= op + " completed in {:0.2f} s."32 self.completed = False33 self.abandoned = False34 self.abandoned_text= op + " abadoned after {:0.2f} s."35 def complete(self):36 self.elapsed_time = time.perf_counter() - self._start_time37 self.completed = True38 info(self.completed_text.format(self.elapsed_time))39 def abandon(self):40 if not self.completed:41 self.abandoned = True42 self.elapsed_time = time.perf_counter() - self._start_time43 error(self.abandoned_text.format(self.elapsed_time))44 else:45 raise TimerException(f"Operation {self.op} has already completed.")46 47 def __enter__(self):48 info(self.op + '...')49 self._start_time = time.perf_counter()50 return self51 def __exit__(self, ex_type, ex, tb):52 if not (self.completed or self.abandoned):53 if (ex is not None): 54 debug(f'Exception raised in context: {ex}')55 self.abandon()56 return ex is None57 ...
elapsed.py
Source:elapsed.py
1import os2from collections import UserList3from pathlib import Path4import yaml5from pydantic import BaseModel6from .config_type import ConfigType7APP_DIR = Path(os.getenv("APP_DIR", ".")).resolve()8DEFAULT_ELAPSED_FILE = APP_DIR / "data" / "default_elapsed.yml"9class ElapsedSprintSpan(BaseModel):10 start: int11 end: int12class ElapsedFeature(BaseModel):13 name: str14 elapsed: dict[str, ElapsedSprintSpan]15class ElapsedFeatureList(ConfigType, UserList[ElapsedFeature]):16 @classmethod17 def from_text(cls, elapsed_text):18 if not elapsed_text:19 elapsed_text = cls.get_default_text()20 elapsed_as_dict = yaml.safe_load(elapsed_text)21 return [ElapsedFeature(**a) for a in elapsed_as_dict]22 @staticmethod23 def get_default_text() -> str:24 with DEFAULT_ELAPSED_FILE.open() as elapsed_file:25 default_elapsed = elapsed_file.read()...
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!!