Talk on Python by Svenne Krap in DKUUG

Python
Last night I attended a talk on Python by Svenne Krap in DKUUG (Danish Unix User Group). The video, slides and examples can be found here: video.dkuug.dk/2008-03-25-python/

The talk was just what I wanted – a hands on example-based introduction to various cool uses of Python. I already have experience with Python so I wasn’t interested in a very basic lecture – I just wanted to be inspired to use Python in new ways.

The following is just my random notes from the talk – perhaps someone will find it interesting and be inspired to watch video of talk which can be found on the page above.

Introduction
For those who haven’t got any experince with python I can recommend the following two (free) books:
How to Think Like a (Python) Programmer
Dive Into Python

Notes from the night
The styleguide PEP-8 should be read as it gives the coding conventions for python.

  • A indentation level should only be 4 spaces, not a tab.
  • Tabs and spaces shouldn’t be mixed.
  • A line should only be 79 characters
  • 2 blank lines should be used to seperate functions and classes, 1 blank line otherwise

Getting help
The most important commands are:

  • help() — help(modulname) searches help on the module.
  • dir() — return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it.

So when you are programming having a shell open with python running you can get help on how to use a module by using help(modulename) and to see the methods and options for a object or module try dir(object). help(modulename) is the most help IMO.

Some random code snippets that might be usefull

age = 25
name = "Thomas"
print "My name is %s and I am %s years old " % (name,age)
My name is Thomas and I am 25 years old
import random
random.random()
0.73658477402788702
import time
time.sleep(2.25)
>>> from getpass import getpass
>>> password = getpass("Enter password: ")
Enter password:
>>> username = raw_input("User name: ")
User name: Thomas
>>>

Eksempel med urllib2

import urllib2
tjansson = urllib2.urlopen("http://www.tjansson.dk")
txt = tjansson.read()
txt[2:100]
'DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm'

MySQLdb
MySQLdb is a module which makes it possible to connect and interact with a MySQL database. I found a good introduction here:
Writing MySQL Scripts with Python DB-API
Unfortunately its not one of the modules and needs separate installation (here Ubuntu):

root@dirac:~# aptitude install python-mysqldb

and some sample code from the guide:

   import MySQLdb
 
   conn = MySQLdb.connect (host = "localhost",
                           user = "testuser",
                           passwd = "testpass",
                           db = "test")
   cursor = conn.cursor ()
   cursor.execute ("SELECT VERSION()")
   row = cursor.fetchone ()
   print "server version:", row[0]
   cursor.close ()
   conn.close ()

PDF generation with reportlab

root@dirac:~# aptitude install python-reportlab
from reportlab.pdfgen import canvas
def hello(c):
    c.drawString(100,100,"Hello World")
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

Google and python
While browsing the web I found this page about Google and their Python api, which could allow me to make a program to create a todo list from my Google calendar. Download and examples:
code.google.com/apis/calendar/developers_guide_python.htm

Links
Index of modules: docs.python.org/modindex.html
Even more modules pypi.python.org/pypi/
Python for scientific use www.scipy.org/
Convert af python til Windows exe filer www.py2exe.org
Google codesearch www.google.com/codesearch
Another list of python guides www.whoishostingthis.com/resources/python/

This entry was posted in Articles, Computer. Bookmark the permalink.

One Response to Talk on Python by Svenne Krap in DKUUG

  1. Julie Bohman says:

    Cool Thanks for your article. I am a newbie at python and this will be a big help.

Leave a Reply