tadhg.com
tadhg.com
 

Posts concerning coding

Some JavaScript Programming Patterns

15:09 11 Oct 2009

“JavaScript Programming Patterns”, by Klaus Komenda, is an excellent walkthrough of several JS patterns. If you code JavaScript regularly you probably know at least some of these but will likely still find something useful in it—I particularly like the lazy function definition, a pattern originated by Peter Michaux:

var foo = function() {
    var t = new Date();
    foo = function() {
        return t;
    };
    return foo();
};

Simple, elegant, solves the problem of making sure that the heavy lifting only gets done once.

Permalink     2 Comments     [, , ]    

Test-Driven Development: A Bad Example

23:59 05 Oct 2009

Test-Driven Development (TDD) is a programming methodology that calls for programmers to first write tests that will only be passed by code that meets the specifications for whatever component they’re working on, and then to write the code for the component and keep working on it until it passes the tests.

I don’t tend to use Test-Driven Development, even though I often think I should. When working on personal projects, I don’t even write many tests after the code is done, and that’s something I should definitely do. But I generally regard it as a good practice.
[more...]

Permalink     Comment     [, , ]    

Python Extended List Slicing

23:18 01 Oct 2009. Updated: 08:20 02 Oct 2009

I never knew about this optional third parameter to list slices—in addition to e.g. getting the third to fifth items in a list with somelist[2:5], you can also get every nth item with somelist[::n].

This makes it very easy to get every odd or even item in a list, odd with somelist[::2] and even with somelist[1::2].

Where I think this is particularly handy is if you have to grab e.g. three-line chunks out of some text:

chunks = []
lines = text.split("\n")
for i in range(0, len(lines))[::3]:
    chunk = []
    chunk.append(lines[i])
    chunk.append(lines[i+1])
    chunk.append(lines[i+2])
    chunks.append("\n".join(chunk))

The docs on extended slices are here.

Permalink     1 Comment     [, ]    

RTF/Word–reStructuredText Toolchain

12:05 24 Sep 2009. Updated: 14:19 06 Oct 2009

It took me a while to get there, but I now have a working toolchain to automate going from an RTF file (or a Word document) to reStructuredText. The final link took the longest to find, and turned out to have been right there all along (no, I’m not going to turn this into a retelling of The Alchemist). But if you’re interested in how to get from Word to a sane format (like reStructuredText), this post will interest you.
[more...]

Permalink     1 Comment     [, , , , , , ]    

Time to Try Vim

09:14 09 Aug 2009

There are three kinds of major text editors: Emacs, vi (now really Vim), and “other”. I’m sure there are some other quirky small ones out there, but those are basically the major categories. “Other” encompasses what I think most people would expect from a text editor: a window in which you can type freely and see your text appear, and which behaves somewhat like other applications, including word processors, in its basic functionality.

I’ve used the “other” category for years. I think that the first one I used heavily (I’m not counting MS-DOS Editor) was TextPad. I stuck with that for quite some time, and then started using HomeSite. I think I played with UltraEdit for a while too. Eventually, however, I wanted a cross-platform and free software editor, and shifted over to jEdit. I’ve used it heavily for several years, and consider it the best of the “other” class.

I have, however, always thought about making the effort to learn vi, because I’ve always loved the keyboard-only approach and like to save time by making my text editor use more efficient.
[more...]

Permalink     8 Comments     [, , , ]    

Typeface Considerations

23:31 06 Aug 2009. Updated: 09:20 07 Aug 2009

I used to be somewhat obsessed with fonts. When I was doing more graphic design, I regularly looked for new fonts, tried to use just the right font for just the right occasion, and generally was very picky about them. Over time, that’s faded considerably, and I settled down to using fairly unspectacular fonts for most things, accepting that for the most part the “basic” serif and sans serif fonts available on the majority of systems were good enough.

After a system upgrade today, Lucida Sans Typewriter, the font I’ve been using in jEdit, suddenly stopped working properly without anti-aliasing. I couldn’t get it back to what it had looked like before, and so I was left with finding another suitable editor font.
[more...]

Permalink     4 Comments     [, , ]    

Python Script for Subversion Status

23:48 02 Aug 2009. Updated: 11:33 06 Apr 2010

I find in my use of Subversion that I often want to see a side-by-side list of files that aren’t under version control and files that have some other status. I also want these lists to be sorted alphabetically. Naturally, I ended up writing a Python script for this.
[more...]

Permalink     Comment     [, , , , ]    

Better reST–WordPress Pipeline

23:15 28 Jul 2009

Last week I posted about my setup for going from reStructuredText to WordPress. It involved a shell script, some Python scripts, and the pbpaste and pbcopy commands. It worked, but it was a little on the convoluted side.

Now I have a slightly better process, and one that I will have used to publish this post.
[more...]

Permalink     Comment     [, , , , , , , ]    

Better Word Count in jEdit

22:33 19 Jul 2009

I tend to care about word count in my writing. I’ve never been paid by the word, but nevertheless, it matters to me. From time to time I write fiction where I set the word count in advance, and then I try to hit it precisely. Even when that’s not the case, I just like to know how many words there are in a piece I’m writing. For this reason, a "word count" function is completely critical to me for whatever word processor or text editor I’m using to write.

jEdit has such a feature. It’s more or less the same as the one that I’ve been using in AbiWord, and in various word processors before that. But for quite some time I’ve wanted a better word counter. Since jEdit is now my application for all writing and I can script for it in Python, it was time to make the word counter I wanted.
[more...]

Permalink     Comment     [, , , , , ]    

jEdit Macros in Python

16:57 17 Jul 2009. Updated: 17:52 18 Jul 2009

jEdit has long been my text editor of choice, and I’m using it more than ever now that I’m writing more or less everything in it. I’ve been waiting a while for 4.3 to come out, but overall I remain quite happy with it. I do occasionally wonder about switching to vim or Emacs, but jEdit’s generally been able to do whatever I wanted it to.

I haven’t done much scripting with it, though. I recently came up with some use cases for scripts—involving reStructuredText, naturally—but I was a little reluctant to do the scripting because it involves Java and I really want to keep my current focus on Python and JavaScript.

I was therefore rather happy to discover the existence of the JythonInterpreter plugin, which makes it possible to write macros for jEdit in Python.
[more...]

Permalink     2 Comments     [, , , , ]    

Some Character Encoding Gotchas

10:31 16 Jul 2009

While scripting my reStructuredText to WordPress workflow, I ran into a bunch of character encoding problems.
[more...]

Permalink     Comment     [, , , ]    

Blog Workflow with reStructuredText

22:57 14 Jul 2009. Updated: 23:13 28 Jul 2009

I wrote about moving my writing over to reStructuredText on Sunday, and since then I’ve moved both my morning pages and my blog writing to it. The latter proved more complicated, primarily because I wanted to make the process almost as easy as writing pseudo-HTML (which is more or less WordPress’ native format, and kind of mine, too, for the last several years). With some hacky wrangling, I’ve managed to set that up.
[more...]

Permalink     4 Comments     [, , , , , , , ]    

Python Script to Change OS X Desktop Backgrounds

22:42 29 Jun 2009

OS X has built-in functionality to rotate between different desktop backgrounds, but if you have multiple monitors and want backgrounds that fit together (i.e. that have two halves, one on each monitor), you need to set that manually. I wanted a script to do this for me, selecting a pair of backgrounds for each day. I wrote one in AppleScript, but was so unimpressed by it that I decided I’d do it over in Python.
[more...]

Permalink     Comment     [, , ]    

Python Syntax Highlighting for star-light

23:37 26 Jun 2009

A couple of years ago I plugged star-light, a syntax highlighter that’s entirely client-side. I’ve been happy with it, but wanted a Python mode for it. I was going to post some other code this evening, and then decided that I should just make the Python mode myself.

This led to fun with regular expressions.
[more...]

Permalink     Comment     [, , , , ]    

PyWebSF Logo Attempt

23:03 21 Jun 2009

My brother asked me a while ago to design a logo for PyWebSF, and tonight I took a shot at it. Part of me thinks it has a certain something, and part of me thinks it’s exactly the sort of thing you get when you ask an engineer to do a designer’s job.
[more...]

Permalink     Comment     [, , ]    

WordPress 2.8 Upgrade

17:44 14 Jun 2009

Today I upgraded this blog to WordPress 2.8, a relatively smooth process. I ran into completely unrelated problems (hitting the process limit for my shell account) that derailed things for a while, but the WordPress upgrade itself was smooth.

I use Subversion to upgrade, first using
svn export --force http://core.svn.wordpress.org/tags/2.8/ .
in my development environment, seeing if things look okay there, then checking in the 2.8 changes to my own repository (the only niggly part because I neglected to clean the dev environment of changes before the export, so I had to look through things to see what was part of the upgrade), backing up my live database, and then checking the changes out to the live environment. It all looks fine, and hopefully will continue to function normally.

Permalink     Comment     [, , , , , ]    

Pylons Via Apache Port Issues

16:42 12 Jun 2009

I’ve been using Pylons (and, more recently, the Pylons-based TurboGears 2.0) for various projects for a while, and a few weeks ago ran into an annoying and specific problem: using Pylons via Apache made Pylons occasionally think it was running on a different port.

There’s a relatively easy answer to this, but until I was reading through TurboGears documentation, I didn’t find it.
[more...]

Permalink     2 Comments     [, , , , , ]    

PyWebSF: Meetup for SF-Area Python Web Developers

22:07 09 Jun 2009. Updated: 19:42 11 Jun 2009

My brother is organizing what will hopefully become a regular web-centric Python meeting. The first meeting is planned for 18:00 Tue 23 Jun 2009 at the SF Public Library. I think a couple of speakers are lined up already, although I don’t have details on the talks. I’ll be there, and if you’re a Python developer with web interests, or a web developer into or curious about Python, you should attend too!

Permalink     Comment     [, , , , ]    

WP Plugins: Recent Commentary and Show Tags in RSS

19:38 23 Mar 2009

I finally got around to writing up docs for, and then packaging, the two WordPress plugins I’ve finished recently:

I’m currently looking at support for series in WordPress, and am considering either writing my own plugin or using/forking the Organize Series plugin.

Permalink     Comment     [, , , , , ]    

Some RSS Changes

19:44 19 Mar 2009

I’ve made some changes to my Related Posts by Tags plugin, so that it’s now possible to add the list of related posts to the RSS feed entries. As I was experimenting with this, I realized that I wanted the RSS entries to show the tags for a post as well, so I wrote a (very simple) plugin to do that, too.

As a result, those of you reading this via RSS will now see a list of tags at the end of posts, followed by links to related posts. Please let me know if this doesn’t work as intended, or if you have other comments on the change.

Permalink     5 Comments     [, , , , ]    

Yahoo!’s Website Performance Rules

16:32 12 Mar 2009. Updated: 02:38 15 Mar 2009

While I’m familiar with a lot of them, I’d never actually read Yahoo!’s performance recommendations. They’re clearly laid out, with lots of good information in there. I should have a look at changing some of my own stuff into CSS sprites, as well as some of the other suggestions—these rules make a difference to users even on sites like mine.

Except for those of you who read all this via RSS, of course.

Permalink     Comment     [, , ]    

New Blog Functionality: Recent Commentary

21:39 23 Feb 2009

I’ve had a Recent Comments section on the front-page sidebar of the site for quite some time, and decided it was time for an upgrade. The old version was straightforward: it displayed a list of the most recent comments and who made them. What I decided I wanted was, rather, a list of the posts with the most recent comments on them, how many comments there were, and a list of the people who made the comments in timestamp order.
[more...]

Permalink     Comment     [, , , , , ]    

My WordPress Development Setup

16:29 13 Feb 2009

One of the first things I did with my resuscitated blog was to figure out how I could develop for it in a reasonable fashion.
[more...]

Permalink     1 Comment     [, , , , ]