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