Sphinx as a Static Site Generator

In this article, we will take a look at Sphinx (specifically Sphinx 1.1.3) as a static
site generator tool. Let’s get started.

Installing Sphinx

Installing sphinx is as easy as pip install sphinx (on Linux and
Windows). pip will download a bunch of other dependencies (docutils,
Jinja2, pygments
as well). Once installed, check whether sphinx is
available to you by typing $ sphinx-quickstart at the command
line. This should give you a welcome message. We proceed from there in
the next section. (On Windows, if you have added
C:\Python27\Scripts to the Windows Path variable, the sphinx
commands should be already available to be executed since the
sphinx binaries are also installed in this location).

Read the entire article here.

Compilation and Interpretation in C and CPython

It is common knowledge that programs written in high level languages
have to be translated into a low level language using programs
referred to as translators. This low level language is either in a native form, in the sense that it is understood by the operating system itself, or in an intermediate
form which is understood by an intermediate program such as the bytecode
interpreter
. It is also known that C is a compiled language, whereas
CPython is both first compiled and then interpreted.

Read the entire article here.

Data in C and CPython: A comparison

In my last two articles (Data in CPython and Data in C), as it
turned out, I discussed two fundamental points in each language:

  • What happens in an assignment operation (such as a=2)?
  • Are separate copies of data created or references passed to the original when they are sent as function parameters?

In this article, I will summarize the findings by presenting a
comparative analysis. Read the entire article here.

Data in C

In C, the data you use in your programs will usually fall into one of the three
basic categories: int, char and float. Data in C has no
existence without an associated memory location labeled by an
identifier, usually referred to as a variable (the term variable
is a bit misleading, since it essentially means that it must always
vary, but you can have constant variables – i.e. variables whose
values do not vary). Considering this and C’s requirement for static
typing
, a variable declaration statement is required before data
can be stored in a variable. This declaration statement usually takes the
form of data-type var-name [= value], where the =value part may
or may not be present. For example, the statement int a=1;
declares a variable a which will store integer data and stores
1 in it. What this statment basically tells the C compiler is
that it should allocate a block of memory large enough to store an
integer and it will referred to as a. It is possible to obtain the
address of this memory location using the & operator.

Read the entire article here.

Data in CPython

When writing programs in Python (CPython), you have access to data types such as a int, str, tuple, list and a dict. It is fairly obvious what each of these data types would be used to represent: an int data type would represent an integer and a list would represent a list of items – homeogeneous or
heterogenous. As opposed to a language like C, the Python compiler
automatically decides what type to use for your data without the need
to be explicitly specified.

Read the entire article here.

Personalising my toolset: Konsole, BASH, Emacs, weechat, tmux..

Terminal Emulator and Shell

I use the KDE desktop (on Fedora), hence my choice of terminal emulator is KDE’s Konsole with the Solarized theme, which is built-in. I use the BASH shell, with almost no aliases and exports. However, I use a custom prompt so that I have a different color for the prompt and the output of the commands:

# prompt
# http://maketecheasier.com/8-useful-and-interesting-bash-prompts/2009/09/04
PS1="\[33[35m\]\t\[33[m\]-\[33[36m\]\u\[33[m\]@\[33[32m\]\h:\[33[33;1m\]\w\[33[m\]\$ "

Emacs

I have been a Emacs user for few years now. Unlike most people, I usually do very few customisations and/or install extra Emacs packages to enhance its functionality beyond the default features. However, last time I installed it after my upgrade to Fedora 18, I decided to spend some time customising it to my liking. I came across the awesome Mastering Emacs [1] blog of Mickey and used it as a reference point for making my Emacs customized to the way I hadn’t done before. When you first install Emacs (Emacs 24 is what I am using), on starting it what you see is something like this:

Image

The first things I do is remove the Menu bar, tool bars, scrollbar and the startup splash screen and message, which can be done with the following lines of Emacs Lisp:

(setq inhibit-startup-message t
      inhibit-startup-echo-area-message t)  
(menu-bar-mode -1) 
(tool-bar-mode -1) 
(scroll-bar-mode -1)

Now, where do you put these customization code snippets? There are three choices: ~/.emacs, ~/.emacs.el and ~/.emacs.d/init.el (where ~ indicates your home directory). I have the customization scripts in ~/.emacs.d/init.el (since that allows me to have everything in .emacs.d directory). See [2] for more details on init files.

Next, I add a few more customisations: always have line numbers and column numbers displayed and set auto-fill mode on (to automatically wrap lines). Here is how my init.el looks like now (lines beginning with ;; are comments are hence ignored):

;; hide them all.
(setq inhibit-startup-message t
inhibit-startup-echo-area-message t)
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)

;; Line number
(global-linum-mode 1)
;; Show column-number in the mode line
(column-number-mode 1)

;; Auto-fill mode
(add-hook 'text-mode-hook 'auto-fill-mode)

With the writing area quite to my liking, I next explore Emacs’s package manager [3] which will allow me to customize Emacs with a few more enhancements by installing packages. The package-list-package command will list all the currently available pacakages in ELPA with their status in my Emacs installation such as available, built-in.:

M-x package-list-packages

M-x package-list-packages

To install a new package, I can use the command package-install RET RET (or, you could use the mouse).

Theme: The first package I want to install is the solarized theme for Emacs. However, it is not available in ELPA for that, I will need to add another package repository, that of MELPA [4]. The following code in my init.el does that:

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)

I restart Emacs and the command package-list-packages displays a number of other packages from the MELPA repository, one of them being the solarized theme which I can install using: package-install RET color-theme-solarized RET . Now, I will need to add code to load the theme on startup. The following snippet does it:

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/solarized")
(load-theme 'solarized-dark t)

By default, the solarized theme will be installed (like other packages) in ~/emacs.d/elpa. To keep my themes in a separate directory, I create a sub-directory themes under my emacs.d directory and keep all the solarized files in a separate directory solarized. Now, when I restart Emacs, I see something like this:

Emacs + Solarized

Emacs + Solarized

This is pretty much my workable Emacs configuration right now leaving aside one more package: IEdit [5]. I think this will be pretty useful for a bulk search and replace. I installed it using: package-install RET iedit RET. To enable it, use the command iedit-mode or, use C-;. See: http://ascii.io/a/1923 for a quick demo. . I hope to learn more about IEdit as I start using it more.

Like other editors, Emacs creates a backup copy when you are editing a file. This leads to a pollution of your working directory with files whose names end with ~. To tell Emacs to save to a different directory, you can add the following snippet to your init.el (taken from [7]).:

(setq
   backup-by-copying t      ; don't clobber symlinks
   backup-directory-alist
    '(("." . "~/.saves"))    ; don't litter my fs tree
   delete-old-versions t
   kept-new-versions 6
   kept-old-versions 2
   version-control t)       ; use versioned backups

 

That’s it for my Emacs configuration for now. You may want to see some of the resources I have gathered so far here [6]. My init.el file looks like this now:

;; hide them all.
(setq inhibit-startup-message t
      inhibit-startup-echo-area-message t)  
(menu-bar-mode -1) 
(tool-bar-mode -1) 
(scroll-bar-mode -1)

;; Line number
(global-linum-mode 1)
;; Show column-number in the mode line
(column-number-mode 1)

;; Auto-fill mode
(add-hook 'text-mode-hook 'auto-fill-mode)

;; Package repo
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)
;; ;; (add-to-list 'package-archives
;; ;;              '("marmalade" . "http://marmalade-repo.org/packages/") t)

;;(package-initialize)

;; Solarized theme
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/solarized")
(load-theme 'solarized-dark t)

;; Font setting
(add-to-list 'default-frame-alist '(font . "DejaVu Sans Mono-14"))

;; Backup settings
(setq
   backup-by-copying t      ; don't clobber symlinks
   backup-directory-alist
    '(("." . "~/.editing_backups"))    ; don't litter my fs tree
   delete-old-versions t
   kept-new-versions 6
   kept-old-versions 2
   version-control t)       ; use versioned backups

Links:

[1] http://masteringemacs.org/
[2] http://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html
[3] http://emacswiki.org/emacs/ELPA
[4] http://melpa.milkbox.net/
[5] http://www.masteringemacs.org/articles/2012/10/02/iedit-interactive-multi-occurrence-editing-in-your-buffer/
[6] https://gist.github.com/4600244
[7] http://emacswiki.org/emacs/BackupDirectory

One last bit of customisation I do is always start Emacs in maximized mode. Hence, I have added a BASH alias: alias emacs='emacs -mm' in my .bashrc.

Weechat for IRC

I have been using Weechat for few weeks now and really like it. I have been meaning to switch to a console based IRC client for a while now, and weechat finally looked friendly enough for me to switch to one. Here are my notes [1] on how to setup a new server, autoconnect on startup, auto join channels, etc.

[1] https://gist.github.com/4487378

tmux

No customisations yet.

input() and raw_input() in Python

Consider the following Python interactive console session with the input() function:

>>> input()
1
1
>>> input()
'f00 bar'
'f00 bar'
>>> input()
'f00
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1
    'f00
       ^
SyntaxError: EOL while scanning string literal
>>> input()
1 67
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1
    1 67
       ^
SyntaxError: unexpected EOF while parsing

>>> input()
'1 67'
'1 67'

And now, the raw_input() function:

>>> raw_input()
1
'1'
>>> raw_input()
2 56
'2 56'
>>> raw_input()
'f00 bar
"'f00 bar"
>>> raw_input()
f00bar
'f00bar'
>>> 

And finally:

>>> input()
5+4
9
>>> raw_input()
5+4
'5+4'

As mentioned in the documentation for the input() function (Python 2), the eval() function is called on the input using input(). Hence, the input should be syntactically valid Python. With raw_input(), the input is returned as a string and hence can be anything that your program wants to handle. In Python 3, raw_input() has been renamed to input() and raw_input doesn’t exist.

Python 3.2.3 (default, Jun  8 2012, 05:36:09) 
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input

>>> raw_input
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'raw_input' is not defined
>>> input()
'f00bar
"'f00bar"
>>> 

Simulating c like scanf()

You can simulate C like scanf() behavior using raw_input() and split(). For example ( In Python 2):

>>> x,y,z=raw_input('Enter the co-ordinates:: ').split()
Enter the co-ordinates:: 1 5 6
>>> x
'1'
>>> y
'5'
>>> z
'6'
>>> int(x)
1
>>> int(y)
5
>>> int(z)
6

Since the input is considered a string, we have to do explicit type conversion if we want to treat the input as integers, for example.

self in Python

self keyword is a reference to the invoking class instance/object. Consider the following code:


''' Self really refers to the instance called with '''
class Point:
    
    def __init__(self):
        print 'Self identifier:: ',id(self)

if __name__ == '__main__':
    # create an instance of the class
    p = Point()

    print 'Point object identifier', id(p)
    
    # create an instance of the class
    p = Point()

    print 'Point object identifier', id(p)

As you can see, a class (that does nothing) called Point is defined and then two objects/instances are created. To verify that self is really a reference to the invoking object, we print the identifier for the calling object and self. Unsurprisingly, both are the same.

Update:
Soon after posting this, I realized that self is not a keyword. In fact its just a convention to use self as a reference to the invoking object. You can use anything else, for example, this:

class Point:
    
    def __init__(this):
        print 'This identifier:: ',id(this)

if __name__ == '__main__':
    # create an instance of the class
    p = Point()

    print 'Point object identifier', id(p)
    
    # create an instance of the class
    p = Point()

    print 'Point object identifier', id(p)

Passing around Arrays in Python (as compared to C)

For those of you with a C/C++ background (at some point of time, like me), passing around Python’s arrays (or lists) to functions can lead to a little bewilderment. For example, consider the following code snippet:

from array import array

def func(arr):
    print('Object Identifier: {0:d}'.format(id(arr)))
    for i in range(0,len(arr)):
        arr[i] = arr[i]**2

if __name__=='__main__':

    arr=array('i',[1,2,3])
    print(arr)
    print('Object Identifier: {0:d}'.format(id(arr)))
    # results in passing a reference to the
    # the original array
    func(arr)
    print(arr)

When you run the above snippet, it results in the following output:

array('i', [1, 2, 3])
Object Identifier: 3075060736
Object Identifier: 3075060736
array('i', [1, 4, 9])

It is a little surprising here, since the array is invoked in a call by value manner in C/C++. So why did the original array change? The answer lies in Python’s mechanism of passing a reference to the original object (the array object) itself, rather than a copy of it to the called function. Since, an array is a mutable object, the changes made to it is reflected in the original array sent. (You may think of this as equivalent to passing a pointer to the function). This idea of reference is better understood when you print the identifier of the array object in the caller and the called functions. As you can see, both of them are same. This won’t affect immutable objects such as a string, or an integer.

So, what do you do if you don’t want the changes made to an immutable object to propagate to the original object? Use the copy module. For example:

import copy
from array import array

def func(arr):
    print('Object Identifier: {0:d}'.format(id(arr)))
    for i in range(0,len(arr)):
        arr[i] = arr[i]**2

if __name__=='__main__':

    arr=array('i',[1,2,3])
    print(arr)
    print('Object Identifier: {0:d}'.format(id(arr)))
    # Pass the copy of 
    # the original array
    myarr = copy.copy(arr)
    func(myarr)

    print(arr)

The above code results in the following output:

array('i', [1, 2, 3])
Object Identifier: 3075063072
Object Identifier: 3075060736
array('i', [1, 2, 3])

As can be seen from the object identifiers, they are two separate objects and hence changes made in the called function is not propagated to the original object.

Code: https://gist.github.com/3487468

Note that I have used Python 3.3 for the examples.

Some related links:

Function objects from Strings in Python

Assume you have a set of functions like these:

def color_red():
    return 'I am red'

def color_green():
    return 'I am green'

def color_blue():
    return 'I am blue'

Now, in your main program, you want to call one or all of these, without creating an explicit ‘list’ of function objects. That is, you don’t want to have a list like this: [color_red, color_green, color_blue] and then simply iterate over the list and call each in turn. In some cases, you may need to construct a string literal of the function name, depending on some condition in your program. In that case, you need a way to convert a string literal to a function object.

The getattr() function can be used for this purpose. The following script demonstrates this:

#!/usr/bin/python
# Demo of constructing function object from strings

from __future__ import print_function

# module where the functions are defined
import funcs

# entry point when executed
if __name__=='__main__':
    colors = ['red','blue','green']
    for color in colors:
        funcname = 'color_{0:s}'.format(color)
        print(getattr(funcs,funcname)())

which results in the following output:


I am red
I am blue
I am green

Code: https://gist.github.com/3479054
Unrelated: This is similar to the function str2func in MATLAB.