VizmayaTech





Which scripting language?

March 17th, 2007 by admin in Programming

Recently I had a problem at hand – I had to parse through a huge text log to parse through mpeg section filter data, to search for lost sections. Since this problem did not occur every test cycle, I had to generate the log again and again and parse through it each time. Obviously, the easiest way was to write a program to do this parsing job.

I know good C/C++, and little bit of python. Although I am not an expert at python, I thought using a scripting language would be much easier here, and I was not wrong. Here’s the code:

import string

#Look for what we want
f_input = open(”log1.txt”)
f_output = open(”result.txt”, “w”)

slist = f_input.readlines();
i = 0
prev = 0
prev_orig = “00″
while i < (len(slist) - 1) :
if slist[i].find("Message_Marker") != -1:
s1 = slist[i+1]
s2 = s1[22:24]
d1 = string.atoi(s2,16)
d2 = (d1 >> 1) & 0×1f # version is bits 1-6
if d2 != (prev+1):
if ((d2 == 0) and (prev == 0×1f)): #This is normal rollover
d2 = 0 #dummy
else:
f_output.write(prev_orig + ” ” + s2 + ” ” + hex(prev) + ” ” + hex(d2) + “\n”)
prev = d2
prev_orig = s2
i = i + 1

f_output.close()
f_input.close()

I am absolutely certain that the above code can be written in much better ways, but the above works well for the purpose. But the point is, it took barely about 15 minutes to develop the above script, and I was happily parsing logs and finding the lost sections.

I look forward to trying out some ruby scripting. Of course, I do not want to compare python/ruby here (that is being done in lot of other forums), I rather choose to play with both and take my time to settle into one of those, or use both in places appropriate.

2 Responses to ' Which scripting language? '

Subscribe to comments with RSS or TrackBack to ' Which scripting language? '.

  1. Pythonista said,
    on April 3rd, 2007 at 7:11 am

    “import re” might help, along with re.findall–get all regex matches in one fell swoop. But not sure since I don’t understand your task.

  2. admin said,
    on April 5th, 2007 at 6:18 pm

    Pythonista,

    You are right – I can make the search much more efficient with a suitable regular expression. Thanks for pointing this out.

Leave a reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: