Python Code Camp (Professionals) is a whole day workshop that aims to enable professionals to learn Python Basics and Django.
Visit: http://devcon.ph/events/python-code-camp-professionals-2016
4. Hello World
from pymongo import MongoClient
client = MongoClient("localhost", 27017)
db = client.databasename
5. Step
from pymongo import MongoClient
Import MongoClient from pymongo
client = MongoClient("localhost", 27017)
Connect to the mongodb server at the IP address and port defined
db = client.databasename
Access a specific database in the mongodb server
collection = db.collectionname
Access a specific collection (table in SQL) in the selected db
8. Step
username = request.forms.get("username")
Get the username, password, email to data from the forms
userdata = {"name":username, "password":password,
"email":email}
Create a dictionary with the data obtained above
try:
Setup anexception handler
11. Step
usercursor = db.users.find({},{"_id":False})
Find allentries in the collection named users and do not show the _id field
users = list(usercursor)
Convert the cursor object into a list,which we can be converted to a jsonobject
return {"users":users}
Return the list of users to the browser
12. The Query Format
db.collectionname.find(filter,projection)
Filter is the expression that needs to be true an element to be included
Examples:
• {} = will return all
• {“name”:”paolo”} = will only return entries with name exactly to“paolo”
• {“name”:”paolo”, “age”:{“$lte”:30}} = will only return entries with names exactly “paolo” and age
is less thanor equal to 30
See more filter operators here: https://docs.mongodb.com/manual/reference/operator/query/
14. Inputs in Bottle
Via POST body
@route('/login', method="POST")
def login():
username = request.forms.get("username")
password = request.forms.get("password")
acceptedpasswords = ["gloopgroup", "devconph" ,
"adventuretime"]
if password in acceptedpasswords:
return "Welcome "+ username
else:
return "Unauthorized"
Visit: localhost:8001/login using login page
15. Activity 1
Create a calculatorapi:
http://localhost:8001/operation/operand1/operand2
will return the value of
operand1 <operation> operand2
Example:
/subtraction/10/2 will output 8
21. Returning Static HTML
HTML
Use static_file from bottle
@route("/loginpage")
def loginpage():
return static_file("login.html", root=".")
22. Testing the POST Handler
Visit localhost:8081/loginpage
Type your nameand use any of theseas passwordg
gloopgroup, devconph, adventuretime
Click submit
23. The POST Request Cycle
<form action="/login" method="POST">
Method dictates what kind of request happens when submitted
Action tells the browser where to submit the request
<input type="text" name="username">
Adds a text input that will be associated to the username field
<input type="password" name="password">
Adds a password inputthat willbe associatedto the password field
<input type="submit">
Adds a button that submits the form whenclicked
24. Returning Dynamic HTML
HTML
Use a templatingengine:
• Library thatcan substitutevariables intoa templatequickly and cleanly
• Isolates thedesigner’s job of designing and thecoder’s job of coding
28. Step
from gevent import monkey;
monkey.patch_all()
from bottle import run, route, request
import tenjin
from tenjin.helpers import *
Import alllibraries, including tenjin and all helper methods
@route("/introduction/<name>/<age>/<work>")
Define a route for the url, and define variables in it
def template1(name, age, work):
Declare a request handler and receive the variables from the url
29. Step
context = {"name":name, "age":age, "work":work}
Define a context variable, which is a dictionary of values that will be substituted into the template
return engine.render("introduction.html", context);
Render the template called introduction.html using the context variable named context, and return it to
the browser
engine = tenjin.Engine(path=['.'])
Instructs the templating engine to look for the templates in the directories listed in path
32. Step
@route("/spell/<word>")
Define a route that accepts a variable assigned to word
def speller(word):
Define a handler that accepts avariable named word
letters = list(word)
Convert the text string into a listobject. From a string “word”, it becomes a list [“w”, “o”, “r”, “d”]
33. Step
context = {"letters":letters}
Define a context variable, which contains the listof letters we just created
return engine.render("speller.html", context);
Render the template called speller.html using the context variable named context, and return it to the
browser
34. Step (Inside the template)
<?py for letter in letters: ?>
Loop on the elements of the list named letters
<h3>${letter}</h3><br>
Display the element letter along with the html
<?py #endfor ?>
Denote where the loop ends inthe template
<a href="/greet/${word}">greet ${word}</a>
Create a link that willcall the greet API we created earlier
{"letters":["s", "u", "s", "h", "i"]}
36. Step
users = [
{"name":"Em", "image":"http://goo.gl/aDxeu1", "age":30, "work":"Futurist"},
{"name":"Paolo", "image":"http://goo.gl/5k2oZr", "age":30, "work":"Synergist"}
]
Define a list of dictionaries, with each dictionary objectcontaining information about a user. It contains information
name,url of the image, age, and work
context = {"users":users, "name":name}
Define a conext containing the name to behighlighted, and the list of users
return engine.render("profiles.html", context)
Render the template named profiles.html with the context
37. Step (Inside the template)
<?py for user in users: ?>
Loop on the elements of the list named users, each element in the listcan be referred to using the user
variable
<div style="height:100px;">
Create a div element in the html to contain one user element
<img src="${user['image']}" style="float:left;
height:100%;"/>
Place an image in the html coming from the url defined in user[‘image’]
38. Step (Inside the template)
<?py if name==user["name"]: ?>
Compare the current user’s name to the name entered in the url
<strong>Name: ${user['name']}</strong>
Thishappens when the above condition istrue, display the name asbold characters
<?py else: ?>
Add an else handler that executes if condition is not met
39. Step (Inside the template)
Name: ${user['name']}
This happens when primary condition is not met, display the user’s name without any decorations
<?py #endif ?>
Denote where the if statement ends
40. Make a Sushi Menu
Create a menu using allthe sushiitems in the sushilistvariable
Display the image and name of the sushi
When the menu item is clicked, it should display another page containingthe name, image, price, and
rating of the sushi(defined in the sushilistvariable)
The most creative presentation of the menu wins aprize
42. Cost per Mille =
• Cost per 1000 impressions
• Abuse of statistics
• i.e. Magic
• i.e. It sucks
• Not based on empirical measured data
ADVERTISING!
Eyeballs
cost
43. How many are watching
me right now?
How many are paying attention?
How many are interested?
How many are still reading this part?
How about now?
Now?
This is our current project onewatt. It’s a device that can save you up to 40% in your electricity bills by utilizing fluctuations in electricicty prices in WESM throughout the day. How?
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I wont be talking about OneWatt. Since, apparently, Wilson deemed this thing not too thingie enough for the internet of things. So I’m going to talk about something more thingie…
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
I did this by exploiting one of the thingiest thinies right now, something that everyone here has on them most fo the time. Your mobile devices. Assuming that an average person has at least 1 devic eon them, I was able to get how many people walked by the screen, how many stayed for at least 2 secs, and how many people finished the ad.
One of the key metrics used in advertising is the number of eyeballs gained by an ad. Makes sense, since advertisers are paying to have their ads seen by the most amount of people. The problem with this though is that this number, or Cost per Mille, is an abuse of statistics, it is not based on a solid methodology.
So back then we did this media wall project in Shanghai. we wanted to prove how many people saw the ad, and how long people were looking at it.
So we devised a way to have more empirical data.
As of now, I can empirically estimate about ___________ watching me right now.
(turn on device and get count)
The setup is actually quite simple. You can even do it with a router, but for this purpose, I used a pi, connected to a wifi dongle, and this cute little screen i salvaged from an old tv, all powered by a power bank. Very mcgyvery.
This wifi transmitter is set to promiscuous mode, para syang carinderia na bukas sa lahat ng gutong kumain, actually worse, bukas kahit sa mga walang planong kumain. Using this, I am able to sniff all the traffic being trasmitted by all the dvices, specifically probe requests, within a certain range, (with this range varying based on the yyy used).
The CPU, running python, gets the output of tcpdump, mining all the sniffed mac addresses and adding timestamps to each record withing the vicinity, without them installing anything on their device or being aware. So right now I am getting all the mad accdresses of the devices of this group right here, since they are withing x meters form me, which I can now use to check for uniqueness, use for idnetification or use in whatever evil scheme I plan. Sounds excting diba? Borderline snooping.
We ran this on 2 pilot sites, one in Zhngshan park, and the other in Lujiazhui station. Using my setup, on the average, there were 23000 people who pass by our screens everyday, 36% of which stopped and watched for at least 2 secs, and around 10% stayed finished the 15 second ad.
So, especially for the group here <referto prev group>, so what if you got our mac addrsses, that doesn’t sound too bad, are there other possible application of this?
I have used this device in stores to measure foot traffic. I can identify how a customer moves between shelves, and identifying which items attract the most views. Which is important data for a retailer or advertiser.
You can also use this as a replacemnt for gps in geofencing. By attenuating the broadcast range to a specific area. You can then identify new and returning customers to your shop, and instruct your staff to be more accommodating to new customers going in.
And the most fun thing to do with this s probably just snoop. Imagine if we install one of these in each classroom. Using simple data such as class schedules, we can then relate a mac address to a specific student. Imagine then if we leave this device in the lagoon area. We will now have a list of people visiting that spot at say 10pm onwards. Who is meeting who, who is coming in in groups of 2 or even people coming in groups of 3 or more.
So in summary, turn off your phones when going to the lagoon.