It is still in development
Yet another Deta Space SDK for Python.
But this one is:
- Fully typed
- Well tested
- Follow pythonic style and conventions
- Pass strongest linter
- Probably actively maintained
You can install the Deta Base Python SDK using pip:
But not now, because it's not published yet.
pip install deta-py
To use the Deta Base SDK in your Python application, you need to import it as follows:
from deta_py.deta_base.base import DetaBase
# or async version
from deta_py.deta_base.async_base import AsyncDetaBase
To get started, you need to initialize a Deta Base client by providing your Data Key and the name of the base you want to work with. You can obtain the Data Key from your Deta project or collection settings.
data_key = 'your_data_key_here'
base_name = 'your_base_name_here'
db = DetaBase(data_key, base_name)
The DetaBase provides several methods for interacting with your Deta Base. Below are some common operations:
You can use the put
method to add or update items in your Deta Base. It supports batch operations for efficient processing.
items = [
{'value': 1},
{'value': 2}
]
# Put the items into the base
processed_items = db.put(*items)
# processed_items == [{'key': '012345678910', 'value': 1}, {'key': '123456789101', 'value': 2}]
Retrieve an item from the base using its key:
item = db.get('key1')
if item is not None:
print('Retrieved item:', item)
else:
print('Item not found')
Delete an item from the base using its key:
db.delete('key1')
Insert an item into the base. If an item with the same key already exists, it will not be inserted.
item = {'key3': 'value3', 'value': 1}
# Insert the item into the base
inserted_item = db.insert(item)
if inserted_item is not None:
print('Inserted item:', inserted_item)
else:
print('Item with the same key already exists')
Update an item in the base using various operations like setting fields, incrementing values, appending to lists, and deleting fields:
key = 'key1'
# Create an update request
operations = ItemUpdate()
operations.set(name='John')
operations.increment(age=1)
operations.append(friends=['Jane'])
operations.delete('hobbies')
# Update the item with the specified key
success = db.update(key, operations)
if success:
print('Item updated successfully')
else:
print('Item not found')
You can query items in the base based on specific criteria:
# Define a query (example: retrieve items where 'age' is greater than 18)
query = [
{'age?gt': 18, 'age?lt': 20}, # age > 18 and age < 20
{'age?gt': 30, 'age?lt': 35}, # or age > 30 and age < 35
]
# Query the base
result = db.query(query)
# Retrieve items and handle pagination if necessary
items = result.items
while result.last:
result = db.query(query, last=result.last)
items += result.items
We welcome contributions to this SDK. Feel free to open issues or submit pull requests on GitHub.
Also see CONTRIBUTING.md for more information on how to contribute.
This SDK is licensed under the MIT License. See the LICENSE file for details.