Skip to content

Instantly share code, notes, and snippets.

View Jimmy2027's full-sized avatar

Hendrik_Klug Jimmy2027

View GitHub Profile
@Jimmy2027
Jimmy2027 / cleaning_schedule.py
Last active October 6, 2020 07:17
Code snippet to create a cleaning schedule for your flat
import random
from datetime import datetime
import numpy as np
import pandas as pd
targets = ['big bathroom', 'kitchen', 'floor', 'small bathroom']
cleaners = ['larry1', 'larry2', 'larry3', 'larry4', 'larry5']
nbr_weeks = 11
@Jimmy2027
Jimmy2027 / dataparallel_with_distributions.py
Last active November 11, 2020 13:58
A minimal working example to reproduce the error when using torch.nn.DataParallel with torch.distributions. This code runs when commenting out line 59 but throws an `TypeError: 'Laplace' object is not iterable` when using DataParallel
from torch import nn
from torch.distributions import Laplace
from torch.utils.data import Dataset, DataLoader
import torch
class ToyNet1(nn.Module):
def __init__(self):
super().__init__()
self.dens1 = nn.Linear(in_features=16, out_features=8)
@Jimmy2027
Jimmy2027 / DistributedDataParallel_with_distributions.py
Last active November 11, 2020 20:51
How to correctly run DistributedDataParallel when using torch.distributions. (The same code fails when using torch.nn.DataParallel: https://gist.github.com/Jimmy2027/a8b460180bc8bca757da4cc647df6595) Solves the problem of https://discuss.pytorch.org/t/using-torch-nn-dataparallel-with-torch-distributions-laplace-throws-typeerror-laplace-object-is…
import os
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from torch import nn
from torch.distributions import Laplace
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data import Dataset, DataLoader
@Jimmy2027
Jimmy2027 / andrew
Created December 3, 2020 11:26
Python script to get notified via telegram when bash command is finished
#!/usr/bin/env python3
import argparse
import subprocess
import time
import requests
parser = argparse.ArgumentParser()
parser.add_argument('command', type=str, help='command to be exectued')
@Jimmy2027
Jimmy2027 / snippet.py
Created December 13, 2020 13:25
[How to create a temporary folder with tempfile] #temp_folder #tempfile #python
import tempfile
with tempfile.TemporaryDirectory() as tmpdirname:
with open('some_file.txt', mode='w') as f:
f.write('Hi, and goodbye.')
@Jimmy2027
Jimmy2027 / snipped.py
Created December 13, 2020 13:45
[How to create a unique string with datetime] #unique_string #datetime #experiment_string #python
def get_str_experiment(prefix: str = '') -> str:
from datetime import datetime
dateTimeObj = datetime.now()
dateStr = dateTimeObj.strftime("%Y_%m_%d_%H_%M_%S_%f")
if prefix:
return prefix + '_' + dateStr
else:
return 'exp' + '_' + dateStr
@Jimmy2027
Jimmy2027 / logger.py
Created December 13, 2020 13:52
[python logger] #logger #python
import os
import logging
import pathlib
import datetime
import multiprocessing, threading
import warnings
# filter out warnings with :
# warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
@Jimmy2027
Jimmy2027 / snippet.sh
Created December 13, 2020 15:31
[update live ebuilds with smart-live-rebuild] #gentoo #bash
smart-live-rebuild
@Jimmy2027
Jimmy2027 / main.py
Created December 14, 2020 08:22
[capture the output of subprocess while still printing it in real time] #subprocess-tee #python #ppb
from subprocess_tee import run
output = run('python script1.py', shell=True)
print(output)
@Jimmy2027
Jimmy2027 / snippet.py
Created January 2, 2021 17:04
[recurisvely change file permissions with duplicate dict as reference]
# HK, 01.01.21
import argparse
import os
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument('--dir_path', type=str, help='directory which file permissions will be changed')
parser.add_argument('--ref_path', type=str, help='directory of which file permissions will be taken as reference')
flags = parser.parse_args()
dir_path = Path(flags.dir_path)