Skip to content

Instantly share code, notes, and snippets.

import hashlib
def compute_file_checksum(
filepath: str,
algorithm: str = "md5",
chunk_size: int = 1024,
) -> str:
hasher = hashlib.new(algorithm)
with open(filepath, "rb") as fp:
from typing import Type, TypeVar, Union
import numpy as np
Number = TypeVar("TypeVar", int, float)
class Rectangle(np.ndarray):
def __new__(
cls,
#!/bin/bash
set -eu
# Install PyEnv
if [ ! -e ~/.pyenv ]; then
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
@odoku
odoku / coroutine.py
Created May 10, 2019 07:18
python - coroutine
import asyncio
async def wait():
print('Waiting')
for x in range(10 ** 100):
z = 10 + 20
print('Done')
@odoku
odoku / threading.py
Created May 10, 2019 07:16
python - threading
import concurrent.futures
def wait():
print('Waiting')
for x in range(10 ** 100):
z = 10 + 20
print('Done')
@odoku
odoku / multiproc.py
Created May 10, 2019 07:14
python - multi processing
import concurrent.futures
def wait():
print('Waiting')
for x in range(10 ** 100):
z = 10 + 20
print('Done')
@odoku
odoku / command.py
Created March 22, 2019 11:27
Abstract command wrapper for Python
import subprocess
class Command(object):
def __init__(self, *args, encode='utf8'):
self.encode = encode
self.args = args
def __repr__(self):
return '<Command({})>'.format(' '.join(self.args))
@odoku
odoku / docker-install.sh
Last active August 18, 2016 12:21
ubuntu16.04用のdockerインストールスクリプト。
#!/bin/bash
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
sudo sh -c "echo 'deb https://apt.dockerproject.org/repo ubuntu-xenial main' > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get purge lxc-docker
@odoku
odoku / discriptor.py
Last active December 31, 2015 01:09
Pythonのディスクリプタのサンプル。
class Thumb(object):
def __init__(self, path):
self.url = 'http://hoge.com' + path
self.path = path
class ThumbDiscriptor(object):
BASE_PATH = '/img/{0}/{0}_{1}.{2}'
def __init__(self, name):
self.name = name
@odoku
odoku / models.py
Last active December 26, 2015 20:59
DjangoのModelForm拡張。 Metaにerror_messagsを書けるようにしたので、バリデーションエラー時に出力される文言を簡単に変更出来る。
#-*-coding=utf8-*-
from __future__ import absolute_import, unicode_literals
from django.utils import six
from django.forms.forms import get_declared_fields
from django.forms.models import BaseModelForm
from django.forms.widgets import media_property
from django.utils.datastructures import SortedDict
def fields_for_model(model, fields=None, exclude=None, widgets=None, error_messages=None, formfield_callback=None):