Skip to content

Instantly share code, notes, and snippets.

@pgp
pgp / enumerate_combinations.py
Last active April 17, 2023 09:35
simple recursive algorithm and corresponding iterative variant to enumerate subsets of size k of a n-sized set
import collections
def enumcombs(l: list, k: int):
# assert len(l) > 0 and k > 0
L = []
# s = sorted(l) # should only be done on first call
if k==1:
return [[x] for x in l]
for i,item in enumerate(l):
# assumption: no repeated items, otherwise have to use an auxiliary index set
import os.path
import requests
import tqdm
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
def create_callback(file_size):
bar = tqdm.tqdm(total=file_size, unit='iB', unit_scale=True)
import requests
files = {'file': ('3.bin', open('/dev/shm/3.bin', 'rb'))}
r = requests.post("https://x0.at/", files=files)
download_link = r.text.strip()
print(download_link)
# Generalization of dict, representing a multijection of m key sets K = k1,...,kn ; J = j1,...,jn ; ... ; Z = z1,...,zn
# storage complexity is O(m*n)
# set_view_index chooses the current key set to be used, all the remaining key sets are treated as value sets (the "total" value is returned as list)
# semantically equivalent to a SQL database table, in which every single column can be considered as a primary key one
# Assumption: every single column must not contain duplicate values within itself
# (BUT, an item can appear in different columns, even in the same position)
class Multijection(dict):
# each one of args is a list of items, each one that can be used as key
def __init__(self, *args):
@pgp
pgp / delayed_fuse_passthrough.py
Created April 14, 2020 10:22
Delayed FUSE passthrough filesystem in Python
from __future__ import with_statement
import os
import sys
import errno
import time
from threading import Timer
from fuse import FUSE, FuseOSError, Operations
@pgp
pgp / logcat_to_colored_stdout.py
Last active March 9, 2020 12:04
Colorize logcat file and print to console
import sys
import re
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
@pgp
pgp / qsortExample.cpp
Created February 4, 2020 09:15
qsort example
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdint>
#include <cstring>
/**
* g++ -O3 qsortExample.cpp
*
* # MinGW
@pgp
pgp / hybrid_batch_bash.bat
Created January 30, 2020 12:36
Hybrid batch/bash script
:<<BATCH
@echo off
rem Web source: https://stackoverflow.com/questions/17510688/single-script-to-run-in-both-windows-batch-and-linux-bash
echo %PATH%
exit /b
BATCH
#!/bin/bash # CAUTION: SHEBANG WON'T WORK HERE (NOT FIRST LINE)
echo $PATH
#ifndef __RH_PROGRESS_HOOK__
#define __RH_PROGRESS_HOOK__
#include "unifiedlogging.h"
#ifdef _WIN32
#include <shobjidl.h>
#endif
class ProgressHook {
public: