Skip to content

Commit

Permalink
chore: py2 removed from docs, code (#589)
Browse files Browse the repository at this point in the history
* Includes removals for Python 2.5(!) workarounds.
* Always use `python3` over `python` in code and docs.
* Using Python 3 print statements in docs.
* Avoid showing `u'` in docs.
  • Loading branch information
henryiii authored Feb 3, 2022
1 parent 4931a15 commit 8fdb08d
Show file tree
Hide file tree
Showing 33 changed files with 125 additions and 139 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Redirection
>>> from plumbum.cmd import cat, head
>>> ((cat < "setup.py") | head["-n", 4])()
'#!/usr/bin/env python\nimport os\n\ntry:\n'
'#!/usr/bin/env python3\nimport os\n\ntry:\n'
>>> (ls["-a"] > "file.list")()
''
>>> (cat["file.list"] | wc["-l"])()
Expand Down Expand Up @@ -174,7 +174,7 @@ Sample output

::

$ python simple_cli.py -v -I foo/bar -Ispam/eggs x.cpp y.cpp z.cpp
$ python3 simple_cli.py -v -I foo/bar -Ispam/eggs x.cpp y.cpp z.cpp
Verbose: True
Include dirs: ['foo/bar', 'spam/eggs']
Compiling: ('x.cpp', 'y.cpp', 'z.cpp')
Expand Down
30 changes: 15 additions & 15 deletions docs/_cheatsheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Basics
>>> ls
LocalCommand(<LocalPath /bin/ls>)
>>> ls()
u'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\n'
'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\n'
>>> notepad = local["c:\\windows\\notepad.exe"]
>>> notepad() # Notepad window pops up
u'' # Notepad window is closed by user, command returns
'' # Notepad window is closed by user, command returns
Instead of writing ``xxx = local["xxx"]`` for every program you wish to use, you can
also :ref:`import commands <import-hack>`:
Expand All @@ -28,7 +28,7 @@ Or, use the ``local.cmd`` syntactic-sugar:
>>> local.cmd.ls
LocalCommand(<LocalPath /bin/ls>)
>>> local.cmd.ls()
u'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\n'
'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\n'
See :ref:`guide-local-commands`.

Expand All @@ -38,10 +38,10 @@ Piping
.. code-block:: python
>>> chain = ls["-a"] | grep["-v", "\\.py"] | wc["-l"]
>>> print chain
>>> print(chain)
/bin/ls -a | /bin/grep -v '\.py' | /usr/bin/wc -l
>>> chain()
u'13\n'
'13\n'
See :ref:`guide-local-commands-pipelining`.

Expand All @@ -51,11 +51,11 @@ Redirection
.. code-block:: python
>>> ((cat < "setup.py") | head["-n", 4])()
u'#!/usr/bin/env python\nimport os\n\ntry:\n'
'#!/usr/bin/env python3\nimport os\n\ntry:\n'
>>> (ls["-a"] > "file.list")()
u''
''
>>> (cat["file.list"] | wc["-l"])()
u'17\n'
'17\n'
See :ref:`guide-local-commands-redir`.

Expand All @@ -69,7 +69,7 @@ Working-directory manipulation
>>> with local.cwd(local.cwd / "docs"):
... chain()
...
u'15\n'
'15\n'
A more explicit, and thread-safe way of running a command in a differet directory is using the ``.with_cwd()`` method:

Expand Down Expand Up @@ -103,7 +103,7 @@ Command nesting
.. code-block:: python
>>> from plumbum.cmd import sudo
>>> print sudo[ifconfig["-a"]]
>>> print(sudo[ifconfig["-a"]])
/usr/bin/sudo /sbin/ifconfig -a
>>> (sudo[ifconfig["-a"]] | grep["-i", "loop"]) & FG
lo Link encap:Local Loopback
Expand All @@ -127,7 +127,7 @@ and `Paramiko <https://github.com/paramiko/paramiko/>`_ (a pure-Python implement
>>> with remote.cwd("/lib"):
... (r_ls | grep["0.so.0"])()
...
u'libusb-1.0.so.0\nlibusb-1.0.so.0.0.0\n'
'libusb-1.0.so.0\nlibusb-1.0.so.0.0.0\n'
See :ref:`guide-remote`.

Expand All @@ -150,9 +150,9 @@ CLI applications
logging.root.setLevel(level)
def main(self, *srcfiles):
print "Verbose:", self.verbose
print "Include dirs:", self.include_dirs
print "Compiling:", srcfiles
print("Verbose:", self.verbose)
print("Include dirs:", self.include_dirs)
print("Compiling:", srcfiles)
if __name__ == "__main__":
MyCompiler.run()
Expand All @@ -162,7 +162,7 @@ Sample output

::

$ python simple_cli.py -v -I foo/bar -Ispam/eggs x.cpp y.cpp z.cpp
$ python3 simple_cli.py -v -I foo/bar -Ispam/eggs x.cpp y.cpp z.cpp
Verbose: True
Include dirs: ['foo/bar', 'spam/eggs']
Compiling: ('x.cpp', 'y.cpp', 'z.cpp')
Expand Down
18 changes: 9 additions & 9 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ might look like this::

And you can run it::

$ python example.py foo
$ python3 example.py foo
I will now read foo

$ python example.py --help
$ python3 example.py --help
example.py v1.0

Usage: example.py [SWITCHES] filename
Expand Down Expand Up @@ -443,12 +443,12 @@ can be enabled by defining the class-level attribute ``ALLOW_ABBREV`` to True. F

With the above definition, running the following will raise an error due to ambiguity::

$ python example.py --ch # error! matches --cheese and --chives
$ python3 example.py --ch # error! matches --cheese and --chives

However, the following two lines are equivalent::

$ python example.py --che
$ python example.py --cheese
$ python3 example.py --che
$ python3 example.py --cheese


.. _guide-subcommands:
Expand Down Expand Up @@ -525,7 +525,7 @@ attached to the root application using the ``subcommand`` decorator ::

Here's an example of running this application::

$ python geet.py --help
$ python3 geet.py --help
geet v1.7.2
The l33t version control

Expand All @@ -540,7 +540,7 @@ Here's an example of running this application::
push pushes the current local branch to the remote
one; see 'geet push --help' for more info

$ python geet.py commit --help
$ python3 geet.py commit --help
geet commit v1.7.2
creates a new commit in the current branch

Expand All @@ -553,7 +553,7 @@ Here's an example of running this application::
-a automatically add changed files
-m VALUE:str sets the commit message; required

$ python geet.py commit -m "foo"
$ python3 geet.py commit -m "foo"
committing...


Expand Down Expand Up @@ -605,7 +605,7 @@ is ignored and the image will no longer be constrained to scale proportionately.
To directly plot an image, the ``show`` method takes a filename and a double
parameter, which doubles the vertical resolution on some fonts. The ``show_pil``
and ``show_pil_double`` methods directly take a PIL-like object. To plot an image
from the command line, the module can be run directly: ``python -m
from the command line, the module can be run directly: ``python3 -m
plumbum.cli.image myimage.png``.

For the full list of helpers or more information, see the :ref:`api docs <api-cli>`.
Expand Down
12 changes: 6 additions & 6 deletions docs/colorlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ following, typed into the command line, will restore it:

.. code:: bash
$ python -m plumbum.colors
$ python3 -m plumbum.colors
This also supports command line access to unsafe color manipulations, such as

.. code:: bash
$ python -m plumbum.colors blue
$ python -m plumbum.colors bg red
$ python -m plumbum.colors fg 123
$ python -m plumbum.colors bg reset
$ python -m plumbum.colors underline
$ python3 -m plumbum.colors blue
$ python3 -m plumbum.colors bg red
$ python3 -m plumbum.colors fg 123
$ python3 -m plumbum.colors bg reset
$ python3 -m plumbum.colors underline
You can use any path or number available as a style.

Expand Down
12 changes: 6 additions & 6 deletions docs/colors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ when Python exits.

.. code:: bash
$ python -m plumbum.colors
$ python3 -m plumbum.colors
This also supports command line access to unsafe color manipulations, such as

.. code:: bash
$ python -m plumbum.colors blue
$ python -m plumbum.colors bg red
$ python -m plumbum.colors fg 123
$ python -m plumbum.colors bg reset
$ python -m plumbum.colors underline
$ python3 -m plumbum.colors blue
$ python3 -m plumbum.colors bg red
$ python3 -m plumbum.colors fg 123
$ python3 -m plumbum.colors bg reset
$ python3 -m plumbum.colors underline
You can use any path or number available as a style.

Expand Down
10 changes: 5 additions & 5 deletions docs/local_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Now that we can bind arguments to commands, forming pipelines is easy and straig
using ``|`` (bitwise-or)::

>>> chain = ls["-l"] | grep[".py"]
>>> print chain
>>> print(chain)
C:\Program Files\Git\bin\ls.exe -l | C:\Program Files\Git\bin\grep.exe .py
>>>
>>> chain()
Expand Down Expand Up @@ -167,7 +167,7 @@ one you passed::
but not both), you can use the ``run`` method, which will provide all of them

>>> cat["non/existing.file"].run(retcode=None)
(1, u'', u'/bin/cat: non/existing.file: No such file or directory\n')
(1, '', '/bin/cat: non/existing.file: No such file or directory\n')



Expand Down Expand Up @@ -277,11 +277,11 @@ as we've seen above, but they can also be other **commands**! This allows nestin
one another, forming complex command objects. The classic example is ``sudo``::

>>> from plumbum.cmd import sudo
>>> print sudo[ls["-l", "-a"]]
>>> print(sudo[ls["-l", "-a"]])
/usr/bin/sudo /bin/ls -l -a

>>> sudo[ls["-l", "-a"]]()
u'total 22\ndrwxr-xr-x 8 sebulba Administ 4096 May 9 20:46 .\n[...]'
'total 22\ndrwxr-xr-x 8 sebulba Administ 4096 May 9 20:46 .\n[...]'

In fact, you can nest even command-chains (i.e., pipes and redirections), e.g.,
``sudo[ls | grep["\\.py"]]``; however, that would require that the top-level program be able
Expand All @@ -302,7 +302,7 @@ We'll learn more about remote command execution :ref:`later <guide-remote-comman
meanwhile, we should learn that command nesting works by *shell-quoting* (or *shell-escaping*)
the nested command. Quoting normally takes place from the second level of nesting::

>>> print ssh["somehost", ssh["anotherhost", ls | grep["\\.py"]]]
>>> print(ssh["somehost", ssh["anotherhost", ls | grep["\\.py"]]])
/bin/ssh somehost /bin/ssh anotherhost /bin/ls '|' /bin/grep "'\\.py'"

In this example, we first ssh to ``somehost``, from it we ssh to ``anotherhost``, and on that host
Expand Down
31 changes: 16 additions & 15 deletions docs/local_machine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Another member is ``python``, which is a command object that points to the curre
(``sys.executable``)::

>>> local.python
<LocalCommand c:\python27\python.exe>
>>> local.python("-c", "import sys;print sys.version")
'2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]\r\n'
<LocalCommand c:\python310\python.exe>
>>> local.python("-c", "import sys;print(sys.version)")
'3.10.0 (default, Feb 2 2022, 02:22:22) [MSC v.1931 64 bit (Intel)]\r\n'

Working Directory
-----------------
Expand All @@ -35,14 +35,15 @@ The ``local.cwd`` attribute represents the current working directory. You can ch

You can also use it as a *context manager*, so it behaves like ``pushd``/``popd``::

>>> ls_l = ls | wc["-l"]
>>> with local.cwd("c:\\windows"):
... print "%s:%s" % (local.cwd, (ls | wc["-l"])())
... print(f"{local.cwd}:{ls_l()}")
... with local.cwd("c:\\windows\\system32"):
... print "%s:%s" % (local.cwd, (ls | wc["-l"])())
... print(f"{local.cwd}:{ls_l()}")
...
c:\windows: 105
c:\windows\system32: 3013
>>> print "%s:%s" % (local.cwd, (ls | wc["-l"])())
>>> print(f"{local.cwd}:{ls_l()}")
d:\workspace\plumbum: 9

Finally, A more explicit and thread-safe way of running a command in a different directory is using the ``.with_cwd()`` method:
Expand All @@ -65,10 +66,10 @@ And similarity to ``cwd`` is the context-manager nature of ``env``; each level w
it's own private copy of the environment::

>>> with local.env(FOO="BAR"):
... local.python("-c", "import os;print os.environ['FOO']")
... local.python("-c", "import os; print(os.environ['FOO'])")
... with local.env(FOO="SPAM"):
... local.python("-c", "import os;print os.environ['FOO']")
... local.python("-c", "import os;print os.environ['FOO']")
... local.python("-c", "import os; print(os.environ['FOO'])")
... local.python("-c", "import os; print(os.environ['FOO'])")
...
'BAR\r\n'
'SPAM\r\n'
Expand All @@ -77,10 +78,10 @@ it's own private copy of the environment::
Traceback (most recent call last):
[...]
ProcessExecutionError: Unexpected exit code: 1
Command line: | /usr/bin/python -c "import os;print(os.environ['FOO'])"
Command line: | /usr/bin/python3 -c "import os; print(os.environ['FOO'])"
Stderr: | Traceback (most recent call last):
| File "<string>", line 1, in <module>
| File "/usr/lib/python3.5/os.py", line 725, in __getitem__
| File "/usr/lib/python3.10/os.py", line 725, in __getitem__
| raise KeyError(key) from None
| KeyError: 'FOO'

Expand All @@ -93,13 +94,13 @@ properties for getting the username (``.user``), the home path (``.home``), and
>>> local.env.home
<Path c:\Users\sebulba>
>>> local.env.path
[<Path c:\python27\lib\site-packages\gtk-2.0\runtime\bin>, <Path c:\Users\sebulba\bin>, ...]
[<Path c:\python39\lib\site-packages\gtk-2.0\runtime\bin>, <Path c:\Users\sebulba\bin>, ...]
>>>
>>> local.which("python")
<Path c:\python27\python.exe>
>>> local.env.path.insert(0, "c:\\python32")
<Path c:\python39\python.exe>
>>> local.env.path.insert(0, "c:\\python310")
>>> local.which("python")
<Path c:\python32\python.exe>
<Path c:\python310\python.exe>


For further information, see the :ref:`api docs <api-local-machine>`.
2 changes: 1 addition & 1 deletion docs/paths.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Paths can be composed using ``/`` or ``[]``::
You can also iterate over directories to get the contents::

>>> for p2 in p:
... print p2
... print(p2)
...
c:\windows\addins
c:\windows\appcompat
Expand Down
Loading

0 comments on commit 8fdb08d

Please sign in to comment.