Cosmos PL: Alternative Azure Cosmos library
What this library addresses that I don't like about MS library.
-
The MS library does excessive logging so (in my case), Azure Functions logs have to be turned off or are too costly https://github.com/Azure/azure-sdk-for-python/issues/12776
-
The MS library always converts the raw json into python lists and dicts immediately using built-in base
json. -
If running FastAPI then there is often no point to parsing cosmos results to python objects only for FastAPI to convert them right back to json.
-
Other libraries can parse json faster such as orjson for python lists and dicts or polars to DataFrames. In my experience, polars is better at parsing raw json then row oriented python objects anyway.
-
When you try to read an item that doesn't exist, the MS lib raises an Error so checking if an item exists requires try/except blocks. I'd refer it return None or an empty list.
Quick use example
Create a Cosmos DB instance
from cosmospl import Cosmos
cosdb = Cosmos('your_db_name', 'your_container_name', 'your_connection_string')
# if you have an environment variable called 'cosmos' then leave that arg blank.
df = await cosdb.query("select * from c", return_as='pl')
Usage overview and differences from MS
All functionality is inside the Cosmos class which is similar to the container client in the MS SDK.
For details please refer to the source.
To initialize the class pass a database name, container name, and (optionally) the connection string to Cosmos as ordered arguments. If the connection string is omitted it'll use the cosmos environment variable.
The methods in that class are:
query: execute a query against the container. Use the return_as parameter to specify pl for polars dataframe, dict for dict/list, resp for the httpx response. Unlike MS, it returns everything in one call, it isn't an Async generator.
query_stream: executes a query against the container. It returns an async generator of raw json. It is intended to be used in FastAPI streaming responses so it doesn't have to parse json or accumulate results before sending to end-user.
In the case of both query methods, Cosmos returns a nested json where the data is inside a Documents key. In order to avoid parsing this in its entirety while only returning data, it looks for Documents":[ and then only returns from there. Similarly at the end it truncates from ,"_count".
create: creates (not upserts) a record
upsert: upserts a record
delete: deletes a record
read: will read one record based on input id and partition_key
get_container_meta: returns meta data about the container
get_pk_ranges: returns the pk ranges of the container. Can be useful for doing cross partition query requests in chunks using the pk_id parameter
Warning
On the Cosmos python sdk page it says:
[WARNING] Using the asynchronous client for concurrent operations like shown in this sample will consume a lot of RUs very fast. We strongly recommend testing this out against the cosmos emulator first to verify your code works well and avoid incurring charges.
Future polars enhancements (maybe)
- (optionally) detect datetime columns and automatically convert to pl.Date or pl.Datetime
- save a df to cosmos directly (possibly) with metadata so columns can be restored to same types when loaded
Future General Enhancements (maybe)
- Add top level and database classes
Metadata
Release files for cosmospl 0.0.17
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cosmospl-0.0.17.tar.gz | 13.2 kB | Without provenance | Details for cosmospl-0.0.17.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cosmospl-0.0.17-py3-none-any.whl | Python 3 | none | any | Details for cosmospl-0.0.17-py3-none-any.whl |
Total release size: 24.0 kB
Release files / cosmospl-0.0.17.tar.gz
| Download URL | cosmospl-0.0.17.tar.gz |
|---|---|
| Size | 13.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8269fea0aaebeabce61404f47f22d38289974a45176050dc0f1229cad43cf63a
|
|
BLAKE2b-256 checksum How to use checksums |
8e255065e290feeca91045eeb1a3bb6c13398fd0794aec7c714884e2cb141bde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / cosmospl-0.0.17-py3-none-any.whl
| Download URL | cosmospl-0.0.17-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
46cb8b63c593a1917452c1c28261b2ad910415581b0841c0c3a3a9adcb766357
|
|
BLAKE2b-256 checksum How to use checksums |
f450ac6b2fc5183da758624c0bfdbc7c40eea9893b0e7444a6ef1a2108b0e840
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for cosmospl 0.0.17
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.