sqlite-dic 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sqlite_dic/__init__.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# SQLite辞書DB [sqlite-dic]
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
import json
|
|
5
|
+
import sqlite3
|
|
6
|
+
import threading
|
|
7
|
+
from collections.abc import MutableMapping
|
|
8
|
+
|
|
9
|
+
# テーブル定義式
|
|
10
|
+
SCHEMA = "CREATE TABLE IF NOT EXISTS main (key TEXT PRIMARY KEY, value TEXT)"
|
|
11
|
+
|
|
12
|
+
# SQLite辞書DB [sqlite-dic]
|
|
13
|
+
class SQLite_Dic(MutableMapping): # 辞書ライクオブジェクトを継承することで辞書的インターフェースを自動的に実現
|
|
14
|
+
# 初期化処理
|
|
15
|
+
def __init__(self, path):
|
|
16
|
+
self._path = path
|
|
17
|
+
self._local = threading.local() # スレッドごとに接続を保持するためのストレージ
|
|
18
|
+
self.exe_and_com(SCHEMA) # 初期テーブル作成
|
|
19
|
+
# execute と commit を実行
|
|
20
|
+
def exe_and_com(self, *args):
|
|
21
|
+
cur = self.conn.execute(*args)
|
|
22
|
+
self.conn.commit()
|
|
23
|
+
return cur
|
|
24
|
+
# DBへの接続obj取得
|
|
25
|
+
@property
|
|
26
|
+
def conn(self):
|
|
27
|
+
if getattr(self._local, "conn", None) is None: # 存在しない場合新規作成
|
|
28
|
+
conn = sqlite3.connect(self._path)
|
|
29
|
+
conn.execute("PRAGMA journal_mode=WAL") # WALモード (高速)
|
|
30
|
+
conn.execute("PRAGMA busy_timeout=30000") # 30秒タイムアウト
|
|
31
|
+
self._local.conn = conn
|
|
32
|
+
return self._local.conn
|
|
33
|
+
# MutableMapping の必須実装メソッド5つ
|
|
34
|
+
def __getitem__(self, key):
|
|
35
|
+
row = self.conn.execute("SELECT value FROM main WHERE key=?", [key]).fetchone()
|
|
36
|
+
if row is None: raise KeyError(key)
|
|
37
|
+
return json.loads(row[0])
|
|
38
|
+
def __setitem__(self, key, value):
|
|
39
|
+
encoded = json.dumps(value, ensure_ascii=False) # JSON化
|
|
40
|
+
self.exe_and_com("INSERT OR REPLACE INTO main (key, value) VALUES (?, ?)", [key, encoded]) # execute と commit を実行
|
|
41
|
+
def __delitem__(self, key):
|
|
42
|
+
cur = self.exe_and_com("DELETE FROM main WHERE key=?", [key]) # execute と commit を実行
|
|
43
|
+
if cur.rowcount == 0: raise KeyError(key)
|
|
44
|
+
def __iter__(self):
|
|
45
|
+
rows = self.conn.execute("SELECT key FROM main").fetchall()
|
|
46
|
+
return (r[0] for r in rows)
|
|
47
|
+
def __len__(self):
|
|
48
|
+
return self.conn.execute("SELECT COUNT(*) FROM main").fetchone()[0]
|
|
49
|
+
# 第一・第二文字列化
|
|
50
|
+
def __str__(self): return f"<sqlite-dic len={len(self)}>"
|
|
51
|
+
def __repr__(self): return str(self)
|
|
52
|
+
# DB接続を閉じる (複数回呼んでも安全; atexit登録はしなくても安全と考えられるので今回は略)
|
|
53
|
+
def __del__(self):
|
|
54
|
+
conn = getattr(self._local, "conn", None)
|
|
55
|
+
if conn is None: return None
|
|
56
|
+
conn.close()
|
|
57
|
+
self._local.conn = None
|
|
58
|
+
|
|
59
|
+
# モジュールオブジェクトとクラスオブジェクトを同一視
|
|
60
|
+
sys.modules[__name__] = SQLite_Dic
|
sqlite_dic/test.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# SQLite辞書DB [sqlite-dic]
|
|
2
|
+
# 【動作確認 / 使用例】
|
|
3
|
+
|
|
4
|
+
import ezpip
|
|
5
|
+
# SQLite辞書DB [sqlite-dic]
|
|
6
|
+
sqlite_dic = ezpip.load_develop("sqlite_dic", "../", develop_flag = True)
|
|
7
|
+
|
|
8
|
+
db = sqlite_dic("./sample.db") # DB接続
|
|
9
|
+
|
|
10
|
+
db["hoge"] = 33 # DBへの値の追加/更新
|
|
11
|
+
db["fuga"] = {"k": "value"} # 値はJSON化可能な任意の値が使える
|
|
12
|
+
print(db["hoge"]) # 値の取得
|
|
13
|
+
del db["hoge"] # エントリの削除
|
|
14
|
+
print(len(db)) # -> 1
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sqlite-dic
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: A lightweight SQLite-backed database that can be used like a Python dictionary.
|
|
5
|
+
Home-page: https://github.co.jp/
|
|
6
|
+
Author: bib_inf
|
|
7
|
+
Author-email: contact.bibinf@gmail.com
|
|
8
|
+
License: CC0 v1.0
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
11
|
+
Classifier: License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
Requires-Dist: ezpip
|
|
14
|
+
|
|
15
|
+
English description follows Japanese.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
Pythonの辞書のように使える、SQLiteバックエンドの軽量キーバリューストアです。
|
|
20
|
+
|
|
21
|
+
## 概要
|
|
22
|
+
- `sqlite-dic` は、SQLiteデータベースをPythonの辞書と同じ感覚で操作できるライブラリです。値にはJSONシリアライズ可能な任意の型(文字列、数値、真偽値、リスト、辞書、`None`)を格納できます。
|
|
23
|
+
- 内部ではSQLiteのWAL(Write-Ahead Logging)モードを使用しており、読み書きの並列アクセスが可能です。すべての操作はスレッドセーフです。
|
|
24
|
+
- AIでのコーディングにおいてこのツールを使用するときは、そのままこのドキュメントをプロンプトに含めることで、AIにこのDBを使ってコーディングしてもらうことが可能です。
|
|
25
|
+
|
|
26
|
+
## インストール
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install sqlite-dic
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 使い方
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import sqlite_dic
|
|
36
|
+
|
|
37
|
+
# データベースを開く(存在しなければ自動作成)
|
|
38
|
+
db = sqlite_dic("./sample.db")
|
|
39
|
+
|
|
40
|
+
# 値の追加・更新 — 辞書と同じ書き方
|
|
41
|
+
db["count"] = 33
|
|
42
|
+
db["config"] = {"theme": "dark", "lang": "ja"}
|
|
43
|
+
|
|
44
|
+
# 値の取得
|
|
45
|
+
print(db["count"]) # 33
|
|
46
|
+
print(db["config"]) # {'theme': 'dark', 'lang': 'ja'}
|
|
47
|
+
|
|
48
|
+
# エントリの削除
|
|
49
|
+
del db["count"]
|
|
50
|
+
|
|
51
|
+
# 格納数の確認
|
|
52
|
+
print(len(db)) # 1
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 制約
|
|
56
|
+
- **キー** は文字列のみ使用可能です。
|
|
57
|
+
- **値** はJSONシリアライズ可能な型(`dict`、`list`、`str`、`int`、`float`、`bool`、`None`)に限ります。
|
|
58
|
+
- 同時書き込みはSQLiteのファイルロックにより直列化されます(busy timeout: 30秒)。
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
A lightweight key-value store backed by SQLite, with a Python `dict`-like interface.
|
|
63
|
+
|
|
64
|
+
## Overview
|
|
65
|
+
- `sqlite-dic` lets you use an SQLite database as if it were a regular Python dictionary. Values can be any JSON-serializable type, so you can store strings, numbers, booleans, lists, dicts, and `None` without any extra setup.
|
|
66
|
+
- Under the hood, SQLite's WAL (Write-Ahead Logging) mode provides concurrent read/write access, and all operations are thread-safe.
|
|
67
|
+
|
|
68
|
+
## Installation
|
|
69
|
+
```bash
|
|
70
|
+
pip install sqlite-dic
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Quick Start
|
|
74
|
+
```python
|
|
75
|
+
import sqlite_dic
|
|
76
|
+
|
|
77
|
+
# Open (or create) a database
|
|
78
|
+
db = sqlite_dic("./sample.db")
|
|
79
|
+
|
|
80
|
+
# Set values — just like a dict
|
|
81
|
+
db["count"] = 33
|
|
82
|
+
db["config"] = {"theme": "dark", "lang": "en"}
|
|
83
|
+
|
|
84
|
+
# Get values
|
|
85
|
+
print(db["count"]) # 33
|
|
86
|
+
print(db["config"]) # {'theme': 'dark', 'lang': 'en'}
|
|
87
|
+
|
|
88
|
+
# Delete an entry
|
|
89
|
+
del db["count"]
|
|
90
|
+
|
|
91
|
+
# Check the number of stored entries
|
|
92
|
+
print(len(db)) # 1
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Constraints
|
|
96
|
+
- **Keys** must be strings.
|
|
97
|
+
- **Values** must be JSON-serializable (`dict`, `list`, `str`, `int`, `float`, `bool`, or `None`).
|
|
98
|
+
- Concurrent writes are serialized by SQLite's file lock (busy timeout: 30 seconds).
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
sqlite_dic/__init__.py,sha256=B_S7ao2O_S_qz7WBCV-bpmYrTbwsLG3HewXNng2E6HA,2459
|
|
2
|
+
sqlite_dic/test.py,sha256=1CE3rPBScscm8723kEvFD1_1PYXkuDN6wCvCwaAc0gQ,447
|
|
3
|
+
sqlite_dic-0.0.0.dist-info/METADATA,sha256=cj0s8cIv0VjP6JMjW5ZE4Oy7cJE6iIc7slhtRgOCAqY,3414
|
|
4
|
+
sqlite_dic-0.0.0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
|
5
|
+
sqlite_dic-0.0.0.dist-info/top_level.txt,sha256=ZMFbQgNCUHmGdJ4J9c8dPYPRMlk4O7NQH6g7rRu9gXs,11
|
|
6
|
+
sqlite_dic-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sqlite_dic
|