Skip to content

Instantly share code, notes, and snippets.

View error_messages.py
import numpy as np
class MathErrors:
ZeroDivision = 'division failed: argument {argument} is zero.'
NaN = 'multiplication failed: argument {argument} is not a number.'
OutOfRange = 'value {argument} should be in range [{a}, {b}), but {value} received'
def divide(a: float, b: float) -> float:
@devforfu
devforfu / traceback_decorator.py
Last active Jun 23, 2020
Traceback logging decorator
View traceback_decorator.py
import logging
import traceback
from typing import Callable
def report_failure_traceback(logger: logging.Logger, exc: Exception) -> None:
logger.critical('*** Unexpected error ***')
logger.critical('')
lines = traceback.format_tb(exc.__traceback__)
lines = ''.join(lines).split('\n')
@devforfu
devforfu / dicom.txt
Created Nov 20, 2019
Example of DICOM file content
View dicom.txt
(0008, 0018) SOP Instance UID UI: ID_3d2ea4e22
(0008, 0060) Modality CS: 'CT'
(0010, 0020) Patient ID LO: 'ID_e758d530'
(0020, 000d) Study Instance UID UI: ID_ebb91b8154
(0020, 000e) Series Instance UID UI: ID_57e1457f93
(0020, 0010) Study ID SH: ''
(0020, 0032) Image Position (Patient) DS: ['-125', '26', '272.300049']
(0020, 0037) Image Orientation (Patient) DS: ['1', '0', '0', '0', '1', '0']
(0028, 0002) Samples per Pixel US: 1
(0028, 0004) Photometric Interpretation CS: 'MONOCHROME2'
@devforfu
devforfu / example.py
Last active Aug 25, 2019
Catalyst example. (Doesn't work as expected).
View example.py
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
@devforfu
devforfu / concat_and_make_cli.py
Last active Jul 2, 2019
Converting function signature into CLI
View concat_and_make_cli.py
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 Jun 30, 2019
Construct CLI from a signature
View parsing_signature.py
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 Jul 1, 2019
Converting function into CLI
View ancli.py
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 Mar 19, 2019
An illustrative snippet for the blog post
View sample_test.py
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()
View xx_final.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View xx_final.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
You can’t perform that action at this time.