rquote 0.4.1__py3-none-any.whl → 0.4.2__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.
rquote/cache/persistent.py
CHANGED
|
@@ -232,7 +232,7 @@ class PersistentCache(Cache):
|
|
|
232
232
|
freq: str, fq: str) -> Optional[Tuple[str, str, pd.DataFrame]]:
|
|
233
233
|
"""从 duckdb 获取数据"""
|
|
234
234
|
result = self.conn.execute("""
|
|
235
|
-
SELECT name, data,
|
|
235
|
+
SELECT name, data, expire_at
|
|
236
236
|
FROM cache_data
|
|
237
237
|
WHERE cache_key = ?
|
|
238
238
|
""", [base_key]).fetchone()
|
|
@@ -240,7 +240,7 @@ class PersistentCache(Cache):
|
|
|
240
240
|
if not result:
|
|
241
241
|
return None
|
|
242
242
|
|
|
243
|
-
name, data_blob,
|
|
243
|
+
name, data_blob, expire_at = result
|
|
244
244
|
|
|
245
245
|
# 检查过期
|
|
246
246
|
if self.ttl and expire_at:
|
|
@@ -253,44 +253,44 @@ class PersistentCache(Cache):
|
|
|
253
253
|
import pickle
|
|
254
254
|
df = pickle.loads(data_blob)
|
|
255
255
|
|
|
256
|
-
#
|
|
257
|
-
|
|
258
|
-
|
|
256
|
+
# 确保索引是 DatetimeIndex
|
|
257
|
+
if not isinstance(df.index, pd.DatetimeIndex):
|
|
258
|
+
try:
|
|
259
|
+
df.index = pd.to_datetime(df.index)
|
|
260
|
+
except (ValueError, TypeError):
|
|
261
|
+
return None
|
|
262
|
+
|
|
263
|
+
if df.empty:
|
|
264
|
+
return None
|
|
259
265
|
|
|
260
|
-
#
|
|
266
|
+
# 直接从 DataFrame 索引获取实际的日期范围
|
|
267
|
+
cached_earliest = df.index.min()
|
|
268
|
+
cached_latest = df.index.max()
|
|
269
|
+
|
|
270
|
+
# 解析请求的日期范围
|
|
261
271
|
request_sdate = self._parse_date(sdate) if sdate else None
|
|
262
272
|
request_edate = self._parse_date(edate) if edate else None
|
|
263
273
|
|
|
264
|
-
#
|
|
265
|
-
|
|
274
|
+
# 检查是否有重叠:如果请求的日期范围与缓存数据有重叠,就返回过滤后的数据
|
|
275
|
+
# 注意:即使缓存中有部分数据,也应该返回(让上层决定是否需要扩展)
|
|
276
|
+
has_overlap = True
|
|
277
|
+
if request_edate and request_edate < cached_earliest:
|
|
266
278
|
# 请求的结束日期早于缓存的最早日期,无重叠
|
|
267
|
-
|
|
268
|
-
if request_sdate and
|
|
279
|
+
has_overlap = False
|
|
280
|
+
if request_sdate and request_sdate > cached_latest:
|
|
269
281
|
# 请求的开始日期晚于缓存的最晚日期,无重叠
|
|
270
|
-
|
|
282
|
+
has_overlap = False
|
|
271
283
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
actual_sdate = max(request_sdate, cached_earliest) if request_sdate and cached_earliest else (request_sdate or cached_earliest)
|
|
275
|
-
actual_edate = min(request_edate, cached_latest) if request_edate and cached_latest else (request_edate or cached_latest)
|
|
284
|
+
if not has_overlap:
|
|
285
|
+
return None
|
|
276
286
|
|
|
277
|
-
#
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
actual_sdate.strftime('%Y-%m-%d') if actual_sdate else None,
|
|
281
|
-
actual_edate.strftime('%Y-%m-%d') if actual_edate else None
|
|
282
|
-
)
|
|
287
|
+
# 按照请求的日期范围过滤数据(即使缓存中有更多数据,也只返回请求范围内的)
|
|
288
|
+
# 重要:必须按照 edate 截取,和从网络获取的行为一致
|
|
289
|
+
filtered_df = self._filter_dataframe_by_date(df, sdate, edate)
|
|
283
290
|
|
|
284
291
|
if filtered_df.empty:
|
|
285
292
|
return None
|
|
286
293
|
|
|
287
|
-
# 确保索引是 DatetimeIndex
|
|
288
|
-
if not isinstance(filtered_df.index, pd.DatetimeIndex):
|
|
289
|
-
try:
|
|
290
|
-
filtered_df.index = pd.to_datetime(filtered_df.index)
|
|
291
|
-
except (ValueError, TypeError):
|
|
292
|
-
pass # 如果转换失败,保持原样
|
|
293
|
-
|
|
294
294
|
return (symbol, name, filtered_df)
|
|
295
295
|
|
|
296
296
|
def _get_pickle(self, base_key: str, symbol: str, sdate: str, edate: str,
|
|
@@ -311,47 +311,45 @@ class PersistentCache(Cache):
|
|
|
311
311
|
|
|
312
312
|
df = cache_entry['data']
|
|
313
313
|
name = cache_entry.get('name', '')
|
|
314
|
-
earliest_date = cache_entry.get('earliest_date')
|
|
315
|
-
latest_date = cache_entry.get('latest_date')
|
|
316
314
|
|
|
317
|
-
#
|
|
318
|
-
|
|
319
|
-
|
|
315
|
+
# 确保索引是 DatetimeIndex
|
|
316
|
+
if not isinstance(df.index, pd.DatetimeIndex):
|
|
317
|
+
try:
|
|
318
|
+
df.index = pd.to_datetime(df.index)
|
|
319
|
+
except (ValueError, TypeError):
|
|
320
|
+
return None
|
|
321
|
+
|
|
322
|
+
if df.empty:
|
|
323
|
+
return None
|
|
324
|
+
|
|
325
|
+
# 直接从 DataFrame 索引获取实际的日期范围
|
|
326
|
+
cached_earliest = df.index.min()
|
|
327
|
+
cached_latest = df.index.max()
|
|
320
328
|
|
|
321
|
-
#
|
|
329
|
+
# 解析请求的日期范围
|
|
322
330
|
request_sdate = self._parse_date(sdate) if sdate else None
|
|
323
331
|
request_edate = self._parse_date(edate) if edate else None
|
|
324
332
|
|
|
325
|
-
#
|
|
326
|
-
|
|
333
|
+
# 检查是否有重叠:如果请求的日期范围与缓存数据有重叠,就返回过滤后的数据
|
|
334
|
+
# 注意:即使缓存中有部分数据,也应该返回(让上层决定是否需要扩展)
|
|
335
|
+
has_overlap = True
|
|
336
|
+
if request_edate and request_edate < cached_earliest:
|
|
327
337
|
# 请求的结束日期早于缓存的最早日期,无重叠
|
|
328
|
-
|
|
329
|
-
if request_sdate and
|
|
338
|
+
has_overlap = False
|
|
339
|
+
if request_sdate and request_sdate > cached_latest:
|
|
330
340
|
# 请求的开始日期晚于缓存的最晚日期,无重叠
|
|
331
|
-
|
|
341
|
+
has_overlap = False
|
|
332
342
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
actual_sdate = max(request_sdate, cached_earliest) if request_sdate and cached_earliest else (request_sdate or cached_earliest)
|
|
336
|
-
actual_edate = min(request_edate, cached_latest) if request_edate and cached_latest else (request_edate or cached_latest)
|
|
343
|
+
if not has_overlap:
|
|
344
|
+
return None
|
|
337
345
|
|
|
338
|
-
#
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
actual_sdate.strftime('%Y-%m-%d') if actual_sdate else None,
|
|
342
|
-
actual_edate.strftime('%Y-%m-%d') if actual_edate else None
|
|
343
|
-
)
|
|
346
|
+
# 按照请求的日期范围过滤数据(即使缓存中有更多数据,也只返回请求范围内的)
|
|
347
|
+
# 重要:必须按照 edate 截取,和从网络获取的行为一致
|
|
348
|
+
filtered_df = self._filter_dataframe_by_date(df, sdate, edate)
|
|
344
349
|
|
|
345
350
|
if filtered_df.empty:
|
|
346
351
|
return None
|
|
347
352
|
|
|
348
|
-
# 确保索引是 DatetimeIndex
|
|
349
|
-
if not isinstance(filtered_df.index, pd.DatetimeIndex):
|
|
350
|
-
try:
|
|
351
|
-
filtered_df.index = pd.to_datetime(filtered_df.index)
|
|
352
|
-
except (ValueError, TypeError):
|
|
353
|
-
pass # 如果转换失败,保持原样
|
|
354
|
-
|
|
355
353
|
return (symbol, name, filtered_df)
|
|
356
354
|
|
|
357
355
|
def put(self, key: str, value: Any, ttl: Optional[int] = None) -> None:
|
|
@@ -449,7 +447,10 @@ class PersistentCache(Cache):
|
|
|
449
447
|
def _put_duckdb(self, base_key: str, symbol: str, name: str, df: pd.DataFrame,
|
|
450
448
|
earliest_date: Optional[str], latest_date: Optional[str],
|
|
451
449
|
freq: str, fq: str, expire_at: Optional[pd.Timestamp]):
|
|
452
|
-
"""存储到 duckdb
|
|
450
|
+
"""存储到 duckdb
|
|
451
|
+
|
|
452
|
+
注意:earliest_date 和 latest_date 仅用于记录,实际查询时从 DataFrame 索引获取
|
|
453
|
+
"""
|
|
453
454
|
import pickle
|
|
454
455
|
data_blob = pickle.dumps(df)
|
|
455
456
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: rquote
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.2
|
|
4
4
|
Summary: Mostly day quotes of cn/hk/us/fund/future markets, side with quote list fetch
|
|
5
5
|
Requires-Python: >=3.9.0
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
@@ -18,7 +18,7 @@ Requires-Dist: duckdb>=0.9.0; extra == "persistent"
|
|
|
18
18
|
|
|
19
19
|
## 版本信息
|
|
20
20
|
|
|
21
|
-
当前版本:**0.4.
|
|
21
|
+
当前版本:**0.4.2**
|
|
22
22
|
|
|
23
23
|
## 主要特性
|
|
24
24
|
|
|
@@ -11,7 +11,7 @@ rquote/api/tick.py,sha256=nEcjuAjtBHUaD8KPRLg643piVa21PhKDQvkVWNwvvME,1431
|
|
|
11
11
|
rquote/cache/__init__.py,sha256=S393I5Wmp0QooaRka9n7bvDUdEbg3jUhm6u815T86rM,317
|
|
12
12
|
rquote/cache/base.py,sha256=orzG4Yo-6gzVG027j1-LTZPT718JohnCdLDnOLoLUQ4,515
|
|
13
13
|
rquote/cache/memory.py,sha256=7z4keb3q91pzI4ASQWy1MU8T5nbWLCEUjJcStv_3hvk,1933
|
|
14
|
-
rquote/cache/persistent.py,sha256=
|
|
14
|
+
rquote/cache/persistent.py,sha256=dgzH_rKVwUzuYTxsIWEHX2orixFesWd-sIUJ7TwbS7Y,19369
|
|
15
15
|
rquote/data_sources/__init__.py,sha256=WCe1aam4677jM5G6wP4a-dQFTeBzcU5PJqsKieAVMBo,215
|
|
16
16
|
rquote/data_sources/base.py,sha256=JuKsTMxH7y8yRxHg3JbLzQwXPr43rS4pnwc5625u2U4,443
|
|
17
17
|
rquote/data_sources/sina.py,sha256=T_3Dl0Mwlhx8CKRJll_UKobYecRWltGaIOiGkpHS43Q,3300
|
|
@@ -33,7 +33,7 @@ rquote/utils/helpers.py,sha256=V07n9BtRS8bEJH023Kca78-unk7iD3B9hn2UjELetYs,354
|
|
|
33
33
|
rquote/utils/http.py,sha256=X0Alhnu0CNqyQeOt6ivUWmh2XwrWxXd2lSpQOKDdnzw,3249
|
|
34
34
|
rquote/utils/logging.py,sha256=cbeRH4ODazn7iyQmGoEBT2lH5LX4Ca3zDfs_20J1T28,566
|
|
35
35
|
rquote/utils/web.py,sha256=I8_pcThW6VUvahuRHdtp32iZwr85hEt1hB6TgznMy_U,3854
|
|
36
|
-
rquote-0.4.
|
|
37
|
-
rquote-0.4.
|
|
38
|
-
rquote-0.4.
|
|
39
|
-
rquote-0.4.
|
|
36
|
+
rquote-0.4.2.dist-info/METADATA,sha256=wikF__yVw-XXJb85Hy6wQKzcOQQ6gKBW8r1VITfQh3Y,13259
|
|
37
|
+
rquote-0.4.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
38
|
+
rquote-0.4.2.dist-info/top_level.txt,sha256=CehAiaZx7Fo8HGoV2zd5GhILUW1jQEN8YS-cWMlrK9Y,7
|
|
39
|
+
rquote-0.4.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|