Unity Window Quicklists

The Unity desktop that the recent releases of Ubuntu uses is kind of nice overall, but there are some specific things that are really hard to do, one of which is finding and focussing on one window of a particular type of application when you may have many other windows of that application you are not interested in. For example, a lot of the time I have three browser windows, a few gedit editors and around ten terminal windows open. When I am doing something I am normally interested in one browser window, one editor and one terminal. If I want to move from the browser to the terminal I am interested in I don’t want the other 9 terminals popping up over the browser, I only want the one that relates to that browser.

What kind of makes sense to me is that you should be able to right click on the things in the launcher and see the list of windows and choose the one you want. Luckily Unity is quite extensible, there are APIs for adding quicklists to the launcher icons and there is enough information kicking about in dbus to find the window names and get callbacks to happen when things get updated like a window title changes or a window gets added or removed.

I put my thoughts together in a little python script, which I have now packaged and put in a PPA (which was harder than it sounds) so if the screenshot makes sense to you and you are running Ubuntu with Unity (2d or 3d) then you can install it with the following commands:

sudo apt-add-repository ppa:alanbell/unity
sudo apt-get update
sudo apt-get install unity-window-quicklists

Then log out and back in again to get a much more usable desktop if you tend to use lots of windows

How to: OpenERP 6.1, Ubuntu 10.04 LTS, nginx SSL Reverse Proxy

This article follows on (hopefully not unsurprisingly) from the basic 6.1 installation howto.

In this post I’ll describe one way of providing SSL encrypted access to your shiny new OpenERP 6.1 server running on Ubuntu 10.04 LTS.

This time I thought I’d use the nginx (pronounced like “Engine X”) webserver to act as a reverse proxy and do SSL termination for web, GTK client and WebDAV/CalDAV access. nginx is gaining in popularity and is now the second most popular web server in the world according to some figures. It has a reputation for being fast and lean – so it seemed like a good choice for a relatively simple job like this.

I’m indebted to xat for this post which provided the main configuration script for a reverse proxy on OpenERP 6.0. The changes I have made to xat’s original configuration are: different port number, some additional rewrite rules to support WebDAV and the new mobile interface, new location for static files.

NB: For the purposes of this how to, we’ll be using self-signed certificates. A discussion of the pros and cons of this choice is beyond the scope of this article.

Step 1. Install nginx

On your server install nginx by typing:

sudo apt-get install nginx

Next, we need to generate a SSL certificate and key.

Step 2. Create your cert and key

I create the files in a temporary directory then move them to their final resting place once they have been built (the first cd is just to make sure we are in our home directory to start with):


cd
mkdir temp
cd temp

Then we generate a new key, you will be asked to enter a passphrase and confirm:

openssl genrsa -des3 -out server.pkey 1024

We don’t really want to have to enter a passphrase every time the server starts up so we remove the passphrase by doing this:

openssl rsa -in server.pkey -out server.key

Next we need to create a signing request which will hold the data that will be visible in your final certificate:

openssl req -new -key server.key -out server.csr

This will generate a series of prompts like this: Enter the information as requested:

You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:
Email Address []:

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:The Client’s Company

And finally we self-sign our certificate.

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

We only need two of the files in the working directory, the key and the certificate. But before we can use them they need to have their ownership and access rights altered:


sudo chown root:www-data server.crt server.key
sudo chmod 640 server.crt server.key

And then we put them in a sensible place:


sudo mkdir /etc/ssl/nginx
sudo chown www-data:root /etc/ssl/nginx
sudo chmod 710 /etc/ssl/nginx
sudo mv server.crt server.key /etc/ssl/nginx/

Now the key and certificate are safely stored away, we can tell nginx where they are and what it should be doing…

Step 3. Create the nginx site configuration file

We create a new configuration file

sudo nano /etc/nginx/sites-available/openerp

with the following content:

Note: You will need to change all references to 10.0.0.26 in the following file to either the domain name or static IP address of your server. This was the IP address of the machine I built this test script on. It will not work unless changed to suit your own system!


upstream openerpweb {
    server 127.0.0.1:8069 weight=1 fail_timeout=300s;
}

server {
    listen 80;
    server_name    10.0.0.26;

    # Strict Transport Security
    add_header Strict-Transport-Security max-age=2592000;

    rewrite ^/mobile.*$ https://10.0.0.26/web_mobile/static/src/web_mobile.html permanent;
    rewrite ^/webdav(.*)$ https://10.0.0.26/webdav/$1 permanent;
    rewrite ^/.*$ https://10.0.0.26/web/webclient/home permanent;
}

server {
    # server port and name
    listen        443 default;
    server_name   10.0.0.26;

    # Specifies the maximum accepted body size of a client request, 
    # as indicated by the request header Content-Length. 
    client_max_body_size 200m;

    # ssl log files
    access_log    /var/log/nginx/openerp-access.log;
    error_log    /var/log/nginx/openerp-error.log;

    # ssl certificate files
    ssl on;
    ssl_certificate        /etc/ssl/nginx/server.crt;
    ssl_certificate_key    /etc/ssl/nginx/server.key;

    # add ssl specific settings
    keepalive_timeout    60;

    # limit ciphers
    ssl_ciphers            HIGH:!ADH:!MD5;
    ssl_protocols            SSLv3 TLSv1;
    ssl_prefer_server_ciphers    on;

    # increase proxy buffer to handle some OpenERP web requests
    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    location / {
        proxy_pass    http://openerpweb;
        # force timeouts if the backend dies
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;

        # set headers
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
       
        # Let the OpenERP web service know that we're using HTTPS, otherwise
        # it will generate URL using http:// and not https://
        proxy_set_header X-Forwarded-Proto https;

        # by default, do not forward anything
        proxy_redirect off;
    }

    # cache some static data in memory for 60mins.
    # under heavy load this should relieve stress on the OpenERP web interface a bit.
    location ~* /web/static/ {
        proxy_cache_valid 200 60m;
        proxy_buffering    on;
        expires 864000;
        proxy_pass http://openerpweb;
    }

}

UPDATE: 04/04/2012. I have added a line to the above file: client_max_body_size 200m; thanks to Praxi for reminding me about this. The default setting is just 1MB which will stop users from uploading any files larger than that, including databases!

And then we can enable the new site configuration by creating a symbolic link in the /etc/nginx/sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/openerp /etc/nginx/sites-enabled/openerp

Step 4. Change the OpenERP server configuration file

The next step is to re-configure the OpenERP server so that non-encrypted services are not accessible from the outside world.

In /etc/openerp-server.conf the non-encrypted services will only listen on localhost, i.e. not from external connections so in effect only traffic from nginx will be accepted.

After opening the file for editing, just add 127.0.0.1 to the xmlrpc and netrpc interface lines as shown below.

sudo nano /etc/openerp-server.conf


xmlrpc_interface = 127.0.0.1
netrpc_interface = 127.0.0.1

That’s it. Everything is now configured.

Step 5. Try it out

Restart the services to load the new configurations


sudo service openerp-server restart
sudo service nginx restart

You should not be able to connect to the web client on port 8069 and the GTK client should not connect on either the NetRPC (8070) or XMLRPC (8069) services.

For web access you just need to visit https://your-ip-or-domain and in the GTK client you will need to use port 443 (https) and choose the XMLRPC (Secure) protocol.

The nginx configuration above will also redirect any incoming requests for port 80 to port 443 (https) and it also makes sensible redirects for the mobile and WebDAV/CalDAV services. (From what I can gather however WebDAV clients really don’t handle redirects so this bit is probably not that useful). I think the best bet for WebDAV/CalDAV is just to provide the correct URL in the first place.

For CalDAV access then, the URL to a calendar will be something like this:

https://your-ip-or-domain/webdav/DB_NAME/calendars/users/USERNAME/c/CALENDAR_NAME

There you have it. In OpenERP 6.1 this job actually proved to be a little simpler than the previous version largely due to the integrated web interface. There are also fewer configuration changes required in openerp-server.conf.

Finally, I really wanted to try and make use of the WSGI support in OpenERP 6.1 instead of the method above, but my efforts to get this to work from nginx or Apache have so far ended in failure 🙁 Obviously if anyone wants to provide a working config for that please feel free to add a comment and link.

How to install OpenERP 6.1 on Ubuntu 10.04 LTS

OpenERP LogoThe new release of OpenERP 6.1 heralds a great many incremental improvements in the product plus a complete re-write of the web interface; which is a massive improvement and much more an integral part of OpenERP than it’s predecessor.

UPDATE: By popular request here is a subsequent post describing how to set up a reverse proxy and ssl using nginx.

As my previous howto for 6.0 was a such roaring success I thought I’d better do something for the new 6.1 release too.

Before continuing, I should mention that you can simply download a “.deb” package of OpenERP 6.1 and install that on Ubuntu. But that doesn’t provide me with enough fine grained control over what and where things get installed and it restricts our flexibility to modify & customise hence I prefer to do it a slightly more manual way… (It should be said though, that this install process should only take about 10-15 minutes once the host machine has been built)

So without further ado here we go:

Step 1. Build your server

I install just the bare minimum from the install routine (you can install the openssh-server during the install procedure or install subsequently depending on your preference).

After the server has restarted for the first time I install the openssh-server package (so we can connect to it remotely) and denyhosts to add a degree of brute-force attack protection. There are other protection applications available: I’m not saying this one is the best, but it’s one that works and is easy to configure and manage. If you don’t already, it’s also worth looking at setting up key-based ssh access, rather than relying on passwords. This can also help to limit the potential of brute-force attacks. [NB: This isn’t a How To on securing your server…]

sudo apt-get install openssh-server denyhosts

Now make sure you are running all the latest patches by doing an update:

sudo apt-get update
sudo apt-get dist-upgrade

Although not always essential it’s probably a good idea to reboot your server now and make sure it all comes back up and you can login via ssh.

Now we’re ready to start the OpenERP install.

Step 2. Create the OpenERP user that will own and run the application

sudo adduser --system --home=/opt/openerp --group openerp

This is a “system” user. It is there to own and run the application, it isn’t supposed to be a person type user with a login etc. In Ubuntu, a system user gets a UID below 1000, has no shell (it’s actually /bin/false) and has logins disabled. Note that I’ve specified a “home” of /opt/openerp, this is where the OpenERP server code will reside and is created automatically by the command above. The location of the server code is your choice of course, but be aware that some of the instructions and configuration files below may need to be altered if you decide to install to a different location.

A question I was asked a few times in the previous how to for 6.0 was how to run the OpenERP server as the openerp system user from the command line if it has no shell. This can be done quite easily:

sudo su - openerp -s /bin/bash

This will su your current terminal login to the openerp user (the “-” between su and openerp is correct) and use the shell /bin/bash. When this command is run you will be in openerp’s home directory: /opt/openerp.

When you have done what you need you can leave the openerp user’s shell by typing exit.

Step 3. Install and configure the database server, PostgreSQL

sudo apt-get install postgresql

Then configure the OpenERP user on postgres:

First change to the postgres user so we have the necessary privileges to configure the database.

sudo su - postgres

Now create a new database user. This is so OpenERP has access rights to connect to PostgreSQL and to create and drop databases. Remember what your choice of password is here; you will need it later on:

createuser --createdb --username postgres --no-createrole --no-superuser --pwprompt openerp
Enter password for new role: ********
Enter it again: ********

Finally exit from the postgres user account:

exit

Step 4. Install the necessary Python libraries for the server

Update 27/02/2012: Many thanks to Gavin for reporting. Have added python-simplejson to the package list.

sudo apt-get install python-dateutil python-feedparser python-gdata \
python-ldap python-libxslt1 python-lxml python-mako python-openid python-psycopg2 \
python-pybabel python-pychart python-pydot python-pyparsing python-reportlab \
python-simplejson python-tz python-vatnumber python-vobject python-webdav \
python-werkzeug python-xlwt python-yaml python-zsi

From what I can tell, on Ubuntu 10.04 the package python-werkzeug is too old and this will cause the server to not start properly. If you are trying this on a later version of Ubuntu then you might be OK, but just in-case you can also do the following.

I found it necessary to install a more recent version of Werkzeug using Python’s own package management library PIP. The python pip tool can be installed like this:

sudo apt-get install python-pip

Then remove Ubuntu’s packaged version of werkzeug:

sudo apt-get remove python-werkzeug

Then install the up-to-date version of werkzeug:

sudo pip install werkzeug

With that done, all the dependencies for installing OpenERP 6.1 are now satisfied, including for the new integral web interface.

Step 5. Install the OpenERP server

I tend to use wget for this sort of thing and I download the files to my home directory.

Make sure you get the latest version of the application. At the time of writing this it’s 6.1-1; I got the download links from their download page.

wget http://nightly.openerp.com/6.1/releases/openerp-6.1-1.tar.gz

Now install the code where we need it: cd to the /opt/openerp/ directory and extract the tarball there.

cd /opt/openerp
sudo tar xvf ~/openerp-6.1-1.tar.gz

Next we need to change the ownership of all the the files to the OpenERP user and group.

sudo chown -R openerp: *

And finally, the way I have done this is to copy the server directory to something with a simpler name so that the configuration files and boot scripts don’t need constant editing (I called it, rather unimaginatively, server). I started out using a symlink solution, but I found that when it comes to upgrading, it seems to make more sense to me to just keep a copy of the files in place and then overwrite them with the new code. This way you keep any custom or user-installed modules and reports etc. all in the right place.

sudo cp -a openerp-6.1-1 server

As an example, should OpenERP 6.1-2 come out soon, I can extract the tarballs into /opt/openerp/ as above. I can do any testing I need, then repeat the copy command so that the modified files will overwrite as needed and any custom modules, report templates and such will be retained. Once satisfied the upgrade is stable, the older 6.1-1 directories can be removed if wanted.

That’s the OpenERP server software installed. The last steps to a working system is to set up the configuration file and associated boot script so OpenERP starts and stops automatically when the server itself stops and starts.

Step 6. Configuring the OpenERP application

The default configuration file for the server (in /opt/openerp/server/install/) is actually very minimal and will, with only one small change work fine so we’ll simply copy that file to where we need it and change it’s ownership and permissions:

sudo cp /opt/openerp/server/install/openerp-server.conf /etc/
sudo chown openerp: /etc/openerp-server.conf
sudo chmod 640 /etc/openerp-server.conf

The above commands make the file owned and writeable only by the openerp user and group and only readable by openerp and root.

To allow the OpenERP server to run initially, you should only need to change one line in this file. Toward to the top of the file change the line db_password = False to the same password you used back in step 3. Use your favourite text editor here. I tend to use nano, e.g.

sudo nano /etc/openerp-server.conf

One other line we might as well add to the configuration file now, is to tell OpenERP where to write its log file. To complement my suggested location below add the following line to the openerp-server.conf file:

logfile = /var/log/openerp/openerp-server.log

Once the configuration file is edited and saved, you can start the server just to check if it actually runs.

sudo su - openerp -s /bin/bash
/opt/openerp/server/openerp-server

If you end up with a few lines eventually saying OpenERP is running and waiting for connections then you are all set. Just type CTL+C to stop the server then exit to leave the openerp user’s shell.

If there are errors, you’ll need to go back and check where the problem is.

Step 7. Installing the boot script

For the final step we need to install a script which will be used to start-up and shut down the server automatically and also run the application as the correct user. There is a script you can use in /opt/openerp/server/install/openerp-server.init but this will need a few small modifications to work with the system installed the way I have described above. Here’s a link to the one I’ve already modified for 6.1-1.

Similar to the configuration file, you need to either copy it or paste the contents of this script to a file in /etc/init.d/ and call it openerp-server. Once it is in the right place you will need to make it executable and owned by root:

sudo chmod 755 /etc/init.d/openerp-server
sudo chown root: /etc/init.d/openerp-server

In the configuration file there’s an entry for the server’s log file. We need to create that directory first so that the server has somewhere to log to and also we must make it writeable by the openerp user:

sudo mkdir /var/log/openerp
sudo chown openerp:root /var/log/openerp

Step 8. Testing the server

To start the OpenERP server type:

sudo /etc/init.d/openerp-server start

You should now be able to view the logfile and see that the server has started.

less /var/log/openerp/openerp-server.log

If there are any problems starting the server you need to go back and check. There’s really no point ploughing on if the server doesn’t start…

OpenERP 6.1 Home Screen

OpenERP 6.1 Home Screen


If the log file looks OK, now point your web browser at the domain or IP address of your OpenERP server (or localhost if you are on the same machine) and use port 8069. The url will look something like this:

http://IP_or_domain.com:8069

What you should see is a screen like this one:

What I do recommend you do at this point is to change the super admin password to something nice and strong (Click the “Manage Databases” link below the main Login box). By default this password is just “admin” and knowing that, a user can create, backup, restore and drop databases! This password is stored in plain text in the /etc/openerp-server.conf file; hence why we restricted access to just openerp and root. When you change and save the new password the /etc/openerp-server.conf file will be re-written and will have a lot more options in it.

Now it’s time to make sure the server stops properly too:

sudo /etc/init.d/openerp-server stop

Check the logfile again to make sure it has stopped and/or look at your server’s process list.

Step 9. Automating OpenERP startup and shutdown

If everything above seems to be working OK, the final step is make the script start and stop automatically with the Ubuntu Server. To do this type:

sudo update-rc.d openerp-server defaults

You can now try rebooting you server if you like. OpenERP should be running by the time you log back in.

If you type ps aux | grep openerp you should see a line similar to this:

openerp 1491 0.1 10.6 207132 53596 ? Sl 22:23 0:02 python /opt/openerp/server/openerp-server -c /etc/openerp-server.conf

Which shows that the server is running. And of course you can check the logfile or visit the server from your web browser too.

That’s it!

OpenERP 6.1 really is a major step up in terms of improvements from 6.0 and the new integrated web interface (with a Point of Sale and a Mobile interface built-in) are really very cool. Performance has improved considerably and the way the new web service interfaces to OpenERP is very different. So, if I get the time, the next instalment of these posts will go into a bit of detail about how this works and some alternative ways to provide more secure access, such as reverse proxy.

Free Ubuntu 11.10 CDs for the UK

Today the Ubuntu UK LoCo team CD allocation arrived from Canonical, as is traditional I have upgraded the Kubuntu CD that my chickens peck at. I have a different set of chickens to the ones in the last photo due to a series of unfortunate events. In July we restocked by purchasing three rather young hens, Specky, Chocolate and Snowdrop who entertained the crowds at the Oggcamp Crew BBQ and have been growing fast ever since. Pictured here with the new Kubuntu CD is Specky. We wanted three nice new hens to lay lots of eggs and they are approaching egg time. Of the three, Specky has grown the fastest and has a nice comb and a tail and really is quite a lot bigger than the others and . . . actually Specky is looking a bit um, butch, for a hen. . . and those cockadoodledoo noises are not a good sign. I think we may have a problem here. Girls are lovely and useful and hard working. Boys are big, noisy and useless. I think Specky needs to urgently get in touch with it’s feminine side and lay some eggs, or there will be a less full henhouse and a more full curry pot.

Anyhow, back to the subject of this post, which is the CDs. If you are living in the UK and want an Ubuntu CD, a Kubuntu CD or an Ubuntu Server CD (or combination thereof) then you are most welcome to one. Please follow the procedure and I will send one out to you. For a while I will also include an 11.04 CD until I run out of them. If you want more than one CD then do ask me and we will try and work something out particularly if you are wanting to distribute them at a University or similar.

I almost forgot to mention, I removed their old Kubuntu 11.04 CD and washed the muck off and stuck it in a desktop, it still boots!

Hello World in Python

Today I ran a class session as part of the Ubuntu Application Developer week. My topic was a really basic introduction to python, starting with the traditional “Hello World!” program and here is the transcript from the logs:

[19:01] good morning/afternoon/evening all
[19:01] welcome to this Application Developer week session on Python
[19:02] so after jderose turning things up to 11 we are going to go back and start with 1 2 3
[19:02] This session is an introduction to Python from the very very beginning, I going to do my best to assume no prior knowledge at all.
[19:02] just so I can see who is here say hi in the #ubuntu-classroom-chat channel o/
[19:03] great, I love to have an audience :)
[19:03] so Python is a programming language, but not a scary hard one.
[19:03] Python is kind of like BASIC, except you don't have to be embarrassed about saying you are a Python programmer!
[19:04] we are going to write a computer program, which is a set of instructions to the computer to tell it to do some interesting stuff for us.
[19:04] Lets get set up first, we are going to need a text editor to write the instructions in and a terminal to tell the computer to do the instructions.
[19:05] hopefully you will find them next to each other in the Applications-Accessories menu
[19:05] or if you are using unity hit the super key and type gedit and return for the editor
[19:05] and terminal for the terminal
[19:06] they are also somewhere to be found in the apps lens, but that is another story altogether
[19:06] so open both of them now and get comfortable with the three windows on screen, IRC, terminal and gedit
[19:06] are we sitting comfortably?
[19:07] plain old text editor is perfect, none of your fancy IDEs for this session
[19:08] Traditionally the first program you should write in any language is one to get the computer to say hello to the world! so lets do that.
[19:08] in the text editor type the following:
[19:08] print "Hello, World!"
[19:09] that is it, your first program, now lets save it and run it (I did tell you it looked like BASIC)
[19:09] file-save as and call it hello.py
[19:09] this will save it into your home directory by default, fine for now, but you would probably want to be a bit more organised when doing something serious
[19:10] feel free to be more organised right now if you like :)
[19:10] ok, now in the terminal lets run the program
[19:10] python hello.py
[19:10] did it say hello to you?
[19:12] as I saved it in the home directory and terminal starts there by default it should just work, if you are putting things in folders you might need to navigate to it with the cd command or specify the path to the program
[19:13] ok, so that was running the program by running python then the name of our application, but we can do it a different way, by telling Ubuntu that our program is executable
[19:13] What we are going to do now is try to make our program directly executable, in the terminal we are going to CHange the MODe of the program to tell Ubuntu that it is eXecutable
[19:13] so at the $ prompt of the terminal type:
[19:13] chmod +x hello.py
[19:14] now we can try to run it
[19:14] again at the $ prompt
[19:14] ./hello.py
[19:14] oh noes!!!
[19:14] Warning: unknown mime-type for "Hello, World!" -- using "application/octet-stream"
[19:14] everyone get that?
[19:16] ubuntu doesn't know how to run this application yet, we need to add some extra magic at the top of our program to help it understand what to do with it.
[19:16] back in the editor, above the print "Hello, World!" add the following line
[19:16] #!/usr/bin/env python
[19:16] so the /usr/bin/env bit is some magic that helps it find stuff, and the thing it needs to run this application is python
[19:16] now you should be able to save that and flip back to the terminal and run your program
[19:17] ./hello.py
[19:17] that should now run :)
[19:18] as has been pointed out in python 3 you need to put brackets round the string so
[19:18] print ("hello world")
=== mohammed is now known as Guest99895
[19:19] which works in all versions of python
[19:20] OK, lets go on to the next concept, giving our program some structure
[19:20] back to the editor, and between the two lines we have already add a new line
[19:20] while 2+2==4:
[19:20] and on the next line put four spaces before the print ("Hello, World!")
[19:20] and save that
[19:21] Moshanator asked: can i just ./hello.py?
[19:21] lunzie asked: ​ are the quote brackets proper form?
[19:21] the brackets round the quotes are better form as they work on python 3
[19:22] so the while statement we added starts a loop, in this instance it will carry on until 2+2 is equal to something other than 4
[19:22] the double equals means "is equal to" a single equals is used to assign a value to something (more on that later)
[19:23] the colon at the end is an important part of the while statement
[19:23] There is no "until" "wend" "end while" type statement at the end, as you might expect to find in lesser languages :)
=== yofel_ is now known as yofel
[19:23] the indentation of the print statement is not just cosmetic and for our benefit
[19:23] the indentation level is part of the language, when the indentation stops that is the end of the loop (or other structure that you might expect to have an end)
[19:24] this means that python always looks neat and tidy (or it doesn't work)
[19:24] Always use four spaces to indent, not three, not five and certainly not a tab.
[19:24] Other indentations will work, but if you ever have to work with anyone else you must always be using the same indentation, so we all get in the habit of using four spaces.
[19:24] in gedit you can set it up to use 4 spaces instead of a tab
[19:25] edit-preferences, on the editor tab choose tab width 4 and insert spaces instead of tabs
[19:25] many other editors and IDEs have a similar option
[19:25] Lets run our new program, just save it in the editor and run it again in the terminal with ./hello.py
[19:26] and that is 4 spaces per level of indentation you want so if you have a loop in a loop then the inner one will be 8 spaces indented
[19:26] now we can wait for 2+2 to be something other than 4
[19:26] * AlanBell taps fingers
[19:27] or, if you are in a hurry, you can press ctrl+c
[19:28] ok, so ctrl+c is handy for breaking in to an out-of-control python program
[19:28] you can do other fun stuff with the print statement, if you change it to read:
[19:28] print "Ubuntu totally rocks! ",
[19:28] and run it again (note the comma at the end)
[19:29] print ("Ubuntu totally rocks! "), <- for the python 3 contingent I should think
[19:30] it should fill the terminal with text
[19:30] the comma prevents it doing a newline
[19:31] ctrl+c again to break out of it
[19:31] lets do something different now
[19:31] in the terminal, type python at the $ prompt and hit return
[19:31] you should have a >>> prompt and a cursor
[19:32] this is the interactive python console
[19:32] you can type print("hello") here if you want
[19:32] or do some maths like:
[19:32] print 2**1000
[19:32] which will show you the result of 2 multiplied by itself a thousand times
[19:33] python is kinda good at maths
[19:33] you don't need the print statement here either
[19:33] so 2**1000 should work in python 2.7 or 3
[19:34] you could even try 2**100000 it won't take long, and you can always stop it with ctrl+c
[19:35] while we are on the subject of maths, lets get the value of pi
[19:36] print pi won't do anything useful (but feel free to try it)
[19:36] we need more maths ability than the python language has built in
[19:37] so we need to get a library of specialist maths stuff, so type
[19:37] import math
[19:37] it will look like it did nothing, but don't worry
[19:37] now type
[19:37] math.pi
[19:37] >>> import math
[19:37] >>> math.pi
[19:37] 3.141592653589793
[19:38] So we have seen here how to import a library of functions to do something, and called one of the functions from the library (to return the value of pi)
[19:39] ok, so what is in the math package, apart from pi?
[19:39] try typing dir(math) at the python console
[19:39] ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
[19:40] you can also look at http://docs.python.org/library/math.html
[19:40] callaghan asked: Is there a way to see which functions are in the imported library?
[19:40] yes :)
[19:41] and to get more descriptive help on each one try help(math)
[19:41] so dir() lists the names and help() lists names, parameters and a little bit of help text
[19:43] TheOpenSourcerer asked: is help() a python function useful for anything else?
[19:43] try help(help)
[19:44] it is a function that could be used, I can't off hand think of any particularly useful use of it other than for getting help
[19:44] Mipixal asked: Your favourite IDE for dev with Python, on bigger projects. (In before IDE flame wars :p )
[19:45] honestly my favourite is gedit
[19:45] I have used eclipse and pydev
[19:45] and I liked stani's python editor (SPE) for a bit
[19:46] but all I really want is a text editor with syntax highlighting
[19:46] and normally several terminal windows open across a couple of monitors
[19:46] All this command line stuff is all very well, but we want to do applications that have pretty windows and stuff!
[19:47] In the interactive console type or paste the following
[19:47] import gtk
[19:47] which will load a library full of stuff to do with the gtk toolkit that powers the gnome desktop
[19:47] now type
[19:47] foo=gtk.Window(gtk.WINDOW_TOPLEVEL)
[19:47] that assigns a window object to a variable called foo
[19:47] (the name doesn't matter, the single equals does)
[19:48] but nothing much seems to have happened yet, so type:
[19:48] foo.show()
[19:48] yay, a real live little window should be on screen now!
[19:49] lets see what we can do to it with dir(foo)
[19:49] quite a lot! lets try:
[19:49] foo.set_title("my little window")
[19:49] go ahead and change the title a few times
[19:50] so if you type "foo.show"
[19:50]
[19:50] There are 10 minutes remaining in the current session.
[19:51] you get printed out a reference to where the code for the show function is
[19:51] you need to do foo.show() to actually call the function
[19:51] ahayzen asked: In gedit is there anyway of adding code completion for python programming, like pydev in eclipse, via a plugin?
[19:51] teemperor asked: is there any naming convention in python? (because of set_title)
[19:52] ahayzen: I believe there are some plugins for that, last time I tried one it was rubbish though
[19:53] if anyone has any good ones I would be interested to know of them
[19:53] teemperor: http://www.python.org/dev/peps/pep-0008/ here is the python style guide
[19:54] many projects have their own more detailed conventions for object names
[19:54] everyone agrees on the indentation levels though :)
[19:54] Alliancemd asked: In a program changelog I saw a developer saying that he ported the code from python to java to make it faster and he said "because java is sometimes x10-x100 times faster than python". We know that java is very slow, does python have this big impact on speed?
[19:55] this is a myth
[19:55] There are 5 minutes remaining in the current session.
[19:55] sometimes java does take a while to start if it has to launch a JVM, this gave applets a reputation for being slow
[19:56] once up and running it is not particularly slow
[19:56] unless you are doing something massively time sensitive (where every nanosecond counts) then any language will do any task
[19:57] the performance problem is never the language, it is always the algorithm
[19:57] normally rewriting code so that it does fewer disk or database accesses will speed it up thousands of times more than changing the language it is implemented in
[19:58] Alliancemd asked: What do u think of PyQt? Is it that good how people say?
[19:58] mohammedalieng asked: what about python performance compared to Java ?
[19:58] callaghan asked: will there be a follow-up lesson, or where should unexperienced python-devs go from here?
[19:59] not played with Qt much
[19:59] there are some great books
[19:59] snake wrangling for kids is excellent
[20:00] and the dive into python book is in the repos, you can install it from software centre
[20:00] I believe there are other python classes in this channel so check the schedule
[20:00] Logs for this session will be available at http://irclogs.ubuntu.com/2011/09/05/%23ubuntu-classroom.html
[20:00] ok, think I am out of time
[20:00] thanks everyone o/

It was great fun to do, and thanks to David Planella and the classroom team for putting on these regular educational events.

The science of meetings

This post isn’t going to make an huge amount of sense to people who are not using IRC or involved in the Ubuntu project, feel free to let your eyes glaze over and wait for the next article.
The #ubuntu-meeting IRC channel is the place where most of the regular team meetings of the Ubuntu project take place. There was a bot called MootBot in the channel which was there to facilitate meetings, which it did for a number of years. I was never that impressed by what it did with the minutes, there was too much manual writing of meeting minutes which really the bot should have been doing for people. I started making a few tweaks to a copy of the bot so that it would generate formatted minutes in moin wiki syntax for pasting direct into wiki.ubuntu.com I had also been working on a complete rewrite of the code in Python rather than TCL that was started by the Debian project. This was a bit of a background task for me and was nowhere near finished, when a couple of weeks ago the original Mootbot broke and nobody had time to go fix it. As a result of this my next generation meeting bot was pressed into service somewhat ahead of schedule, bugs and all, and is now working hard in the #ubuntu-meeting channel under the name meetingology. The code is at https://code.launchpad.net/~ubuntu-bots/ubuntu-bots/meetingology and patches are very welcome. I am running a development version of the bot in the #meetingology channel, feel free to pop in and test it there.
Meetingology supports rather more commands than the old bot and you can use the old form with square brackets [TOPIC] or just #topic and it isn’t case sensitive about commands. The full list of commands is documented here https://wiki.ubuntu.com/meetingology. In particular note that you can now do “#startmeeting meetingname” to set the overall meeting title. There are a number of improvements and issues to fix with the output minutes, I will get round to that over the next few weeks (patches are welcome remember). Some teams have their own meeting syntax and scripts to parse it, if you want a particular output format for your meeting then do come and find me and we can make it happen. The goal is that the post-meeting effort for the chair is copy-paste-done. Writing up minutes is a task that is not worthy of a human.

« Previous PageNext Page »