#!/usr/bin/python # Python 2 or 3 compatible. """Reads in a Matlab/Octave line of the following form: function [out1,out2,...] = function_name(input1,input2,...) And outputs skeleton comments for documentation. Updated January 2007 to remove tabs. I've switched sides on that particular holy war. Updated January 2009 to match Matlab's documentation style more. Update June 2012 tweaked to work in Python 3 (as well as 2). Tweak to ensure outputs and inputs stripped, Feb 2016 """ import fileinput, re, time txt = fileinput.input()[0] out_pat = r'\[?([^[\]=]*)[^A-Za-z_]*' fn_name_pat = r'([A-Za-z_][^(]*)' in_pat = r'\(([^)]*).*' if '=' in txt: mobj = re.match(r'function ' + out_pat + fn_name_pat + in_pat, txt) outstr = mobj.group(1) fn_name = mobj.group(2) instr = mobj.group(3) outputs = [x.strip() for x in re.split(',', outstr)] inputs = [x.strip() for x in re.split(',', instr)] else: mobj = re.match(r'function ' + fn_name_pat + in_pat, txt) fn_name = mobj.group(1) instr = mobj.group(2) outputs = [] inputs = re.split(',', instr) if inputs[0] == '': inputs = [] # Format considering maximum length of any input or output argument allargs = inputs[:] allargs.extend(outputs) maxlen = max([len(x) for x in allargs]) outformat = '%%%% %%%ds ?x? ' % maxlen print('%' + fn_name.strip().upper() + ' ') print('%') print('% ' + txt[len('function '):].strip()) if inputs: print('%') print('% Inputs:') for inarg in inputs: print(outformat % inarg) if outputs: print('%') print('% Outputs:') for output in outputs: print(outformat % output) print('') print('% Iain Murray, ' + time.strftime('%B %Y', time.localtime()))