Package: selfdocprint
SelfdocprintProvides enhanced and configurableprint()functionality that allows for easily printing values such that they are:self-documentedformattednicely laid outFully compatible with python's built-inprint(). Supports all its keyword arguments, includingfile=andflush=.The module comes with four layout classes. Below is an example of InlineLayout.fromselfdocprintimportPrintFunc,InlineLayout# instantiate a functionprint=PrintFunc()# this will now shadow python's built-in print function# some dataformula="Ultimate Question of Life,\nthe Universe, and Everything"theta=6.006x=7# let's print it with our print functionprint(formula,theta,x,x*theta,layout=InlineLayout())The default value for thelayout=keyword argument (normallyNone) can be configured so that subsequent calls require even less typing. In this case it would be good practice to not call the function 'print' to avoid confusion with the expected behaviour of python's built-inprint().prinline=PrintFunc(default_layout=InlineLayout())prinline(formula,theta,x,x*theta)A layout can be customised by overriding one or more of its fields (The fields are described in detail in the section: 'Style, format and layout specification'). Note that in the example below we overridehead=to suppress the empty line that is normally printed in front of the output when using the InlineLayout.foriinrange(0,99,21):print(i,i*theta,layout=InlineLayout(float_format="12.6f",pointer=" -> ",head=""))Custom layouts can be created and referenced in subsequent calls. Note that in the example below we change thestyle=for the labels, which is specified asANSI Select Graphic Renditionparameters.my_layout=InlineLayout(style="92;3",int_format="4")print(x,x*theta,layout=my_layout)Built-in layoutsNext to the'inline'layout three additional layouts are available. The example below demonstrates the'dict'layout. In addition it shows the use of thebeg=keyword argument which can be used to provide a heading to the output.fromselfdocprintimportDictLayoutprint(formula,theta,x,x*theta,layout=DictLayout(),beg="Using the 'dict' layout:\n")For values that take up a lot of horizontal space the'scroll'layout is particularly useful.fromselfdocprintimportScrollLayoutimportnumpyasnpX=np.random.rand(5,5)print(X,X.T@X,layout=ScrollLayout(),beg="Using the 'scroll' layout:\n")Use the'minimal'layout if you only want to print labels in front of the values.fromselfdocprintimportMinimalLayoutprint(formula,theta,x,theta*x,layout=MinimalLayout(),beg="Using the 'minimal' layout:\n\n")Style, format and layout specificationTheselfdocprint.print_layout_specs()function prints the specification for every built-in layout and a rudimentary description of the layout algorithm.importselfdocprintassdpsdp.print_layout_specs()All parameters are of type str. The format parameters are specified according to python'sFormat Specification Mini-Language. If an alignment character (<, >, or ^) is specified without a fixed width in the lbl_format, then the alignment will be made relative to all other <labels>. For the str_format a missing width value will result in an alignment of all lines in the string representation of a value. The algorithm injects the width of the longest string of the <label> strings and the width of the longest line in a <value> string into lbl_format and str_format respectively.The sty parameter is a string with zero or moreANSI Select Graphic Renditionparameters seperated by semicolons (;).The layout algorithm globally works as follows: 1) adjust lbl_format and str_format where necessary; 2) style and format the concatenations of each <label> withpointer; 3) format the values; 4) layout each <label>/<value> pair in a 'pane'; 5) join these panes together withseperator; 6) print the panes: ifseperatorcontains a newline ('\n') character then the panes are laid out from top to bottom, if not, they are laid out from left to right.LimitationsSelfdocprint cannot be used in the Repl nor witheval(),exec()andcompile(). Selfdocprint uses theinspectmodule to discover the labels and this does not work in these situations.Selfdocprint's print function cannot accept arguments that use the unpack operator, for example to unpack a tuple:*my_tuple. This does not make sense anyway because there is nothing to label the individual values of the tuple with, so it's just as informative to just use the tuple variable as is.