0% found this document useful (0 votes)
8 views11 pages

Python Command-Line Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Python Command-Line Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link][1]
# what if [Link][1] is missing?

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

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

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

def main():
# ...

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

class Item([Link]):
id = [Link]([Link], primary_key=True)
text = [Link]([Link])

def main():
# ...
if cmd == 'add':
text = [Link][2]
item = Item(text=text)
[Link](item)
[Link]()
# ...
print existing rows

def main():
# ...
elif cmd == 'list':
for item in [Link]():
print [Link], [Link]
# ...
extra column "done"
class Item([Link]):
# ...
done = [Link]([Link])

def main():
# elif cmd == 'list': ...
status = '[x]' if [Link] else '[ ]'
print [Link], status, [Link]
# ...
elif cmd == 'done':
id = int([Link][2])
item = [Link](id)
[Link] = True
[Link]()
# ...
export as csv

import csv

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

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

import json

def main():
# ...
elif cmd == 'loadjson':
print [Link]([Link])
# ...

You might also like