If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Tuesday, August 22, 2006

SCons

SCons is a great make replacement written in Python. It offers the following goodies (and many more):

  • Cross platform. Same script will compile your sources for Linux or Windows or ...

  • Support many tools out of the box (such as gcc/VC/java/tar ...)

  • Calculate if a file has change by digital signature
  • (and not by time stamp)
  • Automatically cleans after itself (no more make clean)

Scons Makefile is called SConstruct, here is a simple one:

Program("hw", ["hw.c"])


Which tells scons to build an executable called hw from the source file hw.c

Calling scons on Linux will produce:

[15:10] /tmp/hw $scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hw.o -c hw.c
gcc -o hw hw.o
scons: done building targets.
[15:10]


and on Windows it finds VC and invokes it:

C:\Temp\hw>scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
cl /nologo /c hw.c /Fohw.obj
hw.c
link /nologo /OUT:hw.exe hw.obj
scons: done building targets.
C:\Temp\hw>


Cleaning is simple as well:

[15:19] /tmp/hw $scons -c
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Cleaning targets ...
Removed hw.o
Removed hw
scons: done cleaning targets.
[15:19] /tmp/hw $


All of this in one line of SConstruct.

(Shameless plug: See here for a longer article on the subject)

Monday, August 14, 2006

Better dir

The builtin dir command is very useful. However it does not help that much when an object has many attributes.

I have a little function called la in my $HOME/.pythonrc.py

def la(obj, key=None, ignore_case=1):
'''List all attributes of object'''
import re
if key:
if ignore_case:
flags = re.I
else:
flags = 0
func = re.compile(key, flags).search

else:
func = None
print "\n".join(filter(func, dir(obj)))


This prints the attributes of an object one per line and also gives you the ability to search for specific items.

>>> from user import la
>>> import wx
>>> la(wx, "open")
ART_FILE_OPEN
ART_FOLDER_OPEN
EVT_MENU_OPEN
ID_OPEN
OPEN
Process_Open
wxEVT_MENU_OPEN
>>>


(Sun Aug 27 14:03:24 JDT 2006: Added regular expression to "la")

Saturday, August 05, 2006

Script Template

Sorry for the long delay, times are interesting around here.

Since many of my scripts share the same structure, I have a little template that I use for new scripts. I have a makepy command that copies this template to a new script.

#!/usr/bin/env python
''' '''

# Miki Tebeka

def main(argv=None):
if argv is None:
import sys
argv = sys.argv

from optparse import OptionParser

parser = OptionParser("usage: %prog ")

opts, args = parser.parse_args(argv[1:])
if len(args) != 1:
parser.error("wrong number of arguments") # Will exit

if __name__ == "__main__":
main()

As you can see, there is a place holder for the script documentation string. There is also a main function (inspired by Guido) and also a template for calling optparse command line parser module (most of my scripts take one argument, so this is the default).

Blog Archive