Update playwright/README.md
This commit is contained in:
@@ -42,72 +42,66 @@ 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:
|
||||||
cur = 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):
|
||||||
value = value if isinstance(value, bytes) else json.dumps(value)
|
value = value if isinstance(value, bytes) else json.dumps(value)
|
||||||
with self:
|
with self:
|
||||||
cur = self.execute("""
|
result = self.execute('''
|
||||||
INSERT OR REPLACE INTO kv_store
|
INSERT OR REPLACE INTO kv_store
|
||||||
(key, value) VALUES (?, ?)
|
(key, value) VALUES (?, ?)
|
||||||
""", (key, value))
|
''', (key, value)).rowcount
|
||||||
return {"modified_count": cur.rowcount}
|
return {"modified_count": result}
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
with self:
|
with self:
|
||||||
cur = self.execute("""
|
result = self.execute('''
|
||||||
SELECT value FROM kv_store
|
SELECT value FROM kv_store
|
||||||
WHERE key = ?
|
WHERE key = ?
|
||||||
""", (key,))
|
''', (key,)).fetchone()
|
||||||
result = cur.fetchone()
|
|
||||||
if result:
|
if result:
|
||||||
if isinstance(result[0], str):
|
if isinstance(value := result[0], str):
|
||||||
try:
|
try: return json.loads(value)
|
||||||
return json.loads(result[0])
|
except json.JSONDecodeError: pass
|
||||||
except json.JSONDecodeError:
|
return value
|
||||||
pass
|
|
||||||
return result[0]
|
|
||||||
|
|
||||||
def delete(self, key):
|
def delete(self, key):
|
||||||
with self:
|
with self:
|
||||||
cur = self.execute("""
|
result = self.execute('''
|
||||||
DELETE FROM kv_store
|
DELETE FROM kv_store
|
||||||
WHERE key = ?
|
WHERE key = ?
|
||||||
""", (key,))
|
''', (key,)).rowcount
|
||||||
return {"deleted_count": cur.rowcount}
|
return {"deleted_count": result}
|
||||||
|
|
||||||
def keys(self, pattern="*"):
|
def keys(self, pattern="*"):
|
||||||
pattern = pattern.translate(str.maketrans(
|
pattern = pattern.translate(str.maketrans(
|
||||||
{"\\": "\\\\", "%": "\\%", "_": "\\_", "*": "%", "?": "_"}))
|
{"\\": "\\\\", "%": "\\%", "_": "\\_", "*": "%", "?": "_"}))
|
||||||
with self:
|
with self:
|
||||||
cur = self.execute("""
|
result = self.execute('''
|
||||||
SELECT key FROM kv_store
|
SELECT key FROM kv_store
|
||||||
WHERE key LIKE ? ESCAPE '\\'
|
WHERE key LIKE ? ESCAPE '\\'
|
||||||
""", (pattern,))
|
''', (pattern,)).fetchall()
|
||||||
result = cur.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("""
|
result = self.execute('''
|
||||||
SELECT 1 FROM kv_store
|
SELECT 1 FROM kv_store
|
||||||
WHERE key = ?
|
WHERE key = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""", (key,))
|
''', (key,)).fetchone()
|
||||||
return bool(cur.fetchone())
|
return bool(result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user