A Geek’s Life

Breaking Down a Python One Liner

Recently a friend of mine asked me to help her with a Python script she was working on. The script had to process a log file. So the problem was that the script had to identify the most recently modified log file among the list of log files present in the current working directory. And another issue was that there might be some other files and directories present in the working directory of the script.

So I came up with this one liner to find out the most recently updated log file.

1
sorted([f for f in os.listdir('.') if f.startswith('log')], key=lambda a: os.path.getmtime(a))[-1]

I know it looks scary, and long enough to not fit on a single line, but it shows the expressive nature of Python. That’s one of the reasons I decided to learn it and I ended up liking it!

So lets break it down.

First we need to get the list of all files in the current working directory. The built-in os module in Python provides a utility function listdir which can used as:

1
os.listdir('.')

That returns a list - just another name for an array in Python lingo, containing the names of all files and directories in the current directory of the script. We do need to import the os module for that. So the following must be added at the top in the script:

1
import os

A special thing about log files was that their name starts with log. So now we just need to filter out the log files. Using list comprehension in Python makes it a lot easier.

1
logFiles = [f for f in os.listdir('.') if f.startswith('log')]

This will give us just the list of log files in the current working directory. Sweet.

To find out the most recently modified file, we need to know the last modified time of each file and then sort them according to it. The path sub-module in os module contains a function getmtime that returns the last modified time of the file. It can be used as:

1
os.path.getmtime(filename)

For sorting we need to use the sorted function. Its syntax is:

1
sorted(iterable, key)

The first argument iterable is the list that we want to sort. The second argument key has to be a function which defines the criteria for sorting.

Functions can be defined in two ways in Python. The normal syntax is:

1
2
def addOne(a):
  return a + 1

We can also define an anonymous function known as lambda in Python land. So the above function can be written as a lambda:

1
add = lambda a: a + 1

I use the lambda synatx to define the key for the sorted function.

Our key function would look like this:

1
keyFn = lambda a: os.path.getmtime(a)

This will return the last modified time for the filename represnted by the variable a.

So far, we have this:

1
2
3
logFiles = [f for f in os.listdir('.') if f.startswith('log')]
keyFn = lambda a: os.path.getmtime(a)
logFilesSortedByLastModifiedTime = sorted(logFiles, key=keyFn)

The sorted list that is being returned here is:

1
['least_recently_modified',.....,'most_recently_modified']

We want the last element of the list. In Python, we can use negative indexes with list. So an index of -1 refers to the last element of the list.

Now we have:

1
2
3
4
logFiles = [f for f in os.listdir('.') if f.startswith('log')]
keyFn = lambda a: os.path.getmtime(a)
logFilesSortedByLastModifiedTime = sorted(logFiles, key=keyFn)
mostRecentlyModifiedLogFile = logFilesSortedByLastModifiedTime[-1]

Eliminating the variables, we can write the above four lines in just one line:

1
sorted([f for f in os.listdir('.') if f.startswith('log')], key=lambda a: os.path.getmtime(a))[-1]

Hope that was fun! Happy Coding :)

Journey With Programming Languages - Part 3

Diploma - B. Tech - Current

Been a long time since the last post.

So, the next stop in my journey was the Web land. There was HTML, CSS, and JavaScript. This is where I learned the principle of separating the actual content from its presentation and behaviour. Now this principle is very general, hence is not only applicable to the Web land, but also to (insert your favorite development platform here) which basically includes some kind of User Interface. It can also be considered as a special case of the Separation of Concerns principle. I didn’t touch much on the JavaScript as I was unware of its real power!

Then I got interested in Game Development (O yeah!). I tried many approaches including Allegro, Game Maker etc. and finally ended up using Blitz BASIC. Its just a derivative of the original BASIC along with some specific features for game development such as graphics, input and audio handling etc. The syntax was easy and the approach was pretty RAD. I developed a simple vertical scrolling shooter with help of my awesome younger brother (who is way more creative than me!). We together did some game UI elements with Photoshop.

I designed the ship for the game myself with the Allegro Sprite Editor. It was a 32x32 bmp. For the rest of the sprites and game music, I searched the web. The game lagged as the number of asteroids increased, due to my inefficient algorithm, as it was my first attempt on building a complete game. Somehow I managed to finish it. It was a fun experience.

The next language was the cousin of the previous one, Visual Basic. I didn’t like it much and was not convinced by the whole drag and drop idea for developing user-interfaces. It might have been a boon in early times, but for me, it seemed less programmatic. I just did for the sake of my curriculum.

As time went by, my diploma finished and I came to Chennai for the B. Tech. in Computer Science.

Python seemed to be very popular those days on the web. I looked up for some examples and decided to give it a shot. I loved it! It was fun, easy to learn and understand, had cool in-built data structures. The syntax was plain and simple, just like English. I found it very useful for quick tasks and programming competitions. I used it for solving problems from websites like Project Euler, CodeEval, SPOJ etc. My solutions to some of the problems from Project Euler and CodeEval can be found on my Github.

Though I loved Python, I couldn’t decided whether I wanted to pursue my careeer in it. I didn’t pursue the advanced concepts but had learnt enough for trivial problem solving.

My current favorite is JavaScript. It is the language of Web, and Web is the Future. So JavaScript is the language of future! To quote Douglas Crockford, the great JavaScript wizard:

The World’s Most Misunderstood Programming Language Has Become the World’s Most Popular Programming Language

and yes, he is referring to JavaScript. I am very comfortable with the syntax as coming from a C background and I appreciate its dynamic and functional nature as I was already exposed to those concepts through Python. I am impressed by the recent developments in the JavaScript ecosystem, especially Node.js, and the rise of front-end MV* frameworks. Though it has its quirks, I have planned to take it very seriously and pursue it to be a full stack JavaScript developer. I am also looking forward to learn some un-conventional but exciting! technologies like MongoDB, Redis and the list goes on.

By now, you might have concluded that I am a language geek! For people out there planning to go on a similar adventure, I would like to say that, try to focus on problem solving rather than getting lost in the syntax details. The syntax will come naturally once you learn the basics. I wish you good luck for your programmatic journey. Just don’t forget to have fun! :)

Journey With Programming Languages - Part 2

The Diploma

C was my first friend in diploma. It was a fresh beginning from the basics of programming. The books I referred at that time were Let Us C, How To Program - C Edition. C is an important language when it comes to both programming and understanding how low level stuff, like memory, works, as most of the things has to be taken care by the programmer himself. I was scared by pointers initially, but gradually overcame by trial and error method (what most programmers often do). I used Turbo C, but switched to Dev-C++. On the way I also tried Code Blocks.

In the beginning, it gets bit difficult to decide which is the appropriate tool for the job, but the ultimate choice is what suits you the best.

I learned about random numbers and created fun games for number guessing etc. I am glad that I learnt C, as it helped me in building a good foundation and a sound programming perspective.

Then I came across C++. This time I read about OOPs properly. It felt very similar to C and later I realized that it was a superset of C. The new stuff was classes, objects, templates etc. I didn’t look much into the STL (which I should have done!), but I liked the concept of templates, as they were great for abstraction and generalization of code. The books that I referred to for C++ were C++ for Artists, How To Program - C++ Edition, and Thinking in C++.

I just love abstraction. Learning to think abstractly also leads to a better life (as it is all about ignoring unnecessary stuff).

Next one was SQL. It is actually more of a DSL rather than a programming language. And that means you only need to specify what you want instead of how you want. The query engine handles all that complex stuff. It is easy to learn but it also got complicated with stuff like database specific implementations of SQL, sub-queries etc.

SQL is relevant for RDBMS. I was also introduced to Entity-Relationship model along with that. It is a nice and effective tool for high level design of the database, again providing abstraction. For some hands-on SQL, I preferred to use SQLite, as it didn’t need a server and used simple files as the database. For executing queries and connecting to the database, I used Database .NET. Its a freeware and fine for practicing.

To be continued

Journey With Programming Languages - Part 1

The School

I first saw a PC at a relative’s house, on which I just played NES ( the Nintendo Entertainment System ) like games.

My first PC was Intel Pentium III, 800 MHz CPU, 256 MB RAM, but it was good enough to play the original GTA Vice City.

My first encounter with programming was with MS Logo as part of my school curriculum. I can’t exactly remember which grade I was in at that time. It used simple commands to draw lines, shapes and patterns. It was an awesome first time experience. I used to try various commands trying to build spider-web like patterns.

Then I got introduced to HTML, the language of Web, as it was also a part of my school curriculum. It gave me a sense of working with something that’s really being used heavily out there. It was fun to make awfully looking colorful pages and link them with each other.

In my final grade, we had Java. It felt a lot more related to programming than HTML (I think everybody agrees with that). With that came stuff like Object Oriented Programming, classes, object etc. I was able to understand the basics of programming like variables, types, conditionals etc. but was not pretty clear with the whole Object-Oriented stuff and why things were supposed to be done in that way. I learned the definitions but failed to grasp the true meaning, may be due to my small age and lack of abstract thinking.

That was the time I decided that this is the thing I am going to do for making a living. It gave me a sense of creativity and it was a great feeling to see my code work (for the first time). It gave me a sense of happiness. I felt emotionally attached to code. After completing my matriculation I decided to go for a Diploma in Computer Science instead of conventional higher secondary education, as Maths, Physics and Chemistry failed to excite me much, and there was no point in continuing them ( which I regretted later as my interest aroused in Game Development which heavily relied on Maths and Physics)

This was end of my schooling and then I continued my education by joining Diploma in Computer Science.

To be continued