Programming and writing about it.

echo $RANDOM

Category: Uncategorized

Moved to GitHub pages

Hi all, my domain name https://echorand.me now points to my GitHub pages site. If you are a subscriber to my blog, please consider updating your feeds.

Atom feed: http://echorand.me/feeds/all.atom.xml

In more exciting news, my new book “Doing Math with Python” is out!

Advertisement

Book Review: Linux System Programming

I received a review copy of the book as part of the blogger review program.

I review for the O'Reilly Blogger Review Program

Title:  “Linux System Programming (2nd Edition) ” by Robert Love; O’Reilly Media.

Summary:  

This book consists of 11 chapters. The first chapter introduces you nicely to the the core topics and lays the foundation for the rest of the book. Files (including some hints on the role of the virtual file system and how they are represented in the Kernel), Input/Output (User buffered I/O, I/O scheduling, Scatter-Gather I/O), Processes (including their creation mechanisms and management), Threads (and how Linux implements them along with a treatment of the POSIX threads library), Memory (Process address space, dynamic memory allocation strategies, and how they work, memory locking) form the core of the book. The second last chapter discusses signal handling. The last chapter of the book is on time (the different types of time, how you can get/set time, measure time elapsed and timers) and is sort of a “standalone” topic for the book. The first appendix discusses the GCC extensions to the C language and can be handy when you read the Kernel source code.

Reactions:

In this book, the author discusses some of the most important topics that one would want to learn about when venturing into the area of “system programming” on Linux. He introduces the topics in a friendly manner adding some fun anecdotes from time to time (what does the “c” in calloc() stand for?).At various places, the reader is given a peek under the hood (for example, pause() is one of the simplest system calls implemented) which can only make the curious reader happy and itchy to download the kernel source code and start grepping. The book includes code examples throughout and hence if you are learning a topic for the first time, these are very useful starting points.

Verdict: 

System programming on Linux is an area encompassing number of related topics most of which can fill up whole books on their own. I also could not help comparing this book with “The Linux Programming Interface” by Michael Kerrisk (a book which I own already). Should you buy this book if you already own the latter? Yes, you should. While not being “encyclopedic” and not covering topics such as socket programming at all, Robert Love’s “Linux System Programming” has the right level of treatment and detail for the reader interested in system programming.

Product page:

Using the __cleanup__ variable attribute in GCC

GCC’s C compiler allows you to define various variable attributes. One of them is the cleanup attribute (which you can also write as__cleanup__) which allows you to define a function to be called when the variable goes out of scope (for example, before returning from a function). This is useful, for example to never forget to close a file or freeing the memory you may have allocated. Next up is a demo example defining this attribute on an integer variable (which obviously has no practical value). I am using gcc (GCC) 4.7.2 20121109 on Fedora 18.

Read the article here.

Book Review: Instant SymPy Starter

I received a review copy of Instant SymPy Starter from Packt Publishing. Here is my review.

Contents

As the name implies, this book is really short introduction to SymPy, and intended to get someone started. It starts by describing the installation procedures on various  operating systems and also mentioning the live shell which can be used to get a taste of SymPy without any installation. Then, the author introduces a curve sketching problem and uses it as a means to introduce various features of SymPy.

After that, the author describes some of the SymPy features as outlined in the table of contents.

Summary and my thoughts

I am not very clear about the purpose the book is aimed to serve other than making readers aware of SymPy. It is neither an exhaustive introduction to SymPy nor is it a collection of “recipes”.

In my opinion, the official tutorial is good to get started with and the documentation is detailed and exhaustive for getting started with and learning all about SymPy. 

Python classes: basics beyond the absolute basics

In this article, I will discuss a couple of things about Python’s classes assuming you know what the following does:

class Point():

    def __init__(self, point):
        self.point = point

To refresh, we define a class Point which we will use to represent the co-ordinates of a point in space. We can create an instance of this class representing a point as follows:

p1 = Point((1,2,3))

There are two things which you must note:

  • I assume that that the point is in a three dimensional space
  • You describe the point as a tuple (no particular reason)

And you can define methods to do stuff with these points (such as finding the euclidean distance between two points, and such), etc. You know all of that. Let’s start with the things you may not know.

I am using a Python 3 interactive session. The final program will however work on both Python 2 and Python 3.

Informative representation of your point

Let’s get back to creating the representation of a point using the above class: p1=Point((1,2,3)). Let’s see what print(p1) gives us:

>>> print(p1)
<__main__.Point object at 0x7fb123e67dd0>

So it tells us that p1 is a Point object and few other things which you can ignore. This is exactly what is, an object of Point class, but toPython. What is it to you or your program’s user? It is a point in space. Can we tell the user something more useful? We sure can. We will have to define a special method, __str__() in our class. This is how it will look after adding the method to class Point:

class Point():

    def __init__(self, point):
        self.point = point

    def __str__(self):
        return 'Point: {0}'.format(str(self.point))

The __str__() method above returns a string consisting of a string representing the point (str(self.point)) and a word saying that it’s a point. You may include any other helpful information you may find relevant here. The only thing you have to keep in mind is that the return value should be a string. Let’s try printing a point object now:

>>> print(p1)
Point: (1, 2, 3)

Much better isn’t it? Now your program’s user will have no problem in understanding what p1 is and also makes it easy for you to display points in your program’s output.

Definining custom operators

One of the basic things that you may want to do when you are working with points in space is find what is usually known as the euclidean distance. To refresh, for two points, p1 and p2 having co-ordinates (x1,y1,z1) and (x2,y2,z2), the distance is calculated as follows (in Python speak) math.sqrt((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2) (where math.sqrt is the square root function defined in the math module). For our Point class, you could define a method to do this. But, how about being able to do something like print(p1-p2) to print the distance between the two? This also plays well with our intuition of distance in 1 dimension (How far is 5 from 2? 5-2 = 3). Let’s try doing that with our current code:

>>> p1=Point((1,2,3))
>>> p2=Point((1,2,3))
>>> print(p1-p2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'Point' and 'Point'

Of course, Python doesn’t have any idea what you want it do when you ask it to subtract p2 from p1. However, we can tell Python what to do by adding a new method, __sub__(). After adding this method, the class looks like this:

import math

class Point():

    def __init__(self, point):
        self.point = point

    def __str__(self):
        return 'Point: {0}'.format(str(self.point))

    def __sub__(self, other):
        s=0
        for x1,x2 in zip(self.point, other.point):
            s = s + (x1-x2)**2

        return math.sqrt(s)

The _sub__ method basically calculates the euclidean distance and returns it. The speciality of this method is that when you ask Python to subtract something from an object of Point class, it calls this method and the return value of this method is returned as the result of the subtraction operation. If you are curious as to which is self and which is other in __sub__() when you ask Python to evaluate: p1-p2,self refers to p1 and other refers to p2.

Let’s now try subtracting p2 from p1 again:

>>> p1=Point((1,2,3))
>>> p2=Point((1,2,3))
>>> p1-p2
0.0
>>> p2=Point((1,2,4))
>>> p1-p2
1.0

So, you have basically defined the subtraction operator for objects of your Point class. Can you define other mathematical operators in such a fashion? Sure. Learn all about it here.

Here is the complete program (works with both Python 2 and 3):

from __future__ import print_function
import math

class Point():

    def __init__(self, point):
        self.point = point

    def __str__(self):
        return 'Point: {0}'.format(str(self.point))

    def __sub__(self, other):
        s=0
        for x1,x2 in zip(self.point, other.point):
            s = s + (x1-x2)**2

        return math.sqrt(s)

You may want to add the logic to your __sub__() method so that it checks if the points are of the same dimension. If not, then print a nice error message instead of printing a traceback. That’s it.

Also published here

Book Review: Learning IPython for Interactive Computing and Data Visualization

I received a review copy of Packt Publishing’s Learning IPython for Interactive Computing and Data Visualization by Cyrille Rossant. Although the book title mentions only IPython, the book looks into using a number of other Python tools and libraries of potential use to the intended audience. Here is my review.

(The book uses Python 2).

Chapters

The book has six chapters, so it’s a quick read. In the first two chapters, the author helps the reader getting started with using IPython (installation, basic things to do, using IPython as a shell) and also using IPython notebook for interactive python programming. He demonstrates how to perform basic profiling, measuring the run time of your scripts/statements and also discusses plotting with matplotlib (via pylab) from IPython notebook.

Chapter 3 introduces vector operations and using NumPy for performing the same. Topics such as indexing, reshaping are introduced in this chapter. This chapter also introduces the Pandas tool and demonstrates using it using a publicly available data set.

Chapter 4 discusses plotting, graphing and visualization in detail using matplotlib and others.

Chapter 5 discusses two main of concepts. One, running your programs on multiple cores and basics of using the Message Passing Interface (MPI). The second main concept discussed is using Cython. At the end, the chapter also mentions libraries such as Blaze and Numba which are of potential usefulness to  the intended audience.

The final chapter of the book discusses customizing IPython (creating profiles, etc.), and also shows you can create an extension that introduces a new cell magic.

Interesting Features

  • Hands-on style
  • Up -to-date information and references
  • Just enough information for the reader to learn and explore more

Summary

The book is interesting and pleasant to read and follow. It does well in introducing features of IPython and other tools of interest to the book’s audience. Definitely worth buying.

Book Review: Understanding and Using C Pointers

I received a review copy of the book as part of the blogger review program.

I review for the O'Reilly Blogger Review Program

Title:  “Understanding and Using C Pointers” by Richard Reese; O’Reilly Media.

Summary:  As the title implies, in the book “Understanding and Using C Pointers”, the author discusses pointers in C. It discusses various aspects of pointers and their application in dynamic memory allocation (and implementing linked lists, dynamic arrays, etc.), their usage in the form of function pointers, structure pointers and various other related concepts. The treatment of the topics have a nice flow. The author gently begins by introducing the concepts of a stack and a heap in the context of the runtime memory layout of a executing program and then proceeds to discuss memory allocation, before discussing the topics mentioned earlier. The book’s contents describe an area of C which can be scary and confusing sometimes, but need not necessarily be so – which is what the book aims to demonstrate.

Reactions: The book is absolutely fantastic and if you have anything to do with C or simply wanted to read an entire book on pointers, this is it. You will  learn a number of new things, and suddenly a number of pointer mysteries will be demystified.

Verdict: A must have for your collection if you would love to learn more about pointers in C or simply understand them better.

Product Page(s):

Linux System Mining with Python

In this article, we will explore the Python programming language as a tool to retrieve various information about a system running Linux. We start by exploring the platform module. For example, the platform.uname() function returns similar information to the Linux command, uname:

>>> import platform
>>> platform.uname()
('Linux', 'fedora.echorand', '3.7.4-204.fc18.x86_64', '#1 SMP Wed Jan 23 16:44:29 UTC 2013', 'x86_64')

Read the entire article here

Book Review: Computer Science Programming Basics in Ruby

I received a review copy of the book as part of the blogger review program.

I review for the O'Reilly Blogger Review Program

Title:  “Computer Science Programming Basics in Ruby” by Ophir Frieder, Gideon Frieder, David Grossman; O’Reilly Media.

Summary: In the book “Computer Science Programming Basics in Ruby”, the authors aim to introduce concepts from computer science to its readers. Instead of pseudo code, the authors use the programming language, Ruby (a small subset of it) to demonstrate the concepts. At the very outset, the authors make it clear that they will not go into the details of the programming language itself and instead focus on the computer science/algorithmic aspects. The book is quite brief and has an interesting objective.

Reactions: I think the book fails to achieve its objective. As far as introducing computer science concepts go, there is a chapter on computer hardware, bits and bytes, memory and a chapter on sorting algorithms. The rest of the chapters fall somewhere in between talking about purely programming constructs and computer science concepts.

Verdict: Overall, the book could do with treatment of more computer science focused topics, as it happens to be the objective of the book. This could be done with an “example” computer science focused topic at the end of each chapter which makes use of the programming concept introduced in that chapter.

Product Page(s):

Book Review: Packt Publishing’s NumPy Beginners Guide

I received a review copy of Packt Publishing’s NumPy Beginners Guide by Ivan Idris. The book looks into using the NumPy library for numerical computing in Python. Here is my review.

(The book uses Python 2).

Chapters

The book has 11 chapters. The book starts with a bit of history about NumPy and the first seven chapters look into various NumPy functions for array and matrix manipulation, linear algebra specific functions, statistical and financial calculations and others. Chapter 8 looks into unit testing your programs, followed by chapter 9 which looks into plotting with matplotlib. Chapter 10 discusses SciPy and the last chapter discusses PyGame and how to integrate it with SciPy and NumPy to implement cool-looking games.

Each chapter also has sections titled “Time for Action” and “What just happened?” with the former discussing a practical programming solution and the latter discussing it.

Interesting Features

  • Hands-on style
  • Pop quizzes are a fun addition
  • “What just happened?” explaining the “Time for Action” is interesting.

Source code

The book has lots of code. The code examples are short and mostly easy to follow and well explained most of the times. I didn’t sit on my computer and type the codes, so don’t know if there are any non-functioning codes.

Summary

The book is interesting and it does well in introducing features of NumPy using real life examples, instead of simply stating what a function does. However, depending on the reader, the examples can range from difficult to alien which will make the reader skim through those pages. The author does a good job, but book needs serious editing polish.