Skip to content

Instantly share code, notes, and snippets.

View devforfu's full-sized avatar

Ilia devforfu

View GitHub Profile
@devforfu
devforfu / concat_and_make_cli.py
Last active July 2, 2019 18:19
Converting function signature into CLI
import argparse
from pathlib import Path
import signature
def concat(folder: str, output: str = 'concat.txt', include_names: bool = True):
"""Concatenates a list of files from a folder and saves them into a single file."""
p = Path(folder)
files = []
for filename in p.iterdir():
@devforfu
devforfu / parsing_signature.py
Last active June 30, 2019 15:51
Construct CLI from a signature
import argparse
import signature
def make_cli(func: callable):
parser = argparse.ArgumentParser()
sig = inspect.signature(func)
empty = inspect._empty
for param in sig.parameters.values():
annot = param.annotation
@devforfu
devforfu / ancli.py
Last active July 1, 2019 19:29
Converting function into CLI
from pathlib import Path
def concat(folder: str, output: str = 'concat.txt', include_names: bool = True):
"""Concatenates a list of files from a folder and saves them into a single file."""
p = Path(folder)
files = []
for filename in p.iterdir():
content = filename.open('r').read().strip()
if include_names:
content = f'{filename}\n{content}\n'
@devforfu
devforfu / sample_test.py
Last active March 19, 2019 10:35
An illustrative snippet for the blog post
import pytest
@pytest.mark.parametrize('state', [
'ooo|x.x|..x',
'xo.|xo.|x..',
'x..|.xo|o.x'
])
def test_game_has_winner(state):
game = Game(state)
assert game.has_winner()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@devforfu
devforfu / gendata.py
Created March 6, 2019 13:46
A simple random dataset generating script
def generate_dataset(n_rows, num_count, cat_count, max_nan=0.1, max_cat_size=100):
"""Randomly generate datasets with numerical and categorical features.
The numerical features are taken from the normal distribution X ~ N(0, 1).
The categorical features are generated as random uuid4 strings with
cardinality C where 2 <= C <= max_cat_size.
Also, a max_nan proportion of both numerical and categorical features is replaces
with NaN values.
"""
@devforfu
devforfu / cursed.py
Created February 16, 2019 09:12
An illustration of the debugging issues when using curses
import curses
def main(term):
term.clear()
for i in range(10):
# try to debug after curses mangled the terminal
breakpoint()
term.addstr(i, 0, f'String: {i + 1:d}')
term.refresh()
term.getkey()
@devforfu
devforfu / example_click.py
Created February 10, 2019 15:16
CLI with Click
from itertools import chain
import click
import matplotlib.pyplot as plt
from matplotlib import rcParams
default_style = {
'font.family': 'monospace',
'font.size': 18,
'figure.figsize': (8, 6)
@devforfu
devforfu / example_docopt.py
Created February 10, 2019 15:15
CLI with Docopt
"""
Scatter plots generator.
Usage:
scatter <filename> <X> <Y> (<X> <Y>)...
"""
from itertools import chain
import docopt
import matplotlib.pyplot as plt