BCBvcl4pyAPI 0.1.3__tar.gz → 0.1.5__tar.gz

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.
@@ -0,0 +1,137 @@
1
+ import os
2
+ import pickle
3
+ from typing import Generic, TypeVar, Optional
4
+ import pymysql
5
+ from dataclasses import is_dataclass
6
+
7
+ T = TypeVar("T")
8
+
9
+ class HyperDynamicArray(Generic[T]):
10
+ def __init__(
11
+ self,
12
+ mysql_host: str,
13
+ mysql_port: int,
14
+ mysql_user: str,
15
+ mysql_password: str,
16
+ mysql_db: str,
17
+ table_name: str = "dynamic_array_table",
18
+ pickle_file: Optional[str] = None,
19
+ table_type: str = "standard", # "standard" = InnoDB, "memory" = MEMORY
20
+ sample_element: Optional[T] = None # 用來檢測元素型別
21
+ ):
22
+ self.mysql_host = mysql_host
23
+ self.mysql_port = mysql_port
24
+ self.mysql_user = mysql_user
25
+ self.mysql_password = mysql_password
26
+ self.mysql_db = mysql_db
27
+ self.table_name = table_name
28
+ self._count = 0
29
+
30
+ # 初始化 table_type
31
+ self.table_type = table_type.lower()
32
+ if self.table_type not in ("standard", "memory"):
33
+ raise ValueError("table_type must be 'standard' or 'memory'")
34
+
35
+ # 自動切換 MEMORY / InnoDB
36
+ if self.table_type == "memory":
37
+ if sample_element is None:
38
+ # 無法判斷元素型別,安全起見改標準 table
39
+ print("⚠ Cannot determine element type, using standard table (InnoDB)")
40
+ self.table_type = "standard"
41
+ else:
42
+ if is_dataclass(sample_element) or not isinstance(sample_element, (int, float, str)):
43
+ # dataclass 或任意 Python 物件 → 改 InnoDB
44
+ print("⚠ Dataclass / Python object not supported in MEMORY Table, switching to standard table (InnoDB)")
45
+ self.table_type = "standard"
46
+ elif isinstance(sample_element, str):
47
+ # 字串超長也改 InnoDB
48
+ if len(sample_element.encode("utf-8")) > 65532:
49
+ print("⚠ String exceeds MEMORY Table maximum length, switching to standard table (InnoDB)")
50
+ self.table_type = "standard"
51
+
52
+ # 建立 pymysql 連線
53
+ self._conn = pymysql.connect(
54
+ host=mysql_host,
55
+ port=mysql_port,
56
+ user=mysql_user,
57
+ password=mysql_password,
58
+ database=mysql_db,
59
+ autocommit=True,
60
+ charset='utf8mb4',
61
+ cursorclass=pymysql.cursors.Cursor
62
+ )
63
+ self._cursor = self._conn.cursor()
64
+
65
+ # 建立 table
66
+ self._create_table()
67
+
68
+ # 如果有 pickle_file,轉入 MySQL
69
+ if pickle_file and os.path.exists(pickle_file):
70
+ self._load_pickle_to_mysql(pickle_file)
71
+
72
+ # 計算元素總數
73
+ self._cursor.execute(f"SELECT COUNT(*) FROM {self.table_name}")
74
+ self._count = self._cursor.fetchone()[0]
75
+
76
+ def _create_table(self):
77
+ engine = "InnoDB" if self.table_type == "standard" else "MEMORY"
78
+ # MEMORY Table 只能用 VARBINARY(65532)
79
+ column_def = "LONGBLOB" if engine == "InnoDB" else "VARBINARY(65532)"
80
+ create_sql = f"""
81
+ CREATE TABLE IF NOT EXISTS {self.table_name} (
82
+ id BIGINT PRIMARY KEY AUTO_INCREMENT,
83
+ data {column_def}
84
+ ) ENGINE={engine};
85
+ """
86
+ self._cursor.execute(create_sql)
87
+
88
+ def _load_pickle_to_mysql(self, pickle_file: str):
89
+ with open(pickle_file, "rb") as f:
90
+ try:
91
+ while True:
92
+ item = pickle.load(f)
93
+ self.Add(item)
94
+ except EOFError:
95
+ pass
96
+
97
+ def Add(self, value: T) -> int:
98
+ data_blob = pickle.dumps(value)
99
+ sql = f"INSERT INTO {self.table_name} (data) VALUES (%s)"
100
+ self._cursor.execute(sql, (data_blob,))
101
+ self._count += 1
102
+ return self._count - 1
103
+
104
+ def Count(self) -> int:
105
+ return self._count
106
+
107
+ def __getitem__(self, index: int) -> T:
108
+ if index < 0 or index >= self._count:
109
+ raise IndexError("HyperDynamicArray index out of range")
110
+ sql = f"SELECT data FROM {self.table_name} WHERE id = %s"
111
+ self._cursor.execute(sql, (index + 1,))
112
+ row = self._cursor.fetchone()
113
+ if row:
114
+ return pickle.loads(row[0])
115
+ else:
116
+ raise IndexError("HyperDynamicArray index not found")
117
+
118
+ def __setitem__(self, index: int, value: T):
119
+ if index < 0 or index >= self._count:
120
+ raise IndexError("HyperDynamicArray index out of range")
121
+ data_blob = pickle.dumps(value)
122
+ sql = f"UPDATE {self.table_name} SET data = %s WHERE id = %s"
123
+ self._cursor.execute(sql, (data_blob, index + 1))
124
+
125
+ def __iter__(self):
126
+ for i in range(self._count):
127
+ yield self[i]
128
+
129
+ def Clear(self):
130
+ """清空 table"""
131
+ sql = f"TRUNCATE TABLE {self.table_name}"
132
+ self._cursor.execute(sql)
133
+ self._count = 0
134
+
135
+ def Close(self):
136
+ self._cursor.close()
137
+ self._conn.close()
@@ -0,0 +1,4 @@
1
+ from .BCBvcl4pyAPI import TStringList, TList, DynamicArray
2
+ from .HyperDynamicArrayAPI import HyperDynamicArray
3
+
4
+ __all__ = ["TStringList", "TList", "DynamicArray", "HyperDynamicArray"]
@@ -1,11 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: BCBvcl4pyAPI
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary: BCBvcl4pyAPI Python package
5
5
  Author-email: James Lin <tylin123@ms27.hinet.net>
6
6
  Project-URL: Homepage, https://mis.gotech.biz
7
7
  Description-Content-Type: text/markdown
8
+ Requires-Dist: pymysql
8
9
 
9
10
  Version 0.1.3: 2025/09/19
10
- --------------
11
11
  Mainly fixed the issue where Packages could not be found after an update.
12
+ Version 0.1.5: 2025/09/20
13
+ Add the HyperDynamicArray class to provide large memory access functionality.
@@ -2,8 +2,10 @@ README.md
2
2
  pyproject.toml
3
3
  setup.cfg
4
4
  BCBvcl4pyAPI/BCBvcl4pyAPI.py
5
+ BCBvcl4pyAPI/HyperDynamicArrayAPI.py
5
6
  BCBvcl4pyAPI/__init__.py
6
7
  BCBvcl4pyAPI.egg-info/PKG-INFO
7
8
  BCBvcl4pyAPI.egg-info/SOURCES.txt
8
9
  BCBvcl4pyAPI.egg-info/dependency_links.txt
10
+ BCBvcl4pyAPI.egg-info/requires.txt
9
11
  BCBvcl4pyAPI.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ pymysql
@@ -1,11 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: BCBvcl4pyAPI
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary: BCBvcl4pyAPI Python package
5
5
  Author-email: James Lin <tylin123@ms27.hinet.net>
6
6
  Project-URL: Homepage, https://mis.gotech.biz
7
7
  Description-Content-Type: text/markdown
8
+ Requires-Dist: pymysql
8
9
 
9
10
  Version 0.1.3: 2025/09/19
10
- --------------
11
11
  Mainly fixed the issue where Packages could not be found after an update.
12
+ Version 0.1.5: 2025/09/20
13
+ Add the HyperDynamicArray class to provide large memory access functionality.
@@ -0,0 +1,4 @@
1
+ Version 0.1.3: 2025/09/19
2
+ Mainly fixed the issue where Packages could not be found after an update.
3
+ Version 0.1.5: 2025/09/20
4
+ Add the HyperDynamicArray class to provide large memory access functionality.
@@ -4,13 +4,16 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "BCBvcl4pyAPI"
7
- version = "0.1.3"
7
+ version = "0.1.5"
8
8
  description = "BCBvcl4pyAPI Python package"
9
9
  authors = [
10
10
  { name="James Lin", email="tylin123@ms27.hinet.net" }
11
11
  ]
12
12
  readme = "README.md"
13
13
  license = { file = "LICENSE" }
14
+ dependencies = [
15
+ "pymysql",
16
+ ]
14
17
 
15
18
  [project.urls]
16
19
  Homepage = "https://mis.gotech.biz"
@@ -1,3 +0,0 @@
1
- from .BCBvcl4pyAPI import TStringList, TList, DynamicArray
2
-
3
- __all__ = ["TStringList", "TList", "DynamicArray"]
@@ -1,3 +0,0 @@
1
- Version 0.1.3: 2025/09/19
2
- --------------
3
- Mainly fixed the issue where Packages could not be found after an update.
File without changes