Skip to main content

It does what `python -c` does and a few more

Project description

What is pythonp?

pythonp is a simple utility script that helps you using python on the command line. Basically, it's a python -c command with a handy print function p. See examples below to see how convenient it can be.

How to install

You can install it via pip

python -m pip install pythonp

or you can simply download this repository and copy __main__.py to one of your $PATH locations

cp pythonp/__main__.py ...../pythonp

Handy global variables defined

p

A handy print function with commandline usage in mind. It has the same interface as the default print function except that it specially handles a single iterable as an argument, in which case it prints as many times as the number of elements in the iterable. Giving extra positional arguments along with an iterable is not allowed.

lines

Standard input lines where each line ends with a newline character. You can think of it as sys.stdin except that it's subscriptable and allows a one-time random access, which means you can do something lines[3], lines[10:].

_lines

Lazy evaluted non-stream-like version of lines. Becuase it's a collections.abc.Sequence, you can access its lines multiple times, reverse it, do inclusion test on it, and so forth. The lines are not prepared until you actually use it to save up memory.

Examples

Print numbers

$ pythonp 'p(range(3))'
0
1
2

Get files whose names are longer than 5

$ ls | pythonp "p((l for l in lines if len(l)>5), end='')"
LICENSE
README.md
pythonp
setup.py

Concatenate filenames

$ ls | pythonp "p((l.strip() for l in lines if not 'bombs' in l), end=',')"
LICENSE,README.md,pythonp,setup.py,

Get the 4th column of the processs status

$ ps | pythonp 'p(l.split()[3] for l in lines[1:])'
/usr/local/bin/fish
-fish
python3
ssh

You can also do some crazy stuffs becuase pythonp can do anything that python can do

$ ls | pythonp 'from random import sample; p(sample(_lines, 2))'
$ ls | pythonp 'p(sum(len(l) for l in lines))'
$ cat urls.txt | pythonp 'from requests import get; p(get(url.strip()) for url in lines)'

Misc

Project details


Download files

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

Source Distribution

pythonp-0.0.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distributions

pythonp-0.0.0-py3.6.egg (6.6 kB view hashes)

Uploaded Source

pythonp-0.0.0-py2.py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page