Skip to content

zerotosingularity/snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 

Repository files navigation

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

Pip install from Github Commit

pip install git+https://github.com/username/repo-name.git@commit-hash

GPU Count PyTorch

import torch
torch.cuda.device_count()

Select GPU PyTorch

import torch
torch.cuda.set_device(device_num)

More PyTorch GPU options

Nvidia

Cuda version

cat /usr/local/cuda/version.txt

or with nvcc installed:

$ nvcc --version

Install nvcc:

$ (sudo) apt install nvidia-cuda-toolkit

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

or

$ export CUDA_VISIBLE_DEVICES=0
$ python some_script.py

Bash

Rename all files in subdirectories

In the example we replace all *.JPG files with *.jpg:

$ find . -name "*.JPG" -exec bash -c 'mv "$1" "${1%.JPG}".jpg' - '{}' \;

Only count images in all directories

$ ls -lR | grep jpg | wc -l

Untar multiple files into their own folders

#!/bin/bash

for pkg in *.tar; do
    where="${pkg%.tar}"

    echo $where

    [ -d "$where" ] || mkdir -p "$where"

    tar xfv $pkg -C "$where"
done

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

detach current session

Ctrl+b d

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

Equal horizontal width

Ctrl+b, Alt+1

Or (default on Ubuntu)

Ctrl+b, Esc+1

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}') 

Remove all unused data

$ docker system prune -a

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •