Simple Intro to Git

Looks like a nice intro to Git. Skips a lot of the in-depth details you don’t necessarily need to get started.

http://rogerdudler.github.com/git-guide/

Roll Your Own OS Links

Just a couple of links I’d like to save for the future.

Roll Your Own Toy UNIX-Clone OS – Less theory and more implementation details.

OS Dever Links – Lots of good stuff here. Especially the ‘Lets Build a Compiler’ article.

Flat File (Fixed Width) File Generator in C#

I came up with a custom attribute and class that would allow you to generate a fixed width file from a list of objects.

Basically, you create a class to store a file record. Apply the FlatFileAttribute the the class properties describing where a field starts and how long it is. You then pass a list of objects of your record type along with a file path to the writeFile method. Out comes a file in the format you specified in your record class.

Check out the code here https://github.com/mbowcock/flat-file-generator

Remap Caps Lock to Ctrl in Ubuntu 11.10

I like to remap Caps Lock to Ctrl for use in Vim. Remapping in Ubuntu 11.10 (and recent versions) is pretty easy.

System Settings > Keyboard Layout > Options

You’ll be presented with a list of layout options. Click Ctrl key positions and select ‘Make Caps Lock an additional Ctrl’. Done.

Subversion Commit Emails on Windows Using Python

Setting up subversion commit emails using python on Windows.

First things first – you’ll need subversion and python installed.

Subversion hooks are setup by dropping an executable in the /repositoryPath/hooks/ directory. On Windows this executable needs to be either a .BAT or .CMD file. From this script you can call your python code to perform any processing. One gotcha on Windows is the $PATH environment variable is not passed so you’ll need to set this up in your cmd/bat file.

My post-commit.cmd file looks like this:

@echo off set REPOS=%1 set REV=%2 set PATH=c:\python31; python.exe d:\repositories\reponame\hooks\processCommit.py %REPOS% %REV%

The post-commit hook gets sent two variables from svn – the repository and the revision. I save those, set the path, and call processCommit.py.

processCommit.py

import smtplib, subprocess, sys import email.utils from email.mime.text import MIMEText repo = sys.argv[1] rev = sys.argv[2] repository = 'This_is_the_name_in_the_email' sender = 'subversion@emailserver.com'                     # Change to any email address receivers = ['recp1@your.com', 'recp2@your.com']          # List of email recipients # Get subversion information # 'svnlook changed' returns what files / paths have changed # 'svnlook info' returns author, date/time, log message cmd = 'c:\\program files\\Path to SVN\\bin\\svnlook changed %s -r %s' % (repo, rev) changed = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] cmd = 'c:\\program files\\Path to SVN\\bin\\svnlook info %s -r %s' % (repo, rev) commitMsg = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] message = """A commit has been made to the %s repository *** Commit Information *** """ % (repository) message = message + commitMsg.decode('utf-8') + '\n*** Changed Files ****\n\n' + changed.decode('utf-8') msg = MIMEText(message) msg['To'] = ', '.join(receivers)          # Generate string containing recpts. comma separated. msg['From'] = email.utils.formataddr(('Subversion', 'subversion@emailserver.com')) msg['Subject'] = 'Subversion Commit | %s' % (repository) try:         session=smtplib.SMTP('SMTPSERVER')      # Your SMTP Server         session.sendmail(sender, receivers, msg.as_string())         print('Notification of commit sent') except SMTPException:         print('Unable to send notification')

svnlook is your friend when writing svn hooks. The core of processCommit.py is using svnlook to retrieve information about what was changed and who changed it. The rest of the code is straigh-forward, basic python. After the information is retrieved from svn, the message is formatted, and sent on it’s way using email.

Vim, Tidy, Windows

Getting Vim and Tidy to play nice on Windows.

Tidy is a nice little app to pretty-print / beautify your XML (html, etc.). It’s straight-forward to get working with Vim on Windows.

(I assume Vim is already installed)

1. Get the Tidy binary. Drop that somewhere on your system. (I dropped it in the root, c:\)

2. Edit c:\program files\Vim\vim7x\ftplugin\xml.vim
– If file doesn’t exit then create it
– For 64-bit os edit – c:\program files (x86)\…

map <F5> :%! c:\tidy.exe -q -i -xml % <CR>

This will map F5 to run tidy on the current buffer

You can find my xml.vim in my github config files repo.

Webpy Sessions with WSGI

I was having trouble getting sessions to work using webpy 0.34 and apache/WSGI and realized late last night what the issue was.

I created a basic app and added the obligatory wsgi code -

if __name__ == "__main__":app.run() #code necessary for wsgi here app = web.application(urls, globals(), autoreload=False) application = app.wsgifunc()

Then without thinking about it I went and looked over the examples of session handling in the webpy docs and added the code to create the session store as was shown in the examples. This code was added to the head of the file just after the imports –

store = web.session.DiskStore('sessions') app = web.application(urls, globals()) session = web.session.Session(app, store)

This looked good but no luck. Tried disk store and a db store for the sessions and nothing. Well after a couple of hours of staring at the code I realized I was declaring “app = …” twice in the code. Once to setup the sessions and then again to setup wsgi. So to fix I moved the code to handle the wsgi setup to run before the session setup code.

So now my session and wsgi setup code looks like this. This code is just after the definition of the URLs -

urls=('/', 'index') #code necessary for wsgi here app = web.application(urls, globals(), autoreload=False) application = app.wsgifunc() #code for sessions here store = web.session.DiskStore('sessions') session = web.session.Session(app, store)

So far this seems to work.

Video: So You Want to Start a Web Startup (NSFW)

Project Euler Problem 3 in Scheme

Since I’m working my way through SICP I ended up using some of the code from the examples in section 1.2.6 to search for prime numbers. Six of the eight functions below are used to test if a number is prime. (next-prime …) just starts at the current prime number and iterate upward until it finds the next prime number. (search-for-factors …) does the actual searching for prime factors.

(define (smallest-divisor n)   (find-divisor n 2)) (define (divides? a b)   (= (remainder b a) 0)) (define (square n)   (* n n)) (define (next-divisor n)   (if (= n 2)       3       (+ n 2))) (define (find-divisor n test-divisor)   (cond ((> (square test-divisor) n) n)         ((divides? test-divisor n) test-divisor)         (else (find-divisor n (next-divisor test-divisor))))) (define (prime? n)   (= n (smallest-divisor n))) (define (next-prime n)   (let ((m (+ n 1)))     (if (prime? m)          m          (next-prime m)))) (define (search-for-factors n factor)   (cond ((= (/ n factor) 1) (display factor)                                     (newline))         ((divides? factor n) (display factor)                                    (newline)                                    (search-for-factors (/ n factor) factor))         (else (search-for-factors n (next-prime factor))))) (search-for-factors 600851475143 2)

SICP Section 1.2.4

The code below is exercises 1.16 and 1.17. I’ll finish up 1.18 and 1.19 some time in the future. I’ve created a repository on github to store the code I write while working through SICP – if you’re interested it can be found at
http://github.com/mbowcock/SICP.

Exercise 1.16 -

(define (square n)   (* n n)) (define (fast-expt-iter b n a)   (cond ((= n 0) a)         ((even? n) (fast-expt-iter b (- n 2) (* a (square b))))         (else (fast-expt-iter b (- n 1) (* a b))))) (define (fast-expt-new b n)   (fast-expt-iter b n 1))

Exercise 1.17 –

(define (double n)   (* n 2)) (define (halve n)   (/ n 2)) (define (*. a b)   (cond ((= b 0) 0)         ((even? b) (*. (double a) (halve b)))         (else (+ a (*. a (- b 1))))))