But how to create a nice datastructure to hold routes ? The answer is namedtuple.
Route = namedtuple("Route",headers) routes = [Route(*line.split()) for line in lines[2:]]
route = routes[0] print(route) print("as a tuple %s" % route[0]) print("as a class like structure" % route.Destination) print("as a dictionary %s" % route._asdict())
The output is.
Route(Destination='default', Gateway='172.17.0.1', Genmask='0.0.0.0', Flags='UG', Metric='0', Ref='0', Use='0', Iface='eth0') as a tuple : default as a class like structure : default as a dictionary : OrderedDict([('Destination', 'default'), ('Gateway', '172.17.0.1'), ('Genmask', '0.0.0.0'), ('Flags', 'UG'), ('Metric', '0'), ('Ref', '0'), ('Use', '0'), ('Iface', 'eth0')])
The flexibility of python never ceases to amaze me, and I am always finding hidden gems and quicker ways to do things.