Like React Query but for Python
Project description
Python Query
Python library heavily inspired by react-query.
Getting Started
Here is a compilation of some actions that are achievable with this library.
import asyncio
import python_query
async def function() -> None:
await asyncio.sleep(1)
return 2
async def main():
query_cache = python_query.QueryCache()
query_cache["query1"] = lambda: 1
query_cache["query2"] = function
assert await query_cache["query1"].fetch_async() == 1
assert await query_cache["query2"].fetch_async() == 2
query_cache["query1"] = lambda: 3
assert await query_cache["query1"].fetch_async() == 3
query_cache["parent", "child1", {"page": 1}] = lambda: 4
query_cache["parent", "child1", {
"page": 1, "per_page": 10}] = lambda: 5
queries = query_cache.get_queries_not_exact("parent")
queries2 = query_cache.get_queries_not_exact(["parent", "child1"])
queries3 = query_cache.get_queries_not_exact(
["parent", "child1", {"page": 1}])
assert len(queries) == 2
assert len(queries2) == 2
assert len(queries3) == 2
asyncio.run(main())
Decorators
The library also provides decorators to easily create queries.
import asyncio
import python_query
query_cache = python_query.QueryCache()
# Static keys
@query_cache.cache(["key", "1"])
async def function() -> None:
await asyncio.sleep(1)
return 2
# Generate keys based on the arguments
@query_cache.cache(lambda number: ["key", "1", number])
async def function2(number : int) -> None:
await asyncio.sleep(1)
return number
async def main():
# Only added to cache when called first time
assert query_cache.get_query(["key", "1"]) is None
assert await function() == 2
assert query_cache.get_query(["key", "1"]) is not None
assert await query_cache.get_query(["key", "1"]).fetch_async() == 2
assert await function() == 2
# Only added to cache when called first time
assert query_cache.get_query(["key", "1", 3]) is None
assert await function2(3) == 3
assert query_cache.get_query(["key", "1", 2]) is None
assert query_cache.get_query(["key", "1", 3]) is not None
assert await query_cache.get_query(["key", "1", 3]).fetch_async() == 3
assert await function2(3) == 3
asyncio.run(main())
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 Distribution
python-query-0.0.1b2.tar.gz
(43.3 kB
view details)
Built Distribution
File details
Details for the file python-query-0.0.1b2.tar.gz
.
File metadata
- Download URL: python-query-0.0.1b2.tar.gz
- Upload date:
- Size: 43.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 492f8f2f1945d439419f71c82e08f041d0b1016a7f0bdbc5cb4fa9e0b5617a6f |
|
MD5 | 85921455b3ab9ab98a8444f2887600a5 |
|
BLAKE2b-256 | 48cf1501af9eb2bac9826b565df5decc1916cca7b32ddb27261be2eb3bec2b95 |
File details
Details for the file python_query-0.0.1b2-py3-none-any.whl
.
File metadata
- Download URL: python_query-0.0.1b2-py3-none-any.whl
- Upload date:
- Size: 30.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 003c9055995b2b4992564a9e1a915201e4f8d773109a5706a7a36b368146f639 |
|
MD5 | 300be75f6badeab2ef009ac4c03ccebe |
|
BLAKE2b-256 | e3e0718de985a7d47d6eadb26f176597990bf40a950d47e39612611558bff3a1 |