Tuesday, October 24, 2006

Python: Myghty vs. Django templates

It came out today on the Genshi mailing list that Myghty performs far better than Django templates.

I personally think that the productivity improvements afforded by Genshi by things such as its match mechanism and the fact that you don't have to worry about XSS attacks fully compensate for any loss in performance.

However, I'd like to say something specifically about Myghty vs. Django templates. Both are good software. Both have friendly authors. I personally think in this case the performance issue is a red herring. If you can't trust your template authors to run with scissors, you should not use Myghty. If your template authors know the difference between a function and a dict, you shouldn't use Django templates.

I think that what the Django guys did makes a lot of sense for their situation, but using Django templates drives someone like me crazy. I like to create functions (containing re-useable bits of HTML) within my templates. I don't like repeating myself, even in HTML. Nor are custom tags a simple alternative. In stark contrast, Myghty's Component-Call-With-Content Arguments is frickin' awesome!

Ok, no religious wars. I really think you should just use the right tool for the right job!

Monday, October 23, 2006

Web Design: Faking Alpha Blending with gifs

pngs are nicer than gifs because they have true alpha blending, not just transparency. However, they don't work in IE. I'm using the "pngfix.js" script to make alpha blending work in IE, but it's just not perfect. The divs that the script adds mess up my CSS. True alpha blending becomes important if you're rendering your image on a complex background such as a gradient.

I found a suitable workaround. If I start with a png and get it to look right in the browser, I can grab a screen shot. Then I trim the outside of the image to make it transparent. Hence, the actual background is "hard coded" into the gif along the edges of where it becomes transparent so that true alpha blending is not necessary, but most of the image is still transparent so that the complex background shows through. Best of all, the gifs are a lot smaller than the original pngs.

I'm sure this trick is well known, but it was new to me. Oh the depths I go through to make IE users happy!

Friday, October 20, 2006

Vim: Getting Started With Vim

A couple of readers asked me to do an overview of how to get started with Vim. I'm sure there are far better overviews than mine, but here are some personal notes:

Be sure you take the Vim tutorial. You can use "vimtutor" from the shell command line.

When you're taking the tutorial, learn how to use "hjkl" instead of the arrow keys. Not having to use the mouse or the arrow keys and rarely needing to use the control key is one of the great benefits of being a Vim power user.

It's best to get Vim out of compatibility mode. You can do this by creating a .vimrc file (even an empty one) in your $HOME directory.

Next, I personally prefer to use gvim. Using the gvim command starts Vim in GUI mode. I like the GUI menus, etc. Of course, you'll need a version of Vim with GUI support. There are GUI versions of Vim for Windows, Linux, Mac, etc.

Next, if you can get Vim 7, do that. It has nice, native tabs and a bunch of other useful features.

In response to a reader's question, I think the auto folding comes builtin. I've never used it, so I can't say more. I have seen it though.

There's a Vim cheat sheet.

Here's a list of my favorite Vim commands copied from my notes:
  Format something: <highlight>gq<or motion>
Set a map:
:map <key> <several keystrokes>
Don't be afraid to use C-V to type special characters.
Visual mode (so you can visually highlight text):
v: highlight by characters
V: highlight by lines
C-v: highlight vertical blocks
<highlight><cmd><or motion>
cmd may be either "<" or ">"
You often follow this up with one or more .'s
Named buffers: "<letter><some cut or paste like command>
Create: q<letter><go about your business>q
Use: @<letter>
If you're on a search and replace mission, end your macro with a search for
the next thing you need to search for. Otherwise, your "search next"
will probably have been blown away during the macro.
@@ will repeat the last macro you executed.
Where was I?
^o: go backwards
^i: go fowards
This will even change your file!
m<letter>: Set a marker named <letter>
'<letter>: Jump back to that marker
cmdline mode:
"q:": Enter into cmdline mode.
^f: Enter into cmdline mode in the middle of typing a command.
omni complete:
^x^o: autocomplete code
I have a pretty simple, self-explanatory .vimrc file. About the only thing complex is that I specify how to indent various programming languages based on my psychotic understanding of the various style guides for those languages. To learn more about any of these settings, use "help somesetting" withing Vim:
" These are general settings.
set guifont=Monospace\ 9
syntax on
colorscheme torte

" autoindent works better if you fix the backspace key.
set backspace=indent,eol,start

" "T" toggles the taglist for ctags.
map T :TlistToggle

" These are settings for various file types.
au BufRead,BufNewFile *.c,*.h,*.cpp set ai sw=4
au BufRead,BufNewFile *.css set ai sw=4 sts=4 et
au BufRead,BufNewFile *.html set ai sw=2 sts=2 et
au BufRead,BufNewFile *.java set ai sw=4 sts=4 et
au BufRead,BufNewFile *.js set ai sw=4 sts=4 et
au BufRead,BufNewFile *.mhtml,*.mas set ai sw=2 sts=2 et ft=mason
au BufRead,BufNewFile *.ml,*.mll,*.mly set ai sw=2 sts=2 et
au BufRead,BufNewFile *.php,*.php3,*.php4 set ai sw=2 sts=2 et
au BufRead,BufNewFile *.pl,*.pm set ai sw=4 sts=4 et
au BufRead,BufNewFile *.py set ai sw=4 sts=4 et tw=72 " Doc strs
au BufRead,BufNewFile *.scm set ai sw=2 sts=2 et
au BufRead,BufNewFile *.sh set ai sw=4 sts=4
au BufRead,BufNewFile *.sql set ai et
au BufRead,BufNewFile *.tmpl set ai sw=2 sts=2 et
au BufRead,BufNewFile *.txt set ai sw=2 sts=2 et tw=79
You can download plugins for Vim from the Vim Web site. The taglist plugin is a must have for using ctags. I use this to see all of the functions and classes in the Python module I'm currently editing. The minibufexpl plugin is useful if you're not using Vim 7. It shows all of your open buffers in fake tabs. Be sure to add "set hidden" to your .vimrc if you use it so that you don't have to save the current buffer before switching to another one.

Vim 7 has some neat features such as an improved ability to edit and manage files over scp. I'm just getting started with it, but "gvim scp://foxmarks.com/" is already useful.

Also, I should mention something about workflow. IDE users and Emacs users rarely have more than one copy of their editor running at any one time. Since startup in Vim is so fast, I must admit to opening up multiple gvim windows all the time. I might have 5 of them spread across my screen at the same time. However, it's now possible in Vim 7 to open up a document from the command line in a new tab within the currently running editor: "gvim --remote-tab file.txt".

Furthermore, I should state that I don't use Vim as an IDE replacement. If you want a full IDE for Python, I can recommend Wing IDE. I don't use an IDE. Rather, I use Vim as an important part of my "development environment". I'm still very shell oriented. I use the shell for all sorts of things such as file management, Subversion, grep, find, wc, etc. Recently, I switched to zsh, but that's a topic for another post ;)

To finish this post, as well as to give a flavor for how I use the shell and Vim together, I'll show you how I do global search and replace across my entire project. Start at the top of the project in your shell. Then do: "gvim $(grep -rl sometext .)". This will open up all the files of interest. Then, "/sometext". This will jump to the text you're looking for. Use the "c" command to change the text in one shot. Now, "n" to jump to the next instance and "." to change that instance too. At the end of the file ":wn" to write and jump to the next file. Following this "work idiom" you can make each change by hand, but still do so really fast: "n.n.n.n.:wnn.n.n.n:wn". To make more complicated edits, I follow the same pattern, but I use a macro to change the text.

Happy Hacking!

Tuesday, October 17, 2006

Foxmarks: We Just Launched!

We just launched the new version of Foxmarks!
Foxmarks keeps your bookmarks automatically synchronized between two or more computers. A simple wizard guides you through the startup process. Foxmarks works silently in the background. As a bonus feature, you can access your bookmarks from anywhere via my.foxmarks.com. It's simple and solid.
We had to hustle in order to get it out the door before Firefox 2 comes out. The dust hasn't yet settled, but at least I got some sleep last night ;) Word to the wise--vainly attempting an all-nighter every night for a week just doesn't work. No, it doesn't matter how much coffee you drink!

Anyway, I'm pretty excited. Despite the short timeline, our team was amazingly well organized, and the code is really clean. My co-worker wrote the synchronization server using Twisted, and it's performing amazingly well. My boss is still primarily responsible for the extension itself, although he was very helpful with all the other code. I wrote the code for the new versions of www.foxmarks.com, my.foxmarks.com, and login.foxmarks.com (the common login server), but of course I borrowed from the previous code base.

If you're wondering, I used Pylons, Genshi, and SQLAlchemy, and I've been really pleased with the results. I also decided to go the whole "semantic markup" route. I used CSS for layout (although I'm still using tables for forms--old habits die hard). My knowledge of CSS has come a long way in the last week, but the markup looks really clean!

Saturday, October 14, 2006

Vim 7: Lovin' It

I finally installed Vim 7. I also took the time to install various plugins. Here are some things that are making me happy:
  • Honest to goodness, real tabs.
  • Auto-completion and built-in documentation lookup for many different programming languages.
  • Editing files and browsing directories remotely over scp works.
  • Subversion integration via the vcscommand plugin is helpful.
  • Vim is still charityware which makes me proud to use it.
Vim is still my favorite editor because I truly believe that its style of keybindings is faster for experts. Furthermore, it has one of the strongest and most flexible syntax highlighting systems:
  • It supports a ton of languages by default.
  • It does well with files that mix languages like HTML, CSS, and JavaScript.
  • It doesn't get confused by Python strings such as """He said, "hi"!""" ;)
I code in a lot of languages, and I love that Vim is consistent and helpful.

Friday, October 13, 2006

Books: Thank God for Editors!

Thank God for editors in general, and O'Reilly editors especially!

As I was reading an O'Reilly book on the train today, I realized how grateful I am for all of the hard work editors put into improving the quality of technical books. Let's face it, technical topics are tough, and programmers aren't always the best writers. As programmers, there's a monumental amount of learning we need to do in very little time using books written by other programmers who also have very little time. Editors help by making the text more uniform. Uniform text can be ingested more quickly. Without the painstaking effort that editors put in, I would have a much harder time reading what I need to read. In fact, every time I come across glaring typos or grammar mistakes, it distracts me from the technical material I'm trying to learn. Editors enable me to focus on the topic at hand.

I know that authors always thank their editors, and that this post might seem a little silly. However, I'd like to tell all the editors out there how thankful I am for the work they do!

Python: Modifying a Counter in a Closure

I helped a fellow Pythonista with a problem today, and it reminded me that a lot of people might not know about the trick of using a list to box variables within a closure. If that doesn't make sense, read the code below:
def counter(start):

"""Create a counter starting at ``start``."""

# The value "curr" needs to be wrapped in a list. Otherwise, when
# "+=" is used in "inc", Python binds the variable at that scope
# instead of at this scope. We don't want to redefine a variable at
# the inner scope. We want to modify the variable at this outer
# scope. Java programmers would call this "boxing", but they would
# use an Integer instance instead of a list.

curr = [start]

def inc():
"""Increment the counter and return the new value."""
curr[0] += 1
return curr[0]

return inc

if __name__ == '__main__':
c = counter(5)
for i in range(3):
print c() # Prints 6 7 8.

Wednesday, October 04, 2006

Python: Greenlets

Someone pointed me at Greenlets the other day, and I'm really quite excited about them. This fully addresses the Limitations of Coroutines via Enhanced Generators I spoke of earlier. I see that "some very smart people" have gone down the path of tying it together with kqueue and select.poll along with an IO-based scheduler.

Greenlets look polished enough, but I sure wish the other stuff was polished and documented so that I could use it in production. The pragmatist in me (which doesn't come out very often) is still insisting that Twisted is still the safer route in the near term even if it is more irritating to code because of its asynchronous nature.

Monday, October 02, 2006

Hacking: Yahoo Hack Day

I attended Yahoo Hack Day. It was a lot of fun. They put on a concert by Beck, which I unfortunately didn't get to see. They also loaded us up with plenty of swag, including a T-shirt, a copy of Yahoo Hacks, and a 256 mb non-branded USB flash drive, which was one of the weirdest but most useful bits of swag ever. I attended talks all day long. Here are some of the takeaways:
  • Yahoo! UI Library: Fonts CSS is a CSS library that completely levels the playing field concerning fonts across all of the browsers. I'm not that great a CSS developer, so I wasn't all that excited about this, but my friends explained to me how hard cross-browser uniformity is to achieve.

  • Yahoo! UI Library: Grids CSS is a CSS library that lets you do standardized layout in a cross-browser way that works now. I wasn't so thrilled about this either since I knew that even though CSS layout isn't ready for prime time, tables still do the trick. However, once again, my friends set me straight by explaining how powerful it was to simply flip a few class attributes and have the layout change drastically, even within the user's browser. That at least is a bit harder to do with tables.

  • There was a fantastic talk given concerning the history of JavaScript and the Web. The speaker made a good point that it's amazing the stuff even works if you consider that the Web is really just a series of hack upon hack. He gave a plug for JavaScript: The Definitive Guide, specifically the 5th edition. He said that this book sucks the least in terms of really understanding the functional nature of JavaScript. He also confirmed what I had long believed that JavaScript is the most misunderstood language ever. He also made a good point about how bad the standards were for JavaScript:

    • The ECMAScript standard is unreadable. Furthermore, Microsoft insisted on encoding various bugs in the standard itself.
    • Neither was the W3C blameless. Consider how hard it is to use the DOM API in contrast to innerHTML. However, the DOM API is in the standard, and innerHTML isn't. Next consider how "un-JavaScript" code such as .setAttribute('foo', 'bar') is when you consider it next to code such as img.src = 'foo.jpg'!

    I asked him if there was anything substantial missing from Dojo Toolkit that was present in the Yahoo! UI Library (YUI). He said no, but that there were probably too many JavaScript frameworks right now, and the market would probably only support Microsoft Atlas and YUI in the long term. For being such a well-educated guy, this really surprised me. Perhaps he was just taking the party line, or perhaps he had a case of "not invented here".

  • "Semantic markup" is the idea that your markup should say what it means instead of how it should appear. "Progressive enhancement" is the idea that when you build a Web application, you should start with semantic markup that actually works for any browser. Then, on top of that, you use deep CSS and JavaScript tricks to enhance the experience for "A-grade" browsers. Notice that Yahoo's graded browser support is pretty inclusive. Also note that Yahoo is still using HTML 4.01 Strict.

  • Yahoo! UI Library: Event is exactly what you would expect from a JavaScript event library. There was nothing astonishing or surprising.

  • FireBug, Venkman, Web Developer toolbar, and the JavaScript debugger found on the Microsoft Office CD are great tools to debug JavaScript, but JSLint got a couple of plugs as well. Fiddler is an HTTP debugging proxy which logs all HTTP traffic between your computer and the Internet, which can be helpful to debug AJAX requests. Drip is a memory leak detector for Internet Explorer.

  • Yahoo's Design Pattern Library got a plug as well. I saw a talk on this a few weeks ago, and it looked pretty useful.

  • And of course, one of the speakers gave a plug for "working is better than elegant", which is always a challenge to perfectionists like me.