Git Product home page Git Product logo

deta_py's Introduction

wemake-python-styleguide Linters Tests

DetaPy

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

Installation

You can install the Deta Base Python SDK using pip:

But not now, because it's not published yet.

pip install deta-py

Getting Started

Importing the SDK

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

Initialize Deta Base Client

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)

Usage

The DetaBase provides several methods for interacting with your Deta Base. Below are some common operations:

Putting Items

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}]

Getting an Item

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')

Deleting an Item

Delete an item from the base using its key:

db.delete('key1')

Inserting an Item

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')

Updating an Item

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')

Querying Items

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

Contributing

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.

License

This SDK is licensed under the MIT License. See the LICENSE file for details.

deta_py's People

Contributors

butvinm avatar

Watchers

 avatar

deta_py's Issues

Query response without paging

Related to docs Base API query always has paging section and paging.last only if paginated. However Drive API can not contains whole paging section if result not paginated.

So, for consistency and to prevent errors on possible API changing, we should handle queries responses like this:

items = data.get('items', [])
paging = data.get('paging', {})
return QueryResult(
    items=items,
    count=paging.get('total', len(items)),
    last=paging.get('last'),
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.