»
S
I
D
E
B
A
R
«
Converting HTML into Unicode in Python
Nov 28th, 2009 by admin

To convert HTML into Unicode in Python, Python has the htmlentitydefs module, but this doesn’t include a function to unescape HTML entities.

Python developer Fredrik Lundh (author of elementtree, among other things) has such a function (you can find it here) on his website, which works with decimal, hex and named entities.

Switch Case in Python
Nov 28th, 2009 by admin

If you want to write a function in Python that returns different fixed values based on the value of an input index. In other languages you would use a switch, case statement, but Python does not appear to have a switch statement. In this case, the equivalent to switch is dictionary, here is how you can use it:

def f(x):
    return {
        'a': 1,
        'b': 2,
    }[x]
Ignoring case in Python strings
Nov 28th, 2009 by admin

To ignore case in Python strings, one could use ctypes to execute the c function “strcasecmp”. Ctypes is included in Python 2.5. It provides the ability to call out to dll and shared libraries such as libc. Here is a quick example (Python on Linux; see link for Win32 help):

from ctypes import *
libc = CDLL("libc.so.6")  // see link above for Win32 help
libc.strcasecmp("THIS", "this") // returns 0
libc.strcasecmp("THIS", "THAT") // returns 8

Here is the reference to strcasecmp documentation

Creating Graphs and Charts in Python
Nov 28th, 2009 by admin

Among many libraries that creates Graphs and Charts in Python, CairoPlot is the one that stands out. Surely matplotlib is great, but CairoPlot is better looking. So, for presentations and websites, it’s a very good choice.

Check it out at CairoPlot v1.1

Checking if a list is empty in Python
Nov 28th, 2009 by admin

Here is the best way to check if a list is empty in Python:

a = []
if not a:
  print "List is empty"
5 Best Django search app
Nov 28th, 2009 by admin

Here is a list of some of the best Django search applications:

Best way to implement an ‘enum’ in Python
Nov 28th, 2009 by admin

Python doesn’t have an native method of implement an ‘enum’, but you can implement your own.

Myself, I like keeping it simple (I’ve seen some horribly complex examples on the net), something like this:

class Animal:
    DOG=1
    CAT=2

x = Animal.DOG
Connecting to Oracle from Python
Nov 27th, 2009 by admin

cx_Oracle is a Python extension module that allows access to Oracle databases and conforms to the Python database API specification.

More information is available here. Here is a sample showing how to connect and process a query.

#!/usr/bin/python

import cx_Oracle
connstr='scott/tiger'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

curs.execute('select * from emp')
print curs.description
for row in curs:
    print row
conn.close()
IDE for Python
Nov 27th, 2009 by admin

There are many IDEs for Python that provides code completion and syntax highlighting. PyDev is one of the best. It is a plugin for Eclipse has a full support for code completion (try PyDev Extensions too). You can easily try it here. Another editor worth mentioning is WingIDE, which is really powerful. For more on Python editors check this page.

Using XPath in Python
Nov 27th, 2009 by admin

There are many ways of using XPath in Python. If you are doing simple path selection, stick with ElementTree ( which is included in Python 2.5 ). If you need full spec compliance or raw speed and can cope with the distribution of native code, go with libxml2.

Sample of libxml2 XPath Use:

import libxml2

doc = libxml2.parseFile("tst.xml")
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval("//*")
if len(res) != 2:
    print "xpath query: wrong node set size"
    sys.exit(1)
if res[0].name != "doc" or res[1].name != "foo":
    print "xpath query: wrong node set value"
    sys.exit(1)
doc.freeDoc()
ctxt.xpathFreeContext()
from elementtree.ElementTree import ElementTree
doc = ElementTree(file='tst.xml')
for e in mydata.findall('/foo/bar'):
    print e.get('title').text
»  Substance: WordPress   »  Style: Ahren Ahimsa