GithubHelp home page GithubHelp logo

leechael / leancloud-better-storage-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nnnewb/leancloud-better-storage-python

0.0 2.0 0.0 70 KB

更优雅且pythonic的方式使用leancloud storage。

License: GNU Lesser General Public License v3.0

Python 100.00%

leancloud-better-storage-python's Introduction

leancloud-better-storage-python

PyPI version travis-ci codecov

better leancloud storage wrapper. Simple and lightweight.

Sorry for my bad grammar and spell :)

Installation

install by easy_install or pip.

pip install leancloud-better-storage

Quick start

Simple model declaration and query syntax.

Model declaration

from datetime import datetime
from leancloud_better_storage.storage.models import Model
from leancloud_better_storage.storage.fields import Field

class Product(Model):
    name = Field(nullable=False)
    price = Field(nullable=False)
    publish_at = Field()

Field constructor accept series option arguments.

  • name, specify field name in database.
  • nullable, should you provide initial value for this field when you create new record.
  • default, if you are not set default value, commit will not create new field or set null in leancloud storage. Because undefined and null is different in javascript :( . leancloud storage based on MongoDB, and MongoDB use JavaScript as first class citizen.
  • type_ not used yet.

CRUD operations

You can use Model.create to create new record, this method should check your record has been correctly initialized.

product = Product.create(name='FirstProduct', price=100)
product.commit()

# v0.1.3 now default null value
assert product.publish_at is None

If you are not pass default initial value for non-default field, it will be null (in storage, undefined).

Read & Query

use filter and/or filter_by to select records.

filter support more human readable condition selection.

  • >, <, >=, <=, == compare field with some value. Not supprot compare between fields in same or not same model.
  • contains simple pattern match for string field.
  • and_, or_ between two or more queries.

See examples below.

# find by simple equation
products = Product.query().filter_by(name='product').find()

# support >,<,>=,<=,==.but not support compare to another field.
products = Product.query().filter(Product.price < 10).find()

# support contains
products = Product.query().filter(Product.name.contains('art')).find()

# support and_(), or_().
products = Product.query().filter(Product.created_at > datetime(2018,8,1)).and_() \
    .filter(Product.created_at < datetime(2018,9,1)).find()

# find support limit and skip argument.
products = Product.query().order_by(Product.price.desc).find(limit=10)

# support pagination, start from page 0 and 10 elements per page.
pages = Product.query().paginate(0, 10)
for page in pages:
    print(page.items) # access elements

Update

You can update record by assign new value and commit change.

product = Product.query().filter_by(name='FirstProduct').first()
product.name = 'LastProduct'
product.commit()

Delete

drop method will delete record and set object invalid. Use drop_all for bulk operation.

product = Product.query().filter_by(name='FirstProduct').first()
product.drop()

products = Product.query().filter_by().find()
Product.drop_all(products)

Known limit

Some bulk modification/deletion does not support execute with conditions. It's limited by leancloud storage service. For more detail, see document below.

Change log

  • 0.1.7 修复初始值 null 覆盖了存储服务生成字段值的问题。

leancloud-better-storage-python's People

Contributors

leechael avatar nnnewb avatar

Watchers

James Cloos avatar  avatar

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.