0% found this document useful (0 votes)
59 views

Python by Example: Rosedu Osss 2013, Python, Alex Morega

The document discusses using Python to build a command line application that allows adding, listing, marking as done, and exporting to CSV and JSON items stored in a SQLite database. It defines an Item model with text and done fields, handles different commands by checking the first argument, and provides examples of inserting, querying, and exporting the item data.

Uploaded by

jorge sierra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Python by Example: Rosedu Osss 2013, Python, Alex Morega

The document discusses using Python to build a command line application that allows adding, listing, marking as done, and exporting to CSV and JSON items stored in a SQLite database. It defines an Item model with text and done fields, handles different commands by checking the first argument, and provides examples of inserting, querying, and exporting the item data.

Uploaded by

jorge sierra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Python by example

ROSEdu OSSS 2013, Python, Alex Morega


hello world

def main():
print "hello world"

if __name__ == '__main__':
main()
command-line argument

import sys

def main():
print sys.argv[1]
# what if sys.argv[1] is missing?

if __name__ == '__main__':
main()
dispatch based on 1st argument
import sys

def main():
cmd = sys.argv[1]
if cmd == 'add':
text = sys.argv[2]
print 'adding', text
else:
print 'unknown comand', cmd

if __name__ == '__main__':
main()
set up flask-sqlalchemy
#$ pip install flask-sqlalchemy
import flask
from flask.ext.sqlalchemy import SQLAlchemy
app = flask.Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)

def main():
# ...

if __name__ == '__main__':
app.app_context().push()
db.create_all()
main()
define database model, insert row

class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String)

def main():
# ...
if cmd == 'add':
text = sys.argv[2]
item = Item(text=text)
db.session.add(item)
db.session.commit()
# ...
print existing rows

def main():
# ...
elif cmd == 'list':
for item in Item.query.all():
print item.id, item.text
# ...
extra column "done"
class Item(db.Model):
# ...
done = db.Column(db.Boolean)

def main():
# elif cmd == 'list': ...
status = '[x]' if item.done else '[ ]'
print item.id, status, item.text
# ...
elif cmd == 'done':
id = int(sys.argv[2])
item = Item.query.get(id)
item.done = True
db.session.commit()
# ...
export as csv

import csv

def main():
# ...
elif cmd == 'csv':
writer = csv.writer(sys.stdout)
writer.writerow(['id', 'done', 'text'])
for item in Item.query.all():
writer.writerow([item.id,
'y' if item.done else '',
item.text])
# ...
export as json
import json

def main():
# ...
elif cmd == 'json':
data = []
for item in Item.query.all():
data.append({
'id': item.id,
'done': item.done,
'text': item.text,
})
#print data
print json.dumps(data, indent=2)
# ...
load json into database

import json

def main():
# ...
elif cmd == 'loadjson':
print json.load(sys.stdin)
# ...

You might also like