Skip to content

Instantly share code, notes, and snippets.

View shiumachi's full-sized avatar

Sho Shimauchi shiumachi

View GitHub Profile
@shiumachi
shiumachi / pyproject.toml
Created May 24, 2023 01:38
reproduce poetry export issue with explicit source
[tool.poetry]
name = "poetry-export-issue-2023-05-23"
version = "0.1.0"
description = ""
authors = ["XXX <[email protected]>"]
readme = "README.md"
packages = [{include = "poetry_export_issue_2023_05_23"}]
[tool.poetry.dependencies]
python = "^3.10"
@shiumachi
shiumachi / example_detect_unexpected_callable.py
Created February 22, 2023 07:29
Pythonで誤って関数に括弧をつけず呼び出した場合にどこまで検知できるか
# Pythonで誤って関数に括弧をつけず呼び出した場合にどこまで検知できるか
# @shiumachi, 2023/02/22
# Python 3.11.2, mypy 1.0.1
def main():
def func(x: int = 1) -> int:
return x
# 検知できない例1: 元々callableでも受け付けられる使い方をする場合
print(f"{func=}")
@shiumachi
shiumachi / .flake8
Last active September 12, 2022 05:46
Config files for a new python project
[flake8]
max-line-length = 88
select = C,E,F,W,B,B950
extend-ignore = E203, E501
@shiumachi
shiumachi / validate_read_csvs.py
Created September 15, 2020 00:44
simple validation function for a list of pandas.DataFrame
import pathlib
import typing
import pandas as pd
# data definition
## valid data
df1 = pd.DataFrame(
[
{"c1": 100, "c2": "a100"},
@shiumachi
shiumachi / pytest.md
Last active March 3, 2020 08:59
Pytestの書き方入門

インストール

pip install pytest pytest-cov pytest-randomly pytest-mock
  • pytest-cov: カバレッジ計測プラグイン
  • pytest-randomly: 実行順のランダム化
  • pytest-mock: mock利用プラグイン
@shiumachi
shiumachi / README.md
Last active August 7, 2019 06:00
Bot Saying Migrate CLI

saying migrate CLI

Usage

  • Copy the output of saying list from the source saying command.
  • Run the following command with pasting the copied outputs
$ python saying_migrate.py --from-saying foo --to-saying bar - <<EOM
(paste the copied text here)
EOM
@shiumachi
shiumachi / bot_commands.py
Created April 18, 2019 08:25
pyspa-bot: phonics, lower, upper command
# phonics
ph = {"a": "アッ", "b": "バッ", "c": "カッ", "d": "ダッ", "e": "エ", "f": "フッ", "g": "グッ", "h": "ハッ", "i": "イ", "j": "ジャ", "k": "クッ", "l": "ル", "m": "ム", "n": "ヌ", "o": "オ", "p": "パッ", "q": "カッ", "r": "ル", "s": "ス", "t": "ツ", "u": "ウッ", "v": "ヴッ", "w": "ウッ", "x": "クス", "y": "ヤッ", "z": "ズ"}
arr = []
for a,b in ph.items():
arr.append(f'rsub("{a}", "{b}",')
result = ''.join(arr) + "{@1}"" + ')' * len(arr)
result
# upper
d = {}
@shiumachi
shiumachi / random_pick.py
Created November 29, 2018 02:02
return a random item in a list, which has different probability.
# -*- coding: utf-8 -*-
# Reference: Python Cookbook 2nd Ed. p.188
import random
def random_pick(item_list, probabilities):
""" return a random item in a list, which has different probability.
# -*- coding: utf-8 -*-
# Reference: Python Cookbook 2 Ed. p.198
def get_sorted_dict_values(d):
keys = d.keys()
keys.sort()
return [d[key] for key in keys]
import datetime
def convert_to_datetime(date_string):
""" input: %Y-%m-%d %H:%M:%S,%f
example: 2014-01-05 22:20:50,307
return: datetime object
"""
date_format = "%Y-%m-%d %H:%M:%S,%f"