Integrating Python and Bash: release of libraries python-shell and smart-env v. 1.0.1

Good day to everyone!

February 29, 2020 the official micro-release of the libraries took place smart-env and python-shell. For those who are not aware, I suggest reading the first post.

In short, among the changes are command autocompletion, enhanced command execution capabilities, a bit of refactoring, and bug fixes.

For details, please see below.

What's new in python-shell?

I'll start right with the dessert.

Command Autocompletion

Isn't it convenient when an editor/IDE/terminal suggests the name of the command, and sometimes even parameters? Python-shell is gradually making progress in providing such functionality. Given that the fields of the Shell class are not actually its fields behind the scenes (thanks to the ubiquitous __getattr__), autocompletion has also been created from scratch (overloading the __dir__ method, respectively). Currently, autocompletion works in BPython and IPython environments. Naturally, we would like to see integration with more mature products like PyCharm, and that direction is being explored.

Adding properties

As part of the release, the Shell class received a new property called last_command. This was needed because, when raising a ShellException with a non-zero return code, the Command object was not returned from the __call__() method of the command object. Now it is possible to do this:

try:
    command = Shell.touch('/foo.txt')
except ShellException:
    command = Shell.last_command

The list of properties of the Command object has also been expanded. A field called errors has been added, which returns the command's output to the error stream.

Executing commands with Python-invalid names

In almost any system, there is at least one program whose name does not qualify as a valid identifier in Python (for example, the well-known utility 2to3). You cannot invoke it with

Shell.2to3()

, as the interpreter won't allow it.
The solution is to invoke the command indirectly:

Shell("2to3")  # returns the command object

It's worth noting that this way you can also launch commands that are valid from the interpreter's perspective, which allows for flexible scripts like

cmd = "python{}".format(sys.version_info[0])
Shell(cmd)(*args, **kwargs)

Minor changes

  • The methods __repr__() and __str__() of the Command class object have been implemented, which now provide intuitive values (the command with parameters and its stdout output, respectively).
  • Minor code fixes.
  • Adding test coverage and reorganizing existing ones.
  • Adding the Subprocess and Process classes, aimed at creating an additional level of abstraction when working with the subprocess module. This is mainly necessary to eliminate code repetitions when working with Python 2/3, but it may also provide other benefits.

What's new in smart-env?

Compared to python-shell, there have been fewer changes in the smart-env library. The reason is simple — lack of free time, during which some potential enhancements (for example, environment variable autocompletion) have been postponed to the next release.

In fact, the following changes have been made to the library:

  • Minor code fixes.
  • Refactoring.
  • Reorganization and improvement of existing tests.

Plans for upcoming releases

The python-shell library

  • Adding support for non-blocking command calls (parallel execution).

The smart-env library

  • Implementation of environment variable autocompletion in the ENV class.
  • Support for the 'in' operator for checking the existence of an env variable.
  • Implementation of support for str() and repr() functions for the ENV class.

Information on the dates of upcoming releases will be announced through the following communication channels:

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster