interactive
interactive is a Python 3 package that helps building interactive scripts using the full power of a Read-Eval-Print-Loop.
Installation
Use pip to install this package.
$ pip install interactive
If you have to install offline you can install it from the sources:
- get the sources from here.
- unzip them (
unzip interactive-main.zip) - go into the directory (
cd interactive-main) - install with pip (
pip install .)
If you want to include this code in your own package, just make the interactive folder available to your project scope.
Usage
Basics
Here is a small self explanatory example:
#!/usr/bin/env python3
from interactive import start, query
from interactive.expect import ExpectPredicate
start(locals()) # Initialize the context
print("Start here")
v = query(int, default=42) # give the user a REPL and wait for an answer
# the user may answer by calling `resume(some_value)`
print(f"Got v = {v} !")
sq = query( # a custom validator for the user answer
ExpectPredicate(lambda x: x == v * v, description="the square of v"),
msg="Provide the square of v",
)
print("Nice, thanks.")
Main classes and functions
The package is very simple, thus the help command should be enough to discover it.
The basic interface is made of the start, query and resume functions.
start and query are used in the script.
resume is used in interactive mode by the user to resume the script.
They are all wrapper around the interactive.context.Context methods.
A custom subclass of Context can be used for advanced control of the process.
It should be passed to start like start(locals(), Context=MyContext).
The sources is currently so simple that you can probably just go check it out.
The Expect class and its children are used to validate the data sent by the user. You may derive them and pass some instances as the type_ argument of query to provide custom validation.
Scoping
The scope of the REPL is handled manually two ways.
If you want to bind to the local scope of a function, use with rescope(locals()):.
Every query in the with block will share the scope of the current function.
An example of that use can be found in samples/02_scoping.py.
If you want arbitrary scope manipulation you may use the start and stop functions.
start(some_scope) bind a new REPL to some_scope and stop() pop the last REPL from the stack.
After a start all query will use some_scope. After stop the previous scope will be restored.
start will also be used to open the top level REPL at the beginning of a script (as demonstrated above).
Metadata
Release files for interactive-script 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| interactive-script-0.2.1.tar.gz | 3.3 kB | Without provenance | Details for interactive-script-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| interactive_script-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for interactive_script-0.2.1-py3-none-any.whl |
Total release size: 6.8 kB
Release files / interactive-script-0.2.1.tar.gz
| Download URL | interactive-script-0.2.1.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9421268f02f03ee5fa3895fe4af356654b927b5238121f36fa1d4cb726db8d64
|
|
BLAKE2b-256 checksum How to use checksums |
be8b81b2cab43c080188364d8717d9455aa574011158862cf4196ad824f4f788
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
|
Release files / interactive_script-0.2.1-py3-none-any.whl
| Download URL | interactive_script-0.2.1-py3-none-any.whl |
|---|---|
| Size | 3.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c75c8b118ca4ce09b77a8f59867042f1fcfd3033324acc6238400be0c2f0d298
|
|
BLAKE2b-256 checksum How to use checksums |
e929055cb345f922db9bcee5755816778e7285b7efa4342b12847214af834c5b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
|
Security for interactive-script 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.