2012-07-05

Python Blog

Continuing my whirlwind tour through scripting languages, Here are the main parts of my site generator redone in Python:

Site Generation

os.chdir("entries")
entries = os.listdir(".")
entries.sort()
entries.reverse()

posts = ""
for e in entries:
  cmd = "markdown %s" % e
  post = os.popen(cmd).read()
  date = e

  post_html = template_post % (date,date,post)
  posts += post_html

main_page = template_master % posts

print main_page

Table of Contents

#!/usr/bin/env python

import os
import sys
import re

output = []
for arg in sys.argv:
    with open(arg) as f:
        for line in f:
            if (re.search(r"^###", line)):
                date = arg
                line = re.sub(r"### ", "", line)
                line = re.sub("\n", "", line)
                output_line = '<a href="index.html#%s">%s</a><br>' % (date, line)
                output += [output_line]

output.reverse()
for line in output:
    print line

Python SimpleHTTPServer

Also serving the site using the below code utilizing Python’s included SimpleHTTPServer package.

import SimpleHTTPServer
import SocketServer
import os
import grp
import sys

PORT = int(sys.argv[1])
user = int(sys.argv[2])
group = int(sys.argv[3])

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

os.setgid(group)
os.setuid(user)

print "dropped privileges to user: %s group: %s" % (os.getuid(), os.getgid())
print "Serving at port", PORT
httpd.serve_forever()

Favorite Programming Languages

Currently the list of languages that I am most attracted to is:

  • C
  • Go
  • Perl
  • Erlang
  • JavaScript

This is different from the languages that I would actually claim to be somewhat competent in:

  • C (would like to be more so)
  • Awk
  • Tcl
  • Python
  • JavaScript
  • PHP (somewhat)

I feel like I’m becoming competent in Perl, and I am working to become competent in C# and possibly F# since those are languages that are used or potentially used at work.


Emacs Setup

Setting up yet another emacs instance:

Here are the entries in .emacs that I have to have:

(setq-default indent-tabs-mode 'nil)
 (global-set-key [(control h)] 'delete-backward-char)
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s