Skip to content

Instantly share code, notes, and snippets.

View i-zaitsev's full-sized avatar

Ilia Zaitsev i-zaitsev

View GitHub Profile
@i-zaitsev
i-zaitsev / example.py
Last active August 25, 2019 17:12
Catalyst example. (Doesn't work as expected).
import os
import re
from pdb import set_trace
from multiprocessing import cpu_count
from pprint import pprint as pp
from imageio import imread
import numpy as np
import pandas as pd
import PIL.Image
@i-zaitsev
i-zaitsev / 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():
@i-zaitsev
i-zaitsev / 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
@i-zaitsev
i-zaitsev / 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'
@i-zaitsev
i-zaitsev / 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()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@i-zaitsev
i-zaitsev / 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.
"""
@i-zaitsev
i-zaitsev / 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()
@i-zaitsev
i-zaitsev / 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)