Skip to content

Instantly share code, notes, and snippets.

View shubhamagarwal92's full-sized avatar
☃️
Focusing

Shubham Agarwal shubhamagarwal92

☃️
Focusing
View GitHub Profile
@shubhamagarwal92
shubhamagarwal92 / get_file_name.sh
Created August 2, 2019 22:35
Get the file name of running bash script without extension
#!/usr/bin/env bash
export CURRENT_DIR=${PWD}
export PARENT_DIR="$(dirname "$CURRENT_DIR")"
echo "File is in: " $CURRENT_DIR
# https://stackoverflow.com/questions/192319/how-do-i-know-the-script-file-name-in-a-bash-script
export FileNameWithExt=${0##*/}
export FILE_NAME=${FileNameWithExt%.*}
@shubhamagarwal92
shubhamagarwal92 / get_chunks_list.py
Created August 1, 2019 21:47
Get batch size chunks from list
# Yield successive n-sized
# chunks from l.
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]
test_list = list(range(100))
@shubhamagarwal92
shubhamagarwal92 / save_h5_data.py
Created August 1, 2019 21:14
Save h5 data in a file
def save_h5_data(file_path, data_split="train", **data_dump):
hf = h5py.File(file_path, 'w')
hf.attrs['split'] = data_split
for key in data_dump:
hf.create_dataset(key, data=data_dump[key])
hf.close()
@shubhamagarwal92
shubhamagarwal92 / get_class_from_str.py
Created July 22, 2019 21:57
Call a class by using its name as string
# Also refer https://stackoverflow.com/a/1176225
import sys
class Foobar:
pass
def str_to_class(str):
return getattr(sys.modules[__name__], str)
str_to_class("Foobar")
@shubhamagarwal92
shubhamagarwal92 / get_bert_embeddings.py
Created July 22, 2019 21:49
Use pytorch-transformers from hugging face to get bert embeddings in pytorch
import torch
import logging
from pytorch_transformers import BertModel, BertTokenizer
from pytorch_transformers import *
from typing import List
CLS_TOKEN = "[CLS]"
SEP_TOKEN = "[SEP]"
logger = logging.getLogger(__name__)
@shubhamagarwal92
shubhamagarwal92 / ssh_jupyter.sh
Last active December 27, 2022 13:29
How to start jupyter notebook over server and doing ssh
# Refer https://fizzylogic.nl/2017/11/06/edit-jupyter-notebooks-over-ssh/
# On server
jupyter notebook --no-browser --port=8080
# On local machine
ssh -N -L 8080:localhost:8080 <remote_user>@<remote_host>
@shubhamagarwal92
shubhamagarwal92 / convert_args_dic.py
Last active August 12, 2019 17:30
convert args from argparse to dictionary
import argparse
def parse_args():
....
return args
args = parse_args()
config = vars(args)
for arg in vars(config):
git submodule deinit <path_to_submodule>
git rm <path_to_submodule>
git commit-m "Removed submodule "
rm -rf .git/modules/<path_to_submodule>
@shubhamagarwal92
shubhamagarwal92 / get_current_date.sh
Created July 10, 2019 18:45
Get current date as variable in shell script
today="$(date '+%Y-%m-%d')"
yesterday="$(date -d yesterday '+%Y-%m-%d')"
echo $today
echo $yesterday
@shubhamagarwal92
shubhamagarwal92 / .gitignore
Created June 14, 2019 20:39
Generic git ignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.idea/
.DS_Store/
# C extensions
*.so
# Distribution / packaging