Skip to content

Latest commit

 

History

History
98 lines (67 loc) · 2.43 KB

Python.md

File metadata and controls

98 lines (67 loc) · 2.43 KB

Python tips and tricks

  1. Log to multiple files
import logging
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')


def setup_logger(name, log_file, level=logging.INFO):
    """To setup as many loggers as you want"""

    handler = logging.FileHandler(log_file)        
    handler.setFormatter(formatter)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger

# first file logger
logger = setup_logger('first_logger', 'first_logfile.log')
logger.info('This is just info message')

# second file logger
super_logger = setup_logger('second_logger', 'second_logfile.log')
super_logger.error('This is an error message')

def another_method():
   # using logger defined above also works here
   logger.info('Inside method')
  1. Use imageio to read image easier
import matplotlib.pylab as plt
from imageio import imread
filename = <IMAGE_PATH>
img = imread(filename)
plt.imshow(img)
  1. Fix pipenv imcomptability error
pipenv lock --pre --clear
  1. Make isort compatible with Black
isort --profile=black .
  1. Use vars to access class's variable

  2. Some useful commands to debug pipenv

✘ Locking Failed!

pipenv lock --pre --clear

Your Pipfile requires python_version 3.9, but you are using 3.8.3

which python
pipenv install --python=/path/to/your/python
  1. Serve static website locally
python -m http.server 8000
  1. Download files on Google Colab
from google.colab import files
files.download("model.h5")
  1. It's possible to install third party extensions directly from Jupyter Lab now

  2. Jupyterlab Code Formatter Error Unable to find server plugin version (Ref: jupyterlab-contrib/jupyterlab_code_formatter#193)

jupyter server extension enable --py jupyterlab_code_formatter
  1. Change Kernel name (Ref: https://queirozf.com/entries/jupyter-kernels-how-to-add-change-remove)
  • Use $ jupyter kernelspec list to see the folder the kernel is located in
  • In that folder, open up file kernel.json and edit option "display_name"