Monday, January 26, 2015

Shell script to delete redis keys matching a pattern


A quick way to delete redis keys from the command line would be:
        redis-cli DEL "del_key"

If you wish to delete all keys matching a certain pattern,
        redis-cli KEYS "*del_pattern*" | xargs redis-cli DEL

A more easy shell script for it would be:

Save it as redis-delete.sh and you can just run it as:
        ./redis-delete.sh del_pattern

UPDATE:
The above code using KEYS does not scale well and so for a more performance efficient code, we can use SCAN which is a cursor-based iterator.


Sunday, December 28, 2014

Modal dialog box and popups in Angular.js using ngDialog


If you are building an Angular.js application and wish to add a modal dialog box or a simple popup, then the ngDialog module provides an easy, simple solution. It provides two default css themes and we can also add our own custom themes.

Let's see how to create a simple dialog box with ngDialog.
For our example, we will have a base page with a button which when clicked will open a contact-us form as dialog box. The dialog box will have an 'OK' button which will save the changes and close the box and a 'Cancel' button which will just dismiss the box.

1. First, you need to download ngDialog.js and ngDialog.css (and the themes that you want) from https://github.com/likeastore/ngDialog

2. Create the base template - 'base.html'


3. Create the dialog box template - 'contact_us.html'

closeThisDialog() and confirm() are ngDialog's methods and they return an Angular promise object that is either rejected or resolved.

4. Angular.js code to handle the promise:

Now if you wish to add a popup message as per the response success or failure:

The plain option, if set to 'true' allows to use a plain string as template.
The API also provides a lot of other useful options and event handlers.


Saturday, December 06, 2014

Lookahead in regular expressions


As I was solving a couple of problems in Checkio, I came across a new concept - lookaheads in regular expressions. At first, it was slightly confusing to understand how lookaheads are different from normal regular expressions. But they actually prove to be very useful in certain cases.

Lookaheads are zero-length assertions as they do not consume characters in the string but only assert whether a match is possible or not. Positive lookaheads are represented as (?=regex) and negative lookaheads as (?!regex).

Example: 
    Hello(?=World) - matches any "Hello" followed by "World"
  Hello(?=World) - matches any "Hello" which is not followed by "World" like "Hello Today" or even simply "Hello"

The real power of lookaheads arise when you chain two or more lookaheads together.

Consider a password validation which requires the password to have atleast one letter and one digit. To construct a regular expression for this case, you can't use something like, "(.*\d)(.*[A-Za-z]) " as that implies one or more digits followed by one or more letters. So a password like "123hello" would pass whereas "hello123" would fail the regex. The regex engine would start scanning the string for a digit and when it reaches "123", there are no more letters following it, so it would fail.

Instead, this can be easily achieved by using lookaheads,  "(?=.*\d)(?=.*[A-Za-z])". As the regex engine begins matching the first lookahead for digits, it traverses the string "hello123" from 'h' till it reaches the digit '1'. The first lookahead is now matched. But since the lookahead doesn't consume any string, the regex engine again starts from 'h' till it reaches a letter which is 'h' itself. Now both lookaheads are matched and the entire regex returns true.

For a more complex password validation, we could use:
((?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[@?&$!]).{8,20})
which implies the password needs to have min 8 characters and max 20 characters. And it should have atleast one digit, one lowercase, one uppercase letter and one special character among "@,?,&,$,!".


Saturday, November 22, 2014

Configure Nginx for load balancing


Recently, in our project, I was asked to configure Nginx for load balancing as our app was becoming slow, owing to high traffic. I haven't worked much with Nginx, so I was a bit apprehensive at first. As it was Diwali holidays, nobody else was there in office and I had no choice. However, on fumbling around, it turned out to be much more easier than I had thought.

Imagine that you wish to run two different instances of your application to distribute the high traffic.
With Nginx, you can enable all requests to be proxied to your main domain, while Nginx takes care of distributing the requests to different servers in a round robin fashion.

Open the Nginx configuration file located at /etc/nginx/nginx.conf and add an upstream block and modify the server block as shown below: 

http {
    upstream yourapp {
        ip_hash;
        server domain1.com:8080;
        server domain2.com:8080;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://yourapp;
        }
    }
}


That's it! Make sure your app is running on both the servers on your specified ports and then restart Nginx by running:
/etc/init.d/nginx restart

The ip_hash specified in the upstream block is used for session persistence so that subsequent requests of the same client can be directed to the same server. Nginx uses the client's ip address as a hash key to determine which server should be selected for the client's requests. Nginx also offers other options like weighted load balancing, least connected load balancing and health checks.


Sunday, November 16, 2014

At Wayanad - In the lap of nature


"Actually, the best gift you could have given her was a lifetime of adventures..."
- Lewis Carroll in 'Alice in Wonderland'

Last week, owing to Muharam and Guru Nanak Jayanthi, we again had a couple of holidays and as I was heading out on another trip, my friends at office were kidding me on how "she never misses a holiday". I never realized that I was travelling that frequently. But yeah, I love travelling and adventures. As a kid, I loved reading "Alice in Wonderland", her adventures as she falls through the rabbit hole and meets queer, interesting characters. The ever tensed rushing White Rabbit, the mischievous grinning Cheshire cat quoting philosophical points now and then, the Mad Hatter and March Hare always having a tea-party and singing an Unbirthday song, the hookah-smoking caterpillar, the melancholic and sobbing Mock Turtle, the Queen of Hearts whose only way of settling difficulties was "Off with his head!", every character and adventure was interesting and exciting. As a kid, I always hoped that some day I too would fall into a rabbit hole and have such wonderful adventures. But growing up, I realized that adventures and interesting people are all around us and we only need to open our hearts to embrace them.

As Roald Dahl says in the book "Charlie and the Chocolate Factory":
“And above all, watch with glittering eyes the whole world around you because the greatest secrets are always hidden in the most unlikely places. Those who don't believe in magic will never find it.”

So coming back to my holiday, this was a "Deja-vu" of my dad's MIT batch-mates and their family, all getting together at Wayanad for four days of fun and reminiscing the old college days.

Nov 5th, 2014 (Wed): We arrived at Calicut railway station via Mangalore mail at around 8:30 am. A Tempo traveller had been arranged to take us to Wayanad. The drive uphill was dizzy and had lot of hairpin bends. We reached our accommodation at Silverwoods Resort around mid-day. The hotel staff courteously received us with a welcome drink. Sipping the fresh mint lime juice, I stepped out into the balcony of the lobby which overlooked the beautiful Banasura Lake. The clean, fresh air of the hill station and the calm, peaceful tranquility surrounding the entire place was enough to relax and rejuvenate the mind and soul. The resort was neatly maintained and as we walked to our rooms, we could see a variety of flowers and greenery all along. Our room was quite big with a nice comfy bed, airconditioning, LCD TV, and a kettle to make hot water/coffee/tea along with coffee, milk and sugar packets. The bathroom was so big that it was a room in itself complete with bathtub, a hairdryer and an assortment of kits - dental kit, shaving kit, shower kit, sewing kit, etc. To make everything perfect, there was the stunning view of the lake from the balcony. The entire day, we were relaxing from the travel and evening we all got together in a conference room and I got to know many of my dad's friends and their families.

Nov 6th, 2014 (Thu): Morning, we visited the Thirunelli temple located around 70kms from our resort and had fun playing in the waters of the joyful Papanasini stream nearby. Post-lunch we went for boating in the Banasura lake. The lake had an aura of serenity and utter placidity. There were little ripples on the surface here and there, but as the boat moved deeper, the surface was clear, calm and silent. Observing the still waters running deep evokes an inner stillness within and you feel a sense of connectedness with everything and everyone. The lone duck swimming, a kingfisher searching for its prey, the surrounding mountains and trees, the vast blue sky reflected in the waters, the people in the boat clicking pictures and joking around.. For a moment the mind stills, you become aware of awareness and experience a profund sense of peace and Oneness.

Nov 7th, 2014 (Fri): Today we visited the Kuruva Island, a dense uninhabited evergreen forest in the middle of the Kabini river. We crossed the river in bamboo rafts pulled by overhead ropes. The island was a dense forest with a narrow trail leading around the whole place. At one place, we could get down into the river and the rushing waters massaged my tired feet. After lunch, we took a nap in the room while the ladies went out for shopping. Evening was a perfect relaxation. I made a cup of coffee for me and my dad and we chilled out in the balcony, breathing in the fresh air and spotting some rare flowers in the garden. It was a full moon night and it added to the magic and mystery of the place. A refreshing warm bubble bath to revive the senses and a quiet, stroll around the place in the night were enough to end the holiday.

Nov 8th, 2014 (Sat): After breakfast, we checked out of the resort and on the way to Calicut visited Pookot Lake. The lake was pretty big and a lovely path wound around the entire lake. After lunch, we also spent some time in the Calicut beach and then finally reached the railway station for boarding the train to Chennai.

Some thoughts about the entire trip:
* I got to taste a variety of Kerala cuisine during this trip. Aappam with veg stew and coconut milk, rice and wheat puttu with kadala curry, avial, more curry, ela ada, palada pradhaman, kappa, etc.
* I seem to have fallen in love with spiders. Inside the resort, I saw a couple of spiders with beautifully designed webs hanging from the trees. The web was so carefully, intricately designed with the same repeating pattern all over in a circle. Again at Pookot Lake, I saw one clever spider which had weaved a thin line between two trees, like a telephone line. One cannot help but marvel at the wonderful creation of nature. Where did the insect learn to be an artist? I so wished I could be a spider, weave my own net and sleep in it hanging in mid-air.
* It was interesting to meet all my dad's friends. Each of them were unique - some were always joking around and making people light-hearted, some always wanted to make their opinions heard, one of them had a unique talent of conducting magic shows and confusing people, one of them was a passionate photographer amazing people with his works, some were good at organizing games and fun activities, some had travelled far and wide and talked about lots of places, and some were nerds sitting quietly in a corner and observing the entire chaos. Some of the ladies were talkative, some had a wide variety of home remedy tips and some of them were so keen on shopping green pepper before going home. It is in the midst of such a group that you realize how unique everyone is, yet beneath all those differences, at the level of spirit, we are all but one, like distinct waves of a larger ocean. Each is but a reflection of the other and like in a hall of mirrors each one reflects a different version of ourselves.

Our room at Silverwoods Resort

Sunset view from the balcony

Bamboo raft to Kuruva Island

I would love to hear your feedback! What do you think of my travel blog posts? Are they too long and descriptive? Please share your thoughts in the comments.


Sunday, October 12, 2014

Night Trek and Cave Exploring At Antaragange


"We live in a wonderful world that is full of beauty, charm and adventure. There is no end to the adventures we can have, if only we seek them with our eyes wide open."
 - Jawaharlal Nehru

Last weekend was one of those rare long weekends with five days of festival holidays. I had been planning for these holidays since a month back, searching through all travel websites for best hotel deals and reading travel blogs for weekend getaways near Bangalore. Finally zeroed in on a night trek and cave exploring at Antaragange organized by BMC (Bangalore Mountaineering Club). The words "night trek" and "cave exploring" were enough to excite my adventurous nature. The BMC group was very well organized. I had sent them a mail to enquire whether the trek would be easy enough for my parents. They had a good ticketing system for all enquiries and responded promptly.
Next on list was booking hotels for the weekend. Luckily, Goibibo was offering 50% discount as part of their anniversary sale.
Everything planned and set, here's a short summary of the weekend:

Oct 3rd, 2014 (Fri): My parents were arriving from Chennai in the morning train. I had gone to the railway station to receive them and from there we took an auto to our hotel, "Abids Inn Homestay" which was located just behind Silk Board, in a very calm and peaceful residential area away from all the city traffic. It was more like a serviced apartment and we were given a room in a 3BHK flat. The other two rooms of the flat were already occupied. The kitchen was furnished with all vessels, a stove, microwave and toaster. Our room was separated from the rest by a bamboo mat curtain and hence total privacy for us. It was a tiny, clean and cosy room. We spent the afternoon relaxing and chit-chatting. Evening, we took a walk in the neighbourhood and reached the nearby bus stop. A bus to Banashankari was just arriving. We boarded the bus and within twenty minutes reached Banashankari. We visited the temple there and then spent timepassing in the area - having a hot coffee and masala dosa in the hotel there, window shopping of the small shops there, found an internet cafe and took a copy of our confirmation voucher for tomorrow's trek. Finally took a bus to Silk Board and reached our hotel for a good night's sleep (I managed to sneak in a face pack to pamper myself before sleeping. ;) )

Oct 4th, 2014 (Sat): We got up late in the morning and were too lazy to get ready and go out and have breakfast. Luckily, they offered us a complimentary breakfast in the hotel. Toast and egg omelette and a cup of hot tea. Yummy! There's nothing better than getting up late on a holiday and having breakfast served to you in bed (and then going back to sleep again. ;) ). Next, my most favorite part of the trip came. Packing my backpack for the trek - track pants, tshirts, ponchos, water bottles, chocolate bars to munch on and emergency medicines. Unfortunately the camera was not working. So I had to do with my mobile camera. But I doubted whether it could click pictures in the dark. After packing, we went to have lunch. We couldn't find any hotels nearby and finally after walking around for 15 minutes, we found Hotel Adigas. After lunch, we took a nap, as we were going to be awake most of the night, we needed all the rest. Evening, we checked-out the hotel, took a bus to Majestic, dropped our luggages in the cloak room and then reached BMC office in Shivaji Nagar which was the boarding point for our trek. We still had lot of time to kill, so we had our dinner and kept timepassing in the area. Around 09:30 pm, the bus arrived. We were the only people boarding at Shivaji Nagar apart from the guides and instructors. The bus picked up more people at Domlur and Tin Factory. Totally, there more than 60 people so it was a lot of fun.

Antargange is about 60kms from Bangalore in Kollar. It is a massive field of volcanic boulders and rocks which heaped on one another create a whole network of cave-like formations.
We reached the place around 12:30 am. Getting down from the bus, everything was totally dark and only the flash lights of everyone lit the place. Each of us were given a sleeping bag. It was a like a perfect adventure from a Famous Five book! We had to climb a long staircase that lead to a temple with a pond. There was a stone bull with water flowing out of its mouth. From there, we had to climb uphill for about half an hour. The trail was very narrow and we could go only in a single line. The experience was pretty spooky with screeching owls, bats flying overhead and an occasional nocturnal animal scurrying around in the bushes. It was pretty much like the Forbidden Forest from Harry Potter and I wishfully hoped I could meet a sleeping dragon, tickle it and make it angry and slay it! But sadly, we didn't even run into a bear or a fox.

On reaching the peak, we dropped our sleeping bags there and proceeded for cave exploration. This was my first caving experience and it was just too fun. There were many places where we had to crawl, climb, lie down and slide, and perform other gymnastics. Some places were too narrow and tiny, that we couldn't even walk straight, we had to turn and walk sideways in a single line. After more than an hour, at about 3:00 am, we came out and reached the place where we had left our sleeping bags. Some people slept on the rocks and some down on the grass. It was a beautiful night. The moon had already set. I had never spent a night under the open star-lit sky. For a long time, I was awake counting the stars and watching some moving lights (mostly planes). Also, got lucky enough to see a shooting star in the clear night sky which excited me so much that I lay wide awake waiting for more. Finally, dozed off.

Oct 5th, 2014 (Sun): Waking up in the morning to witness the sunrise was the most beautiful experience ever. The stillness of the dawn (something that we never experience in a city), the freshness of the morning air and dew drops on the grass, few birds chirping around, everything seems like you are in heaven. Somehow being with nature opens up a deep inner space within you. You realized how insignificant you are in the universe and at the same time how interconnected you are with everything and everyone else. We then proceeded on the trek downhill and reached our bus. On the way, they stopped at Hotel Kamath for a good, hearty breakfast. We reached Bangalore around 10:00 am, went to the station to collect our luggages from cloak room and took an auto to our hotel, "Hotel VT Orchid", near Corporation circle. One thing I love about booking different hotels at different locations is we get to experience a different area each time. Also, it pushes us out of our comfort zone to explore something new. Contrary to our previous hotel, which was located in a peaceful residential area, this hotel was in the midst of a crowded and very busy street. The room was too tiny, but thankfully, atleast it had air conditioning. We were so tired out by the trek, that we just slept the entire day. Evening we took a stroll outside to have coffee and dinner. There was a nice restaurant nearby "Hotel Pai Vihar". Every area of Bangalore seems to have a good budget restaurant, a nice bakery and a juice shop. We went back to the hotel and slept late after talking about yesterday's trip.

Oct 6th, 2014 (Mon):
After breakfast, we checked-out the hotel and went to the railway station. My parents were leaving by the 3:00 pm train to Chennai. After loving goodbyes and seeing them off, I returned to my room.

Overall, it was a memorable, fun and adventurous weekend.
(Pictures below were taken during sunrise. Couldn't take during night with my phone.)




As always, I take away some key learnings from every travel. On this trip, what fascinated me most was trekking in the dark with flash lights. The flash lights just illuminate the next step in the trail. At any point of time, you can only see one step ahead of you. But then, actually, one step ahead is all you need to see for the entire journey. When morning comes and you see the entire stretch ahead of you, it just makes you wonder "Did I actually climb all this way in the night?". Life too is like that. You can never see your entire life ahead in advance. Planning too much just creates fear and doubt. "Will I be able to do all of this? Is it possible for me to try that?". However, at every moment, you know what action to take next. And that's all needed. When you learn to embrace the uncertainty of life and trust the present moment, life too becomes an amazing adventure.


Wednesday, August 06, 2014

Calculating the number of working days using Python

At times, we have a requirement to calculate the number of weekdays / working days between two dates.
Here's how you do it using Python:

But the above method involves usage of loops.
A more optimized way of doing it would be:

In our project, we faced a requirement where given a start date, we had to calculate end date as start date + x working days.
Using the above logic, we can do:

If you have a list of holidays to exclude, you could then do: