Code Monkey Do

2010-05-19

A git workflow using bundles

Disclaimer: I am a git n00b, so there are probably better ways to do this.

Below is the git workflow I use for cases when I do development on two boxes (named Local and Remote below), but there is no way to clone/fetch/pull directly.

(Local) Create the initial bundle and create a tag:

$ git bundle create toremote.bundle HEAD
$ git tag toremote

Transfer the bundle file to the remote computer.

(Remote) Initialize an empty repository, and pull from the bundle file:

$ mkdir repo
$ cd repo
$ git init
$ git pull /tmp/toremote.bundle

(Remote) Create a tag:

$ git tag fromlocal

You now do some bugfixing or development on the remote computer. Use git as normal and commit your changes.

(Remote) Then create a bundle containing your changes, and create a tag:

$ git bundle create tolocal.bundle fromlocal..HEAD
$ git tag -f tolocal

Transfer the bundle file to the local computer.

(Local) Pull from the bundle to update the local repository:

$ git pull tolocal.bundle
$ git tag -f fromremote

You then do some hacking on the local computer. Use git as normal and commit your changes.

(Local) Then create a bundle containing your changes, and create a tag:

$ git bundle create toremote.bundle toremote..HEAD
$ git tag -f toremote

Transfer the bundle file to the remote computer.

(Remote) Pull from the bundle and create a tag:

$ git pull /path/to/toremote.bundle
$ git tag -f fromlocal

2009-06-21

Using the Blogger API from Python

Blogger exposes a Google Data API. Using this API, you can access all parts of your blog using simple HTTP requests. The REST-style API is based on the Atom Publishing Protocol.

It is very easy to access the blogger API (or any Google Data API) from Python using the Google Data APIs Python Client Library (gdata-python-client). This library is easy to use, has great documentation and great sample code included.

Using reStructuredText and gdata-python-client I created a simple application (blogger.py) that takes a rst file, converts it to HTML and publishes the HTML on my blog.

Using this approach, I can edit blog posts in a my editor of choice, using an easy-to-read plaintext markup syntax.

Example use:

# first, get the blog id
(vanilla)[~/misc/blogger]
$ python blogger.py --username my.email@gmail.com --listblogs
Password:
nnnnnnnnnnnnnnnnnnn: Code Monkey Do

# then create a new blog post
(vanilla)[~/misc/blogger]
$ python blogger.py --username my.email@gmail.com --blog nnnnnnnnnnnnnnnnnnn post1.rst
Password:

$ python blogger.py --help
...

blogger.py source code (on github):

import rstdirective

def login(username, password):
    import gdata.service
    service = gdata.service.GDataService(username, password)
    service.service = 'blogger'
    service.server = 'www.blogger.com'
    service.ProgrammaticLogin()
    return service

def create_entry(title, content, draft=False):
    import atom
    import gdata
    entry = gdata.GDataEntry()
    entry.title = atom.Title(title_type='text', text=title)
    entry.content = atom.Content(content_type='html', text=content.encode('utf8'))
    if draft:
        control = atom.Control()
        control.draft = atom.Draft(text='yes')
        entry.control = control
    return entry

def listblogs(service):
    feed = service.Get('/feeds/default/blogs')
    for blog in feed.entry:
        print "%s: %s" % (blog.GetSelfLink().href.split('/')[-1],
            blog.title.text)

def listposts(service, blogid):
    feed = service.Get('/feeds/' + blogid + '/posts/default')
    for post in feed.entry:
        print post.GetEditLink().href.split('/')[-1], post.title.text, "[DRAFT]" if is_draft(post) else ""

# ... see full source code on github: http://github.com/codeape2/python-blogger/tree/master/blogger.py

If you are not familiar with the reStructuredText format, have a look at this example, the rst source for this post.

You can download the entire source code from github.

2009-06-04

Stack overflow reputation

I've been using SO for a while now. Nice site for programming QA (I try to answer some Python questions when I have the time). Recently, they added 'flair' - the ability to display your SO reputation on you site. Here's my SO reputation points:

2008-08-29

A new brace style for C-like languages

Today I came up with a brand new brace style, heavily influenced by Python. It is like K&R, but with the closing brace appended to the last line of the block. It looks like this (the example code is C# that targets CLR v 1.1):

using System;
using System.Collections;
using System.Reflection;

namespace Utils.Documentation {

public class TypeDocumenter {
   private Type _type;
   private MethodInfo[] _methods;

   public TypeDocumenter(Type type) {
       Debug.Assert(type != null);
       _type = type;
       _methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); }
  
   public MethodDocumenter[] Methods {
       get {
           ArrayList retval = new ArrayList();
           foreach (MethodInfo m in _methods) {
               if (! m.IsSpecialName) {
                   retval.Add(new MethodDocumenter(m)); }}
           return (MethodDocumenter[]) retval.ToArray(typeof(MethodDocumenter)); }}

   public DocumentationParser XmlDocumentation(DocumentationReader docreader) {
       Debug.Assert(docreader != null);
       return new DocumentationParser(docreader.GetXmlDocumentationForType(_type)); }}}

This style feels and looks very natural to me, and I am going to start using it for all C# code I write from now on (which is very little - I do 90% of my development in Python).

I use a combination of VIM and Visual Studio .NET when developing C# code. The VIM editor handles this brace style very naturally. I haven't had time to tinker with VS.NET's indentation settings yet.

Notice that the namespace declaration has the same indent level as the class declaration. I have always done this when programming C#, since the horizontal whitespace used for indenting a class does not add any information.

Previously I used the Allman/Emacs brace style, BTW.