Automation, let’s accept it, it is not easy, either it is a product line or getting your code built on the click of a button or as we have all done at some point of time, manually changing those  long and long repeated lines of code and hoping that a robot will do it for you(or maybe that rookie intern ;)).

So, talking in terms of code, what actually is automation and how useful it is and what kind of tools does it use. To analyse all this, let me talk about a scenario(read mundane) that recently occurred during my day job which rather forced me to come up with a small ‘robot’. As the client would have it, While writing an objective-c client for a server, I had some java code lying around, which I was referring to and this Java code had a rather long list(~300) of enums(string based) and as we all know c does not support those string based pixies and would need to be stripped away. So, I was wondering that how can I just copy these into a .m file and quickly get away with it. After pondering for like 5 mins, I realized, that it would be better if I could just write some code which can edit my code and hence save me some time.

Having decided so, I picked up Python as the tool to work with and after 15 or so mins(Sorry, my bad, my Python is really lousy, since I mostly read about it but never practice it, but am sure it won’t be so after this post.), came up with this little piece on terminal:

f = open("xyz.h", 'r')
lines = f.readlines()
f.close()
f = open("xyz.h", 'w')
import re
for line in lines: p = re.compile('\((.*?)\)'); line = p.sub('', line); f.write(line);
f.close()

AND, voila, I had a file but this time with only enum constants(Before: ENUM_CONSTANTS(“string”) After: ENUM_CONSTANTS). The little robot just accomplished this in few lines code.

Now, I’m looking forward to use Python as much as possible to create little robots like these to edit my code so that I can create a few more bugs in the meantime 😉

Happy Coding.

Advertisements