This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
337 lines (274 loc) · 10.2 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
Python やるときにいつもあって欲しい自分用モジュールです。
これは使いまわしたいのでリポジトリのビジネスロジック入れないでね。
NOTE: DbClient 内なら OK。
以下、できること。
# Dependencies
pipenv install python-dotenv mysql-connector-python
# MySQL への接続。
with utils.DbClient() as db_client:
records = db_client.sample_select()
# logger の取得。
logger = utils.get_my_logger(__name__)
# Slack メッセージの送信。
utils.send_slack_message(message)
"""
# Built-in modules.
import logging
import datetime
from decimal import Decimal
# Third-party modules.
import mysql.connector
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
import pytz
# User modules.
import consts
class DbClient:
"""DB アクセスを行うクラスです。 with 構文で使用可能です。
with utils.DbClient() as db_client:
records = db_client.sample_select()
"""
def __enter__(self):
mysql_connection_config = {
'host': consts.MYSQL_HOST,
'user': consts.MYSQL_USER,
'password': consts.MYSQL_PASSWORD,
'database': consts.MYSQL_DATABASE,
}
self.connection = mysql.connector.connect(**mysql_connection_config)
return self
def __exit__(self, exc_type, exc_value, traceback):
self.connection.close()
def sample_select(self) -> list:
"""サンプルです。
Returns:
list: Select 結果のリスト。
"""
select_sql = ' '.join([
'SELECT',
'id', # noqa: E131
'FROM sampletable',
'WHERE',
'id = %s',
'ORDER BY id DESC',
])
cursor = self.connection.cursor(dictionary=True)
cursor.execute(select_sql, (1,))
records = cursor.fetchall()
cursor.close()
return records
def sample_update(self):
"""サンプルです。
"""
update_sql = ' '.join([
'UPDATE sampletable',
'SET',
'foo = %s', # noqa: E131
'WHERE id = %s',
])
cursor = self.connection.cursor()
cursor.execute(update_sql, (1, 1))
cursor.close()
self.connection.commit()
def fetch_completed_tradings(self,
user: int,
with_stock: bool = False) -> list:
"""完了済みの trading レコードを取得します。
Args:
user (int): trading.user
with_stock (bool, optional): Defaults to False.
LEFT JOIN stock が欲しい場合が出ていたので追加した optional 引数です。
Returns:
list: tradings
"""
select_sql = ' '.join([
'SELECT *',
'FROM trading',
'LEFT JOIN stock ON trading.stock_id=stock.id' if with_stock else '',
'WHERE user_id=%s AND sold_at IS NOT NULL',
])
cursor = self.connection.cursor(dictionary=True)
cursor.execute(select_sql, (user,))
records = cursor.fetchall()
cursor.close()
return records
def fetch_completed_tradings_with_stock(self, user: int) -> list:
"""完了済みの trading レコードを LEFT JOIN stock で取得します。
Args:
user (int): trading.user
Returns:
list: tradings
"""
return self.fetch_completed_tradings(user=user, with_stock=True)
def fetch_newest_trading(self, stock_id: int) -> dict:
"""最新の trading レコードを取得します。
存在しなければ None を返します。
Args:
stock_id (int): stock.id
Returns:
dict: trading
"""
select_sql = ' '.join([
'SELECT *',
'FROM trading',
'WHERE stock_id=%s',
'ORDER BY created_at DESC',
'LIMIT 1',
])
cursor = self.connection.cursor(dictionary=True)
cursor.execute(select_sql, (stock_id,))
record = cursor.fetchone()
cursor.close()
return record
def fetch_stocks(self) -> list:
"""stocks を取得します。
Returns:
list: stocks
"""
select_sql = ' '.join([
'SELECT * FROM shuumulator.stock',
])
cursor = self.connection.cursor(dictionary=True)
cursor.execute(select_sql)
records = cursor.fetchall()
cursor.close()
return records
def create_stock_log(self, stock_id: int, price: Decimal) -> int:
"""stock_log を INSERT します。
Args:
stock_id ([type]): stock.id
price ([type]): stock_log.price
Returns:
int: created id
"""
insert_sql = ' '.join([
'INSERT INTO stock_log (stock_id, price, created_at)',
'VALUES (%s, %s, %s)',
])
cursor = self.connection.cursor(dictionary=True)
cursor.execute(
insert_sql,
(stock_id, price, datetime.datetime.now(tz=pytz.utc))
)
last_row_id = cursor.lastrowid
cursor.close()
self.connection.commit()
return last_row_id
def create_trading(self, stock_id: int, user_id: int,
price: Decimal) -> int:
"""trading を一件追加します。
Args:
stock_id (int): trading.stock
user_id (int): trading.user
price (Decimal): trading.buy
Returns:
int: created trading.id
"""
current_utc = datetime.datetime.now(tz=pytz.utc)
insert_sql = ' '.join([
'INSERT INTO trading',
'(stock_id, user_id, buy, bought_at, created_at)',
'VALUES',
'(%s, %s, %s, %s, %s)',
])
cursor = self.connection.cursor(dictionary=True)
cursor.execute(
insert_sql,
(stock_id, user_id, price, current_utc, current_utc)
)
last_row_id = cursor.lastrowid
cursor.close()
self.connection.commit()
return last_row_id
def update_trading(self, trading_id: int,
sell_price: Decimal) -> None:
"""trading.sell と trading.sold_at を更新します。
Args:
trading_id (int): trading.id
sell_price (Decimal): trading.sell
"""
update_sql = ' '.join([
'UPDATE trading',
'SET sell=%s, sold_at=%s',
'WHERE id=%s',
])
cursor = self.connection.cursor(dictionary=True)
cursor.execute(
update_sql,
(sell_price, datetime.datetime.now(tz=pytz.utc), trading_id)
)
cursor.close()
self.connection.commit()
def get_placeholder(count: int) -> str:
"""count ぶんのプレースホルダ文字列を作ります。
%s, %s, %s, %s, ...
Args:
count (int): 欲しい %s の数。
Returns:
str: %s, %s, %s, %s, ...
"""
return ','.join(('%s' for i in range(count)))
def get_my_logger(logger_name: str) -> logging.Logger:
"""モジュール用のロガーを作成します。
logger = get_my_logger(__name__)
Args:
logger_name (str): getLogger にわたす名前。 __name__ を想定しています。
Returns:
logging.Logger: モジュール用のロガー。
"""
"""
メインの処理とは別に関係ない。
Returns:
Logger -- モジュール用のロガー。
"""
# ルートロガーを作成します。ロガーはモジュールごとに分けるもの。
logger = logging.getLogger(logger_name)
# ルートロガーのログレベルは DEBUG。
logger.setLevel(logging.DEBUG)
# コンソールへ出力するハンドラを作成。
handler = logging.StreamHandler()
# ハンドラもログレベルを持ちます。
handler.setLevel(logging.DEBUG)
# ログフォーマットをハンドラに設定します。
formatter = logging.Formatter(
# NOTE: 改行は逆に見づらいので E501 を無視します。
'%(asctime)s - %(levelname)s - %(filename)s - %(name)s - %(funcName)s - %(message)s') # noqa: E501
handler.setFormatter(formatter)
# ハンドラをロガーへセットします。
logger.addHandler(handler)
# 親ロガーへの(伝播をオフにします。
logger.propagate = False
return logger
def send_slack_message(message: str) -> None:
"""slack_sdk を用いたメッセージ送信を行います。
Document: https://github.com/slackapi/python-slack-sdk/blob/main/tutorial/01-creating-the-slack-app.md # noqa: E501
Args:
message (str): 送信したいメッセージ。
"""
slack_client = WebClient(token=consts.SLACK_BOT_TOKEN)
try:
# NOTE: unfurl_links は時折鬱陶しいと思っている「リンクの展開機能」です。不要です。 False.
response = slack_client.chat_postMessage(
channel=consts.SLACK_MESSAGE_CHANNEL,
text=message,
unfurl_links=False)
# NOTE: Slack api のドキュメントにあるコードですが、正常に終了しても false になる場合があるので注意。
# リンクの含まれるメッセージを送信すると、返却値が勝手に変更されるため一致しません。
# - リンクの前後に <, > がつく
# - & -> & エスケープが起こる
assert response['message']['text'] == message
except SlackApiError as e:
assert e.response['ok'] is False
# str like 'invalid_auth', 'channel_not_found'
assert e.response['error']
logger.error(f'Got an error: {e.response["error"]}')
# utils モジュール用のロガーを作成します。
logger = get_my_logger(__name__)
if __name__ == '__main__':
logger.debug('でばーぐ')
logger.info('いんーふぉ')
logger.warning('うぉーにん')
logger.error('えろあ')
logger.fatal('ふぇーたる(critical と同じっぽい)')
logger.critical('くりてぃこぉ')