11     """Return <datetime.datetime() instance> for the given 
   12     datetime string given in OpenCog's date time log format 
   13     >>> _datetime_from_str("2009-12-25 13:05:14:453") 
   14     datetime.datetime(2009, 12, 25, 13, 5, 14, 453000) 
   16     fmt = 
"%Y-%m-%d %H:%M:%S:%f" 
   17     return datetime.datetime.strptime(time_str, fmt)
 
   19 if __name__ == 
"__main__":
 
   20     parser = argparse.ArgumentParser(description=
'Reorder chronologically the log.')
 
   21     parser.add_argument(
'logfile', help=
'Log file to reorder')
 
   22     parser.add_argument(
'-o', 
'--output',
 
   23                         help=
'Output file. If unused stdout is used instead')
 
   24     args = parser.parse_args()
 
   26     timestamp_re = 
r'\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}:\d{3})\]' 
   27     timestamp_prog = re.compile(timestamp_re)
 
   30     of = open(args.output, 
"w") 
if args.output 
else sys.stdout
 
   34     dt, dt_line_num = 
None, 
None 
   36     for l 
in open(args.logfile):
 
   37         m = timestamp_prog.match(l)
 
   40             dt_line_num = line_num
 
   41             dt2txt[(dt, dt_line_num)] = l            
 
   43             dt2txt[(dt, dt_line_num)] += l
 
   51     for dt, dt_line_num 
in sorted(dt2txt.keys()):
 
   52         of.write(dt2txt[(dt, dt_line_num)])