More Related Content
Similar to Python for the Mobile and Web (20)
Python for the Mobile and Web
- 1. © 2013 NUS. The contents contained in this document may not be reproduced in any form or by any means, without the written permission of ISS, NUS other than for the purpose for which it has been supplied.
Software Development
Python for Mobile and Web
Total: 15 pagesATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0
Derek Kiong
dkiong@nus.edu.sg
- 2. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 2
What is Python?
Small and consistent programming language
Supports multiple programming paradigms –
object-oriented, imperative and functional
Design philosophy emphasizes code
readability – has clear syntax
has comprehensive standard library
latent (dynamic) type system
run-time model similar to Scheme/Lisp
- 3. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 3
Python Features
Dynamic typing – does not require
declaration
Contrast C and Java which have strong typing
(implemented through compile-time checks)
Dynamic objects with built-in memory
management
Similar with Java and C#
Contrast C and C++
Even function definition is dynamic
- 4. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 4
Python Language Features
int, float, complex, bool, str, bytes
list, tuple, set, dict
control flow statements, iterator, generator,
exception handling
functions, parameters
class, object, lambda
extensive function/class libraries
kivy and django are Python modules
- 5. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 5
Python Sample
def factorial(n):
if n > 1:
return n*factorial(n-1)
else:
return 1
def list(n):
for i in range(n):
if i%2 == 0:
print "{0} is even".format(i)
else:
print "{0} is odd".format(i)
- 6. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 6
Python Sample
def factorial2(n):
result = 1
for i in range(n,0,-1):
result = result*i
return result
def myadd(a,b):
"""
Adds a and b using + operator
"""
return a+b
- 7. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 7
Android phone/tablet
app = androidhelper.Android()
app.dialogCreateSpinnerProgress("Coffee", "checking...")
app.dialogShow()
pg = urlopen("http://www.beans-r-us.biz/prices.html")
text = pg.read().decode("utf8")
m = re.search(r'>$([.d]+)<',text)
if m:
price = float(m.group(1))
else:
price = 0
app.dialogDismiss()
app.vibrate()
- 8. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 8
Android phone/tablet
app.dialogCreateAlert("Current price of coffee beans:")
app.dialogSetItems([price])
app.dialogSetPositiveButtonText('OK')
app.dialogShow()
resp = app.dialogGetResponse().result
app.makeToast("Bye!")
- 9. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 9
Django for the Web
Model-View-Controller (MVC) Web
framework
Object-relational mapping
Generated admin pages
URL driven site
Templating system
- 10. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 10
Django Model
Model specified in Python
from django.db import models
class Product(models.Model):
code = models.CharField(max_length=8)
description = models.CharField(max_length=22)
price = models.IntegerField()
def __str__(self):
return self.description
class Admin: pass
- 11. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 11
URL matching
import mysite.inventory.views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
url(r'^product/?$', mysite.inventory.views.productlisting),
url(r'^product/(.+)$', mysite.inventory.views.product),
)
- 12. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 12
Views
from mysite.inventory.models import Product
def productlisting(request):
list = Product.objects.all()
t = get_template('productlisting.html')
html = t.render(Context({'products':list}))
return HttpResponse(html)
def product(request, pcode):
p = Product.objects.get(code=pcode)
t = get_template('product.html')
html = t.render(Context({'product':p}))
return HttpResponse(html)
- 13. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 13
productlisting.html template
<html><body>
<h1>Products</h1>
<ul>
{% for p in products %}
<li>
<a href="/product/{{ p.code }}">
{{ p.code }}
</a>
</li>
{% endfor %}
</ul>
</body></html>
- 14. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 14
product.html template
<html><body>
<h1>Product Information</h1>
<table border="1" style="solid">
<tr>
<th>Code</th>
<th>Description</th>
<th>Price</th>
</tr>
<tr>
<td> {{ product.code }} </td>
<td> {{ product.description }} </td>
<td> {{ product.price }} </td>
</tr>
</table>
</body></html>
- 15. © 2013 NUS. All rights reserved.ATA/SA-DIP/TUS/Python Mobile and Web.ppt/v1.0 Python for Mobile and Web 15
Summary
Python late binding provides much flexibility
http://docs.python.org/2/reference/
QPython in Android
http://qpython.com/
Django for the Web
https://docs.djangoproject.com/en/1.5/