A place to collect some code snippets I seem to usually need and forget at the same time
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md

README.md

snippets

A place to collect some code snippets I seem to usually need and forget at the same time...

Table of Contents

  1. Python
  2. Nvidia
  3. Bash
  4. Tmux
  5. Linux
  6. Docker
  7. Conda
  8. Tensorflow

Python

Package version

import some_package
some_package.__version__

Module content

import some_module
dir(some_module)

Pip package version

$ pip list | grep the_pip_package_you_seek

Nvidia

Cuda version

cat /usr/local/cuda/version.txt

or with nvcc installed:

$ nvcc --version

CuDNN version

$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

Check Nvidia GPU

$ nvidia-smi

alternative: gpustat

$ pip install gpustat
$ gpustat -cp

Run script on a specific GPU

$ CUDA_VISIBLE_DEVICES=0 python some_script.py

Nginx

Nginx test config

$ nginx -t

Nginx restart (Ubuntu)

$ systemctl restart nginx

Tmux

list sessions

$ tmux list-sessions

attach to session

$ tmux attach-session -t #tmux_session_id

kill session

$ tmux kill-session -t SESSION_ID

split vertically

Ctrl+b %

split horizontal

Ctrl+b "

cycle through panes

Ctrl+b o

zoom to current pane

Ctrl+b z
# Use again to zoom out again

kill pane

Ctrl+b x

Linux

Print linux version info

$ cat /etc/*-release

update your linux

#!/bin/sh
sudo apt-get update && sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
sudo apt-get autoclean -y
if [ -f "/var/run/reboot-required" ]; then
    /bin/cat /var/run/reboot-required
fi

Which kernel you are running

$ uname -r

Having trouble installing update because of a full /boot drive

Ipbastola: Safest way to clean up boot partition

Docker

List containers

# Running containers
$ docker ps

# Local container images
$ docker images

Build container in current directory

$ docker build -t container_tag_name .

Run container detached

# -d: detached
# -p: external_port:internal_port
# --name: running container name
# (frontend): container_tag_name

$ docker run -d -p 8080:80 --name=frontendlocal frontend

Connect bash to a running container

$ docker exec -it container_name /bin/bash

Start a container that will be proxied by nginx-proxy

(note: the --network flag is not mentioned in the docs, but I needed it to fix a connection problem)

$ docker run -e VIRTUAL_HOST=DNS_NAME --network=nginx-proxy -d zerotosingularity_site

Force remove a container:

docker rmi -f <image_id> 

Remove all containers (except the ones that need to be forced)

```
docker rmi $(docker images | grep none | awk '{print $3}')
```

Conda

Update the current environment based on environment file

$ conda env update

Create an environment

$ conda create --name myenv

Create an environment from a file

$ conda env create -f environment.yml

Create an environment with a specific Python version

$ conda create -n myenv python=3.6 # change 'myenv' and '3.6' to your values

List existing environments

$ conda info --envs
# OR
$ conda env list

Save all info about your packages

conda list -e > requirements.txt

delete an environment

conda env remove --name ENV_NAME

Tensorflow

Tensorflow version

$ python -c 'import tensorflow as tf; print(tf.__version__)'  # for Python 2
$ python3 -c 'import tensorflow as tf; print(tf.__version__)'  # for Python 3