Package: pyinspect
pyinspect - the python package for lazy programmersDon't remember a function's name?Usepyinspectto look for it.Don't remember what a function does?Usepyinspectto print its source code directly to your terminal.Can't figure out why you keep getting an error?Usepyinspect's fancy tracebacks to figure it outStill can't figure it out, but too lazy to google it?Usepyinspectto print Stack Overflow's top answer for your error message directly to your terminal!... and a bunch of other features to make your life easier when coding.An example of how `pyinspect` can be used to find and inspect functions lazily.Table of ContentsInstalling pyinspectWhen you can't remember a variable's name...When you can't remember a function's name...When you can't remember what a function doesWhen you can't fix that bug...When youstillcan't fix that bug...When you got a question, ask GoogleWhen you...Installing pyinspectIt's as simple as:pipinstallpyinspectWhen you can't remember a variable's name..Allright, you've defined a bunch of variables and now can't remember the name or content of the one you need. Fear not, becuse you can usepyinspectto print all variables in your local scope:importpyinspectaspia='my variable'b='another variable'pi.what()# print all local variablesor to look at a single variable in detail with:my_favourite_number=21pi.what(a)This will show you the variable content, where it has been defined and some of its attributes and methods. Something like:When you can't remember a function's name...That's okay! You can usepyinspectto search for a function by its name!
E.g. to look for functions withsearcin their name inpyinspect:# import pyinspectimportpyinspectaspi# Find the functions you're looking forpi.search(pi,name='what')This results in a table with all the function's matching your searchname:note: search also looks for functions in sub-modules of the module given.
e.g.search(matplotlib, 'plot')will look for methods across the entirematplotliblibrary!pyinspect.findcan also be used to find class methods. For example to look for a method withexportin the name inrich.console.Console:pi.search(Console,'export')note:searchalso looks for methods matching your query
among the parents of theclassyou passed. Useinclude_parents=Falsewhen callingsearchto restrict the search to just the class you've passed.
Methods of the parent class are highlighted in a different color!PRO TIP:if you don't pass a search name topyinspect.search(e.g.pyinspect.find(Console)),pyinspect.searchwill printallfunctions and methods.When you can't remember what a function doesOkay, you've found the function you need, that's great.But how does it work?You could openthe file where it's defined, scroll down to it etc... but thispyinspect, the package for lazy programmers! Instead of going thruogh that hastle why not printing the function's code directly to your terminal with a simple command:# import pyinspectimportpyinspectaspi# Look at how pyinspect.search workspi.showme(pi.search)which yields:When you can't fix that bug...Sometimes you know what's causing an error, sometimes you don't. When you don't, it helps to know what the variables involved in the error are, possibly without having to go through the extra work of debugging stuff!Once againpyinspecthas a labour-saving solution: an advancedtracebackfunctionality that gives you all the information you need to fix your bug (hopefully!). Just installpyinspect'stracebackat the start of your script: when get an error you'll get a helpful summary of what's going on with your code!E.g.:# import pyinspect and install the traceback handlerimportpyinspectaspipi.install_traceback()# use hide_locals=True to hide locals panels from your traceback# make some buggy codeimportnumpyasnpa=np.ones(5)b="ignore this"# a local variable not being usedc=np.zeros(4)# ooops, wrong sizea+c# this will give an errorand this is the traceback:note: although we defined three variables (a,b,c) only two where in the line causing the error (a + c).pyinspectthen highlightsaandcin the traceback as this is what you need to know to fix your bug. If you wantpyinspecttoonlyshow the variables in the error line passrelevant_only=Truetopi.install_traceback()pro tips:if you want to showallitems in thelocalscope (e.g. also imported modules, not just variables) then you can useall_locals=Trueinpi.install_traceback()if you don't want the locals to be shown at all, then usehide_locals=Trueif you want more or less extensive tracebacks, you can usekeep_framesto decide how manyframesto shown in nested tracebacks (i.e when a functionacalls a functionband the error comes up inb, do you want to see only the locals inbor inaandb?)When youstillcan't fix that bug...Time to do what any real programmer does in this situation... google it / copy-paste an answer from Stack Overflow. But that involves pulling up your browser, opening a new tab, typing stuf... too much work!When an error comes up,pyinspectgives you the opportunity to automate this work away by doing the googling for you.
You can do that in two ways:passingenable_prompt=Truetoinstall_traceback: after the error traceback a prompt will come up asking if you want to look for solutions online, typey.In a terminal window use thewhycommand andpyinspectwill automatically lookup solutions to the last error you've had [note you need to have installedpyinspect's tracebacks for it to record errors].Either way, you get 3 things:A description of your errorLinks to the top 3 results on GoogleA neat render of a Stack Overflow question and answer related to your error.Check it out:When you got a question, ask GoogleEver found yourself googling the same basic command over and over because you keep forgetting what the syntax is?
If you do (and I know you do), or if you have any other question, now you can look for answers directly in python withpynspect.ask.
Using it is fairly simple:pi.ask("python Concatenate two lists?")note: you can also useaskin your terminal to lookup answers to your questions. E.g.:ask "Python how to concatenate strings"When you...pyinspecthas a few more useful little features you might find yourself using from time to time. One of our favourites ispanels: a simple why to print neat messages to terminal, for when you need to communicate with your users.importpyinspectaspipi.warn('This is a warning','Ooops, something might be wrong!')pi.ok('You got this!','Panels are simple, but nice. Checkout `pyinspect.panels` to see what other kind of panels there are!')You can also useReport, a more advanced panel which allows you to create a more structured and detailed panel (seepi.whats_pi()):As an example, see how I've usedReportto rendermy CVContributingContributions are welcome! Start a pull request if you have a change you'd like to submit or open an issue to report a bug or request a new feature to be added topyinspectAknowledgementspyinspectis mostly a thin wrapper on top of theawesomerichlibrary, so a huge thank you goes to @willmcgugan for the great job done withrich.