Frequent and Useful Code Snippits

A collection of useful and frequent code snippets that I am often trying to remember.

Anaconda

List Anaconda Environments

conda info --envs

Update Anaconda

conda update -n base -c defaults conda

Create a Conda Environment

conda create -n envName python=version

Remove a Conda Environment

conda remove -n envName --all

Remove all the downloaded tarballs for installed packages

conda clean --all    # no active envs needed

Conda Revisions

Revisions allows us to track changes to our environment over time and roll-back to earlier time.

List revisions

conda list --revisions

Roll back to earlier time

conda install --revision revision_number

Export Conda enviroment details

conda env export --file environment.yml

Create a conda environment from environment YAML file

conda env create -n conda-env-name -f /path/to/environment.yml

List installed packages in a conda environment

conda list

Git

To stop tracking a file that is currently tracked

git rm --cached

Jekyll

Run the Site Builder

bundle exec jekyll server --watch --livereload

Jupyter

Auto-import commonly used libraries

  1. Create startup folder in ~/.ipython/profile_default
  2. Create a python file start.py
  3. Add imports in the file.
# start.py example
import numpy as np
import pandas as pd

# and so on ...

Convert Jupyter notebook to HTML, markdown, PDF and more…

jupyter nbconvert --to=html or markdown or pdf notebook.ipynb

Python

Get memory usage of python script

import os
import psutil
process = psutil.Process(os.getpid())
print(process.memory_info().rss)

Set random seed for the project

seed_value = 42
random.seed(seed_value)
np.random.seed(seed_value)
torch.manual_seed(seed_value)
torch.cuda.manual_seed_all(seed_value)
tf.random.set_seed(seed_value)

Shell

Set execute persmission for a script

chmod +x script_name.sh

Run a script

# First way
bash script_name.sh

# Second way
sh script_name.sh

# Third way
./script_name.sh