tadhg.com
tadhg.com
 

Posts concerning python

A Little More Functional Programming

23:55 25 Feb 2010. Updated: 01:41 26 Feb 2010

After reading Dhananjay Nene’s comment on my post about a functional style approach to the “find longest repeater” problem, I decided to follow the line from that comment and divide the program into functions for finding the longest contiguous block and then for comparing the blocks. Naturally, I wanted to do this without using any variables…
[more...]

Permalink     1 Comment     [, ]    

Minor Foray into Functional Programming

15:56 23 Feb 2010

Last night a friend asked me what functional programming was, and as part of my answer I decided to rewrite a trivial program in the functional style to see what it was like. I did this in Python without using the functional module.
[more...]

Permalink     5 Comments     [, ]    

Some Vim Script Implementation, Testing, and Hackery

23:50 16 Feb 2010. Updated: 00:57 17 Feb 2010

As a result of my porting over jEdit (Jython) macros to Vim, I now have a fair amount of (Python) Vim scripts, and have learned some things about how to set up those scripts. I’ll go through some of that below, and hopefully other people writing Python scripts for Vim will find it useful.
[more...]

Permalink     Comment     [, , , , , , ]    

First Post With Vim

20:05 14 Feb 2010

Over the last couple of weeks I’ve been hacking away on scripts to customize Vim, replicating the scripts I made for jEdit. I’m more or less done, and this blog post is being written in MacVim. This hopefully means that when I’m done with it I’ll be able to publish it from within Vim, the same as with jEdit.
[more...]

Permalink     1 Comment     [, , , , , , , ]    

The Python Challenge

15:18 04 Feb 2010

The Python Challenge seems like a good way to have fun with Python through puzzle-solving. As with all riddles, it’s important to read the questions carefully…

Permalink     Comment     [, ]    

Better Word Count in Vim

23:40 17 Jan 2010

I’m currently trying out Vim (again), and have made more progress this time, mainly due to Seth’s help. The key things that have made it better:

  • :set hidden. Absolutely critical, this. Stops Vim from complaining when you try to switch buffers and your current buffer has unsaved changes.
  • bufexplorer. Makes switching buffers a lot easier.
  • A better Python syntax file. I didn’t like the defaults.
  • My own indentation and syntax files for reStructuredText.

Really, though, the key first one was :set hidden. Before that I felt that I had completely misunderstood Vim’s file management model.
[more...]

Permalink     1 Comment     [, , , , , , , ]    

Code Katas

17:26 15 Jan 2010

I really like this idea from Dave Thomas: code katas, small pieces of programming practice involving some repetition. I came by it via Katacasts, a collection of screencasts of people doing the katas. I particularly recommend Gary Bernardt’s String Calculator in Python and Vim. (Which has inspired me to try once again to get past the vim file management issues I have.)

Permalink     Comment     [, , ]    

Python Coding Exercise: Nested Dictionaries

23:54 08 Jan 2010. Updated: 03:05 09 Jan 2010

I’ve been looking at a bunch of coding exercises recently, including the demo for Codility, and recalled an exercise that I came up with as an interview question. It’s not incredibly difficult, but strikes me as a good “real-world” exercise—it’s based on a task I had to perform while working on the discuss functionality for freebase.com.
[more...]

Permalink     Comment     [, ]    

Python Optimization Tips

06:34 05 Jan 2010. Updated: 09:41 14 Jan 2010

I came across these on Hacker News recently, and think they’re worth calling out: Python Speed Performance Tips.
[more...]

Permalink     4 Comments     [, ]    

CrossFit and Coding (and Meat)

10:28 31 Dec 2009

In 2009 I achieved some significant things that weren’t on my list of goals, although they’re not concrete achievements in the same sense.
[more...]

Permalink     1 Comment     [, , , , , , , ]    

Some Python Tips and Tricks

05:08 21 Dec 2009

Python Tips, Tricks, and Hacks at Siafoo is an excellent overview of useful Python knowledge. I was familiar with most of it but still think it’s worth reading over. I did learn a couple of new things, too.
[more...]

Permalink     Comment     [, ]    

GPS, URLs, Math, Python, Featuritis

23:53 08 Dec 2009. Updated: 01:43 09 Dec 2009

Earlier this evening Gever suggested a service dedicated to shortening URLs that had geolocation data in them. My immediate responses were that a) this was a great idea, and b) that I wanted the shortened URLs to still be human-readable in some sense—specifically, I wanted a person to be able to look at two URLs returned by this service and have some idea of how close to each other they were.
[more...]

Permalink     1 Comment     [, , , ]    

addons.mozilla.org Moving from CakePHP to Django

20:43 17 Nov 2009

This post details why they’re making the move. I find this of interest partly because it’s a move from a very popular web language (PHP) to one that’s become vastly more popular in the last couple of years (Python), and also because Django is the one major Python framework I haven’t tried out yet. Because of my liking for Python, I have a personal bias that makes me happy to see a prominent project such as this one move to the language.

I find it odd that they’re going from Subversion to git instead of to Mercurial, but I like the fact that they’re moving their documentation to the reStructuredText-based Sphinx.

Permalink     3 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     Comment     [, , , , , , ]    

404 Pages

23:36 10 Sep 2009. Updated: 00:38 11 Sep 2009

wpbeginner has a list of their favorite WordPress 404 pages. There are some good ones in there, but none of them do what I think they should do—that is, as well as provide helpful links to popular pages, also provide a list of guesses about where the user was actually trying to go. Coincidentally, today I also read this excellent article about finding the longest common subsequence between two strings. So, at some point, I’ll improve my blog’s 404 page so that it calls, probably via asynchronous request, a Python script that checks the user’s requested URL against all valid URLs WordPress knows about, and then suggests to the user whatever the closest matches are. I’m somewhat surprised that this isn’t done more often, but it seems that far too many sites are really blasé about 404s.

Permalink     Comment     [, , , ]    

Python Script for Subversion Status

23:48 02 Aug 2009. Updated: 00:54 03 Aug 2009

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     [, , , , , , , ]