Skip to content

Instantly share code, notes, and snippets.

Edvard Majakari EdvardM

Block or report user

Report or block EdvardM

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@EdvardM
EdvardM / dhontd.py
Last active Apr 14, 2019
Toy program for playing with D'Hondt system
View dhontd.py
import argparse
import logging
import random
import string
from itertools import groupby
from operator import itemgetter
PARTIES = ["Kesk", "Kok", "SDP", "Sin", "PS", "Vihr", "Vas", "RKP", "KD", "TL", "EOP", "FP", "IPU", "KP", "KTP", "Lib",
"PP", "SKE", "SKP"]
@EdvardM
EdvardM / mapply.py
Created Sep 14, 2018
Just a simply utility to apply sequence of functions, applying each in turn to return value of previous application, stopping at None
View mapply.py
def mapply(init, *funs):
"""
Apply list of funs from first to last, stopping at first None value.
Used eg. to transform & decorate a value, stopping at any point if
last evaluation produced None.
Args:
init (Any): initial value
*funs (Callable[Any, Any]): List of functions to apply
@EdvardM
EdvardM / pattern_to_seq.py
Last active Jul 14, 2018
Just a simple tool to remap long regular patterns with shorter ones, useful for comparing two sets of data for equality
View pattern_to_seq.py
import re
import sys
import string
def _get_fh(has_fname):
if has_fname:
return open(sys.argv[1])
else:
return sys.stdin
@EdvardM
EdvardM / pre-commit.sh
Created Apr 24, 2018
Simple pre-commit hook for checking python files with pycodestyle before committing
View pre-commit.sh
#!/bin/sh
set -u
FILES=$(git diff --cached --name-only --diff-filter=ACM)
pycodestyle ${FILES}
@EdvardM
EdvardM / better_mock_error_messages.py
Last active Mar 27, 2018
Make mock assertion failures readable
View better_mock_error_messages.py
# mock objects print assertion failures in a way that is hard to read.
#
# Example:
#
# AssertionError: Expected call: <mock assertion failure: 1122331 ...>
# Actual call: <mock assertion failure: 112331 ...>
#
# After the patch:
#
# AssertionError:
@EdvardM
EdvardM / bowling.hs
Last active Mar 19, 2018
Bowling kata, haskell version
View bowling.hs
-- Credits to awesome Haskell Master Sami Hangaslammi (shangaslammi) who told to learn unfoldr, which
-- leads IMO to very elegant solution.
--
-- 2012: original version, Hspec outdated, no longer available
-- 2018-03-19: Updated to recent Hspec
-- Running: cabal install hspec-2.4.4 && runhaskell bowling.hs
import Test.Hspec
import Data.List (unfoldr)
@EdvardM
EdvardM / stats.py
Created Feb 21, 2018
Simple stats module
View stats.py
from statistics import mean, median_low, stdev, mode
def stat(lst):
if not lst:
return None
_min = min(lst)
_max = max(lst)
_mean = mean(lst)
@EdvardM
EdvardM / stats.py
Created Feb 21, 2018
Simple stats module
View stats.py
# Simple stats module. For large sets of data, check Numpy instead
from statistics import mean, median_low, stdev, mode
def stat(lst):
if not lst:
return None
_min = min(lst)
@EdvardM
EdvardM / progress_bar.py
Created Jan 26, 2018
Simple python progress bar updater, using progressbar2
View progress_bar.py
import contextlib
import os
import progressbar
@contextlib.contextmanager
def build_updater(**kwargs):
kwargs['max_value'] = kwargs.get('max_value', progressbar.UnknownLength)
update_interval = kwargs.get('update_interval', 1)
@EdvardM
EdvardM / avgtime.sh
Created Sep 26, 2017
Script to run command multiple times and return averages
View avgtime.sh
#!/bin/bash
# Script originally by bobmcn, taken from
# https://stackoverflow.com/questions/17601539/calculate-the-average-of-several-time-commands-in-linux
# Changelog: added NSAMPLES to run given script only given number of times (defaults to 3)
NSAMPLES=${NSAMPLES-3}
rm -f /tmp/mtime.$$
You can’t perform that action at this time.