0001: Hello Bioinformatics

peanutunderwearΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

50 εμφανίσεις

1


0001: Hello Bioinformatics

We should start using the very first

Hello

Bioinformatics


program like

Hello World


program of the
long tradition.

We want to write down some snippets of code that can print the inspiring text


“Hello
Bioinformatics”

on the output screen of python (default

screen
)
.

It is assumed that we have already
installed python engine (Python 2.7) along with its IDLE (for detail see ChapterXXXX)
.
And we will
write the code on python IDLE and runt the code file from the python IDL
E.



As we have mentioned earlier that we would try walk
ing

in the python programming in Object Oriented
Programming (OOP) manner, we need to start thinking with the concept of
python
class
,
very simply a
python
-
class is a python
-
coded representation of any real
-
life objects (better we can say, bioinformatics
objects).
In the real
-
life problems an object is characterized with some attributes and functionalities, and
surely it is identified by
some names. For this very first case of “Hello Bioinformatics” problem, we can
think that there is needed an object (let name this object
HelloBioinformatics
) in python that is capable of
showing/printing the text “Hello Bioinformatics” on the output scree
n. From a little bit deeper thinking
and analogy to our previous sentences, the HelloBioinformatics class needs to perform the task (or
functionality) of printing a text
as

the output.
Within a class a functionality or task performer machinery is
called
fu
nction
.
Lets name this machinery (function)
say_hello
.


In python a class is coded in the following structure.

A class starts with the python key word
class

followed by the class name. And the following colon indicates the start of that specific class. Al
l codes
(line of codes) bellow this line with one
-
tab (right
-
tab) indentation belongs to this class. Unlike some
other languages, it does not mark
-
up the code regions by braces (curly braces), rather by code
-
indentation.
So the coder needs to be very caref
ul of this. For the convenient of the programmer, an
-
end line (for this
class) can be put as a do
-
nothing line using the python keyword
pass
.

This line indicate the last line of the
corresponding code
-
block with the right indentation.


************** A Simple Picture
(two pictures side by side)
**************


In python a function is defined as the following schematics diagram.
A function (the programmers own
built function) is defined by the keyword
def

followed by the function name, and

the input parameter(s)
for the function is specified in the argument list named within the first
-
bracket region. Then the colon (
:
)
declares the start of the corresponding function, as like the case with class (discussed earlier). Same as
class coding, a
function body is identified with indentation of the code line. And the handy coding the
pass



line can also be added in this case.

The body of the function will contain all the codes to perform
the desired functionality for this respective function.

In th
is particular case, the functionality is to print a
text on the output screen. A built
-
in function of python can do this printing job. That is
print

function
(there many such important functions in python, we will learn in the course of time). This print
-
f
unction
takes a string as input and prints it on the python default output screen.


************** A Simple Picture (two pictures side by side) **************


So the final code looks like the following.
Lets create a new file (python code file) and name it as
HelloBioinformatics.py
,
and save the file in your favorite directory
.
Running this code

(press
F5 or click Run
-
>Run Module) will do nothing.

2



Filename:
HelloBioinformatics.py

1

class

HelloBioinforma
tics
:

2

def

say_hello
(self):

3

print
(
"Hello Bioinformatics!"
)

4

pass

5

pass



Class represents an object in general. Without giving life to this object, how can it be used! An instance of
this object needs to be created first,

this is called instantiation of a class or object. Lets say, we will create
an instance of object like
HelloBioinformatics
,
this is the

easiest work to do. Just name the
instance as you wish (
objHelloBioinformatics
)

and assign the instance just writing the class
name followed by opening and closing first brackets.
This instance has been created, still we have not
poked the right function so do our desired task. This can be done just calling the function by name from
the instance name through a dot (
.
).
The final work is to define the entry point of this program. The
main

function
. This entry point is defined by the code line
if
(
__name__=="__main__"
)
:

(more details
will be discussed latter). Your are DONE, now save and

run this code file. The final code file is as bellow.


Filename:
HelloBioinformatics.py

1

class

HelloBioinformatics
:

2

def

say_hello
(self):

3

print
(
"Hello Bioinformatics!"
)

4

pass

5

pass

6

7

if
(
__name__==
"__main__"
)
:

8

objHelloBioinformatics = HelloBioinformatics()

9

objHelloBioinformatics.say_hello()


Output:


Hello Bioinformatics!