diff --git a/playwright/README.md b/playwright/README.md index b57e3c3..c46b732 100644 --- a/playwright/README.md +++ b/playwright/README.md @@ -41,92 +41,117 @@ if __name__ == "__main__": ```python # db.py + import sqlite3, json + class DB(sqlite3.Connection): def __init__(self, db_name=".db.sqlite"): super().__init__(db_name) with self: - self.execute(''' + self.execute( + """ CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value BLOB) - ''') + """ + ) def __setitem__(self, key, value): - if not isinstance(value, bytes): value = json.dumps(value) + if not isinstance(value, bytes): + value = json.dumps(value) with self: - cur = self.execute(''' + cur = self.execute( + """ INSERT OR REPLACE INTO kv_store (key, value) VALUES (?, ?) - ''', (key, value)) + """, + (key, value), + ) return {"modified_count": cur.rowcount} def __getitem__(self, key): with self: - result = self.execute(''' + result = self.execute( + """ SELECT value FROM kv_store WHERE key = ? - ''', (key,)).fetchone() + """, + (key,), + ).fetchone() if result: if isinstance(result[0], str): - try: return json.loads(result[0]) - except json.JSONDecodeError: pass + try: + return json.loads(result[0]) + except json.JSONDecodeError: + pass return result[0] def delete(self, key): with self: - cur = self.execute(''' + cur = self.execute( + """ DELETE FROM kv_store WHERE key = ? - ''', (key,)) + """, + (key,), + ) return {"deleted_count": cur.rowcount} - def keys(self, pattern='*'): - pattern = pattern.translate(str.maketrans({ - '\\': '\\\\', '%': '\\%', '_': '\\_', '*': '%', '?': '_' - })) + def keys(self, pattern="*"): + pattern = pattern.translate( + str.maketrans({"\\": "\\\\", "%": "\\%", "_": "\\_", "*": "%", "?": "_"}) + ) with self: - result = self.execute(''' + result = self.execute( + """ SELECT key FROM kv_store WHERE key LIKE ? ESCAPE '\\' - ''', (pattern,)).fetchall() + """, + (pattern,), + ).fetchall() return [row[0] for row in result] - def __repr__(self): return repr(self.keys()) + def __repr__(self): + return repr(self.keys()) def exists(self, key): with self: - cur = self.execute(''' + cur = self.execute( + """ SELECT 1 FROM kv_store WHERE key = ? LIMIT 1 - ''', (key,)) + """, + (key,), + ) return bool(cur.fetchone()) -if __name__ == '__main__': + +if __name__ == "__main__": db = DB() # 문자열 저장 및 조회 - db['hello'] = 'world' - print(db['hello']) # 출력: world (str 타입) - - # 숫자 저장 및 조회 - db['number'] = 42 - print(db['number']) # 출력: 42 (int 타입) - - # 바이너리 데이터 저장 및 조회 - db['binary'] = b'binary data' - print(db['binary']) # 출력: b'binary data' (bytes 타입) - - # 복잡한 객체 저장 - complex_obj = {'name': 'John', 'age': 30, 'city': 'New York'} - db['complex'] = complex_obj - loaded_obj = db['complex'] - print(loaded_obj) # 출력: {'name': 'John', 'age': 30, 'city': 'New York'} (dict 타입) - - db['test_key'] = 'test_value' - - print(db.exists('test_key')) # True - print(db.exists('non_existent_key')) # False + db["hello"] = "world" + print(db["hello"]) # 출력: world (str 타입) + # 숫자 저장 및 조회 + db["number"] = 42 + print(db["number"]) # 출력: 42 (int 타입) + + # 바이너리 데이터 저장 및 조회 + db["binary"] = b"binary data" + print(db["binary"]) # 출력: b'binary data' (bytes 타입) + + # 복잡한 객체 저장 + complex_obj = {"name": "John", "age": 30, "city": "New York"} + db["complex"] = complex_obj + loaded_obj = db["complex"] + print( + loaded_obj + ) # 출력: {'name': 'John', 'age': 30, 'city': 'New York'} (dict 타입) + + db["test_key"] = "test_value" + + print(db.exists("test_key")) # True + print(db.exists("non_existent_key")) # False ```