Package: prepr
preprA highly customizible system that allows your class representations (e.g. in__repr__), when printed in the terminal, to go from looking like this:to this:Installpip install preprConstructing a pretty representationIn order to show how to construct a pretty repr string, we will be using the below example class:classExample:def__init__(self,posarg,kwarg=None):self.posarg=posargself.kwarg=kwargself.attr="something"To start, we create an instance ofpreprinitialized with the class instance:importpreprclassExample:def__repr__(self,*args,**kwargs):R=prepr.prepr(self)Then, we start adding positional arguments (of which we have one,posarg) by providing the argument value:classExample:def__repr__(sel,*args,**kwargs):R=prepr.prepr(self)R.arg(self.posarg)Next, we start adding our keyword arguments (of which we have one,kwarg) by providing a kwarg name and value. Additionally, we can set thed(default) optional argument to a value, in which case the kwarg will not be added to the representation if it is equal to that default value:classExample:def__repr__(self,*args,**kwargs):R=prepr.prepr(self)R.arg(self.posarg)R.kwarg("kwarg",self.kwarg,d=None)We can also add attributes that will be displayed after the initial representation. These should only be added for attributes that can change outside of initialization, as displaying attributes that are simply determined by the input variables isn't really necessary (although theres nothing stopping you from adding those attributes too). In the same way as thekwargmethod, we can provide a default value if wanted:classExample:def__repr__(self,*args,**kwargs):R=prepr.prepr(self)R.arg(self.posarg)R.kwarg("kwarg",self.kwarg,d=None)R.attr("attr",self.attr,d="something")Finally, we return the response from thebuildmethod. Note that passthrough args (*args) and kwargs (**kwargs) arerequired:classExample:def__repr__(self,*args,**kwargs):R=prepr.prepr(self)R.arg(self.posarg)R.kwarg("kwarg",self.kwarg,d=None)R.attr("attr",self.attr,d="something")returnR.build(*args,**kwargs)A few notes: firstly, each of the addition methods (arg,kwarg, andattr) have convenience methods that can be used to add multiple in a single function call; those are:args,kwargs, andattrs. Second, each of the addition methods returns the class instance, which means you can chain if desired. Below, we will create an instance of our example class, then print it:importpreprclassExample:def__init__(self,posarg,kwarg=None):self.posarg=posargself.kwarg=kwargself.attr="something"def__repr__(self,*args,**kwargs):# here we also provided a note which will be displayed# in the representation, as you will see laterreturnprepr.prepr(self,note="here are some notes").arg(self.posarg).kwarg("kwarg",self.kwarg,d=None).attr("attr",self.attr,d="something").build(*args,**kwargs)inst=Example("abcd",kwarg=1234)inst.attr=[1,2,3]print(inst)In our terminal, we get:Changing the global display settingsThe display settings are available through theprepr.settingsclass, and allow you to completely change how the representations are created. Settings can be batch-updated with thesettings.updatemethod, and can be reset to default settings with thesettings.defaultmethod. All of the below examples are using this instance of theExampleclass above:inst=Example(Example(1234,"abcd"),kwarg={"a":[1,2,3],"b":(True,False)})inst.attr="SOMETHING"Changing the colorspaceThe colorspace defines how the representation is displayed in the terminal when printed. There are four main colorspaces available (examples of each are also below):Colorspace.rgbfull- Full RGB colors that, in my opinion, look the best. The full RGB ANSI syntax is not supported in every terminal, so if it does not work, try one of the others.Colorspace.rgb256- Uses a 16x16 color pallete instead of full RGB, and is more widely supported.Colorspace.rgb8- A colorspace that uses the 8 widely supported ANSI color codes (30 through 37). This colorspace has the most support.Colorspace.none- No terminal formatting codes will be applied to the representation. Also useful if the text from the representation will be used elsewhere instead of being printed, in which case the raw text would most likely be desired over the text filled with color codes.Setting the global colorspace is quite easy. All you need to do is setsettings.cshto aCSHandlerinstance initialized with a validColorspace; example below:importpreprprepr.settings.csh=prepr.CSHandler(prepr.Colorspace.rgb8)Additionally, you can create a custom colorspace by using theColorspace.customclass. Examples of each of these colorspaces (and their implementations) are below:Colorspace.rgbfullBy default, thergbfullcolorspace is used.print(inst)Colorspace.rgb256importpreprprepr.settings.csh=prepr.CSHandler(prepr.Colorspace.rgb256)print(inst)Colorspace.rgb8importpreprprepr.settings.csh=prepr.CSHandler(prepr.Colorspace.rgb8)print(inst)Colorspace.noneimportpreprprepr.settings.csh=prepr.CSHandler(prepr.Colorspace.none)print(inst)Custom indentA custom indent can be set by assigningsettings.indentastr(which by default is four whitespaces):importpreprprepr.settings.indent="  "print(inst)Custom line breakA custom line break can be set by assigningsettings.line_breakastr(which by default is"\n"):importprepr# we set the indent to a single space just so the repr isn't# too obnoxious, given the default is four whitespacesprepr.settings.indent=" "prepr.settings.line_break=""print(inst)Force lists, tuples, dicts and/or sub-preprs to be collapsedBy default, lists, tuples, dicts and sub-preprs are fully expanded. You can force them to be collapsed with thesettings.force_lists_collapsed,settings.force_tuples_collapsed,settings.force_dicts_collapsed, andsettings.force_sub_preprs_collapsedsettings. For example:importpreprprepr.settings.force_lists_collapsed=Trueprint(inst)importpreprprepr.settings.force_tuples_collapsed=Trueprint(inst)Any objects within the list/tuple/dict/sub-prepr (if set to be collapsed) will also be collapsed. For example:importpreprprepr.settings.force_dicts_collapsed=Trueprint(inst)Other notesIn addition to thesettings.defaultpreset method, there is asettings.minimalmethod, which will force the entire representation into one line with as little spacing as possible. For example:importpreprprepr.settings.minimal()print(inst)