#Sora#peewee plus celery = ?

來源:互聯網
上載者:User

標籤:

最近瘋看全職獵人


初步學習了peewee,因為sqlalchemy實在是太重量級,文檔也太噁心,peewee上手容易得多,也非常light

結合celery和peewee,寫了點東西,查詢年齡

myapp/db.py:

from __future__ import absolute_importfrom myapp.celery import appfrom peewee import *db = SqliteDatabase(‘people.db‘)class Person(Model):    name = CharField()    age = IntegerField()    class Meta:        database = db@app.taskdef agequery(name):    db.connect() #   collection = []    for i in Person.select().where(Person.name == name):       return {‘name‘:name,‘age‘:i.age}  #    collection.append({‘name‘:name,‘age‘:i.age})  #  return collection    db.close()@app.taskdef savedata(message):    db.connect()    newdata = Person.create(name=message.get(‘name‘),age=message.get(‘age‘))    newdata.save()    db.close()@app.taskdef initdb():    db.connect()    db.create_tables([Person,])    db.close()

測試案例:

Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> from myapp.db import savedata,agequery>>> res = agequery(‘miaoting‘)>>> res = agequery.delay(‘miaoting‘)>>> res.get(){u‘age‘: 17, u‘name‘: u‘miaoting‘}>>> savedata({‘name‘:‘Jc‘,‘age‘:65})>>> savedata.delay({‘name‘:‘Jc‘,‘age‘:65})<AsyncResult: c6d06e50-42b3-42a1-b06b-2561eb0c64af>>>> res = agequery.delay(‘Jc‘)>>> res.get(){u‘age‘: 65, u‘name‘: u‘Jc‘}>>> savedata.delay({‘name‘:‘FanShu‘,‘age‘:99})<AsyncResult: a348b074-a6f1-430c-af78-42d8e29cf4e6>>>> res = agequery.delay(‘Fanshu‘)>>> res.get()>>> res = agequery.delay(‘Jc‘)>>> res.get(){u‘age‘: 65, u‘name‘: u‘Jc‘}>>> res = agequery.delay(‘Fanshu‘)>>> res.get()>>> res = agequery.delay(‘FanShu‘)>>> res.get()


第一次使用db時要用initdb初始化,除非刪除sqlite資料庫,否則只需調用savedata和agequery



peewee筆記:

peewee 學習:

在peewee中,一個繼承model類的類定義為某個資料庫的一個表格

類中的feild,為資料庫的列(欄位)

一個model類執行個體則是一行


example:

from peewee import *


db = SqliteDatabase(‘people.db‘)


class Person(Model):

    name = CharField()

    birthday = DateField()

    is_relative = BooleanField()

    class Meta:

        database = db # This model uses the "people.db" database.



class Pet(Model):

    owner = ForeignKeyField(Person, related_name=‘pets‘)

    name = CharField()

    animal_type = CharField()

    class Meta:

        database = db # this model uses the "people.db" database


db.connect()


db.create_tables([Person, Pet])



儲存資料:

>>> from datetime import date

>>> uncle_bob = Person(name=‘Bob‘, birthday=date(1960, 1, 15), is_relative=True)

>>> uncle_bob.save() # bob is now stored in the database

1


>>> bob_kitty = Pet.create(owner=uncle_bob, name=‘Kitty‘, animal_type=‘cat‘)

>>> herb_fido = Pet.create(owner=herb, name=‘Fido‘, animal_type=‘dog‘)

>>> herb_mittens = Pet.create(owner=herb, name=‘Mittens‘, animal_type=‘cat‘)

>>> herb_mittens_jr = Pet.create(owner=herb, name=‘Mittens Jr‘, animal_type=‘cat‘)


刪除資料:

>>> herb_mittens.delete_instance() # he had a great life

1



查詢資料(根據類的feild訪問相關欄位的資料):

>>> grandma = Person.select().where(Person.name == ‘Grandma L.‘).get()


>>> grandma = Person.get(Person.name == ‘Grandma L.‘)




列出所有記錄:

>>> for person in Person.select():

...     print person.name, person.is_relative

...

Bob True

Grandma L. True

Herb False                



比較兩種查詢的區別:

>>> query = Pet.select().where(Pet.animal_type == ‘cat‘)

>>> for pet in query:

...     print pet.name, pet.owner.name

...

Kitty Bob

Mittens Jr Herb


>>> query = (Pet

...          .select(Pet, Person)

...          .join(Person)

...          .where(Pet.animal_type == ‘cat‘))

>>> for pet in query:

...     print pet.name, pet.owner.name

...

Kitty Bob

Mittens Jr Herb


後者的效率更高,後者不做無謂查詢


#Sora#peewee plus celery = ?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.