Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

shell command invocation with backquotes, like Perl, Ruby.

Project Description

Introduction

backquotes brings (Perl / Ruby)’s shell invocation syntax to Python.

backquotes is experimental module by now, using it in a serious program is not recommended.

Syntax

Firstly, remember to import backquotes module.

import backquotes

Basic

You can use similar syntax to Perl / Ruby.

import backquotes
print(`date`)

Pipes and redirections

Yes, you can use pipes, redirections too.

import backquotes
print(`ls | tr [a-z] [A-Z]`.splitlines())

Local variables substitution

To bring local variables in Python code to shell command, use Perl-like variables substitution.

import backquotes
spam = 'spam'
print(`echo $spam`)

$$ is substituted to a literal $.

import backquotes
print(`echo $$PATH`)

Usage

Runtime-preprocessing

You can use runtime-preprocessing only in Python 2. This works transparently when 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 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'`)

Execute backquotes module

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.

python -m backquotes date.py 2015 10 31

Preprocess Python code

Run python with -m backquotes -E option to only preprocess the given source file and print to stdout.

Warning

Preprocessed python code is almost the same as the original code semantically, but whitespaces are 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

Restrictions

  • backquotes does not work in Python REPL. Import it in REPL causes warnings.
  • a module which imports backquotes does not work when it is imported.

Install

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
Release History

Release History

This version
History Node

0.0.3

History Node

0.0.2

History Node

0.0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
backquotes-0.0.3-py2-none-any.whl (6.7 kB) Copy SHA256 Checksum SHA256 2.6 Wheel Nov 3, 2015
backquotes-0.0.3.tar.gz (6.8 kB) Copy SHA256 Checksum SHA256 Source Nov 3, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting