Archive for the ‘Computers’ Category

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…

Some sites to get to free eBooks

Monday, October 6th, 2008

There are lots of free books available on the internet. This page summarises some very good sites for books on topics such as computer programming, literature, etc.

The Best 6 Sites to Get Free Ebooks.

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.

    Pybel on Mac OS X

    Thursday, March 20th, 2008

    I just tried to install openbabel and its python wrapper pybel on a Mac running OS X 10.4. OpenBabel compiled fine from source, no problems with that. But when I tried to run python setup.py build in the scripts/python subdirectory, the build failed at the linking stage with

    /usr/bin/ld: for architecture ppc
    /usr/bin/ld: can't locate file for: -lopenbabel
    collect2: ld returned 1 exit status

    The solution turned out to be that—at least for my computer—I needed to set the environment variable OPENBABEL_INSTALL to /usr/local explicitly. Normally, the library is looked for in the src directory, i.e., where openbabel was built, but that didn’t work, for whatever reason (I haven’t checked up on that).

    After setting the appropriate environment variable with export OPENBABEL_INSTALL=/usr/local everything works fine, the install as root finished without any problems, and everything works:

    host:~/models/ flo$ python
    Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
    [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import pybel
    >>>

    Some Mac links

    Saturday, March 8th, 2008

    Boot PowerPC Macs via USB 2.0 drives

    Can’t find that symbol on the Mac keyboard?

    Sunday, March 2nd, 2008

    Well, should you ever be switching keyboards for your Mac computer, then you may be interested in the following. The following link is to an information sheet (that you may print out!!) which shows your for every symbol which key combination you need to produce it on your Mac keyboard. Or use it the other way round: which combination results in what symbol. In any way, it’s quite useful!
    Reference for EVERY Character Key on a Mac

    Changing the CRAN repository of R

    Thursday, February 21st, 2008

    After the last upgrade R didn’t seem to want to install new packages. The likely reason is that one of the UK CRAN mirrors (http://www.sourcekeg.co.uk/cran/) is not working (at least as of 21/02/08). R fails to install packages with
    Warning: unable to access index for repository http://www.sourcekeg.co.uk/cran/bin/macosx/universal/contrib/2.6

    To be able to choose a different mirror, just issue options(”repos”=c(CRAN=”@CRAN@”)) on the R command line and the next time a package has to be installed from CRAN you will be asked which mirror to use. See ?setRepositories and ?options for more info.

    Writing Style Tips

    Wednesday, February 6th, 2008

    When writing documents, be it literary or scientific text, formatting and style is very important. Here are a couple of pages that I found of use, they do not only apply to LateX documents.