Posts Tagged ‘python’

Dictionary lookup with regular expression as key

Wednesday, May 13th, 2009

I just remembered that I was meant to post that thing here, too. Problem is the following: you want to retrieve all values of a python dictionary whose keys match a certain regular expression. Solution: Just make a custom dictionary class derived from the builtin one:

class redict(dict):
    def __init__(self, d):
        dict.__init__(self, d)

    def __getitem__(self, regex):
        r = re.compile(regex)
        mkeys = filter(r.match, self.keys())
        for i in mkeys:
                yield dict.__getitem__(self, i)

With this you can do the following:


>>> keys = ["a", "b", "c", "ab", "ce", "de"]
>>> vals = range(0,len(keys))
>>> red = redict(zip(keys, vals))
>>> for i in red[r"^.e$"]:
...     print i
...
5
4
>>>

Using MySQLdb 1.2.2 with python 2.6.1

Thursday, April 9th, 2009

When importing MySQLdb 1.2.2 into python 2.6.1 an error is reported:

Python 2.6.1 (r261:67515, Dec  7 2008, 08:27:41)
[GCC 4.3.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import MySQLDB
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ImportError: No module named MySQLDB
>>> import MySQLdb
/usr/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet

This is because the sets module is not built-in into the core distribution of python.

To get rid of this error and use the more efficient built-in set
type, do the following in the __init__.py file that was reported:

* comment line 34:

When importing MySQLdb 1.2.2 into python 2.6.1 an error is reported:

Python 2.6.1 (r261:67515, Dec  7 2008, 08:27:41)
[GCC 4.3.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import MySQLDB
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ImportError: No module named MySQLDB
>>> import MySQLdb
/usr/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet

This is because the sets module is not built-in into the core distribution of python.

To get rid of this error and use the more efficient built-in set
type, do the following in the __init__.py file that was reported:

* comment line 34: from sets import ImmutableSet

* add after that line: ImmutableSet = frozenset

* comment line 41 in the original file: from sets import BaseSet

* add after that line: BaseSet = set

Like this the built-in types will be used anytime a BaseSet or an ImmutableSet is referenced.

Now everything works fine:

Python 2.6.1 (r261:67515, Dec  7 2008, 08:27:41)
[GCC 4.3.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import MySQLdb
>>> MySQLdb.version_info
(1, 2, 2, ‘final’, 0)
>>>

Installing numpy 1.2.1 on Mac OS X Leopard 10.5.6

Friday, March 6th, 2009

NOTE - UPDATE: While the following will install numpy 1.2.1, it’s perhaps not the best solution. If interested just inform yourself about the Apple system python as opposed to a standalone framework python installation.

That stuff bugged me for a while, now it seems as if I found a pretty decent solution to this.

Leopard comes with python 2.5.1 preinstalled and also includes numpy 1.0.1 (if I remember well). If you want to install something that uses a newer numpy (e.g., matplotlib) then you need to upgrade numpy. There are all these issues about the differing versions/distributions of python on Apple computers, I won’t dwell on that. If you don’t want to upgrade your python distribution that there’s an easier way:

1) Set the following symbolic link: (see http://wiki.python.org/moin/MacPython/Leopard)

cd /Library/Frameworks
sudo ln -s /System/Library/Frameworks/Python.framework/ Python.framework

This allows you to use the ‘normal’ installer for numpy, see step 2.

2) Install the numpy 1.2.1 .dmg from here: Download NumPy

3) Your numpy installation ends up here:

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/

3.5) You may want to make a backup of the old numpy if there is one here:

/Library/Python/2.5/site-packages/ (just rename it, delete it, chmod 000 it, whatever)

4) Move it away from the Framework location:

cd /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/
sudo mv numpy* /Library/Python/2.5/site-packages/

5) DONE:

hostname$ python
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import numpy
>>> numpy.__version__
‘1.2.1′
>>>

And that’s it! The next things now will be to get matplotlib to work, the easy_install does not yet want to work, but that has nothing to do with numpy anymore! This time it’s rather freetype2…

Functional programming in Python

Tuesday, September 30th, 2008

Just found some interesting stuff concerning functional programming using Python. Here are the links:

  • Functional programming in Python, Part 1
  • Functional programming in Python, Part 2
  • Functional programming in Python, Part 2
  • These articles are from a larger collection of pages on Python called Charming Python on the IBM website.

    Add field(s) to SDF file

    Monday, February 4th, 2008

    Add information to each molecule in a SDF file. The information for each molecule is specified as a colon-separated list in an additional file. This allows multiple values to be added under the same tag for each molecule. Alternatively, this allows for an easy extension to be used to add multiple tags with different values (not implemented, but easy to do).

    Python script to add fields to a SDF file
    (more…)

    Circular fingerprints in Python

    Monday, February 4th, 2008

    This script is basically a re-implementation of the MOLPRINT 2D fingerprint. It takes a Sybyl MOL2 file as input and analyses for each atom its neighbouring environment and encodes this as a string ot Sybyl atom types. The depth level to which the environment goes can be specified, as well as if to separate each depth-level from the others. And, being in python it is easily amenable to additions, improvements, etc. The script only converts valid molecules as defined by regular expressions for atom and bond lines in the corresponding block of the MOL2 files.

    Update 16/10/2007: Modified regular expressions for CTAB, included possibility to dump everything to MySQL database - see source code.

    Circular fingerprints in Python
    (more…)