Package: inpromptu
InpromptuA library for inferring interactive prompts from object instances.What Inpromptu IsInpromptu is a near-direct replacement for Python's built-incmd.pyutility.
Rather than rewrite an extra class with specialdo_methods, Inpromptu will infer a prompt from the class directly.
Inpromptu takes an object instance's callables and exposes them in a read-evaluate-print-loop that supports tab-completion.Born from a need to quickly interact with real-world devices and a frustration from the manual overhead of cmd.py, Inpromptu automatically generates an interactive prompt session by taking advantage of Python's type hinting and introspection capabilities.
Features includeseamless automatic tab completion using a method's function signaturesupports:bool,int,float,str, anything that inherits fromEnumautomatic help generation using a method's docstringInpromptu also provides aprompt_toolkit-compatible completer so you can build more complicated prompts while getting all of inpromptu's introspection elements for free.What Inpromptu Isn'tInpromptu creates an interactive prompt. Inpromptu is not:a command line interface generator. Seeargparse,python-fire, orclickfor that.a api-replacement for cmd.py. There are some differences, mainly the lack ofdo_methods. Have a go at the examples.RequirementsPython 3.6 or laterall methods that will support completion must have all parameters type-hintedInstallationYou can install this latest stable version of this package from PyPI withpip install inpromptuOr you can clone this repository and, from within this directory, install inpromptu ineditablemode withpip install -e .Example TimeStart with a class in file such as test_drive.py.classTestDrive:def__init__(self):"""initialization!"""self.vehicle_speed=0honk(self):"""beep the horn."""print("Beep!")speed(self):"""return the vehicle speed."""returnself.vehicle_speedCreate a prompt with Inpromptu.frominpromptuimportInpromptumy_test_drive=TestDrive()my_prompt=Inpromptu(my_test_drive)my_prompt.cmdloop()Run it!python3 test_drive.pyThis should produce a prompt:>>>Press tab twice to show all your callable attributes.honk            speed
>>>Great! Now let's demo argument completion.First, add a function withtype-hinted annotationsfor all input arguments (except self or cls).add_fuel(self,gallons:float=0,top_off:bool=False):"""Add fuel in gallons.""passRun it!python3 test_drive.pyStart typing at the prompt>>> add_fPress tab to complete any function.>>> add_fuelPut a space between the command and press tab twice.gallons=<float> top_off=<False>
>>> add_fuelMagic! At this point you can finish entering the command in many ways.>>> add_fuel gallons=10 top_off=FalseOR>>> add_fuel 10 FalseOR>>> add_fuel 10 top_off=FalseIn other words, arguments can be filled out by name or by position or by a combination of position first, then by name--just like how *args and **kwds behave on normal python functions.So what are you waiting for? Why not take it for a test drive? From the example directory, run:python3 test_drive.pyFAQsWhy not just use the python shell?You could! Inpromptu is intented to be a bit more minimalistic and user-friendly.
Inpromptu can be used as a minimalistic UI on its own.Is there any way I can tease out the core elements to build my own interface?Yes. In fact, core elements of Inpromptu can be hooked directly intoPython Prompt Toolkitto provide the same kind of object-based completions with richer prompt features.
See the examples folder for some inspiration.What's not implemented?functions that use*argsand**kwargsas inputThe@overloadoperator.input arguments that can be various types (i.e:Union[int, str, float]).functions wrapped in decorators: like@cache,@cached_propertyfrom functoolsNote: some cases may work already.What's Going to be Implemented Next?Bare Minimum Union type implementation:Unions where None is one of the optionsUnion[None, int]Explicit handling of functions wrapped in decorators.About the AuthorInpromptu was written by someone who used cmd.py one-too-many times. There had to be a better solution.
And Inpromptu is one of many.