My python codebase (including packages/libraries)


Virtual Environment

conda env create -f environment.yml
conda env remove --name myenv
source activate py33
conda deactivate
python -m venv advanced


# get method

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964

x = car.get("price", 15000)
car.setdefault('Age', None)


# slice a dictionary

l = ["english_text","correct_answer","wrong_answer","french_schema_no","pmi_able"]

wino = {a dictionary}

{k:wino[k] for k in l if k in wino} # slice a dictionary

[{"Date": dic["Date"], **dic} for dic in my_list] # reorder keys of a dict using destructuring/unpacking

desired_order_list = [5, 2, 4, 3, 1]

reordered_dict = {k: sample_dict[k] for k in desired_order_list} # reorder 2nd way

d = {0:0, 1:1, 2:2, 3:3}
{"two" if k == 2 else k:v for k,v in d.items()} # rename keys


random.sample(v6,1) # list
random.sample(v6.items(),1) # dict

Reload a package

# import *
reload_package = "hehe"
import importlib
import sys
from utils import \*

# import package

import importlib

Watchdog (use unit test as an exemple)

watch_dog = "hehe"
watchmedo shell-command patterns="*.py;*.txt" --recursive command='echo "${watch_src_path}" .

See my medium post

Get text from web

See my stackoverflow explanation.

import requests
response = requests.get("")
# set encoding with response.encoding = "utf-8"
hehe = response.text

Jupyter to Python

jupyter nbconvert –to script *.ipynb