Code

Conventions

  • Ignore output by using an underscore

    _ = foo()
  • Single line functions in python are lambda functions

    perms = lambda xs: (list(x) for x in itertools.permutations(xs))  

Plotting

A nice gallery of plots made using matplotlib and seaborn can be found at python graph gallery.

According to this page, it is good to start with matplotlib and seaborn, and maybe ggplot if you feel up for the challenge. Bokeh is also used often but I think it is good for interactive plots (not yet).

Something to try out: https://github.com/Kanaries/pygwalker

Setup

IDE

Jupyter lab

jupyterlab_shortcuts: Jupyter lab shortcuts are described here and there markdown file was downloaded from here.

Managing kernels
  • Installing new kernels (ensure the environment is active before running this)

    python -m ipykernel install --user --name kernel_name --display-name "kernel_name"
  • Checking installed kernels: jupyter kernelspec list

Virtual environments

Virtualfish

Use virtualfish if you are using fish terminal.

pipx install virtualfish

Also install the following plugins if possible and refer to this page for more details on the plugins.

vf install compat_aliases projects environment

This will allow you to use the following:

  • workon <envname> = vf activate <envname>
  • deactivate = vf deactivate
  • mkvirtualenv [<options>] <envname> = vf new [<options>] <envname>
  • mktmpenv [<options>] = vf tmp [<options>]
  • rmvirtualenv = vf rm <envname>
  • lsvirtualenv = vf ls
  • cdvirtualenv = vf cd
  • cdsitepackages = vf cdpackages
  • add2virtualenv = vf addpath
  • allvirtualenv = vf all
  • setvirtualenvproject = vf connect

This works well for standard python packages with M1 support. If there are problems with the packages, try the below approach

Conda

Virtualfish works fine for most environments and use cases. However, if you need specific python version or if you need it for a particular architecture (like i386 in M1 macs), using virtual fish becomes tricker. One way is to use pyenv to maintain different python versions, and using rosetta terminal to run and install pyenv for i386, like described here. I found it a bit complicated.

I found an alternate approach here and realized conda might be better for these sort of things (instead of pyenv). I installed conda using brew and ran conda init fish to set up the env for fish environment. I created an i386 environment as described in here and it worked easily (later created a function for it too). I recommend using it.

Here is some stuff I found about conda (didn’t know too much about it till then):

Once a conda environment is created, if you want to use it with jupyter lab, just install ipykernel (and maybe ipywidgets) and link jupyter (as always).

  • conda install ipykernel ipywidgets
  • python -m ipykernel install --user --name env_name --display-name "env_name"
Things to avoid
  • DO NOT update from miniforge 4 to 22. It will break openssl. I had to reinstall it after messing it up. Luckily it was easy.
Tricks and tips
  • If you initialize fish shell, it will add a line into your fish config. That makes the terminal slow as it activates conda everytime. Instead, you can create an alias and activate it when you want.
About conda and mini-forge

Differences between conda and mini-forge