backquotes
brings (Perl / Ruby)'s shell invocation syntax to Python.
Since backquotes
is an experimental module,
using it in a serious program is not recommended.
Firstly, do not forget to import backquotes
module.
import backquotes
You can use similar syntax to Perl / Ruby.
import backquotes
print(`date`)
Yes, you can use pipes, redirections too.
import backquotes
print(`ls | tr [a-z] [A-Z]`.splitlines())
To bring local variables in Python code to shell command, use Perl-like variables substitution.
import backquotes
spam = 'spam'
print(`echo $spam`)
$$
is replaced with a literal $
.
import backquotes
print(`echo $$PATH`)
You can use runtime-preprocessing only in Python 2.
This works transparently whenever you import backquotes
.
Warning
Python 3 raises SyntaxError on a backquote character before evaluate the first line. So you CANNOT use runtime-preprocessing.
#!/usr/bin/env python
import backquotes
print(`date`)
Save the above code as date.py
, and run it as usual.
python date.py
You will see the result of date
command.
Runtime-preprocessing sometimes causes SyntaxError
before preprocessing starts,
especially with complex commands invocation.
You can avoid this error by using single-quotes just inside the backquotes.
print(`'for file in *; do echo $file; done'`)
Run python
with -m backquotes
option to invoke backquotes
as a script.
backquotes
compiles a plain Python code and execute it.
Note
This works both in Python 2 / 3.
python -m backquotes date.py
You can pass arguments to the script like this.
python -m backquotes date.py 2015 10 31
Run python
with -m backquotes -E
option to only preprocess the given source file
and print the result to stdout.
Warning
Preprocessed python code is almost the same as the original code semantically, but whitespaces may be moved by the preprocessor.
Note
This works both in Python 2 / 3.
mkdir dist
python -m backquotes -E date.py > dist/date.py
python dist/date.py
backquotes
does not work in Python REPL. Importing it in REPL causes warnings.- A module which imports
backquotes
does not work when it is imported by another module.
pip install backquotes
or
git clone https://github.com/manicmaniac/backquotes.git
cd backquotes
python setup.py install
or
wget https://raw.githubusercontent.com/manicmaniac/backquotes/master/backquotes.py