Saturday, May 30, 2015

Use Python to Explain Adapter of Design Pattern

The Adapter is described in the Design Pattern book. Please refer the book for the details. A better way to learn Design Pattern is to program it by yourself. Python is a good language because it supports class and is easy to program than C++. That is why I select it to practice Design Pattern.

This page uses Python to implement an Adapter that displays a directory tree.

TreeDisplay is an abstract class that has two abstract methods GetChildren() and CreateGraphicNode(). The sample uses raise in both abstract methods to notify a programmer that the both are abstract that must be implemented in a derived class.




















DirectoryTreeDisplay1 is an Adapter that adapts Python OS package (Adaptee). GetChildren() uses Python OS package to get directory tree. CreateGraphicNode() transforms directory tree to readable text.














Test1() is a test function that tests DirectoryTreeDisplay1 ()











Below is a results after running the Python program in MacBook.
$ python ./DesignPattern/Adapter/TreeDisplay.py


The style of the directory tree is not good. A new Adapter, DirectoryTreeDisplay2, refines the style.















Test2() tests the new Adapter.










The better style of the directory tree displays as below after running the Python program.



























The below class diagram summarizes the implementation.


-Count

Friday, May 29, 2015

Some Bugs Happen When Disabling Debug Message

We sometimes find a hard bug that only happens on disabling debug. It is hard to fix because if we enable debug, the bug disappears.  In my experience, we can summarize the list that are principles of tracing source code.

1) Need to add delay.

We need to add delay-time to wait a response from a device. If we disable debug, we lost delay-time so that a bug occurs.

2) A variable must be initialized.

Sometimes an uninitialized variable will be got a good value when debug is enabled. If we disable debug, the value of uninitialized variable is bad so that a bug occurs.

3) Check the bound of a buffer.

We are always easy to write data out of a buffer. If we enable debug, the data doesn't affect our program. But if we disable debug, the data may crack our program.

-Count

Install binutils in Mac OS

Sometimes we need to install binutils in Mac OS to do something. Please refer the sites.

Homebrew
Building a GCC 4.9 cross compiler on OS X Mavericks


I use the below steps to build binutils in OS X Yosemite.

1) Install brew.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2) Installing GCC.

We need GCC to compile binutils in Mac OS

brew tap homebrew/versions
brew install --enable-cxx gcc49

3) Download binutils.

Download the source code of binutils. We get binutils-2.25.tar.gz.

4) Extract binutils.

Extract the source code to the home directory. For example,

~/src/binutils-2.25

5) Set environment variables.

We can set environment variables to build binutils.

export PREFIX="$HOME/opt/cross"
export TARGET=i686-elf
export PATH="$PREFIX/bin:$PATH"

6) Build binutils

In the ~/src/binutils-2.25 directory run the below command.

../binutils-2.24/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-werror

make
make install

8) Check if binutils is installed.

cd ~/opt
find . -name *objdump*

The results display.

./cross/bin/i686-elf-objdump
./cross/i686-elf/bin/objdump
./cross/share/man/man1/i686-elf-objdump.1

-Count