Last active
May 24, 2019 12:11
-
-
Save molcay/faf689f54724b64ae164062fc225ca34 to your computer and use it in GitHub Desktop.
Sample application for peewee ORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""app.py | |
Peewee demo app | |
Usage: | |
app create_db | |
app create_users | |
app update_user --user <user_id> --name <new_name> | |
app update_user2 --user <user_id> --name <new_name> | |
app update_user3 --user <user_id> --name <new_name> | |
app get_user1 | |
app get_user2 | |
Options: | |
-u, --user <user_id> User ID to change | |
-n, --name <new_name> New Name for update record | |
""" | |
from peewee import * | |
from datetime import date | |
from docopt import docopt | |
DATABASE = 'people.db' | |
db = SqliteDatabase(DATABASE) | |
db.connect() | |
# Models | |
class Person(Model): | |
name = CharField() | |
birthday = DateField() | |
class Meta: | |
database = db # This model uses the "people.db" database. | |
def __str__(self): | |
return f"{self.name} - {self.birthday}" | |
# helpers | |
def create_tables(): | |
with db: | |
db.create_tables([Person]) | |
def create_users(): | |
data = [ | |
{ | |
'name': "Ali", | |
'birthday': date(1960, 1, 15) | |
}, | |
{ | |
'name': "Ayşe", | |
'birthday': date(1935, 3, 5) | |
}, | |
{ | |
'name': "Emel", | |
'birthday': date(1998, 7, 27) | |
} | |
] | |
with db: | |
for d in data: | |
p = Person(name=d['name'], birthday=d['birthday']) | |
p_id = p.save() | |
print(d, p_id) | |
print("Kullanıcılar Oluşturuldu.") | |
def update_user(user_id, new_name): | |
with db: | |
p = Person.get(Person.id == user_id) | |
p.name = new_name | |
p.save() | |
print("Değiştirilen Kullanıcı:", p.id, p.name) | |
def update_user2(user_id, new_name): | |
with db: | |
p = Person.select().where(Person.id == 3).get() | |
p.name = new_name | |
p.save() | |
print("Değiştirilen Kullanıcı:", p.id, p.name) | |
def update_user3(user_id, new_name): | |
with db: | |
q = Person.update(name=new_name).where(Person.id == user_id) | |
kac_satir_degisti = q.execute() | |
print("Değişen satır sayısı:", kac_satir_degisti) | |
p = Person.get(Person.id == user_id) | |
print("Değiştirilen Kullanıcı:", p.id, p.name) | |
def get_user1(): | |
with db: | |
q = Person.select().order_by(-Person.birthday).limit(10) | |
for p in q: | |
print(p) | |
def get_user2(): | |
with db: | |
q = Person.select().order_by(Person.name.desc()).limit(10) | |
for p in q: | |
print(p) | |
def run(args): | |
if args['create_db']: | |
create_tables() | |
elif args['create_users']: | |
create_users() | |
elif args['update_user']: | |
update_user(args['--user'], args['--name']) | |
elif args['update_user2']: | |
update_user(args['--user'], args['--name']) | |
elif args['update_user3']: | |
update_user(args['--user'], args['--name']) | |
elif args['get_user1']: | |
get_user1() | |
elif args['get_user2']: | |
get_user2() | |
if __name__ == '__main__': | |
args = docopt(__doc__) | |
run(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment