How to stash a single file in Git

Sometimes you may be working on your Git repo and discover that you have made changes in several files and need to stash changes to only some of those modified files. How do you accomplish that? Well, there’s a handy command that goes as follows:

git stash -p

This will go through each modified file and give you an option to stash the hunk by entering ‘y’, not stash the hunk by entering ‘n’, or perform other actions. A full list, found on Stack Overflow is shown below:

   y - stash this hunk
   n - do not stash this hunk
   q - quit; do not stash this hunk or any of the remaining ones
   a - stash this hunk and all later hunks in the file
   d - do not stash this hunk or any of the later hunks in the file
   g - select a hunk to go to
   / - search for a hunk matching the given regex
   j - leave this hunk undecided, see next undecided hunk
   J - leave this hunk undecided, see next hunk
   k - leave this hunk undecided, see previous undecided hunk
   K - leave this hunk undecided, see previous hunk
   s - split the current hunk into smaller hunks
   e - manually edit the current hunk
   ? - print help

Getting Started with Sphinx Search

As a web developer you may run into occasions where your database struggles to handle search queries. Have you ever been in a situation where you search for a query containing a few keywords and your website completely hangs? Not a very good feeling. This kind of issue happens usually on websites with at least thousands or even hundreds of thousands of rows and where search involves accessing several db tables. Today I’m going to discuss a solution to speed up search on a db. Have you ever searched on your website for something and get rather irrelevant results? The tool I’m going to discuss solves that problem as well. It’s called Sphinx. Sphinx is a powerful search engine written in C++ and allows blazing fast search ranked by relevance, much more than a traditional database can handle. Sphinx is open source and also available under commercial license. I’ll talk about how to set up Sphinx and get started with it on osX.

First, let’s prepare some data to work with. We assume our website is for car catalogs. Note: I got this sample data and PHP file courtesy of a very good Sphinx tutorial at http://www.ibm.com/developerworks/library/os-php-sphinxsearch/

Create a database testdb and add a few tables to it using the following SQL statements:

CREATE TABLE IF NOT EXISTS `Assembly` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `label` varchar(7) NOT NULL,
  `description` varchar(128) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `Assembly`
--

INSERT INTO `Assembly` (`id`, `label`, `description`) VALUES
(1, '5-00', 'Seats'),
(2, '4-00', 'Electrical'),
(3, '3-00', 'Glasses'),
(4, '2-00', 'Frame'),
(5, '1-00', 'Engine'),
(7, '101-00', 'Accessories');

-- --------------------------------------------------------
--
-- Table structure for table `Inventory`
--

CREATE TABLE IF NOT EXISTS `Inventory` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `partno` varchar(32) NOT NULL,
  `description` varchar(256) NOT NULL,
  `price` float unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `partno` (`partno`) USING BTREE
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;

--
-- Dumping data for table `Inventory`
--

INSERT INTO `Inventory` (`id`, `partno`, `description`, `price`) VALUES
(1, 'WIN408', 'Portal window', 423),
(2, 'ACC711', 'Jack kit', 110),
(3, 'ACC43', 'Rear-view mirror', 55),
(4, 'ACC5409', 'Cigarette lighter', 20),
(5, 'WIN958', 'Windshield, front', 500),
(6, '765432', 'Bolt', 0.1),
(7, 'ENG001', 'Entire engine', 10000),
(8, 'ENG088', 'Cylinder head', 55),
(9, 'ENG976', 'Large cylinder head', 65);

-- --------------------------------------------------------

--
-- Table structure for table `Model`
--

CREATE TABLE IF NOT EXISTS `Model` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `label` varchar(100) NOT NULL,
  `description` varchar(256) NOT NULL,
  `begin_production` int(4) NOT NULL,
  `end_production` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `Model`
--

INSERT INTO `Model` (`id`, `label`, `description`, `begin_production`, `end_production`) VALUES
(1, 'X Sedan', 'Four-door performance sedan', 1998, 1999),
(3, 'X Sedan', 'Four door performance sedan, 1st model year', 1995, 1997),
(4, 'J Convertible', 'Two-door roadster, metal retracting roof', 2002, 2005),
(5, 'J Convertible', 'Two-door roadster', 2000, 2001),
(7, 'W Wagon', 'Four-door, all-wheel drive sport station wagon', 2007, 0);

-- --------------------------------------------------------

--
-- Table structure for table `Schematic`
--

CREATE TABLE Schematic (
  id int(10) unsigned NOT NULL auto_increment,
  partno_id int(10) unsigned NOT NULL,
  assembly_id int(10) unsigned NOT NULL,
  model_id int(10) unsigned NOT NULL,
  PRIMARY KEY (id),
  KEY partno_index USING BTREE (partno_id),
  KEY assembly_index USING BTREE (assembly_id),
  KEY model_index USING BTREE (model_id),
  FOREIGN KEY (partno_id) REFERENCES Inventory(id),
  FOREIGN KEY (assembly_id) REFERENCES Assembly(id),
  FOREIGN KEY (model_id) REFERENCES Model(id)
) ENGINE=InnoDB;

--
-- Dumping data for table `Schematic`
--

INSERT INTO `Schematic` (`id`, `partno_id`, `assembly_id`, `model_id`) VALUES
(1, 6, 5, 1),
(2, 8, 5, 1),
(3, 1, 3, 1),
(4, 5, 3, 1),
(5, 8, 5, 7),
(6, 6, 5, 7),
(7, 4, 7, 3),
(8, 9, 5, 3);

-- --------------------------------------------------------

--
-- Structure for view `catalog`
--

CREATE OR REPLACE VIEW Catalog AS
SELECT
  Inventory.id,
  Inventory.partno,
  Inventory.description,
  Assembly.id AS assembly,
  Model.id AS model
FROM
  Assembly, Inventory, Model, Schematic
WHERE
  Schematic.partno_id=Inventory.id 
  AND Schematic.model_id=Model.id 
  AND Schematic.assembly_id=Assembly.id;




A bit of explanation of the tables. Assembly tables is for storing various modules of a car. Inventory stores all the parts we have in store. Model stores the car model. Schematic stores information required to put together a car of a given model with a specific part and for a specific assembly. Finally there’s a view called catalog which brings together the fields we need to serve as source to Sphinx, including the inventory id, part number, description, assembly id and model id.

If we inspect catalog the contents should look like:

+----+---------+---------------------+----------+-------+
| id | partno  | description         | assembly | model |
+----+---------+---------------------+----------+-------+
|  6 | 765432  | Bolt                |        5 |     1 |
|  8 | ENG088  | Cylinder head       |        5 |     1 |
|  1 | WIN408  | Portal window       |        3 |     1 |
|  5 | WIN958  | Windshield, front   |        3 |     1 |
|  4 | ACC5409 | Cigarette lighter   |        7 |     3 |
|  9 | ENG976  | Large cylinder head |        5 |     3 |
|  8 | ENG088  | Cylinder head       |        5 |     7 |
|  6 | 765432  | Bolt                |        5 |     7 |
+----+---------+---------------------+----------+-------+

Next, install Sphinx by downloading the latest release version from http://sphinxsearch.com/downloads/release/. At the moment the latest is 2.2.9. It takes the form of a tar.gz archive.

Extract the contents from the archive and This will give you a folder like sphinx-2.2.9-release-osx10.10-x86_64

Move the folder to /usr/local/sphinx

Next we need to create a Sphinx config file. Change to /usr/local/sphinx and copy the sample config fiel sphinx-min.conf.in to sphinx.conf.

Next, open sphinx.conf and edit it to contain the following contents:

source catalog 
{
	type			= mysql

	sql_host		= localhost
	sql_user		= testdb
	sql_pass		= testdb
	sql_db			= testdb
	sql_port		= 3306	# optional, default is 3306

        sql_query               = \
            SELECT \
                id, partno, description, \
                assembly, model \
            FROM \
                Catalog;
        sql_attr_uint        = assembly
        sql_attr_uint        = model
}
    
index catalog 
{
    source              = catalog
    path                = /usr/local/sphinx/data/catalog
    min_word_len        = 3
    min_prefix_len      = 0
    min_infix_len       = 3
}

indexer
{
	mem_limit		= 128M
}


searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= /usr/local/sphinx/log/searchd.log
	query_log		= /usr/local/sphinx/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /usr/local/sphinx/log/searchd.pid
        max_matches             = 1000
	seamless_rotate		= 1
	preopen_indexes		= 1
	unlink_old		= 1
	workers			= threads # for RT to work
	binlog_path		= /usr/local/sphinx/data
}

Save and close the file.

Some explanation. The source section gives Sphinx information about your database. I use a MySQL db in this tutorial. sql_query is the main document fetch query which Sphinx uses to grab data from our db. sql_attr_uint is used to specify unsigned integers. The index section says how to index value. Update its source parameter to your desired value. The searchd section is for the search daemon

Now configuration is complete. To make command line work easier, add /usr/local/sphinx/bin/ to your PATH.

Next, index the data into Sphinx. Run the following command:

indexer --config /usr/local/sphinx/sphinx.conf catalog

You should see some text appear that ends with something like

total 8 docs, 149 bytes
total 0.062 sec, 2397 bytes/sec, 128.71 docs/sec
total 4 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg

That means indexing is complete. We can now run queries against Sphinx. First, we need to start the search daemon using the command:

sudo searchd -c /usr/local/sphinx/sphinx.conf

Note: to stop the daemon run the command:

sudo searchd --config /usr/local/sphinx/sphinx.conf --stop

Now we are going to write a PHP file that gets data from Sphinx.

Create a file called query_cat.php and add the following content to it:



include('/usr/local/sphinx/api/sphinxapi.php');

$cl = new SphinxClient();
$cl->SetServer('localhost', 9312);
$cl->SetMatchMode(SPH_MATCH_ANY); // match any word of the query
$cl->SetFilter('model', array(3)); // filter results to those where model is 3

$result = $cl->Query('Windshield', 'catalog'); // query the word Windshield from the index called catalog.

if ($result === false){
    echo 'Query failed: ' . $cl->GetLastError()."\n";
} else {
    if ($cl->GetLastWarning()){
        echo "Warning: ". $cl->GetLastWarning()."\n";
    }
    if ( ! empty($result["matches"]) ) {
        foreach ( $result["matches"] as $doc => $docinfo ) {
            echo "$doc\n";
        }
      
        print_r( $result );
    }

}

You will get an output like

9
Array
(
    [error] => 
    [warning] => 
    [status] => 0
    [fields] => Array
        (
            [0] => partno
            [1] => description
        )

    [attrs] => Array
        (
            [assembly] => 1
            [model] => 1
        )

    [matches] => Array
        (
            [9] => Array
                (
                    [weight] => 1
                    [attrs] => Array
                        (
                            [assembly] => 5
                            [model] => 3
                        )

                )

        )

    [total] => 1
    [total_found] => 1
    [time] => 0.001
    [words] => Array
        (
            [cylind] => Array
                (
                    [docs] => 2
                    [hits] => 2
                )

        )

)

A warning may appear above the message saying that calling a method isn’t recommended, but you can ignore it.

As you can see from the result, 9 is the single result and is indeed the only inventory id for model id 3 containing the keyword ‘container’. Refer to the printout of catalog above.

That’s it for now. There are many other options that can be explored as you play around with Sphinx. Also, there’s a so-called SphinxQL which can be use to query Sphinx instead of using the API.

Another Day of Swimming

Yesterday’s swimming lesson was very memorable. I had the privilege of learning under a different instructor, Phil, who covered for the regular instructor Claire. Phil is a children’s swimming instructor with deep knowledge and passion for swimming.

I learnt more fundamental things about swimming. Specifically, it was the first day I felt myself float confidently in water.

First, you need to breathe in deeply before diving into the water. The excess air in the lungs make you float higher longer.

Also, I learnt the importance of our ritual of walking across the water back and forth at the beginning of each lesson. We do it to get warmed up and get our muscles ready for the rigor of swimming.

Yesterday was the first time I tried back stroke and for the first time I felt myself float at the surface while doing it.

Furthermore, I learnt officially that flapping of the feet gives me next to no propulsion. I will need to use my hands to propel myself forward in addition to the initial momentum obtained from jumping into water.

There are two types of swimming aids: the paddle and the noodle – noodle helps you float even better. Previously we had been using just the paddle.

Finally, I learnt that when doing a star float face up I need to lean my upper body into the water so that my feet can come up. Basically I have to think of my body as a see-saw.

Ultimately I figured out that the best way to improve my swimming technique is a tried and true method: Practice.

How to enable vi mode for several terminal applications on Unix (os X)

In order to enable vi mode for several readline-compatible (terminal) applications ilke ipython, MySQL, etc. on Unix (os X)

Set the following in your ~/.inputrc file:

set editing-mode vi
set keymap vi
set convert-meta on

Update:

In case someone’s wandering in here recently, IPython 5.0 switched from readline to prompt_toolkit, so an updated answer to this question is to pass an option:

$ ipython --TerminalInteractiveShell.editing_mode=vi

… or to set it globally in the profile configuration (~/.ipython/profile_default/ipython_config.py; create it with ipython profile create if you don’t have it) with:

c.TerminalInteractiveShell.editing_mode = ‘vi’

Source:

python – How do I use vi keys in ipython under *nix? – Stack Overflow. http://stackoverflow.com/questions/10394302/how-do-i-use-vi-keys-in-ipython-under-nix

How to fix problem of decoder jpeg not available

When doing image manipulation with PIL in a Django app, you may see an error like decoder jpeg not available.

To fix this on os X, first you need to install libjpeg as follows:

brew update
brew install libjpeg libpng

Next, try to reinstall PIL. Use the following command:

pip install  --no-cache-dir PIL --allow-external PIL --allow-unverified PIL

That should be all. However, if you get an error saying cc command failed while installing freetype, then you will need to install freetype2 as follows:

brew install freetype2

Next you need to create a symbolic link to allow pip find freetype2 as follows:

ln -s /usr/local/Cellar/freetype/2.6.1/include/freetype2\
 /usr/local/include/freetype

Note: 2.6.1 above is the version as at the time I ran the command. Replace it with the appropriate number for you.

Troubleshooting

If you do the above steps and still get the error, then uninstall the version installed via pip and build PIL from source. I had to do this once. Here’s code to see the decoders available

import _imaging
dir(_imaging)

See if jpeg_decoder is among the properties

First Day Swimming

It was a nice cool evening and I was ready to swim. Armed with my swimming shorts, one pound coin for using the lockers and membership card, I headed to the a nearby pools and fitness centre. At the receptionist desk a friendly staff showed me the way to the changing room.

Having changed into my swimming shorts and stored away my clothing, I was set to hit to the pool. So I entered the main swimming pool area and feasted my eyes on a few jolly swimmers having the time of their lives in the pool. Behold there was no sign of training going on. So, mildly confused, I asked a lady who appeared to be a life guard where swimming lessons were taking place. She aptly directed me to a room to the left.

On entering the swimming lessons room I discovered another pool with a bunch of swimmers and a nice lady who gave instructions to some apparently neophyte swimmers. “Ah, finally!” I thought. I’m at the right place. The instructor asked me to wait, that our lessons will start shortly. Taking a seat next to another first-time swimmer I chatted away as we both waited patiently for our lessons to begin.

Finally it was our turn to swim. The instructor checked off our names on her register, a POS-like device. The first thing we did was to sit at the edge of the pool with our legs in the water. There were three learners in my group. Next, we stood in the water and then walked up and down the breath of the pool both forward and backwards. I remember feeling quite wobbly as I took my first steps. I had a rather unsteady gait somewhat akin to a child taking his first steps. But I overcame that feeling pretty quickly and within minutes was walking confidently back and forth.

After a while it was time to float. At the instructor’s command we stood by the edge of the pool, stretched our hands toward the wall, turned face down into the water and let our legs float above the pool floor, float for a few seconds and then stand. I was eagerly expecting to float like a beach ball, but something bewildering happened. I sank like a rock!

Something wasn’t right. I decided to look at my fellow learners. To my amazement they floated. One of them, a fine plumpy girl, floated like she had been swimming for years. The other girl, a skinny one, also floated. I then thought “maybe the sinking was a one-off”. So I tried floating again. And again I sank quick. Several sinking experiences later I asked my instructor what was going on. How come I sank while others floated. It was at this point I learnt something crucial about swimming.

Fat floats and muscle sinks. People who have large amounts of fat in their body tend to float on water because fat is lighter than water. On the other hand, people who are skinny or very muscular tend to sink when stationary because muscle is heavier than water. I fall in the second category, so I have to do extra work e.g. kick to stay afloat.

I learnt also that in order to float in water it’s important to relax. If you are too tense you will sink. Don’t fight against the water. Rather you should feel your way through it.

Next we practised push and glide. In doing this we dived into the water, the just glided through until we were almost out of breath at which point we stood, took a breath and did it again.

After that, we learnt to kick in the water. In doing this we stood by the edge of the pool, held our hands straight perpendicular to the pool wall, floated our legs and kicked. The idea was to maintain a position near the surface of the water. Again I got optimistic, hoping this was my time to float. Lo and behold I tried this and sank! Again, my body composition was to blame. In consolation my instructor told me that the more I practised the better I will get and that I needed to kick more vigorously.

I practised push, glide and kick several more times until the end of the lesson at which point the instructor recommended that I buy swimming goggles. I thought she was just giving a friendly admonition. It wasn’t until I got home that I understood the gravity of not wearing goggles in the first place. My eyes hurt like crazy. Apparently the pool had some chemical (likely Chlorine) and my eyes were sensitive to it. So, big advice to anyone attempting to learn swimming in a pool. Buy yourself a good pair of goggles! You won’t regret it.

By and large, I’ve started learning the basics of swimming and will keep you posted. Enjoy.

An approach to Problem Solving – Solving Simple Problems First

I have fond memories of the good old days solving numerous problems in examinations. Often times I ran into really facile problems whose solutions were comparable to eating meat pie. However, there were other instances where things just weren’t that simple. Yep, you got it. I’m talking about complex problems. In this article I discuss how I tackle problems of varying difficulty in limited time. This technique I will cover is actually quite simple and very powerful and it is as follows: solve simple problems first. I will discuss a few reasons why you should take this principle seriously.

First, by solving simple problems first, you increase your productivity and sense of accomplishment. Imagine trying to solve ten problems each carrying equal marks. Let’s assume the first two are deviously difficult for whatever reason (maybe lack of preparation) while the next five are very simple. Should you try to solve these first two completely before moving on to anything else, you may very well realize that by the time you are done with those questions time is up. And guess what? You only score a maximum of 20%. On the other hand, if you skim through the first two questions, find quickly that these are beasts and just move on to the third without solving the first two, then you will found to your delight five easy questions to solve and a maximum of 50% if that’s all you are able to solve.

Apart from the obvious reason that you stand a chance of getting better output by solving simple problems first, there’s an even more interesting and perhaps counter-intuitive reason one should leave the harder problems for later and it is as follows: Leaving a challenging problem for later actually increases the likelihood of you solving the problem. When faced with a really challenging problem, it has been shown scientifically that by taking one’s mind off that problem and doing some other things, one gives the brain time to subconsciously find a solution to the problem. This principle is called Incubation. Now there is no guarantee that this will work 100% but it definitely beats spending arbitrary amounts of time trying to solve the problem in one go without even a guarantee one will be arrive at the right solution.

Thus we see that when confronted with several problems to solve in time-constrained situations, it pays to tackle the easier ones first as this increases our potential output and also gives you a better chance of solving even the initially difficult problems. While I first learnt this principle while taking examinations in school, I have found it quite invaluable in everyday life.

Cut the Chase to Success by Putting Effort

We have always heard that you need to work hard to become successful. This is true for a variety of reasons. Well, first, when whoever it is that is responsible for bestowing success upon you observes your hard-working attitude in comparison that that of your peers, then all things being equal he or she is likely to want to entrust success and its accompanying responsibility onto you seeing that you are suited for it. But that’s not why I am writing this essay. There is another even more pertinent, and perhaps under-emphasized reason why putting effort cuts the chase to success. And it is this: Putting effort helps you tell very quickly when a thing is not worth doing.

Before going further let us put a solid definition behind what it means to be successful in the context of this essay. I will define being successful as the state of doing or having things that bring you lasting happiness and make you feel like you are fulfilling your life’s purpose.

Now to the point of telling when a thing is worth doing. If you put in mediocre effort into an endeavour you greatly desire, there is a chance you will succeed, however minuscule that chance may be. If you succeed, then good luck! Maybe that activity is actually easier than your envisaged or you are just plain lucky. But chances are that you will not succeed. Now if in the more likely situation you do not succeed in that endeavour, then there are two possible solutions: 1. You need to put in more effort in that same activity. 2. You need to do another activity altogether.

As you can see, you have reached a moment of indecision. You can’t say with hundred  percent certainty that the activity you are partaking in is indeed worth your while at all. So you may as well repeat the same endeavour, but with greater and more appropriate effort. Now again after repeating the endeavour with greater rigour and preparation, there is still a chance of success and failure. If the activity was worth engaging in in the first place, then your odds of succeeding will be increased. On the other hand, if the activity was a terrible idea in the first place, then you will find out only after a second attempt. Note that it may take several attempts to weed out all the possibilities of unfavourable results stemming from lack of effort, depending on how much care and effort you exert when carrying out the activity.

If, on the other hand, you put in your best during the first try, then there will be much less uncertainty why things turn out the way they do in the event that the results are unfavourable. At least it won’t be for mediocre effort. As a result you will quickly reach the point where you ask yourself if a thing is worth doing at all or not. Upon great thought on this question, there is a chance you may discover that the activity wasn’t even worth doing in the first place and you will be able to, with a clear conscience, let go of such activity and take on more lucrative ventures.

Here’s an example to put some meat on the bone of the issue. A guy named Bob wants to spend time with a girl. He gets some food cooked, makes little effort to clean up the house, makes a quick and dirty attempt at entertainment, doesn’t bother to take a shower due to waking up late, then invites her over. The girl comes over, spends a little time and rushes off, leaving Bob in utter bewilderment as to why she didn’t hang out with him as he expected. Well, on thorough thought, he may come to ask himself many questions: Is she the right girl? Could I have worked a little more on the food? Could I have cleaned the house a little more? Could I have done a little more in the way of entertainment to keep her interested? Should I have had a good shower and dressed better before seeing her (obviously)?

As you can see, Bob has many things to consider. On the other hand, let’s assume another guy Sam wants to hang out with a girl. Before she comes he gets a lot of assorted food made, does a thorough job in cleaning the house, makes conscientious effort to provide good entertainment, has a bath and dresses in clean clothes. When the girl arrives assume Sam makes solid effort to keep her entertained and give her a good time. If this same girl rushes off without having a genuine emergency, then Sammy will find it much easier to reach the point where he asks if the girl is even worth his time or not. Why? Because he won’t have to ask if the problem was due to a dirty environment, poor entertainment, or poor effort in the nutritional department. He will be able to put his relation with such girl into perspective quickly in light of the question and reach an appropriate decision which will augur well with his conscience. This is the power of putting good effort into whatever we do each and every time.

In conclusion, it really pays to put in one’s possible best into any activity worth doing at all because it makes it easier for us to earn the trust of our fellow man, but also because it helps us reach major decisions quicker in our lives, act quicker, and become successful quicker.