1. 2011
    Nov
    24

    Syndication feeds from templates

    One of the must-have necessities for any blogging or other web publishing software is the RSS (Really Simple Syndication) feed. RSS, or its younger and sexier sibling Atom, is the file format used by news aggregators, which give you a frequent listing of posts and stories published by your favorite blogs, newspapers, journals, whatever you want, without you having to actually go to their website. Usually the RSS feed is generated by some library of code where you construct your feed as an array of objects or data structures. Here's how you put together an RSS feed with PyRSS2Gen, for instance:

    rss = PyRSS2Gen.RSS2(
        title = "Andrew's PyRSS2Gen feed",
        link = "http://www.dalkescientific.com/Python/PyRSS2Gen.html",
        description = "The latest news about PyRSS2Gen, a Python library for generating RSS2 feeds",
    
        lastBuildDate = datetime.datetime.now(),
    
        items = [
           PyRSS2Gen.RSSItem(
             title = "PyRSS2Gen-0.0 released",
             link = "http://www.dalkescientific.com/news/030906-PyRSS2Gen.html",
             description = "Dalke Scientific today announced PyRSS2Gen-0.0, a library for generating RSS feeds for Python.  ",
             guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/news/030906-PyRSS2Gen.html"),
             pubDate = datetime.datetime(2003, 9, 6, 21, 31)),
           PyRSS2Gen.RSSItem(
             title = "Thoughts on RSS feeds for bioinformatics",
             link = "http://www.dalkescientific.com/writings/diary/archive/2003/09 …
  2. 2011
    Nov
    06

    Scilab

    Based on a tip on Stack Overflow, I just discovered Scilab, a FOSS (free/open source software) equivalent to Matlab. Then I discovered that I had actually only rediscovered it: Scilab was already installed on my computer. Maybe my scientific software collection needs a cleanup.

    Anyway, as I mentioned, Scilab aims to do basically what Matlab does. It’s an admirable goal, but I’m not such a fan of the program itself, for a couple of reasons: first, at least on Linux, it looks ugly, and more importantly, the GUI seems to lack some of the basic functionality that people tend to expect these days, like scrolling. If you’re a fan of xterm then perhaps Scilab is for you, but otherwise it’s kind of inconvenient to use. For the rest of us, the need for a Matlab equivalent is pretty well filled by Python with Numpy/Scipy and matplotlib, plus your terminal of choice. Python also has the benefit of being a general-purpose programming language, so once you learn it for numerical computation, you can do all sorts of other cool things with it.

  3. 2011
    Aug
    18

    Python Log Viewer

    For anyone who develops in Python, Python Log Viewer seems like a simple but incredibly useful tool. As the name suggests, it listens on a socket and displays any log records sent to it using the SocketHandler from the Python logging system. You can use the GUI to filter by log level and/or logger name, and also view full details about the log records.

    As a matter of fact, I’m finding this quite useful while I try to figure out how to restore stability to my website. (In other words, sorry about how flaky things have been lately; I’m working on it.)

  4. 2010
    Aug
    07

    More Python voodoo: optional-argument decorators

    I’ve just been doing something that probably should never be done in Python (again), and I figured it might be useful to record this little snippet for posterity. Many Python programmers are probably familiar with decorators, which modify functions by wrapping them with other functions. For example, if you want to print ‘entering’ and ‘exiting’ to trace when a function’s execution begins and ends, you could do that with a decorator:

    def trace(func):
        def wrapper(*args, **kwargs):
            print 'entering'
            func(*args, **kwargs)
            print 'exiting'
        return wrapper
    
    @trace
    def f(a,b):
         print 'in f: ' + str(a) + ',' + str(b)
    

    Note that in practice there are better ways to do this, but it’s just an example.

    Anyway, it’s also possible to customize the behavior of decorator itself by providing arguments, for instance if you wanted to print different strings instead of ‘entering’ and ‘exiting’. But in that case, your “decorator” function is actually a wrapper that creates the real decorator and returns it:

    def trace(enter_string, exit_string):
        def _trace(func):
            def wrapper(*args, **kwargs):
                print enter_string
                func(*args, **kwargs)
                print exit_string
            return wrapper
        return _trace
    
    @trace('calling f', 'returning')
    def f(a,b):
         print 'in f: ' + str …
  5. 2009
    Jul
    27

    More Python voodoo: combination class/instance methods

    Here’s a neat tidbit from my website code: Let’s say you’re writing a class, and you want it to have some methods that can be called as either class or instance methods. Of course, you can do it by writing a class method, but if you want the method to be able to use instance attributes when it’s called on an instance, you can do it using a descriptor. Here’s how.