Skip to main content

A lightweight package for managing local finlab data cache with versioning and time-context features

Project description

finlab-guard

A lightweight package for managing a local finlab data cache with versioning and time-context features.

Python versions Windows Linux macOS License: MIT build lint coverage

Installation

pip install finlab-guard

Usage examples

Two short examples showing the most common flows.

1) Monkey-patch finlab.data.get (installing FinlabGuard)

This project can monkey-patch finlab.data.get so reads go through the guarded cache. Example:

import finlab
from finlab_guard import FinlabGuard

# Create a FinlabGuard instance and install the monkey-patch
guard = FinlabGuard()
guard.install_patch()

# Use finlab.data.get as normal; FinlabGuard will intercept and use cache
result = finlab.data.get('price:收盤價')

# When done, remove the monkey-patch
guard.remove_patch()

2) Set a time context and get historical data

FinlabGuard supports a time context so you can query data "as-of" a past time.

import finlab
from finlab_guard import FinlabGuard
from datetime import datetime, timedelta
guard = FinlabGuard()
guard.install_patch()

# Set time context to 7 days ago
query_time = datetime.now() - timedelta(days=7)
guard.set_time_context(query_time)

# Now call finlab.data.get normally; the guard will return historical data
result = finlab.data.get('price:收盤價')

# Clear the time context and remove the monkey-patch when done
guard.clear_time_context()
guard.remove_patch()

Performance

finlab-guard delivers significant performance improvements through its DuckDB + Polars architecture:

🚀 Cache Hit Performance: 30.6% faster than previous pandas-based implementation

Version Cache Hit Time Improvement
v0.1.0 (pandas.stack) 17.9 seconds baseline
v0.2.0 (DuckDB+Polars) 12.4 seconds -30.6%

Benchmark: etl:adj_close cache retrieval (4,533 × 2,645 DataFrame) - average of 10 runs

Key Optimizations

  • Eliminated pandas.stack() bottleneck: Replaced with vectorized Polars operations
  • Cell-level change tracking: Only stores actual differences, not full datasets
  • DuckDB storage engine: High-performance indexed storage with time-based reconstruction
  • Intelligent thresholding: Large row changes stored efficiently as JSON objects

These improvements make finlab-guard ideal for:

  • Large datasets with frequent updates
  • Historical data analysis and backtesting
  • Production environments requiring consistent performance

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

finlab_guard-0.2.0.tar.gz (175.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

finlab_guard-0.2.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file finlab_guard-0.2.0.tar.gz.

File metadata

  • Download URL: finlab_guard-0.2.0.tar.gz
  • Upload date:
  • Size: 175.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for finlab_guard-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3d74fed53c74d18f82a1ae0a86dac9f0ea5426644e27c8419891d34a42e6bc0f
MD5 3b1e1cefaa0d248d42ef7a58625dceaa
BLAKE2b-256 230b1e5a30b124d14508eb7be62a66ca8d1959364520ade0fc2d1e0ba9810e16

See more details on using hashes here.

File details

Details for the file finlab_guard-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: finlab_guard-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for finlab_guard-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e02e8a0b1627b32108a90457e51328a6bd968343f737a3783e5494387a3ca86a
MD5 8b8dd753ec89cf3b6b028ca97729b481
BLAKE2b-256 884015f7a7fb5e4f32b7471d9ff5f051680a40b9d2f9ba6e569ea4b9667b4d93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page