Skip to content

Instantly share code, notes, and snippets.

View edvardm's full-sized avatar

Edvard Majakari edvardm

  • Rakettitiede Oy
  • Finland
  • 23:46 (UTC +03:00)
View GitHub Profile
@edvardm
edvardm / argparse.py
Created January 16, 2020 10:07
Very simple template for creating simple python CLI tools with argparse
import argparse
def parse_args():
parser = argparse.ArgumentParser(
description="", formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("-v", "--verbose", action="store_true", help="be more verbose")
return parser.parse_args()
@edvardm
edvardm / clean_code.md
Created December 18, 2019 13:52 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@edvardm
edvardm / .pylintrc_compact
Created December 5, 2019 12:38
compact pylintrc
[MASTER]
extension-pkg-whitelist=
ignore=CVS
ignore-patterns=
jobs=4
limit-inference-results=100
persistent=yes
suggestion-mode=yes
unsafe-load-any-extension=no
[MESSAGES CONTROL]
@edvardm
edvardm / pybootstrap.sh
Last active June 17, 2019 12:22
Simple bootstrapt script intended to be used /w curl or similar
#!/usr/bin/env bash
# This script is intended to setup things so that more advanced project commands (doit, poetry) can be easily used
set -eu
VENV_DIR=".venv"
echo "Creating venv to ${VENV_DIR} unless present"
test -d ${VENV_DIR} || python3 -m venv ${VENV_DIR}
echo "Activate venv and install poetry"
@edvardm
edvardm / tenacity-simple.py
Last active June 4, 2019 11:43
Most simple tenacity usage
# Just a simple one-liner to quickly make a thing retrieable
import tenacity as tn
@tn.retry(wait=tn.wait_exponential(multiplier=1, min=10), stop=tn.stop_after_attempt(5))
@edvardm
edvardm / dhontd.py
Last active April 14, 2019 12:26
Toy program for playing with D'Hondt system
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
Last active June 18, 2019 08:07
Just a simply utility to apply sequence of functions, applying each in turn to return value of previous application
def mapply(init, *funs, stop=lambda x: x is None):
"""
Apply list of funs from first to last, stopping when encountering first
return value of None.
Args:
init (Any): initial value
*funs (*Callable[..., Any]): List of functions to apply
Keyword args:
@edvardm
edvardm / pattern_to_seq.py
Last active July 14, 2018 09:20
Just a simple tool to remap long regular patterns with shorter ones, useful for comparing two sets of data for equality
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 April 24, 2018 09:01
Simple pre-commit hook for checking python files with pycodestyle before committing
#!/bin/sh
set -u
FILES=$(git diff --cached --name-only --diff-filter=ACM)
pycodestyle ${FILES}
@edvardm
edvardm / better_mock_error_messages.py
Last active March 27, 2018 13:51
Make mock assertion failures readable
# 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: