Cache-aware session wrapper with disk caching modes and response validation
Project description
smart_session
A lightweight wrapper for sync HTTP sessions that adds transparent disk caching with simple r / w / rr mode control and response condition validation.
Install
pip install smart_session
Standard vs Smart
Standard requests - fetches every time:
import requests
session = requests.Session()
resp = session.get("https://example.com/data.json")
print(resp.text) # hits network every single run
smart_session - fetches once, reads from disk after:
import requests
from smart_session import session_wrapper
session = session_wrapper(requests.Session)
resp = session.get(
"https://example.com/data.json",
mode="r",
filepath="cache/data.json",
)
print(resp.text) # network on first run, disk on every run after
That is the only change. Everything else (headers, cookies, params, data, json) works the same as before.
Modes
| Mode | Behavior |
|---|---|
r |
Return cache if it exists, otherwise fetch and store |
w |
Always fetch from network and overwrite the cache |
rr |
Read from cache only - raises error if file is missing |
rb / wb / rrb |
Same as above for binary responses (images, PDFs) |
Condition Validation
Conditions are callables evaluated against the live response before writing to disk. If any condition fails, the file is not written and a ValueError is raised.
Without smart_session - bad response can be saved silently:
resp = session.post(url, data=payload)
with open("result.html", "w", encoding="utf-8") as f:
f.write(resp.text)
With smart_session - only valid responses are cached:
resp = session.post(
url,
data=payload,
mode="w",
filepath="cache/result.html",
conditions=[
lambda r: r.status_code == 200,
lambda r: len(r.content) > 500,
lambda r: "error" not in r.text.lower(),
lambda r: "captcha" not in r.text.lower(),
],
)
Full Parameter Reference
| Parameter | Type | Description |
|---|---|---|
mode |
str |
One of r, w, rr, rb, wb, rrb |
filepath |
str |
Cache file path. Required when mode is set |
conditions |
list[callable] |
Response validators checked before writing to disk |
Public API
from smart_session import session_wrapper, info
session_wrapperis the main wrapper class export.info()prints module-level documentation.smart_sessionis still available as a compatibility alias.
Contact Developer
Dharmik Vadher
dharmik.vadher@xbyte.io
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file smart_session-1.1-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: smart_session-1.1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 93.4 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a30b3bec378b24dac0fe7938686ea9084bd84ab3310984515fd9d838416fc7f6
|
|
| MD5 |
c15c356043dfdcb63adddf86419b75d8
|
|
| BLAKE2b-256 |
4fadabea5cd664034d753397f14aa63cb6555d29d873bcbf5b5c6abcae7ad1e1
|
File details
Details for the file smart_session-1.1-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: smart_session-1.1-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 88.1 kB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee4dcb20266cb7aa54dd4dcf2dd458876de18172fbbf5e24a018e68800c13b6f
|
|
| MD5 |
2ba1f447725a6c03bbb1e1d3277efbec
|
|
| BLAKE2b-256 |
ac0db2f591bd69caf490580f1dc9f9878d5fa9e75eb65ff8b5c4c59ae0be0467
|