Python のスニペット集
このページは、Python (3) のスニペットなどをまとめる予定のページです。
目次
注意
- コードのライセンスは CC0 (クレジット表示不要、改変可、商用可) です。
スニペット
文字列のフォーマット
a = 1
b = 2
s = f'a = {a}, b = {b}'
sleep
# import time
time.sleep(1)
ファイル
タイムスタンプ変更
# import time
# import os
t = time.mktime((2019, 1, 1, 0, 0, 0, 0, 0, 0)) # 2019-01-01 00:00:00
os.utime('file.txt', (t, t))
パス
実行パスからの相対パス解決
# import pathlib
p = str(pathlib.Path(__file__).parent.joinpath('path/to/file.txt')) # /example/test/main.py で動作している場合 /example/test/path/to/file.txt
URL
パス結合
# from urllib.parse import urljoin
p = urljoin('https://exmaple.com/api/', 'path/to/file.txt')
JSON
エンコード
print(json.dumps(data))
デコード
# import json
data = json.loads('[1,2,3]')
INI
読み込み
[section1]
value1=1
# import configparser
config = configparser.ConfigParser()
config.read('settings.ini')
value1 = config['section1']['value1']
書き込み
# import configparser
config = configparser.ConfigParser()
config['a'] = {}
config['a']['b'] = "c"
with open('settings.ini', 'w') as f:
config.write(f)
HTTP
GET
# import urllib.request
url = 'https://example.com/'
headers = {
'Accept-Language': 'ja',
}
data = {
'foo': 1
}
req = urllib.request.Request(f'{url}?{urllib.parse.urlencode(data)}', None, headers)
with urllib.request.urlopen(req) as res:
body = res.read().decode('utf-8')
print(body)
POST
# import urllib.request
url = 'https://example.com/'
headers = {
'Accept-Language': 'ja',
}
data = {
'foo': 1
}
req = urllib.request.Request(url, urllib.parse.urlencode(data).encode(), headers)
with urllib.request.urlopen(req) as res:
body = res.read().decode('utf-8')
print(body)
POST (JSON)
# import urllib.request
# import json
url = 'https://example.com/'
headers = {
'Content-Type': 'application/json',
}
data = {
'foo': 1
}
req = urllib.request.Request(url, json.dumps(data).encode(), headers)
with urllib.request.urlopen(req) as res:
body = res.read().decode('utf-8')
resData = json.loads(body)
print(resData)
TCP
データの受信 (クライアント側)
# import socket
host = '127.0.0.1'
port = 4000
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((host, port))
res = sock.recv(1024)
print(repr(res))
ログ
ロガーの設定 (コンソール)
# import logging
# import logging.handlers
loglevel = logging.DEBUG # ログレベル
logger = logging.getLogger(__name__) # ロガー生成
logger.setLevel(loglevel) # ログレベル設定
handler = logging.StreamHandler() # 出力ハンドラ (コンソール)
handler.setLevel(loglevel) # ログレベル設定
handler.setFormatter(logging.Formatter(
'%(asctime)s %(name)s [%(levelname)s] %(message)s')) # 出力フォーマット設定
logger.addHandler(handler) # 出力ハンドラのセット
ロガーの設定 (ファイル)
# import logging
# import logging.handlers
loglevel = logging.DEBUG # ログレベル
logger = logging.getLogger(__name__) # ロガー生成
logger.setLevel(loglevel) # ログレベル設定
handler = logging.FileHandler('log.log') # 出力ハンドラ (ファイル)
handler.setLevel(loglevel) # ログレベル設定
handler.setFormatter(logging.Formatter(
'%(asctime)s %(name)s [%(levelname)s] %(message)s')) # 出力フォーマット設定
logger.addHandler(handler) # 出力ハンドラのセット
ロガーの設定 (ファイル + ローテート)
# import logging
# import logging.handlers
loglevel = logging.DEBUG # ログレベル
logger = logging.getLogger(__name__) # ロガー生成
logger.setLevel(loglevel) # ログレベル設定
handler = logging.handlers.TimedRotatingFileHandler('log.log') # 出力ハンドラ (ファイル)
handler.suffix = "%Y%m%d" # ログの後ろにセットする日付フォーマット
handler.setLevel(loglevel) # ログレベル設定
handler.setFormatter(logging.Formatter(
'%(asctime)s %(name)s [%(levelname)s] %(message)s')) # 出力フォーマット設定
logger.addHandler(handler) # 出力ハンドラのセット
ロガーの設定 (設定ファイルから設定)
{
"version": 1,
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "custom"
},
"file": {
"class": "logging.handlers.TimedRotatingFileHandler",
"level": "DEBUG",
"filename": "log.log",
"when": "MIDNIGHT",
"formatter": "custom"
}
},
"formatters": {
"custom": {
"format": "%(asctime)s %(name)s [%(levelname)s] %(message)s"
}
},
"root": {
"handlers": ["console", "file"],
"level": "DEBUG"
}
}
# import logging
# import logging.config
# import json
with open('logconfig.json') as f:
config = json.load(f)
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
ロガーの設定 (設定ファイルから設定 + カスタムハンドラ)
{
"version": 1,
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "custom"
},
"file": {
"class": "logging.handlers.CustomTimedRotatingFileHandler",
"level": "DEBUG",
"filename": "log.log",
"when": "MIDNIGHT",
"formatter": "custom"
}
},
"formatters": {
"custom": {
"format": "%(asctime)s %(name)s [%(levelname)s] %(message)s"
}
},
"root": {
"handlers": ["console", "file"],
"level": "DEBUG"
}
}
# import logging
# import logging.config
# import re
# import json
class CustomTimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
"""独自のハンドラクラスです。ファイル形式を log.log.YYYY-MM-DD などから log.YYYYMMDD.log に変更します。
"""
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None):
logging.handlers.TimedRotatingFileHandler.__init__(self, filename, when, interval,
backupCount, encoding, delay, utc, atTime)
if self.when == 'S':
self.suffix = "%Y%m%d%H%M%S"
elif self.when == 'M':
self.suffix = "%Y%m%d%H%M"
elif self.when == 'H':
self.suffix = "%Y%m%d%H"
elif self.when == 'D' or self.when == 'MIDNIGHT':
self.suffix = "%Y%m%d"
elif self.when.startswith('W'):
self.suffix = "%Y%m%d"
def namer(default_name):
return re.sub(r'log\.([0-9]+)', r'\1.log', default_name)
self.namer = namer
logging.handlers.CustomTimedRotatingFileHandler = CustomTimedRotatingFileHandler
with open('logconfig.json') as f:
config = json.load(f)
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
ロガーの使用
# logger は上記で作成したロガーです
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
try:
raise Exception("exception")
except Exception as e:
logger.exception(e)