Making Python and Bash friends: release of python-shell and smart-env v. 1.0.1

Good day to all!

29 February 2020 year the official micro-release of libraries took place smart env ΠΈ python shell. For those who don't know, I suggest you read first. first post.

In short, among the changes are command auto-completion, expanding the ability to run commands, some refactoring and bug fixes.

For details, please under cat.

What's new in python-shell?

Let's start with dessert.

Command completion

Agree - it's convenient when the editor / IDE / terminal suggests the name of the command, and sometimes the call parameters? So python-shell is gradually moving forward in providing such functionality. Due to the fact that the fields of the Shell class under the hood de-facto are not its fields (the ubiquitous __getattr__), the autocompletion was also created from scratch (by overloading, respectively, the __dir__ method). Completion currently works in BPython and IPython environments. Of course, I would like to see integration with more venerable products like PyCharm, and in this direction we are studying the possibilities of implementation.

Adding properties

As part of the release, the Shell class received a new last_command property. The need for it arose due to the fact that when a ShellException was thrown by a command with a non-zero return code, the Command object was not returned from the __call__() call 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 expanded. Added an errors field that returns the output of a command to the error stream.

Running commands with invalid Python names

Almost every system has at least one program whose name is not suitable as an identifier in Python (for example, the well-known 2to3 utility). Call her with

Shell.2to3()

If it doesn't work, the interpreter won't let it through.
The solution is to call the command in a workaround:

Shell("2to3")  # Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹

It is worth noting that in the same way you can run commands that are valid from the point of view of the interpreter, which leaves the opportunity to make flexible scripts like

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

Minor changes

  • The __repr__() and __str__() methods of the Command class object have been implemented, which now produce intuitively understandable values ​​(a command with parameters and its stdout output, respectively).
  • Minor code fixes.
  • Adding test coverage, as well as reorganizing existing ones.
  • Addition of the Subprocess and Process classes, the purpose of which is to provide an additional layer of abstraction when working with the subprocess module. For the most part, it is necessary to eliminate code repetition when working with Python 2/3, but it can potentially provide other bonuses.

What's new in smart-env?

Unlike the python-shell, there have been fewer changes in the smart-env library. The reason for this is simple - lack of free time, during which some potential improvements (for example, autocompletion of environment variables) were transferred to the next release.

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

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

Plans for next releases

python shell library

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

smart-env library

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

The dates of the next releases will be additionally announced through the following communication channels:

Source: habr.com

Add a comment