Skip to content

Instantly share code, notes, and snippets.

@konradkonrad
konradkonrad / es_features.py
Last active January 25, 2022 23:52
tfidf from elasticsearch
import elasticsearch
from math import log
def tfidf_matrix(es, index, doc_type, fields, size=10, bulk=500, query=dict(match_all=[])):
"""Generate tfidf for `size` documents of `index`/`doc_type`.
All `fields` need to have the mapping "term_vector": "yes".
This is the consuming version (i.e. get everything at once).
:param es: elasticsearch client
@konradkonrad
konradkonrad / message_federation_penetrator.py
Last active November 18, 2020 15:40 — forked from fredo/gist:c8c242c2ca5ba3bba57dcd11d4e42f63
message_federation_penetrator.py
from gevent import monkey # isort:skip
monkey.patch_all() # isort:skip
from collections import defaultdict
from typing import List
import gevent
from gevent.event import Event
from matrix_client.errors import MatrixRequestError
"""Types used to represent transaction and its states.
Each state of a transaction is described by a different wrapper dataclass, this
is used to introduce new fields in a type safe manner, and to enforce correct
usage.
Transactions are by design immutable and don't have access in their field to
objects that do IO, that is why the RPC class is given as an argument to each
method, when necessary.
"""
@konradkonrad
konradkonrad / solc
Last active March 5, 2020 06:03
solc from docker (sort of)
#!/usr/bin/env bash
# Usage:
# ./solc [options] inputfile > outfile
# Notes:
# - file i/o is limited to the current directory
# - this works with the pyethereum solc_wrapper
docker run -i --rm --user $(id -u):$(id -g) -v $(pwd):/tmp --workdir /tmp ethereum/solc:0.4.18 $@
@konradkonrad
konradkonrad / find_ghost_locksroots.py
Created May 15, 2019 10:51
a script for detecting unrecoverable unlock locksroots
#!/usr/bin/env python
import json
import sys
from collections import defaultdict
from raiden.storage.sqlite import SQLiteStorage
def read_args():
if len(sys.argv) < 2 or "-h" in sys.argv or "--help" in sys.argv:
@konradkonrad
konradkonrad / test_and_report.sh
Last active May 8, 2019 10:42
run tests and show coverage report html for raiden
#!/usr/bin/env sh
# pytest executable
PYTEST=${PYTEST:-$(which pytest)}
# specify non temp output directory
OUT_DIR=${OUT_DIR:-$(mktemp -d)}
# which tests to execute
SUITE=${SUITE:-raiden/tests/unit raiden/tests/fuzz}
# report only these files
ONLY=${ONLY:-*}
from itertools import count
import gevent
from gevent.event import AsyncResult
def wait_twice(async_result):
print('wait_twice enter')
gevent.joinall(set([async_result, async_result]), raise_error=True)
print('wait_twice exit')
@konradkonrad
konradkonrad / .sh
Created January 31, 2019 11:30 — forked from schmir/.sh
show git status on tab key
# When Tab is pressed on an empty(!) command line, the contents of the
# directory are printed (`ls`) instead of a menu list of all
# executables:
function my-hg-report() {
tip=$(hg tip 2> /dev/null) || return
echo
hg --config pager.pager= status
echo
hg --config pager.pager= log --limit 5 --style compact
@konradkonrad
konradkonrad / py_watch.py
Last active April 9, 2018 15:53
minimal statusbar watcher
import sys
import time
def reprint(msg):
"""Print msg from beginning of last line."""
sys.stdout.write('\r')
sys.stdout.write(str(msg))
sys.stdout.flush()
def watch(fn, update=0.25):
@konradkonrad
konradkonrad / doof.sh
Last active February 28, 2018 09:26
[D]ocker [O]ne [OF]f script (run a container inside the current working directory)
#!/usr/bin/env sh
## Features:
## - host's working directory is mounted to random location as the container's working directory
## - uid/gid of the current user are preserved inside the container
## - a host tmp dir will be mounted as /home/user and published as HOME environment variable inside the container
## - all EXPOSED ports will be published (check `docker port $(docker ps -q|head -n1)`).
## - container will be removed on exit
##
## Examples: