Beruflich Dokumente
Kultur Dokumente
Lesson 3
Python introduction
OBJECT PRACTICING
Reference
http://learnpythonthehardway.org/
Exercize 40-41-42
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:
Once I have this code, I can use the module MyStuff with import and then access the apple function:
import mystuff
mystuff.apple()
def apple():
print "I AM APPLES!
# this is just a variable
tangerine = "Living reflection of a dream
import mystuff
mystuff.apple()
print mystuff.tangerine
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 style
mystuff['apples']
# module style
mystuff.apples()
print mystuff.tangerine
# class style
thing = MyStuff()
thing.apples()
print thing.tangerine
Class example
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print line
The __init__
method is called
passing lyrics as
parameter
Output:
$ python ex40.py
Happy birthday to you
I don't want to get sued
So I'll stop right there
They rally around tha family
With pockets full of shells
Inheritance example
class 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.
Output:
$ python ex44a.py
PARENT 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.
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.py
PARENT override()
CHILD override()
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.py
PARENT altered()
CHILD, BEFORE PARENT altered()
PARENT altered()
CHILD, AFTER PARENT altered()
class Child(Parent):
def __init__(self, stuff):
self.stuff = stuff
super(Child, self).__init__()
Python introduction
WEB PROGRAMMING
Reference
http://learnpythonthehardway.org/
Exercizes 50-51
Use libthw.py or web.py for this part
See exercize 50 for installation instructions
First website
import web
return greeting
if __name__ == "__main__":
app.run()
$ python bin/app.py
http://0.0.0.0:8080/
Basic templates
The first step is to create a templates/index.html file that looks like this:
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.
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.
Explanation
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.
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'
)
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.
urls = (
'/hello', 'Index'
)
<html>
<head>
<title>Sample Web Form</title>
</head>
/hello is the URL
<body>
<h1>Fill Out This Form</h1>
import web
index.POST
GET method
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.
index.POST
hello_form.html
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.
POST method:
name=greet,
name=name
index.POST
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 <form> says and sends the data as a POST.
POST method:
name=greet,
name=name
index.POST
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 <form> 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.
index.POST
index.html
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 <form> 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.
References
http://learnpythonthehardway.org/
http://www.htmldog.com/guides/
http://www.webpy.org/tutorial3.en