This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import re | |
from typing import Any, Callable, Generator, Iterable, Iterator | |
def split_normal(word: str, sep: str = ',') -> Iterator[str]: | |
return iter(word.split(sep)) | |
def split_camel(word: str) -> Generator[str, None, None]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FYI: https://gist.github.com/FGtatsuro/06387542af093d98ef5b | |
import datetime | |
# timezone=utcのawareオブジェクトを基準にする | |
time_info = datetime.datetime.now(tz=datetime.timezone.utc) | |
print(time_info) | |
# ローカライズしたawareオブジェクト | |
# 夏時間その他を扱う必要がなければこれで十分 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FYI: https://docs.python.org/ja/3/library/subprocess.html#subprocess.run | |
import subprocess | |
proc = subprocess.run(["find . -type f -name '*' | grep -v pycache"], | |
stdout=subprocess.PIPE, shell=True, encoding='utf-8') | |
print(f'status_code:{proc.returncode}') | |
print(proc.stdout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Desp: | |
def __get__(self, instance, owner): | |
# self: デスクリプタ | |
# instance: デスクリプタを属性として持つインスタンス | |
# owner: デスクリプタを属性として持つクラス | |
print(f'__get__self:{self}') | |
print(f'__get__instance:{instance}') | |
print(f'__get__owner:{owner}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Piyo: | |
pass | |
# FYI: https://docs.python.jp/3/reference/datamodel.html#object.__init_subclass__ | |
class Philosopher: | |
def __init_subclass__(cls, default_name, **kwargs): | |
# FYI: https://docs.python.jp/3/library/stdtypes.html#special-attributes | |
print(cls.hoge) | |
print(cls.__class__) | |
print(cls.__class__.__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FYI: https://docs.python.jp/3/reference/datamodel.html#metaclass-example | |
import collections | |
class OrderedClass(type): | |
@classmethod | |
def __prepare__(metacls, clsname, bases, **kwds): | |
# metacls: メタクラスに指定したtype | |
# classname: メタクラスを適用したクラスの名前 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTE: This section name isn't valid on pytest 2.x. | |
[tool:pytest] | |
addopts = --junit-xml=results/results.xml | |
[pycodestyle] | |
max-line-length = 100 | |
# FYI: http://www.pydocstyle.org/en/stable/error_codes.html | |
# Conflict: | |
# - D203 vs D211 (FYI: https://github.com/PyCQA/pydocstyle/issues/141) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Relative import problem | |
# FYI: https://stackoverflow.com/questions/4047227/doctest-and-relative-imports | |
# | |
find tests -name '*.py' | \ | |
# Filtering | |
grep -v -e 'init.py' -e 'config' | \ | |
# This step may be only valid for regular package. | |
# https://www.python.org/dev/peps/pep-0420/ | |
sed -e 's|.py||g' -e 's|/|.|g' | \ # This | |
xargs -L 1 -I {} python -c 'import {}, doctest; doctest.testmod({}, verbose=True)' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ..misc import Namespace | |
config1 = 'config1_dev' | |
config2 = Namespace({ | |
'config2_inner1_key1': {'config2_inner2_key1': 'config2_value1'}, | |
'config2_inner1_key2': Namespace(config2_inner2_key2='config2_value2') | |
}) | |
# dict is converted to Namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
from json import JSONDecodeError | |
from pprint import pformat | |
from requests import Session | |
from ._logging import get_logger |