I have to deal with a really ugly looking response I get back from a particular device. The string represents a table. Each section of the string represents the form:
["id", "date_time", "path", "type","duration","count", "rate"]
Which as an example looks like:
"1,2014-4-3 10:1:10,somerandompath,some/screwy!stringwithstufflike[0,0,0,0]inIt,0,0;"
all table entries as you can see are terminated by ;
and have their elements separated by ,
But some entries like in some/screwy!stringwithstufflike[0,0,0,0]inIt
have commas within their data,.. and times/dates 10:1:10
aren't padded with 0
's to be consistent
This is what I have so far, but it doesn't feel very optimized...
def GetEventTable(self):
# After I split by ',' commas can be inside of [..],
# so I combine entries until their number of starting/end braces match
def split_entry(entry):
splitentry = entry.split(',')
for ii in range(len(splitentry))[::-1]:
start_bracket = splitentry[ii].count('[')
end_bracket = splitentry[ii].count(']')
if not start_bracket == end_bracket:
splitentry[ii-1:ii+1] = [splitentry[ii-1] + ',' + splitentry[ii]]
return splitentry
event_lable = ["id", "date_time", "path", "type","duration","count", "rate"]
## This section just talks to the remote device and gets the unformatted string ###
print "Sending: GET EVENT TABLE"
stat, raw_data = SendRecieve(self, "GET EVENT TABLE")
if IsError(stat):
raise Exception("Error type '{}': {}".format(stat, raw_data))
print raw_data
##################################################################################
events = raw_data.split(';')[:-1]
event_table = [{key:value for key,value in zip(event_lable,split_entry(entry) )} for entry in events ]
return event_table
How can I improve the efficiency and also the readability?
It returns a list of dictionaries with keys ["id", "date_time", "path", "type","duration","count", "rate"]
SendRecieve
function that probably does not exist on a standard installation (the typo is a pretty good hint). \$\endgroup\$ – SylvainD Apr 4 '14 at 15:07