About

Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt

Calendar

««Jul 2009»»
SMTWTFS
    1234
567891011
12131415161718
19202122232425
262728293031

Mailing List

My RSS Feeds








Latest Entries

Helping the Python Excel story

2009.06.19 Fri 3:15 P GMT-07

I have posted previously about Excel and Python. Which led to my blog being a top hit for those terms for a while. Chris Withers has decided that there should be a central hub for reading and writing Excel with Python. This is great and should help provide a clearer story for Python and Excel (remember folks, these libraries are pure python, so you can generate .xls on your Mac or Linux (or MS) box).

As my brother noted if you are looking for professional assistance in generating .xls in python (or for that matter Python in general) feel free to contact me.

tags:            

Early father's day....

2009.06.02 Tue 9:22 P GMT-07

My kind wife let me and a buddy get away last week. We wanted to hike the Black Hole but the 30% chance of rain scared us out of walking down a slot canyon that drains large swaths of land. So, instead we left for Moab early in the morning. Rode on the surreal terrain of Slickrock. Rode into Dead Horse Point, and saw a fascinating interaction between a ladybug, lizard and hummingbird. Saw "Indian Writing". Climbed a crazy (too much run out) slab at Wallstreet. Played Disc Golf at a brand new 18 hole course. That was the first day. Exhausting and awesome.

The next day we went to an Ultimate Hat Tournament. We played some nice Goaltimate (although the hoop was slightly oversized). Then we split up into teams, and played some 5 games. Twas fun. The parks/fields in Moab are super plush and green. (Much nicer than in Salt Lake.) I forgot how much stuff there is to do around Moab....

Now I need to find a way to pay back the wife....

Another reason to learn python: outsourcing insurance

2009.04.29 Wed 10:44 A GMT-07

Looks like they won't be outsourcing your python job to India soon ;) (via Planet Sugar) When I was in India it was clear people were reluctant to work for a small company, there was much pride in being a Java-drone at a large company. Perhaps this is similar to languages. Where is the pride in knowing python if half the people you talk to (who are technical) don't even know what it is?

tags:      

Python and emacs (4): Whitespace, tabs, tab-width, visualizing...

2009.04.09 Thu 11:41 A GMT-07

Whitespace! The bane of all aspiring python programmers! After their first day of python programming, it never seems to bother them. Until they get the dreaded file that uses tabs instead of more useful spaced indentations. (I've heard 6 is the new 2. At least that's what they're teaching at MSU).

Emacs (builtin python-mode) allows you to just open their tab delimited files and start working with them just fine. You will follow pep8, and be consistent. No harm done. Allow the infidels to keep their tabs.

Now weird spaced indentation (ala googly 2 space or MSU 6 space), is handled pretty easily as well. Just use M-x set-variable tab-width to the appropriate size. (M-x describe-variable tab-width will show the current value). (A better explanation of tabs/spaces in emacs is found here)

The real problems come when tabs/spaces are intermixed. Out of the box emacs handles that pretty well too. Select a region (C-x h (to select the whole buffer)) and M-x untabify will replace tabs with the tab-width number of spaces. That works pretty well too. (There's also the tabify command for you infidels).

Since some of the fancy gui editors/IDEs out there visually highlight the differing indentation, surely you should be able to replicate this on your emacsen. Alas, emacswiki to the rescue. After an initial hint to look at "blank-mode", it turns out that whitespace-mode is the blank-mode++, (blank-mode was so 2006). Plus documentation found in the wiki is quite nice.

If you have mixed indentation in python, whitespace-mode is handy. I'm finding it even more handy in my html templates that have dynamic css/js/html (where I've yet been able to get an editor to understand what it's supposed to be doing. Yeah, some of it could be isolated to it's own files. Some of it is dynamic and the templating languages further confuse emacs). So when your co-workers might be using Textmate (which might also be confused on such files), tabs might find their way in. whitespace-mode allows one to quickly view and clean up their until now hidden snafus.

The screenshot below illustrates whitespace-mode. In the top buffer is a previously mentioned html template. Red indicates extra space at the end of the line. >> indicates evil tabs. Dots indicate spaces, and $ is newline. You can see that one line has spaces and tabs intermixed! Arghh, my eyes are burning! The middle buffer shows some python code using tabs with whitespace-mode off. The bottom buffer shows the same code with whitespace-mode on.

I'm not going to leave whitespace-mode on all the time. On clean code it's distracting. But when I'm feeling like refactoring/cleanup/examining new code it comes in handy.

Clone Digger - another tool to add to your python belt

2009.03.19 Thu 9:11 P GMT-07

A few years back my boss asked me how I would implement code duplication detection. Real duplication would ignore variable renames, comments, and indenting (for non-python). A naive solution would just use string comparisons and not be too useful. I thought about it for a minute and said I'd make an AST and for the code and compare the trees. Not that it's a particularly novel idea, we were just discussing the value of the IP of some companies...

Well, there's been code floating around the intertubes for the past year or so that does just this for python code. The project is called Clone Digger and works on Python and Java. The output is an html file showing chunks of code that either differ by variables or operation changes (in red) or code that is the same (in blue).

I think Clone Digger is another useful tool to use for code reviews, when inheriting a bit of code, or after developing a chunk of code. This is going to be very useful for some refactoring we are doing at work. Now if it only worked on JavaScript....

Utah Python March 2009: GridBackup

2009.03.16 Mon 9:45 P GMT-07

Had a great presentation by Shawn Wilden on GridBackup. GridBackup is a friendnet that he's building on top of the allmydata tahoe grid. It's a work in progress, but he's clearly done some up front thinking as evident from his slides.

Shawn's aim is to have the (dumb) end user install an application on their windows/mac/linux box and have it just work as a complete backup system. Sounds like a great goal. It will be fun to follow. Wouldn't it be sublime if a dd-wrt-like firmware included a friendnet solution such as this out of the box?

Easy slideshow creation with rst2odp

2009.03.16 Mon 9:27 P GMT-07

I've used rst2odp a few times now for slide generation. It does a pretty good job. It's still lacking some features that the s5 stuff has, but has other features. Notably missing is footers (the support is in there in the backend library I just don't care for them so I've yet to hook them up in the rst2odp script), table support (again haven't needed to scratch that itch, but would be easy to add), incremental mode only supported in lists (not paragraphs, this is a feature of OOo/ppt), image placement is basic, and only supporting one text frame. Also there is support for slide import (from existing decks, even works for ppt (if you convert it to odp)) in the backend, just haven't written the rst directive for it.

Here's some features that are different from rst2s5.

Titleless slides Instead of underlining some words for a title, just underline nothing and you'll get a blank title. I use this in combination with:

Centered huge text like so:


--------

.. class:: center huge

A slide with centered, huge text and no title

Source code formatting Thanks to pygments we get source formatting like so:


Source code
-----------

.. code-block:: python

  def foo(bar, baz):
    fizzle(bar, baz)

Pretty straightforward eh? The tarball includes a doc directory with an example slideshow illustrating the features. To generate a slideshow using an OOo template do the following:

rst2odp slides.rst --template-file template/darkGradient.otp output.odp

Then use OOo to present it. Or convert to PPT or PDF and use your favorite viewer....

Python and emacs (3): (Balancing) parentheses and others

2009.03.10 Tue 12:54 P GMT-07

(Again not necessarily python specific)

One nice feature of emacs is show-paren-mode. (Naming it show-paren-mode rather than paren-show-mode makes it somewhat less discoverable but I digress). This nice little feature allows one to make sure parens (or brackets or curly brackets) are in order. It does so by visually highlighting the matching pair when the cursor is over the starting one (or after the ending one). It's very useful when you have a long dictionary with lists as values. The emacs wiki page includes a function for showing matching ends when one is offscreen. Very handy indeed.

To enable show-paren-mode type M-x show-paren-mode or put something like this in your .emacs:

;; highlight balanced parens
(add-hook 'python-mode-hook (lambda () (show-paren-mode 1)))

Apparently one of the critically acclaimed of Textmate is autopairs (or something like that). People love it cause it saves them a keystroke. If you type an opening paren, it adds a closing paren and moves the cursor inside the parens. Nice! How to get that in emacs?

As always in the open source world, there are many ways. Here's what I went through:

I searched for electric pair and found this entry in the emacs wiki. It's a good start but it has some drawbacks. It only inserts the matching pair when the cursor is at the end of the line (admittedly that's probably the case 80% of the time). So it won't work on nested items (say a string inside a list).

To deal with that first issue I tried skeleton mode. This gets around the embedding issue.

Then I find this blog post illuminating me on the second issue. I didn't think of this at first but if you have electric completes you probably want electric deletes too. So I'm currently using a modified version of his code (added support for single quote). This appears to handle triple quoting that is common in python docstrings too.

Marcelo's blog also mentioned paredit which seems to be quite featureful for lisp source, but has issues with python (no single quotes, space inserted between started paren and previous character, complex source). So I only tried it for a minute or so.

Good luck with your balancing!