Tag Archives: Pythonic Programming

Week In Links – 8/21/2009

Over the last few weeks I’ve noticed a few things,

  1. I always have a lot of links open in FireFox that I haven’t looked at yet.
  2. Blog traffic drops like mad on Fridays ;)

These are just some sites or articles that I found interesting or helped me in some way this week.

So here’s what I have this week!  (other posters can add to this page as they wish)

  1. http://www.infoq.com/articles/agile-version-control
  2. http://kenai.com
  3. http://oauth.net
  4. http://code.google.com/p/modwsgi/wiki/DebuggingTechniques
  5. http://blog.lowkster.com/2008/01/getting-to-pesky-foreign-key-data-in.html
  6. http://www.box.net
  7. http://beanstalkapp.com
  8. http://www.ducea.com/2006/08/11/apache-tips-tricks-deny-access-to-some-folders
  9. http://www.fogcreek.com/FogBugz
  10. http://www.sitepoint.com/article/build-to-do-list-30-minutes

# Override decorator

While working with pyglet one day, I made the mistake of subclassing the window class and overriding the __init__ method, which prevented the window from showing. To fix this, it’s easy enough to add a call to super(Window, self).__init__(*args, **kwargs), but I went a different route. Here’s a decorator called override that puts it in for you:

def override(function):
    def wrapper(instance, *args, **kwargs):
        super(instance.__class__, instance).__init__(*args, **kwargs)
        return function(instance, *args, **kwargs)
    return wrapper

To use it, simply use the decorator notation in a subclassed method:

class MyWindow(pyglet.window.Window):
    @override
    def __init__(self, *args, **kwargs):
        # code goes here, no need to call super!

# Function/Method Decorators

One of the reasons I like Python is because it’s simple but powerful. All the tools are there for whatever task needs to be done. I’ve found __getattr__ to be a very useful tool for creating sets of standardized class methods without having to explicitly declare them. Another useful tool is function decorators. Read on to get a quick idea of how to make your own class decorators.

Read more »

# __getattr__ Deserves Some Respect

One of Python’s built-in functions is __getattr__, which turns out to be quite useful but at the same time slightly dangerous. It is especially helpful for creating complex classes without using inheritance. For a brief tutorial, read on.

Read more »

# Python Dictionary Coolness

Lately I’ve been working with dictionaries, specifically as a device for storing settings. I ran into a little annoyance and thought of programming a little routine to take care of it. Come to find out, Python already has such a routine built into the dictionary class. Read on to find out more.

Read more »

Python Scripting within a Python Script

UPDATE: In order to use reload in Python 3, it must be imported from the imp built-in module. Thus, some of the examples below will require the line from imp import reload in order to work in Python 3.

I’ve been mulling over an idea for a little while now, but didn’t have the chance to do anything with it until today. Turns it it was way easier than I thought it would be. For reasons I won’t go into here, I wanted to have a Python program which could write and run other Python programs. Clearly Python can write programs (since it can save text as a .py file), and it can easily run programs just by importing a module. The trouble was editing those files and running the updated code in the master program without having to restart the master program. Here’s how I did it.

Read more »