Iot Lesson3

Embed Size (px)

DESCRIPTION

IoT Lesson 3 for Programming for IoT applications at Politecnico di Torino

Citation preview

  • Programming IoT

    Lesson 3

  • OBJECT PRACTICINGPython introduction

  • Reference http://learnpythonthehardway.org/ Exercize 40-41-42

  • How to use modules Imagine I have a module that I decide to name mystuff.py and I put a function in it called apple. Here's

    the module mystuff.py:

    # this goes in mystuff.pydef apple():

    print "I AM APPLES!"

    Once I have this code, I can use the module MyStuff with import and then access the apple function:

    import mystuffmystuff.apple()

    I could also put a variable in it named tangerine:

    def apple():print "I AM APPLES!

    # this is just a variabletangerine = "Living reflection of a dream

    I can access that the same way:

    import mystuffmystuff.apple()print mystuff.tangerine

  • Classes are like modules and objects are like import

    class MyStuff(object): def __init__(self): self.tangerine = I am learning OOP

    def apple(self): print "I AM CLASSY APPLES!

    You can take this MyStuff class and use it to instatiate many of them. When you import a module there is only one for the entire program. You instantiate (create) a class by calling the class like it's a function, like this:

    thing = MyStuff()thing.apple()print thing.tangerine

    Python looks for MyStuff() and sees that it is a class you've defined. Python create an empty object with all the functions you've specified in the class using def. Python then looks to see if you there is a __init__ function, and if there is, it calls that function to initialize

    your newly created empty object. In the MyStuff function __init__ there is an extra variable self, which is that empty object Python made, for

    instance to set variables on it. In this case, I set self.tangerine to a string and then I've initialized this object. Now Python can take this newly created object and assign it to the thing variable.

  • Getting things I now have three ways to get things from things:

    # dict stylemystuff['apples']

    # module stylemystuff.apples()print mystuff.tangerine

    # class stylething = MyStuff()thing.apples()

    print thing.tangerine

  • Class exampleclass Song(object):

    def __init__(self, lyrics):self.lyrics = lyrics

    def sing_me_a_song(self):for line in self.lyrics:print line

    happy_bday = Song(["Happy birthday to you,"I don't want to get sued","So I'll stop right there"])

    bulls_on_parade = Song(["They rally around tha family","With pockets full of shells"])

    happy_bday.sing_me_a_song()bulls_on_parade.sing_me_a_song()

    Output:$ python ex40.pyHappy birthday to youI don't want to get suedSo I'll stop right thereThey rally around tha familyWith pockets full of shells

    The__init__methodiscalledpassinglyricsas

    parameter

  • Inheritance exampleclass Parent(object):

    def implicit(self): print "PARENT implicit()"

    class Child(Parent): pass

    dad = Parent()son = Child()

    dad.implicit()son.implicit()

    The use of pass under the class Child: is how you tell Python that you want an empty block. This creates a class named Child but says that there's nothing new to define in it. Instead it will inherit all of its behavior from Parent. Output:$ python ex44a.pyPARENT implicit()PARENT implicit()

  • Override The problem with having functions called implicitly is sometimes you want the child to behave differently. In this case you want to override the function in the child, effectively replacing the functionality. To do this just define a function with the same name in Child. Here's an example:class Parent(object):

    def override(self): print "PARENT override()"

    class Child(Parent): def override(self): print "CHILD override()"

    dad = Parent()son = Child()

    dad.override()son.override()

    In this example I have a function named override in both classes, so let's see what happens when you run it.

    Output:$ python ex44b.pyPARENT override()CHILD override()

  • A more confusing example The third way to use inheritance is a special case of overriding where you want to alter the behavior

    before or after the Parent class's version runs. You first override the function just like in the last example, but then you use a Python built-in function

    named super to get the Parent version to call. Here's the example of doing that so you can make sense of this description:class Parent(object):

    def altered(self): print "PARENT altered()"

    class Child(Parent):

    def altered(self): print "CHILD, BEFORE PARENT altered()" super(Child, self).altered() print "CHILD, AFTER PARENT altered()"

    dad = Parent()son = Child()

    dad.altered()son.altered()

    Output:$ python ex44c.pyPARENT altered()CHILD, BEFORE PARENT altered()PARENT altered()CHILD, AFTER PARENT altered()

  • Using super() with __init__ The most common use of super() is actually in __init__ functions in base

    classes. This is usually the only place where you need to do some things in a child,

    then complete the initialization in the parent. Example:

    class Child(Parent):

    def __init__(self, stuff): self.stuff = stuff super(Child, self).__init__()

  • WEB PROGRAMMINGPython introduction

  • Reference http://learnpythonthehardway.org/ Exercizes 50-51

    Use libthw.py or web.py for this part See exercize 50 for installation instructions

  • First website Put the following code into bin/app.py:

    import web

    urls = ( '/', 'index')

    app = web.application(urls, globals())

    class index: def GET(self): greeting = "Hello World" return greeting

    if __name__ == "__main__": app.run()

    Then run the application like this:

    $ python bin/app.pyhttp://0.0.0.0:8080/

    Yourbrowsermakesanetworkconnec;ontoyourowncomputer,whichiscalledlocalhostonport8080.

    Onceitconnects,itmakesanHTTPrequesttothebin/app.pyapplica;onandasksforthe/URL,whichiscommonlytherstURLonanywebsite.

    Insidebin/app.pyyou'vegotalistofURLsandwhatclasses

    theymatch.Theonlyonewehaveisthe'/','index'mapping.Thismeansthatwheneversomeonegoesto/withabrowser,lpthw.webwillndtheclassindexandloadittohandletherequest.

    Nowthatlpthw.webhasfoundclassindexitcallstheindex.GETmethodonaninstanceofthatclasstoactuallyhandletherequest.Thisfunc;onrunsandsimplyreturnsastringforwhatlpthw.webshouldsendtothebrowser.

    Finally,lpthw.webhashandledtherequestandsendsthis

    responsetothebrowser,whichiswhatyouareseeing.

  • Basic templates The first step is to create a templates/index.html file that looks like this:

    $def with (greeting_var)

    IoT Programming Course

    $if greeting_var: I just wanted to say $greeting_var$else: Hello, IoT programmers!

    This HTML file is a template, which means that lpthw.web will fill in "holes" in the text depending on variables you pass in to the template. Every place you see $greeting will be a variable you'll pass to the template that alters its contents.

    MatteoNotaSe "greeting_var" esiste si ha un certo output (con una certa formattazione) altrimenti un'altra scritta (righe successive).

    MatteoNotaI templates sono griglie che vanno completate con i contenuti di certe variabili (nel nostro caso la variabile "greeting_var").

  • Managing Templates To make your bin/app.py do this, you need to add some code to tell lpthw.web where to load the template

    and to render it. Take that file and change it like this:

    import web

    urls = ( '/', 'Index')

    app = web.application(urls, globals())

    render = web.template.render('templates/')

    class Index(object): def GET(self): greeting = "Hello World" return render.index(greeting_var = greeting)

    if __name__ == "__main__": app.run()

    Pay close attention to the new render variable and how I changed the last line of index.GET so it returns render.index() passing in your greeting variable.

    MatteoNotaE' un'istruzione a cui viene passata la location della pagina html.

    MatteoNotarender.index (gli viene passata la pagina ma senza il formato) --> index il METODO; come parametro gli viene passata la variabile (riassegnata in questo caso).

    MatteoNotaLa parte finale potrebbe essere diversa a seconda della libreria web a cui si fa riferimento. Questa l'istruzione che permette di far partire l'applicazione web.

  • Explanation In your bin/app.py you've added a new variable, render, which is a web.template.render

    object.

    This render object knows how to load .html files out of the templates/ directory because you passed that to it as a parameter.

    Later in your code, when the browser hits the index.GET like before, instead of just returning the string greeting, you call render.index and pass the greeting to it as a variable.

    This render.index method is kind of a magic function where the render object sees that you're asking for index, goes into the templates/ directory, looks for a page named index.html, and then "renders" it, or converts it.

    In the templates/index.html file you see the beginning definition that says this template takes a greeting parameter, just like a function. Also, just like Python this template is indentation sensitive, so make sure you get them right.

    Finally, you have the HTML in templates/index.html that looks at the greeting variable and, if it's there, prints one message using the $greeting, or a default message.

  • How the web works Please refer to learnpythonthehardway.org for more info

    GETrequestpath:A->B->C->DHTMLlepath:D->C->B->A

  • HTML Forms The best way to play with forms is to write some code that accepts form data, and then see what you can do. Take your bin/app.py file

    and make it look like this:import web

    urls = ( '/hello', 'Index')

    app = web.application(urls, globals())

    render = web.template.render('templates/')

    class Index(object): def GET(self): form = web.input(name="Nobody") greeting = "Hello, %s" % form.name

    return render.index(greeting_var = greeting)

    if __name__ == "__main__": app.run()

    Going to http://localhost:8080/hello it should display, "I just wanted to say Hello, Nobody."

    Next, change the URL in your browser to http://localhost:8080/hello?name=Andrea and you'll see it say, "Hello, Frank." Finally, change the name=Frank part to be your name. Now it's saying hello to you.

    Use web.input to get data from the browser. This function takes a key=value set of defaults, parses the ?name=Andrea part of the URL you give it, and then returns a nice object for you to work with that represents those values.

    The greeting is built from the new form.name attribute of the form object.

    ThisinputispassedaspartoftheURL

    MatteoNotaCon questa riga io definisco "l'indirizzo del webserver" e lo associo all' Index.

    MatteoNotaIl metodo input utilizzato per ricevere input dal server e poi fare qualcosa...

    MatteoNotaSto creando una stringa che deve contenere il contenuto di una variabile (form.name).

    MatteoEvidenziato

    MatteoNotaE' la struttura utilizzata per passare parametri tramite URL.

  • Creating Forms with POST"POST form is a special HTML file that has a tag in it. This form will collect information from the user, then send it to your web application

    Here's the new HTML file you need to create, in templates/hello_form.html:

    Sample Web Form

    Fill Out This Form

    A Greeting: Your Name:

    You should then change bin/app.py to look like this:

    import web

    urls = ( '/hello', 'Index')

    app = web.application(urls, globals())

    render = web.template.render('templates/')

    class Index(object): def GET(self): return render.hello_form()

    def POST(self): form = web.input(name="Nobody", greet="Hello") greeting = "%s, %s" % (form.greet, form.name) return render.index(greeting_var = greeting)

    if __name__ == "__main__": app.run()

    /helloistheURLrunningtheformPOSTisthemethod

    /hellomustbeinthelistofURLs

    NotethatGETreturnshello_form.html,whilePOSTreturnsindex.html

    MatteoNotaUtilizzando questa riga il /hello utilizzer il metodo POST e non GET.

    MatteoMatita

    MatteoMatita

  • How POST works

    1. The browser first hits the web application at /hello but it sends a GET, so our index.GET function runs and returns the hello_form.

    GETmethod

    Execu;on:index.GET

    index.POSThYp://localhost:8080

  • How POST works

    1. The browser first hits the web application at /hello but it sends a GET, so our index.GET function runs and returns the hello_form.

    hello_form.html

    index.POSThYp://localhost:8080

  • How POST works

    1. The browser first hits the web application at /hello but it sends a GET, so our index.GET function runs and returns the hello_form.

    2. You fill out the form in the browser, and the browser does what the says and sends the data as a POST.

    POSTmethod:name=greet,name=name

    index.POSThYp://localhost:8080

  • How POST works

    1. The browser first hits the web application at /hello but it sends a GET, so our index.GET function runs and returns the hello_form.

    2. You fill out the form in the browser, and the browser does what the says and sends the data as a POST.3. The web application then runs the index.POST method rather than the index.GET method to handle this request.

    POSTmethod:name=greet,name=name

    Execu;on:index.POST

    index.POSThYp://localhost:8080

  • How POST works

    1. The browser first hits the web application at /hello but it sends a GET, so our index.GET function runs and returns the hello_form.

    2. You fill out the form in the browser, and the browser does what the says and sends the data as a POST.3. The web application then runs the index.POST method rather than the index.GET method to handle this request.4. This index.POST method then does what it normally does to send back the hello page like before.

    index.html

    index.POSThYp://localhost:8080

  • References http://learnpythonthehardway.org/ http://www.htmldog.com/guides/ http://www.webpy.org/tutorial3.en