Skip to content

Python

File

  • Open modes:

  • Open modes decision tree:

Class & Function

  • Get full path:

    import os
    import inspect
    
    def foo():
      pass
    
    print(os.path.abspath(inspect.getfile(foo)))
    
    class Bar:
      pass
    
    print(os.path.abspath(inspect.getfile(Bar.__class__)))
    

Modules & Packages

  • Get all import able modules & packages:

    import pkgutil
    search_path = ['.'] # set to None to see all modules importable from sys.path
    all_modules = [x[1] for x in pkgutil.iter_modules(path=search_path)]
    print(all_modules)
    

Celery

  • Kill celery process:

    kill -9 $(ps aux | grep celery | grep -v grep | awk '{print $2}' | tr '\n' ' ') > /dev/null 2>&1
    
  • View worker logs:

    celery -A <worker name: karestoon> worker --loglevel=info
    

PIP

  • Install from source (Unix path):

    pip install file:///path/to/package
    

Operators

  • Overview

  • The "i" symbol means "in place"

  • The "a" symbol means "asynchronous"
  • The "r" symbol means "reverse"
  • When we are implementing the "Rich comparison" method we should return a new object, in the other hand for the "Augmented assignment arithmetic" method we should update the current object

Other

  • cProfile example: