Tuesday, June 17, 2008

Things to do after installing openSUSE 11.0

Things to do after installing openSUSE 11.0

Ben Kevan’s Blog :

So you’ve downloaded and installed openSUSE 11.0. Are you now wondering

what you may have to do post installation, here’s a quick run down:

Setup suggested repositories

Setup your Video Card (if you have more configurations to do, ie Nvidia / ATI)

Get Compiz running (if you wish)

Codec Support (Restricted Formats, multimedia)

Install various applications

So here we go:

Setting up repositories:

For my machine I added the following repositories for my final build:

http://download.videolan.org/pub/vlc/SuSE/11.0 (VLC Media, restricted formats)

http://download.opensuse.org/repositories/KDE:/KDE4:/STABLE:/Desktop/openSUSE_11.0/
(KDE 4 Stable) Note: if this was not my workbox, I would actually do
Unstable to get KDE 4.1

http://download.opensuse.org/repositories/Emulators:/Wine/openSUSE_11.0/ (Wine, for windows emulation)

http://download.opensuse.org/repositories/KDE:/Backports/openSUSE_11.0/
(KDE Backports, updates to Amarok and other KDE Applications)

http://download.opensuse.org/repositories/X11:/XGL/openSUSE_11.0/
(Compiz Fusion, staying up to date and hip with the most recent Compiz
builds, I highly recommend since it’s constantly developed on)

http://ftp.skynet.be/pub/packman/suse/11.0/ (Packman, xinelibs / codecs and so much more)

http://download.opensuse.org/repositories/mozilla/openSUSE_11.0/ (Firefox, recent builds of Firefox)

http://download.opensuse.org/repositories/GNOME:/Community/openSUSE_11.0/ (Gnome Community, I use for Pidgin and GIMP)

http://download.opensuse.org/repositories/OpenOffice.org:/STABLE/openSUSE_11.0/
(OpenOffice is an important application for me since I use it daily for
word processing or spreadsheets, keeping it up to date allows for the
best compatabilities with people using restricted formats like xls/doc)

http://download.opensuse.org/repositories/Education:/desktop/openSUSE_11.0/ (Education - For Bluefish)

http://download.opensuse.org/repositories/KDE:/KDE3/openSUSE_11.0/ (KDE
Stable repository, not sure why because not many changes should happen
to the 3.5.9 family)


Here is a quick screenshot of my repositories:

Configured Repos


(Note: I am running KDE, if you are running GNOME, I highly recommend the following repo:

http://download.opensuse.org/repositories/GNOME:/STABLE/openSUSE_11.0/)

(Note: If you are running an NVIDIA card, setup the following repository:

ftp://download.nvidia.com/opensuse/11.0/)

(Note: If you are running an ATI card, setup the following repository:

http://www2.ati.com/suse/11.0/)

(Note: If you are running a wireless card that has a requirement for MadWifi, setup the following repository:

http://madwifi.org/suse/11.0/)


Setup your Video Card (if you have more configurations to do, ie Nvidia / ATI)

Install the packages that are associated with your NVIDIA / ATI card.


For me I downloaded the NVIDIA Driver from nvidia.com and installing running:

sudo sh NVIDIA-Linux-x86-173.08-pkg1.run

Since I had some issues with the 173.14 driver.


Get Compiz running (if you wish)

With openSUSE 11.0 it works out of the box, but I recommend installing
some of the other packages that don’t come installed by default also
updating to the Compiz Fusions newest build service.


Here are the compiz packages I have installed:

rpm -qa | grep compiz

compiz-fusion-plugins-main-0.7.6-2.6

compiz-fusion-plugins-extra-0.7.6-1.3

compizconfig-settings-manager-0.7.6-1.6

compiz-kde-0.7.6-2.5

compiz-emerald-0.7.6-1.6

compiz-manager-0.0.1_git080201-24.1

compiz-emerald-themes-0.6.0-16.1

compiz-0.7.6-2.5

libcompizconfig-0.7.6-1.6

python-compizconfig-0.7.6-1.6


sudo zypper up -t package -r “Compiz Fusion”

(Note: I named my Compiz Fusion repository Compiz Fusion, the -r switch
is for your Repo Alias that you have included into your Repository
Configuraiton)


Codec Support (Restricted Formats, multimedia)

Install the restricted formats you want to (i personally do not install
libdvdcss since I have no need to play dvd’s on my work machine, and it
is prohibited in the US (for now)). This should allow you to play avi /
mpg / mp3 / dvd’s and many other formats


You can install by running: (this requires packman and vlc repositories)

sudo zypper in libdvdcss libxine1 w32codec-all k3b-codecs


Note: if this is a home machine for multimedia I recommend also installing vlc by running:

sudo zypper in vlc


Install various applications

I typically install wine, opera, pidgin and bluefish from the repos.

I also install vmware for my daily work needs.


You can install running:

sudo zypper in wine opera pidgin bluefish

sudo rpm -ivh sudo rpm -ivh VMware-workstation-6.0.4-93057.i386.rpm

I then configured vmware by running:

sudo /usr/bin/vmware-config.pl

(Note: you have to install kernel-source, gcc, gcc++ and make)

You can install running:

sudo zypper in make kernel-source gcc gcc-c++

You can check my wiki located here for more indept installation procedures


Here is a screenshot of my machine running KDE 3.5.9, openSUSE 11.0 and Compiz Fusion:

Screenshot


Bonus: I also installed the Oxygen packages for yast, to beautify my KDE 3.5.9 yast, you can do the same by running:

sudo zypper in yast2-theme-openSUSE-Oxygen


YaST2 Oxygen


Please if you believe there is more to be done after installing
openSUSE 11.0 let me know so I can update this, and it can be helpful
to a bigger vast of people.

Programming Python, Part II

Programming Python, Part II
July 1st, 2007 by José P. E. "Pupeno" Fernandez in

* HOWTOs

Having covered some advanced features in Part I, it's time to include some basics.

The tutorial in last month's issue covered the basics of installing Python, running it and using it. Then, we moved on to building a basic blog in Python. The blog was extremely simple—only a Python list. We focused on the posts and built a Post class:

class Post(object):
def __init__(self, title, body):
self.set_title(title)
self.set_body(body)

def set_title(self, title):
self._title = title

def get_title(self):
return self._title

def set_body(self, body):
self._body = body

def get_body(self):
return self._body

def __repr__(self):
return "Blog Post: %s" % self.get_title()

In this follow-up article, let's focus on the blog itself and go further.
The Blog

Now that we have the Post class, we can make the Blog class. An initial implementation may look like this:

class Blog(object):
def __init__(self):
self._posts = []

def add_post(self, post):
self._posts.append(post)

def get_posts(self):
return self._posts

We are using a list to maintain the posts, but the interface is totally abstract behind a set of methods in the Blog class. This has a huge advantage: tomorrow we could replace that simple list with an SQL back end, and the code that uses Blog will need few, if any, changes.

Notice that there's no way to delete a post. We could tamper with _posts directly, but as long as we do what the class was meant to do, we can't delete a post. That may be good or bad, but the important thing is that by defining a set of methods, we exposed the design of how the class should be used.
To Publish or Not to Publish

The method get_posts returns all the posts. When we are writing a new post, we don't want the whole world to be able to read it until it is finished. The posts need a new member that tell whether it is published. In Post's initalizator, __init__, we add the line:

self._published = False

That makes every new post private by default. To switch states, we add the methods:

def publish(self):
self._published = True

def hide(self):
self._published = False

def is_public(self):
return self._published

In these methods, I introduced a new kind of variable—the boolean. Booleans are simple; they can be true or false. Let's play with that a bit:

/>>> cool = blog.Post("Cool", "Python is cool")
/>>> cool.is_public()
False
/>>> cool.publish()
/>>> cool.is_public()
True
/>>> cool.hide()
/>>> cool.is_public()
False
/>>>

If, when you run is_public, you get:


Traceback (most recent call last):
File "", line 1, in ?
File "blog.py", line 25, in is_public
return self._published
AttributeError: 'Post' object has no attribute
'_published'

That's because _published was not created, it can't be used, and is_public wants to use it. Understanding errors in your tools is important if you want to be a successful programmer.

In this short set of messages, the last line is the error itself. There are various types of errors, and this one is an AttributeError. A lot of important information is given in the traceback. A traceback is a list of “who called whom”, providing an idea of what was being executed when the error occurred.

The first line of the traceback doesn't give much information. It probably relates to the line we typed at the REPL. The second line tells that the error was in the file blog.py, on line 25, on the method is_public. Now we have the line that raised the problem.

This traceback is simple. In a real application, you would have methods that call methods that call methods and so on. In those cases, it is not uncommon to see tracebacks of 25 lines or more. I've seen tracebacks of more than 150 lines, but those were extreme cases in extreme conditions.

The next step is a modification to the Blog class to pick up only published posts. So, we add a new method:

def get_public_posts(self):
published_posts = []
for post in self._posts:
if port.is_public():
published_posts.append(post)

Python tries to be as readable as possible, but that method introduces too many new things, so it requires some careful explanations.
Loops

One of the Python's looping constructs is for. It is designed to iterate over lists, sets, maps and other iterable objects. In this case, it takes all the items in self._posts and, one by one, assigns them to the variable post. In the body of the for, which is executed on each iteration, we can use the variable post.

The body of the for, as with other constructs that need a piece of code, is delimited by nothing more than the indentation. Here's an example:

/>>> the_list = [1,2,3,"a","b"]
/>>> for item in the_list:
... print item
...
1
2
3
a
b
/>>>

Various tasks are solved with a loop. One such task is doing something for each member of a collection, like we did in the previous example. For those types of tasks, the for construct is excellent.

Another common practice is to perform an action a given number of times—for example, printing “Hello, world” three times. To do that we can use:


/>>> a = 0
/>>> while a < 3:
... print "Hello world"
... a = a + 1
...
Hello world
Hello world
Hello world
/>>>

Another loop construct is while, and it will continue to run its body until the check—that is, the expression after while and before the colon—becomes false.

We can rethink the previous loop as iterating over a list containing the numbers 0–9. There's a way to do it with a for construct:

/>>> for a in range(0,3):
... print "Hello world"
...
Hello world
Hello world
Hello world
/>>>>

This is shorter and arguably more readable. What is while useful for then? It is useful any time you don't really know when you are going to stop the loop. Here are some examples:

*

Reading characters from a file until you encounter the End of File (EOF).
*

Reading commands from a user until the user enters the quit command.
*

Reading temperatures from a sensor until the temperature is too high.
*

Reading events from a user interface until the user presses the X button at the top of the window to close the program.

There's a pattern forming here—doing something until something else happens. That's what while is good for.

Some time ago, when we didn't have as many choices in programming languages and we ended up using C most of the time, the while construct tended to be much more useful than the for construct. But today, with a powerful for construct, nice functions such as range and the possibility of putting an iterator around anything, for is being used much more than while.

Here's one last example for your enjoyment:

/>>> for l in "Hello World":
... print l + " ",
...
H e l l o W o r l d

Conditionals

In the fourth line of some previous sample code, if post.is_public(), we have another new construct—an if. This allows programs to make choices based on data. It needs a boolean value and a piece of code. The code is run only if the boolean is True. If you provide something that is not a boolean, Python does its best to interpret it as a boolean. For example, the number 0 is interpreted as False, but all the other numbers as True. Here are some examples:

/>>> if True:
... print "It is true!"
...
It is true!
/>>> if False:
... print "Is it false?"
...
/>>>

We can perform many different types of comparisons on different kinds of objects. Note that the equality operator is ==, not = (that is, two equal signs):

/>>> a = 10
/>>> if a == 10:
... print "Ten!"
...
Ten!

There are other comparisons, such as greater than (>), less than (<) and different (!=). You can experiment with comparisons directly on the REPL:

/>>> 3 == 4
False
/>>> 10 != 5
True
/>>> 4 >= 1
True

It is common to run a piece of code if something is true and another piece of code if it is false. For example, we could do the following:

if a == 10:
print "A is ten."
if a != 10:
print "A is not ten."

This has a big problem. If we change a to b in the first case, we have to remember to change it in the second. And, the same should be done for any other little changes we do. The solution is an extension to the if construct:

if a == 10:
print "A is ten."
else:
print "A is not ten."

The piece of code after the else will be executed if the first piece wasn't executed.

Another common situation is having various conditionals for different cases. In that case, we use a string of ifs:

if a == 10:
print "A is ten."
elif a == 0:
print "A is zero."
elif a != 30:
print "A is not thirty."
else:
print "Who cares about a ?"

elif is the contraction of “else if”, and indeed, the previous code could be written as:

if a == 10:
print "A is ten."
else:
if a == 0:
print "A is zero."
else:
if a != 30:
print "A is not thirty."
else:
print "Who cares about a ?"

But, that is ugly and prone to errors. If you have 10 or 15 different cases, you'll need a 29"-widescreen monitor just to view it. (Not that I have anything against such a monitor. I'd like to have one.)

If you come from other languages that have a switch or select or case construct and are wondering where they are in Python, I'm sorry to disappoint you. Python doesn't have such constructs. There's a proposal to include them, but it hasn't been implemented yet. Right now, the solution is to use a chain of ifs, elifs and elses. After you use this a few times, it's not so bad.

Now that you know about else, here's an interesting tidbit: for and while also can have elses. What do they do? Run Python, and try it out until you discover for yourself. While programming, you'll need to run a lot of code to find out how many undocumented, obscure, almost black-magic, things work, so starting with something simple will help you get some training.
Inheritance

The short introduction to object-oriented programming (OOP) in Part I of this article left out a big topic—inheritance. This feature is what makes OOP really useful, and as OOP tries to mimic real life, I explain inheritance here with real-life examples.

Think about a chair. A chair is made out of some kind of material, has two armrests, a back, a color, a style and maybe even a warranty. Now, think about a table. It is made out of some kind of material, might have some drawers, a color, a style and maybe a warranty. They have a lot in common! If we were to make the two classes, Chair and Table, a lot of code would be repeated. In programming, when you write the same line of code twice, you probably are doing something wrong—inheritance to the rescue.

A chair is a piece of furniture. So is a table. Such similarities can be in the Furniture class. Let's make the Furniture class have a default material and the ability to set other materials:

class Furniture(object):
def __init__(self):
self._material = "wood"

def set_material(self, material):
self._material = material

And now, a Chair class inheriting Furniture:

class Chair(Furniture):
def __init__(self):
self._backrest_height = 30

def set_backrest_height(self, height):
self._backrest_height = height

Now, you know what goes inside parentheses in the class header: the name of the class being inherited, which also is known as a super class or parent class. Let's play a bit with this, so you can see what happens:

/>>> c = Chair()
/>>> c.set_backrest_height(50)
/>>> c._backrest_height
50
/>>> c.set_material("plastic")
/>>> c._material
'plastic'
/>>>

As you can see, the methods of Furniture also are on Chair. I leave the definition of the Table class as an exercise for the reader. But first, here's another interaction:


/>>> d = Chair()
/>>> d._backrest_height
30
/>>> d._material
Traceback (most recent call last):
File "", line 1, in ?
AttributeError: 'Chair' object has no attribute '_material'
/>>>

I bet that is not what you expected. Let's take a closer look at what happened. We created a Chair, the method Chair.__init__ was run setting _backrest_height. Oh! Nobody called Furniture.__init__, which never set _material. There are two solutions to that.

Setting _material in Chair.__init__ is not a solution. If we do that, the classes would be coupled, meaning the implementation of one will depend on the implementation of the other. If we change the name of _material to _materials, suddenly Chair will stop working. If you have hundreds of classes developed by hundreds of different people, keeping track of those changes is difficult. Also, Furniture will grow to have more members, so we have to remember to set all those members to the same defaults in Chair.__init__. I'm getting a headache just thinking about it.

One real solution is calling Furniture.__init__ and rewriting Chair.__init__ this way:

def __init__(self):
Furniture.__init__(self)
self._backrest_height = 30

We had to pass self to __init__, because if we called it with the class instead of the object, it wouldn't know in which object to do its operations.

I personally don't like that solution, because it implies writing the name of the class in two or more places. If you ever change the name, you'll have to remember to run a search and replace. Another solution is more cryptic than it should be, but it doesn't have the problem I just mentioned:

def __init__(self):
super(Chair, self).__init__()
self._backrest_height = 30

In this solution, I call super, passing the current class and the current object, and it allows me to make a call to the parent class using the current object. Here we may have a problem if we change the name of the class itself, but running a search and replace on the file is a good idea when making that kind of change. You'd want to change the documentation as well. The real problem with this solution is hard to understand and to explain—it has to do with multiple inheritance. For more information, read “Python's Super Considered Harmful”. Personally, I've been using this second solution without any problems.

You'll see that all classes I defined inherit from object. That is the most basic class—the root (or top) class. It is a good idea to make all your classes inherit from it unless they inherit from another class. If you don't do that, your class will be an old-style class, and some things won't work, such as super. It is important to know this, because you may encounter old-style classes anywhere, and you should be prepared.
Python 2.5

During the process of writing this article, with much excitement and fanfare, Python 2.5 was released. It is the most important release in almost two years, and it comes with many promises.

It promises to be more reliable due to improvements in the testing procedures used by the Python development team. It now has Buildbot, a program that continuously builds and tests Python, and whenever there's something wrong, it raises an alarm for all the world to see. The shame of being the developer who made the error will make all the developers more careful—at least, that's what happened to me when I had a Buildbot watching my code.

For some, like this author who had a new release at the worst possible time, the most important thing is that Python 2.5 is backward-compatible. All that you've learned here will work. And, not only will it work, it is still the way to do it.

The new release also promises to be faster and has many new advanced features, including new modules and packages. The future is bright for Python and Python coders.
What Now?

This was nothing but a short introduction to Python; there's still much to learn. A good place to start is the official Python Tutorial. You also can read Dive Into Python, a book that you can buy or read for free on the Web. And, of course, a lot of other books and tutorials are available. I learned Python mainly from the Python Tutorial, which is very good.

Whenever you are creating a program in Python, never, and I repeat, never, do anything without checking whether it has been done before. Python has a lot of features and a lot of built-in libraries. And, if that isn't enough, there are hundreds, maybe thousands of third-party Python libraries. In fact, the huge amount of code that's already written in Python is one of the reasons to use it.

The first stop is Python's Documentation. There we have the previously mentioned tutorial, the library reference and the language reference.

The language reference can be a bit hard to use and understand. Programming languages tend to be difficult to understand and so are their references, which often have exclusive jargon, such as lexical analysis, tokens, identifiers, keywords or delimiters. This piece of documentation can be particularly useful in showing how to use language constructs, such as for, if, while and more complex ones that I haven't mentioned, such as yield, break or continue.

The library references let us know about all the classes, methods and functions that Python already provides. It is so important and useful that I always have it open when I am programming on Python. In the second chapter, you can read about the built-in functions and classes. Getting familiar with them is always useful. The rest of the documentation is very specific, and each chapter deals with subjects ranging from runtime internals to string, from the Python debugger to some generic operating systems services. In that chapter, a very important module is documented: os. I can't remember making a single program that didn't use that module.

Finding what you want in so much documentation can be a difficult task. A trick that I find very useful is to use Google to search in a specific site. That is achieved by adding “site:python.org” or “site:docs.python.org” to the search query. The first one is more generic and sometimes leads to countless mailing-list posts that have nothing to do with what you are looking for. In that situation, use the second one. To give it a try, search for “print site:python.org” or “options site:python.org”.

What if all of your searches return nothing? Then, you need to do a broader search to find some third-party libraries or frameworks. If you want to make a graphical user interface, I recommend both PyGTK and PyQt, both are very good and include support for their respective desktops, GNOME and KDE. I've heard good opinions of wxPython, but I've not used it myself.

If you want to build a Web application, I see two paths. If you want something not so spectacular but that gets you there fast, I recommend Django. Django is very similar to Ruby on Rails. It's a framework in which you use the model-view-controller paradigm and a relational database such as MySQL or PostgreSQL; both are well supported on Python.

The other way to build Web sites (that I know of) is Zope. Zope is a big framework with a Web server and object-oriented database. The database is different from other relational databases, and it is very powerful. It allows you to store information in a much more flexible way. Zope 3—I don't recommend the previous versions unless you have to use the award-winning content management system Plone—is prepared to help you build reliable and robust code by means of interfaces, unit testing, adapters and much more.

If you need to build any kind of dæmon—those little applications running in the background making the earth turn—take a look at Twisted Matrix. Twisted Matrix is an event-based framework that solves a lot of the common problems of building dæmons, including separation of protocol and logic. It comes with many protocols already built in, and it allows you to create new protocols. A proof of its usefulness is that Zope, after years of shipping its own Web sever, has migrated to using the Twisted Matrix HTTP server.

Resources

Python Tutorial: docs.python.org/tut/tut.html

Dive Into Python: www.diveintopython.org

Python Documentation: www.python.org/doc

PyGTK: www.pygtk.org

PyQt: www.riverbankcomputing.co.uk/pyqt

Django: www.djangoproject.com

Zope: zope.org

Python's Super Considered Harmful: fuhm.net/super-harmful

José P. E. “Pupeno” Fernández has been programming since...at what age is a child capable of siting in a chair and reaching a keyboard? He has experimented with more languages than can be listed on this page. His Web site is at pupeno.com, and he always can be reached, unless you are a spammer, at pupeno@pupeno.com.

Programming Python, Part I

Programming Python, Part I
June 1st, 2007 by José P. E. "Pupeno" Fernandez in

* Software

This tutorial jumps right in to the power of Python without dragging you through basic programming.

Python is a programming language that is highly regarded for its simplicity and ease of use. It often is recommended to programming newcomers as a good starting point. Python also is a program that interprets programs written in Python. There are other implementations of Python, such as Jython (in Java), CLPython (Common Lisp), IronPython (.NET) and possibly more. Here, we use only Python.
Installing Python

Installing Python and getting it running is the first step. These days, it should be very easy. If you are running Gentoo GNU/Linux, you already have Python 2.4 installed. The packaging system for Gentoo, Portage, is written in Python. If you don't have it, your installation is broken.

If you are running Debian GNU/Linux, Ubuntu, Kubuntu or MEPIS, simply run the following (or log in as root and leave out sudo):

sudo apt-get install python

One catch is that Debian's stable Python is 2.3, while for the rest of the distributions, you are likely to find 2.4. They are not very different, and most code will run on both versions. The main differences I have encountered are in the API of some library classes, new features added to 2.4 and some internals, which shouldn't concern us here.

If you are running some other distribution, it is very likely that Python is prepackaged for it. Use the usual resources and tools you use for other packages to find the Python package.

If all that fails, you need to do a manual installation. It is not difficult, but be aware that it is easy to break your system unless you follow this simple guideline: install Python into a well-isolated place, I like /opt/python/2.4.3, or whatever version it is.

To perform the installation, download Python, unpack it, and run the following commands:

./configure --prefix=/opt/python2.4/
make
make install

This task is well documented on Python's README, which is included in the downloaded tarball; take a look at it for further details. The only missing task here is adding Python to your path. Alternatively, you can run it directly by calling it with its path, which I recommend for initial exploration.
First Steps

Now that we have Python running, let's jump right in to programming and examine the language as we go along. To start, let's build a blog engine. By engine, I mean that it won't have any kind of interface, such as a Web interface, but it's a good exercise anyway.

Python comes with an REPL—a nice invention courtesy of the Lisp community. REPL stands for Read Eval Print Loop, and it means there's a program that can read expressions and statements, evaluate them, print the result and wait for more. Let's run the REPL (adjust your path according to where you installed Python in the previous section):

$ python
Python 2.4.3 (#1, Sep 1 2006, 18:35:05)
[GCC 4.1.1 (Gentoo 4.1.1)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
/>>>

Those three greater-than signs (>>>) are the Python prompt where you write statements and expressions. To quit Python, press Ctrl-D.

Let's type some simple expressions:

/>>> 5
5

The value of 5 is, well, 5.

/>>> 10 + 4
14

That's more interesting, isn't it?

There are other kinds of expressions, such as a string:

/>>> "Hello"
'Hello'

Quotes are used to create strings. Single or double quotes are treated essentially the same. In fact, you can see that I used double quotes, and Python showed the strings in single quotes.

Another kind of expression is a list:

/>>> [1,3,2]
[1, 3, 2]

Square brackets are used to create lists in which items are separated by commas. And, as we can add numbers, we can add—actually concatenate—lists:

/>>> [1,3,2] + [11,3,2]
[1, 3, 2, 11, 3, 2]

By now, you might be getting bored. Let's switch to something more exciting—a blog. A blog is a sequence of posts, and a Python list is a good way to represent a blog, with posts as strings. In the REPL, we can build a simple blog like this:

/>>> ["My first post", "Python is cool"]
['My first post', 'Python is cool']
/>>>

That's a list of strings. You can make lists of whatever you want, including a list of lists. So far, all our expressions are evaluated, shown and lost. We have no way to recall our blog to add more items or to show them in a browser. Assignment comes to the rescue:

/>>> blog = ["My first post", "Python is cool"]
/>>>

Now blog, a so-called variable, contains the list. Unlike in the previous example, nothing was printed this time, because it is an assignment. Assignments are statements, and statements don't have a return value. Simply evaluating the variable shows us the content:

/>>> blog
['My first post', 'Python is cool']

Accessing our blog is easy. We simply identify each post by number:

/>>> blog[0]
'My first post'
/>>> blog[1]
'Python is cool'

Be aware that Python starts counting at 0.
Encapsulating Behavior

A blog is not a blog if we can't add new posts, so let's do that:

/>>> blog = blog + ["A new post."]
/>>> blog
['My first post', 'Python is cool', 'A new post.']

Here we set blog to a new value, which is the old blog, and a new post. Remembering all that merely to add a new post is not pleasant though, so we can encapsulate it in what is called a function:

/>>> def add_post(blog, new_post):
... return blog + [new_post]
...
/>>>

def is the keyword used to define a new function or method (more on functions in structured or functional programming and methods in object-oriented programming later in this article). What follows is the name of the function. Inside the parentheses, we have the formal parameters. Those are like variables that will be defined by the caller of the function. After the colon, the prompt has changed from >>> to ... to show that we are inside a definition. The function is composed of all those lines with a level of indentation below the level of the def line.

So, where other programming languages use curly braces or begin/end keywords, Python uses indentation. The idea is that if you are a good programmer, you'd indent it anyway, so we'll use that indentation and make you a good programmer at the same time. Indeed, it's a controversial issue; I didn't like it at first, but I learned to live with it.

While working with the REPL, you safely can press Tab to make an indentation level, and although a Tab character can do it, using four spaces is the strongly recommended way. Many text editors know to put four spaces when you press Tab when editing a Python file. Whatever you do, never, I repeat, never, mix Tabs with spaces. In other programming languages, it may make the community dislike you, but in Python, it'll make your program fail with weird error messages.

Being practical, to reproduce what I did, simply type the class header, def add_post(blog, new_post):, press Enter, press Tab, type return blog + [new_post], press Enter, press Enter again, and that's it. Let's see the function in action:

/>>> blog = add_post(blog, "Fourth post")
/>>> blog
['My first post', 'Python is cool', 'A new post.',
'Fourth post']
/>>>

add_post takes two parameters. The first is the blog itself, and it gets assigned to blog. This is tricky. The blog inside the function is not the same as the blog outside the function. They are in different scopes. That's why the following:

/>>> def add_post(blog, new_post):
... blog = blog + [new_post]

doesn't work. blog is modified only inside the function. By now, you might know that new_post contains the post passed to the function.

Our blog is growing, and it is time to see that the posts are simply strings, but we want to have a title and a body. One way to do this is to use tuples, like this:

/>>> blog = []
/>>> blog = add_post(blog, ("New blog", "First post"))
/>>> blog = add_post(blog, ("Cool", "Python is cool"))
/>>> blog
[('New blog', 'First post'),
('Cool', 'Python and is cool')]
/>>>

In the first line, I reset the blog to be an empty list. Then, I added two posts. See the double parentheses? The outside parentheses are part of the function call, and the inside parentheses are the creation of a tuple.

A tuple is created by parentheses, and its members are separated by commas. They are similar to lists, but semantically, they are different. For example, you can't update the members of a tuple. Tuples are used to build some kind of structure with a fixed set of elements. Let's see a tuple outside of our blog:

/>>> (1,2,3)
(1, 2, 3)

Accessing each part of the posts is similar to accessing each part of the blog:

/>>> blog[0][0]
'New blog'
/>>> blog[0][1]
'This is my first post'

This might be a good solution if we want to store only a title and a body. But, how long until we want to add the date and time, excerpts, tags or messages? You may begin thinking you'll need to hang a sheet of paper on the wall, as shown in Figure 1, to remember the index of each field—not pleasant at all. To solve this problem, and some others, Python gives us object-oriented programming.

Figure 1. Index Handling the Hard Way
Object-Oriented Programming

Object-oriented programming was born more than 20 years ago so developers could separate each part of a computer program in a way similar to how objects are separated in the real world. Python models objects by using classes. A class is an abstract definition of what an object has and what an object can do. If this sounds foreign, don't worry, OOP (object-oriented programming) is difficult at first.

An example might help. A bridge is a structure that allows people or vehicles to cross an obstacle, such as a river, canal or railway. A bridge has some length, some width and even some color. It may allow vehicles or only persons. It may allow heavy vehicles or not. When I say “bridge”, I am not defining any of those details. Bridge is a class. If I say Golden Gate, Le Pont de Normandie or Akashi-Kaikyo, I am naming particular bridges; they have some specific length, width, vehicle allowance and color. In OOP jargon, they are instances of bridge.

Back to our blog, let's create a class to model our post:

/>>> class Post(object):
... pass
...
/>>>

We start with class, the keyword for creating new classes. Next comes the name of the class—in this case, Post. In parentheses, we have the super-classes—ignore that for now.

Here again, the prompt has changed from >>> to ..., and Python expects something in a class. Because we don't want to put anything in yet, we write pass, which is something, but in fact, it is nothing. Python knows when a class starts and ends because of the indentation, the same as with functions.

To reproduce what I did, simply type the class header, class Post(object):, press Enter, press Tab, type pass, press Enter, press Enter again, and that's it.

Now, we can create a Post:


/>>> cool = Post()
/>>> cool
<__main__.Post object at 0xb7ca642c>

Note that what is being printed when we evaluate a post is a generic representation for the object. We can set its title and body:

/>>> cool.title = "Cool"
/>>> cool.body = "Python is cool."

And retrieve them:

/>>> cool.title
'Cool'
/>>> cool.body
'Python is cool.'

Up to this point, a Post is like a simple container for anything you can imagine putting there. The problem with this is we may get lost as to what to put in it, or what not to put in it. Back to a sheet of paper? No! Although we can't stop making the posts a container in that way, we can put some methods there, so users have an idea of what a post may contain. To do this, we write our own methods in the class Post:

/>>> class Post(object):
... def set_title(self, title):
... self._title = title
... def get_title(self):
... return self._title
...
/>>>

Methods are like functions, but as they are in a class, they are associated with the objects of the class. This means different classes can have different methods with the same name. Just imagine the difference between bat.hit(ball) and stick.hit(drum).

Python has a convention that the first parameter (normally called self) to a method is the object on which we are calling the method. That means running cool.set_title("Cool")will set self to be cool, and title to be "Cool". Running:

cool.set_title("Cool")

is the equivalent of:

cool._title = "Cool"

The leading underscore lets others know that we don't want them playing with it. It means “don't access _title; use get_title and set_title”.

The previous interaction with the cool object can be rewritten as:

/>>> cool = Post()
/>>> cool.set_title("Cool")
/>>> cool.set_body("Python is cool.")
/>>> cool.get_title()
'Cool'
/>>> cool.get_body()
'Python is cool.'

Writing the same set of methods for body should be easy now. But, be aware that you have to write the whole class in one go. Write the class header, the set_title and get_title methods, and then create your set_body and get_body methods. It may take you a couple of tries.
Files

As the Post class becomes bigger, you'll get tired of rewriting it every time you want to add a method. If you're tired already, that's a good sign. And besides, all that's in the REPL will be lost when we quit Python. We should start saving our work in files.

Python modules are simple text files, and you can use any text editor you want. As a programmer, you are going to spend most of your time with your editor, so take some time to choose one you really like and learn to use it well.

Emacs might not be the most beautiful editor, but for many programming tasks, it is awesome. (You could read that as “I don't like Emacs but it makes my life so much easier that I keep coming to it time after time”.) Installing Emacs from source is beyond the scope of this article. As usual, with programs that are so popular, your distribution is likely to provide it. In Debian and its derivatives try:

apt-get install emacs

For Gentoo, the counterpart is:

emerge emacs

To achieve the magic I am going to show here, you need python-mode.

In Debian:

apt-get install python-mode

In Gentoo:

emerge python-mode

Run Emacs. If you are serious about learning how to use it, now it is time to press Ctrl-H T, which in Emacs jargon means press Ctrl-H, release it and then press T. But, you can leave that for later, when you've finished reading this Linux Journal issue. For this article, I provide all the keystrokes you need.

Press Ctrl-X Ctrl-F (Ctrl-X, release, Ctrl-F) to visit a file. On the bottom of the Emacs window, you'll see the cursor waiting for you to type the path and filename. Type blog.py and press Enter. (Python modules should have the extension .py.) Now, you can start typing the Post class we programmed before. Emacs tries to be smart about indentation and places it where you are likely to want it. If you need a different indentation, simply press Tab and keep pressing it until you get the desired results.

On the top, you should have two menus: IM-Python and Python. The first one contains a list of classes and methods in the file you are editing. Click on Rescan if it doesn't show information you know is there. This is very useful when working with huge files. The second menu is even more useful, but explore and play with it later. For now, simply run Start interpreter... or press Ctrl-C !.

Suddenly the window is split, and you have an embedded Python interpreter below the file you are editing (Figure 2). And the fun is only beginning. Click on the file you are editing to set the focus on it. Run Import/reload file from the Python menu or press Ctrl-C Enter. Now, you're ready to test your code on the REPL, but be aware that you'll have to add blog. before the name of the class, Post, because now the class is in the module blog. See Figure 2 for further reference.

Figure 2. Testing the REPL

You can, of course, do the same without Emacs. But for that, you need to learn how Python modules and packages are made. Set PYTHON_PATH, an environment variable, accordingly, and use the built-in function reload. With Emacs, you'll find iterating between coding and testing the code to be very fast. This speed can improve your performance and make programming more fun. In fact, Lisp programmers have been advocating this way of working for more than two decades.
Special Methods

Having to create an object and then set each of its members is not pleasant. It takes a lot of lines and is very error-prone—did I remember to set the tags? There's a better way to do it—using the initialization method.

This special method is called __init__, and the parameters you define it to take have to be passed in the creation of the object. A possible initialization method would be:

class Post(object):
def __init__(self, title, body):
self.set_title(title)
self.set_body(body)

Simply add the __init__ definition to the file and reload it. We now can, and have to, set the title and body at initialization time:

/>>> cool = blog.Post("Cool", "Python is cool")
/>>> cool.get_title()
'Cool'
/>>> cool.get_body()
'Python is cool'
/>>>

Hint: to retrieve previous lines in the REPL inside Emacs use Alt-P.

There are other special methods. Remember how ugly it was to evaluate a Post itself? Let me remind you:


/>>> cool


We can solve that. There's another special method called __repr__, which is used to retrieve that string. Inside the Post class add:

def __repr__(self):
return "Blog Post: %s" % self.get_title()

Reload the file, the same way you loaded it previously, and evaluate a post:


/>>> ## working on region in file /usr/tmp/python...
/>>> cool

/>>>

Oops! That's not what we wanted. The problem here is that the cool object was created with an older version of the Post class, so it doesn't have the new method. That is a very common mistake, and not being prepared for it can cause a lot of headaches. But, simply re-create the object, and you are set:

/>>> ## working on region in file /usr/tmp/python...
/>>> cool = blog.Post("Cool", "Python is cool")
/>>> cool
Blog Post: Cool
/>>>

That's better.
What Now?

Easy—wait for the next issue of Linux Journal for Part II of this tutorial. If you really want something to do now, start learning Emacs.

Resources

Python: python.org

Python Download: python.org/download

Python 2.4.3: www.python.org/ftp/python/2.4.3/Python-2.4.3.tgz

José P. E. “Pupeno” Fern´ndez has been programming since...at what age is a child capable of siting in a chair and reaching a keyboard? He has experimented with more languages than can be listed on this page. His Web site is at pupeno.com, and he always can be reached, unless you are a spammer, at pupeno@pupeno.com.