Before thinking of a web application, we first refactor our program
such that it fits with the classical MVC pattern and a compute component.
The refactoring does not change the functionality of the code, it
just distributes the original statements in functions and modules.
Here we create four modules: model
, view
,
compute
, and controller
.
compute
module contains a function compute(r)
that performs
the mathematics and returns the value s
, which equals sin(r)
.model
module holds the input data, here r
.view
module has two functions, one for reading input data,
get_input
,
and one for presenting the output, present_output
.
The latter takes the input, calls compute
functionalty, and
generates the output.controller
module calls the view to initialize
the model's data from the command line. Thereafter, the
view is called to present the output.model.py
file contains the r
variable, which must
be declared with a default value in order to create the data object:
r = 0.0 # input
s = None # output
The view.py
file is restricted to the communication with the user and reads
import sys
import compute
# Input: float r
# Output: "Hello, World! sin(r)=..."
def get_input():
"""Get input data from the command line."""
r = float(sys.argv[1])
return r
def present_output(r):
"""Write results to terminal window."""
s = compute.compute(r)
print 'Hello, World! sin(%g)=%g' % (r, s)
The mathematics is encapsulated in compute.py
:
import math
def compute(r):
return math.sin(r)
Finally, controller.py
glues the model and the view:
import model, view
model.r = view.get_input()
view.present_output(model.r)
Let us try our refactored code:
Terminal> python controller.py 1.2
Hello, World! sin(1.2)=0.932039
The next step is to create a web interface to our scientific hello world
program such that we can fill in the number r
in a text field, click a
Compute button and get back a new web page with the output text
shown above: "Hello, World! sin(r)=s".