Conversations with a 2yo

Fine summer evening, I'm taking Sempi on a walk. He's carrying a flash light and shining it on every piece of object on the side walk and asking me questions. 

Sempi: Appa, what is this? 

Me: It's a rock.

Sempi: Oh. What is this? 

Me: That's a piece of paper.

Sempi: Oh. What is this? 

Me: It's a cigarette. Actually it's a cigarette butt. 

Sempi: Huh? Do you keep it in the butt? 

Me: Hahahaha. No. Sempi. *Still laughing uncontrollably*

Sempi: Hehehe. *Starts laughing*

He knows when he's made a joke, even though he has no clue why it's funny.

FuzzyFinder - in 10 lines of Python


FuzzyFinder is a popular feature available in decent editors to open files. The idea is to start typing partial strings from the full path and the list of suggestions will be narrowed down to match the desired file. 


Vim (Ctrl-P)

Sublime Text (Cmd-P)

This is an extremely useful feature and it's quite easy to implement.

Problem Statement:

We have a collection of strings (filenames). We're trying to filter down that collection based on user input. The user input can be partial strings from the filename. Let's walk this through with an example. Here is a collection of filenames:

When the user types 'djm' we are supposed to match '' and ''. The simplest route to achieve this is to use regular expressions. 


Naive Regex Matching:

Convert 'djm' into 'd.*j.*m' and try to match this regex against every item in the list. Items that match are the possible candidates.

This got us the desired results for input 'djm'. But the suggestions are not ranked in any particular order.

In fact, for the second example with user input 'mig' the best possible suggestion '' was listed as the last item in the result.

Ranking based on match position:

We can rank the results based on the position of the first occurrence of the matching character. For user input 'mig' the position of the matching characters are as follows:

Here's the code:

We made the list of suggestions to be tuples where the first item is the position of the match and second item is the matching filename. When this list is sorted, python will sort them based on the first item in tuple and use the second item as a tie breaker. On line 14 we use a list comprehension to iterate over the sorted list of tuples and extract just the second item which is the file name we're interested in.

This got us close to the end result, but as shown in the example, it's not perfect. We see '' as the first suggestion, but the user wanted ''.

Ranking based on compact match:

When a user starts typing a partial string they will continue to type consecutive letters in a effort to find the exact match. When someone types 'mig' they are looking for '' or '' not ''. The key here is to find the most compact match for the user input.

Once again this is trivial to do in python. When we match a string against a regular expression, the matched string is stored in the 

For example, if the input is 'mig', the matching group from the 'collection' defined earlier is as follows:

We can use the length of the captured group as our primary rank and use the starting position as our secondary rank. To do that we add the len( as the first item in the tuple, match.start() as the second item in the tuple and the filename itself as the third item in the tuple. Python will sort this list based on first item in the tuple (primary rank), second item as tie-breaker (secondary rank) and the third item as the fall back tie-breaker. 

This produces the desired behavior for our input. We're not quite done yet.

Non-Greedy Matching

There is one more subtle corner case that was caught by Daniel Rocco. Consider these two items in the collection ['api_user', 'user_group']. When you enter the word 'user' the ideal suggestion should be ['user_group', 'api_user']. But the actual result is:

Looking at this output, you'll notice that api_user appears before user_group. Digging in a little, it turns out the search user expands to u.*s.*e.*r; notice that user_group has two rs, so the pattern matches user_gr instead of the expected user. The longer match length forces the ranking of this match down, which again seems counterintuitive. This is easy to change by using the non-greedy version of the regex (.*? instead of .*) on line 4. 

Now that works for all the cases we've outlines. We've just implemented a fuzzy finder in 10 lines of code.


That was the design process for implementing fuzzy matching for my side project pgcli, which is a repl for Postgresql that can do auto-completion. 

I've extracted fuzzyfinder into a stand-alone python package. You can install it via 'pip install fuzzyfinder' and use it in your projects.

Thanks to Micah Zoltu and Daniel Rocco for reviewing the algorithm and fixing the corner cases.

If you found this interesting, you should follow me on twitter


When I first started looking into fuzzy matching in python, I encountered this excellent library called fuzzywuzzy. But the fuzzy matching done by that library is a different kind. It uses levenshtein distance to find the closest matching string from a collection. Which is a great technique for auto-correction against spelling errors but it doesn't produce the desired results for matching long names from partial sub-strings.

Conversation with a 2 yo

Me: Do you like Superman? 

Sempi: No

Me: How about Batman?

Sempi: No

Me: Who do you like? 

Sempi: Amma

Me: Aww. Do you like Appa? 

Sempi: Yeah (matter of fact voice). 

Sempi: I like you when you're at work. I don't like you when you're home. 

Me: o_O

Pycast - Python screencasts

Pycast - Weekly screencasts on Python and DataScience by Matt Harrison. 

Matt is bootstrapping pycast through kickstarter. I'm excited about it because I've attended Matt's tutorials and came away feeling leveled up on my Python chops. 

Nearly 5 years ago I was getting started in Python and learning on my own by writing small scripts to automate silly stuff. I wasn't writing anything adventurous and I was looking for a way to improve my skills.

Right around that time I started getting involved in the open source community in Utah and decided to go to a local conference. Matt was doing a 3 hour tutorial that covered beginner to intermediate Python. When the session was over I felt empowered. I couldn't wait to get back home to do the exercises that he had laid out during the training. After working through them I felt like I really knew the language. I was writing generators and decorators by the end of it. It was an accelerated learning experience that took me from a novice to a journeyman

The beauty of his training is, it wasn't merely a brain dump, he was teaching me to how to learn, where to look up the docs, how to recognize idiomatic python and best practices of programming. 

I eventually landed a job doing full time Python at an awesome company.

That's why I'm excited about his new venture. This is a great opportunity for me to dive into Data Science and I can't wait to see his videos and workout the exercises.

If you're still on the fence about it, leave a comment on his kickstarter page with your question. He's a friendly and responsive person.

Conversations with a 2 yo

Sempi insisted on doing laundry, helping put away the clothes and sweeping the floor. 

Yoshi: When did you become such a big boy?

Sempi: Three minutes ago. 

Continues sweeping the floor with a big smile and a song. 

Sempi: I'm a street sweeper with a broom in my hand.  

Montreal Bagels - 2yo review

I was in Montreal for PyCon 2015. I was told that Montreal was famous for it's bagels. So I brought home half-dozen bagels.

I made my son a delicious toasted bagel with cream cheese in the morning. He ate it with gusto and the following conversation ensues: 

Me: Sempi, did you like the Montreal Bagels.

2yo: It was ok (as he proceeds to lick his fingers clean). 

This kid is hard to impress. :)

Kickstarter: mysql-cli

I'm starting a project called mysql-cli.

mysql-cli will be a command line client for MySQL, with auto-completion and syntax highlighting. An equivalent of pgcli for MySQL database.

I'm raising funds for the project through kickstarter. The goal is to compensate for the development time and resources (hosting, testing etc) as well as motivate me to keep going.

When I launched pgcli earlier this year I had high hopes for it. I anticipated that I might reach a hundred users and even a couple of contributions. I announced it on Twitter and HackerNews and it took about an hour to reach 100 stars. By the second day it was a top trending repo in all of Github. Right now it is hovering near the 1600 mark for the number of stars, with more than 70 pull requests (merged). 

During the first week of launch, I slept about 3 hours each night because the pull requests and issues came flooding. I made a personal resolve to answer every communique within 24 hours. This meant answering personal emails, responding to issues filed, reviewing pull requests etc. I vowed to be polite and respectful to my users and contributors and I've had nothing but pleasant interactions with them. 

My hope is to provide the same level of dedication and support to mysql-cli. There is definitely a need for it, since every time I use the default MySQL client I want to scream obscenities at my computer and I can't be the only one. :)

The plan is to launch mysql-cli in July 2015 and open up the repo to public. If you want to get involved sooner, please back the project on kickstarter and I'll add you to the early access list. :)