matt good

musing coding

Entries tagged "Python"

Simple JavaScript Namespaces

written by Matt, on May 31, 2009 12:52:00 PM.

“Namespaces are one honking great idea — let's do more of those!” - Tim Peters, “The Zen of Python”

After a few years of mostly server-side development I'm getting back into JavaScript, and for the first time I feel like I'm approaching it as an actual engineering challenge rather than a bunch of quick hacks. As our JS code base is growing I'm breaking things up into namespaces to keep things manageable.

Though JavaScript has no built-in notion of "modules" like many other languages you can use objects to create namespaces, which in their simplest form would look something like:

util = {
  my_func: function() {
    alert('my_func');
  }
};
util.my_func();

However, creating nested namespaces like "myapp.util.text" gets a bit ugly with that approach. I liked the basic API of Namespace.js, though I didn't want any of the additional features like remote loading, so I figured I could make something much simpler and smaller.

You can first "declare" a namespace and then assign attributes like:

Namespace('myapp.ui');
myapp.ui.ToggleButton = function() {
  // ...
};

Or pass in the attributes to include in that namespace:

Namespace('myapp.util.text', {
  some_func: function() {
  }
});

Here's the code:

function Namespace(name, attributes) {
  var parts = name.split('.'),
      ns = window,
      i = 0;
  // find the deepest part of the namespace
  // that is already defined
  for(; i < parts.length && parts[i] in ns; i++)
    ns = ns[parts[i]];
  // initialize any remaining parts of the namespace
  for(; i < parts.length; i++)
    ns = ns[parts[i]] = {};
  // copy the attributes into the namespace
  for (var attr in attributes)
    ns[attr] = attributes[attr];
}

This is of course much smaller than the 16kb of Namespace.js, so as an additional challenge I decided see if I could compact it to fit the 140 character limit of a Twitter status message :) (Note: the Twitter version used jQuery's $.extend function to copy the attributes into the namespace which I later replaced with the framework-independent version above.)

I'll follow up later with some tricks I've been thinking about to "import" stuff from other namespaces.

PyCon Trac Presentation

written by Matt, on Feb 28, 2007 11:08:20 AM.

Here are the materials from my PyCon Trac presentation:

The modified rst2s5 script requires Pygments for coloring the example code.

PythongPaste

written by Matt, on Feb 27, 2007 12:51:31 AM.

Ian Bicking has just added a new package to the Paste suite for WSGI utilities

Read on...

Ubuntu package for Germanium

written by Matt, on Feb 3, 2007 4:17:08 PM.

I've built an Ubuntu Edgy package for Germanium. It may work on Dapper, or Debian versions, but I haven't tested it on any of those yet. I think the dependencies should be covered, but if you find any problems you can open a ticket.

Download:
germanium_0.2.0-0ubuntu1_all.deb

Germanium 0.2.0 Released

written by Matt, on Feb 2, 2007 7:07:00 PM.

Germanium 0.2.0 features better GNOME integration including mime handling for .emp files and a GConf schema, keyboard shortcuts, as well as album art display, and optionally saving album art with the tracks.

Download:
emusic-gnome-0.2.0.tar.gz

Darcs:

darcs get --tag=0.2.0 http://projects.matt-good.net/darcs/emusic-gnome/

svn_apply_autoprops.py

written by Matt, on Dec 23, 2005 9:06:05 AM.

Subversion includes a contrib script svn_apply_autoprops.py that scans your auto-props settings and applies them to a working copy. These are normally applied to files you add to the repository, but this script is useful if you have added new properties, or other users have added files without configuring the desired properties. However, since I use SVK this script didn't work for me. I looked at the script to see if I could convert it to use SVK instead, but it was relying on the .svn folders to try to determine what directories to scan and I didn't like the fact that they were parsing the config by hand with regular expressions instead of using the extremely useful ConfigParser module.

I decided it would be quicker to rewrite the script than to fix up the existing one, so here is the new and improved version: svn_apply_autoprops.py

Key features include:

Faster
This version is about twice as fast as the old one on the several repositories I've tested. The old version walked the directory structure for each pattern to find matching files. This version uses svn status -v to find all the versioned files once and simply scans this list to find the matching files.
Simpler
Using the ConfigParser and simplifying the directory scanning made this new version about 1/2 the code of the old one.
SVK
There's a variable that is defined at the top of the script to determine which command is used: svn or svk. Both support the same arguments needed by this script and are easily interchangeable.

Trac 0.9

written by Matt, on Oct 31, 2005 9:08:01 PM.

It's been nearly a year since 0.8 was released, but after resolving over 400 tickets, some major refactoring, and adding plugin support Trac 0.9 is finally available, so get downloading!

Once you've upgraded you'll probably want to check out some plugins:

WebAdmin
Everyone will probably want to install this. It adds a web interface for those administrative tasks you used to use trac-admin for, plus you can even use it to install new plugins, or manage which components are enabled from your existing plugins.
Bitten
Bitten is a "Continuous Integration" build server. It monitors your code repository for updates and will automatically run build and testing scripts to make sure you know when a build was broken. You can run your builds on multiple target platforms and generate historical graphs of unit test results and code coverage.
LDAP Plugin
I haven't tried this one out yet, but it should be useful for administrators that use LDAP to authenticate users, since it now also allows you to manage permissions based on LDAP groups, or even store Trac permission information directly in LDAP.
Account Manager
This is one that I wrote that allows users to register a new account. It currently supports htpasswd or htdigest files, but it is extensible for storing accounts in other formats.
Trac Hacks
Trac Hacks is a site that offers hosting for Trac extensions and will be a good place to look for new plugins, since hopefully the development interest will grow now that the new release has been made final.

Trac AccountManager Plugin

written by Matt, on Jul 19, 2005 11:11:32 PM.

I've repackaged my account management module as a plugin and uploaded it on TracHacks.

Installation instructions are included in the README file in the source. It's a pretty simple process: run setup.py to build the Egg and copy it into your Trac environment.

The next step is to extend the webadmin interfaces to provide a frontend to configure the account manager settings.

Trac Drop-In Plugin Support

written by Matt, on Jul 18, 2005 9:54:26 PM.

Well, PythonEggs sounded like a neat idea, and now they've made it possible to turn Trac's pluggable framework into a real plugin system. Simply stick your extensions into an Egg, create a "plugins" directory in your environment, and drop-in the file.

Jonas is developing a webadmin module for Trac as a plugin, and I took his example and have repackaged my account management module. Turning it into a plugin was quite trivial, but there are a couple of other things I'd like to clean up tomorrow before I release it.

The Trac documentation should be updated soon to explain how to make plugins.

Python Marketing

written by Matt, on Apr 6, 2005 11:17:00 PM.

Ruby on Rails Trac site

(http://dev.rubyonrails.com/about_trac)

Who needs Python marketing when Ruby On Rails runs their development site on a Python-based web application?

For those not familiar with Trac it's a combination of wiki and bug-tracking for software development projects. I use it at work and help out as a developer on it in my spare time. And of course it's all written in Python.

I'm trying to convince the other developers that we should include the "Python Powered" logo as an easter egg in the next release. There's nothing quite like having your competitor do your advertizing for you.