Best Python code snippet using tox_python
config.py
Source: config.py
...3from dtools.enums import LoggingLevel, ConfigAttribute4DTOOLS_DIR = os.path.join(os.path.expanduser("~"), 'dtools')5CONFIG_PATH = os.path.join(DTOOLS_DIR, 'config.json')6REPO_PATH = os.path.dirname(os.path.abspath(__file__))7def _check_logging_levels(logging_levels):8 possible_values = set([e.value for e in LoggingLevel])9 if logging_levels - possible_values:10 raise ValueError(f'Invalid {ConfigAttribute.LOGGING_LEVELS.value} {logging_levels - possible_values}')11def _create_new_config_entry():12 logging_levels = [LoggingLevel.EXCEPTION.value]13 return {14 ConfigAttribute.LOGGING_LEVELS.value: logging_levels,15 ConfigAttribute.LOG_FILE_PATH.value: 'log.txt'16 }17class _Config:18 def __init__(self):19 if not os.path.exists(CONFIG_PATH):20 self._create_new_config_json()21 else:22 with open(CONFIG_PATH, 'r') as f:23 self._config_json = json.load(f)24 if REPO_PATH not in self._config_json:25 self.add_repo_to_config()26 else:27 _check_logging_levels(self.logging_levels)28 def add_repo_to_config(self):29 self._config_json[REPO_PATH] = _create_new_config_entry()30 self.save()31 def _create_new_config_json(self):32 self._config_json = {33 REPO_PATH: _create_new_config_entry()34 }35 self.save()36 def save(self):37 with open(CONFIG_PATH, 'w') as f:38 json.dump(self._config_json, f, indent=4)39 def set_logging_levels(self, logging_levels):40 logging_levels = set(logging_levels)41 _check_logging_levels(logging_levels)42 self._config_json[REPO_PATH][ConfigAttribute.LOGGING_LEVELS.value] = list(logging_levels)43 self.save()44 @property45 def logging_levels(self):46 return set(self._config_json[REPO_PATH][ConfigAttribute.LOGGING_LEVELS.value])47 @property48 def log_file_path(self):49 return self._config_json[REPO_PATH][ConfigAttribute.LOG_FILE_PATH.value]...
lambda_handler.py
Source: lambda_handler.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4"""5import json6import logging7import os8import traceback9import line_handler10logger = logging.getLogger(__name__)11def lambda_handler(event, context):12 _lambda_logging_init()13 headers = event["headers"] # or event["multiValueHeaders"]14 # prm1 = event["pathParameters"]["prm1"]15 # prm2 = event["queryStringParameters"]["prm2"]16 body = event["body"]17 logger.info(headers)18 try:19 result = line_handler.callback(headers, body)20 return {"statusCode": 200, "body": json.dumps(result)}21 except Exception as e:22 result = {"result": "failed", "message": traceback.format_exc()}23 logger.info(json.dumps(result), exc_info=e)24 return {"statusCode": 500, "body": json.dumps(result)}25def _lambda_logging_init():26 """27 logging ã®åæåãLOGGING_LEVEL, LOGGING_LEVELS ç°å¢å¤æ°ãè¦ã¦ããã°ã¬ãã«ãè¨å®ããã28 LOGGING_LEVELS - "module1=DEBUG,module2=INFO" ã¨ããå½¢ã®æååãæ³å®ãèªåã®ã¢ã¸ã¥ã¼ã«ã®ã¿ DEBUG ã«ããã¨ããªã©ã«å©ç¨29 """30 logging.getLogger().setLevel(os.getenv('LOGGING_LEVEL', 'INFO')) # lambda ã®å ´åã¯ãã¬ã¼è¨å®æ¸ã¿ã®ãããã¡ããå¿
è¦31 if os.getenv('LOGGING_LEVELS'):32 for mod_lvl in os.getenv('LOGGING_LEVELS').split(','):33 mod, lvl = mod_lvl.split('=')...
pylogger.py
Source: pylogger.py
1import logging2from pytorch_lightning.utilities import rank_zero_only3def get_pylogger(name=__name__) -> logging.Logger:4 """Initializes multi-GPU-friendly python command line logger."""5 logger = logging.getLogger(name)6 # this ensures all logging levels get marked with the rank zero decorator7 # otherwise logs would get multiplied for each GPU process in multi-GPU setup8 logging_levels = ("debug", "info", "warning", "error", "exception", "fatal", "critical")9 for level in logging_levels:10 setattr(logger, level, rank_zero_only(getattr(logger, level)))...
Check out the latest blogs from LambdaTest on this topic:
In recent times, many web applications have been ported to mobile platforms, and mobile applications are also created to support businesses. However, Android and iOS are the major platforms because many people use smartphones compared to desktops for accessing web applications.
Ever since the Internet was invented, web developers have searched for the most efficient ways to display content on web browsers.
Recently, I was going through some of the design patterns in Java by reading the book Head First Design Patterns by Eric Freeman, Elisabeth Robson, Bert Bates, and Kathy Sierra.
Many theoretical descriptions explain the role of the Scrum Master as a vital member of the Scrum team. However, these descriptions do not provide an honest answer to the fundamental question: “What are the day-to-day activities of a Scrum Master?”
One of the most important tasks of a software developer is not just writing code fast; it is the ability to find what causes errors and bugs whenever you encounter one and the ability to solve them quickly.
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!!