Skip to main content

pyli

Better scripting with python

Haven't you ever missed writing perl one liners? Or maybe you're more familiar with python than all the options of find, but like the text interaction paradigm of the command line? pyli aims to make it easier to use python and all its batteries in conjunction with the shell.

Install

pyli is available from pypi via pip:

pip install pyli

You can also download the source directly:

git clone https://github.com/thenoviceoof/pyli.git
pip install pyli/

Unfortunately, type annotations mean the minimum supported Python version is 3.9.

Examples

Let's do some warmups:

pyli "2+2" # bc
cat file.txt | pyli "line if 'string' in line else None" # grep
cat file.txt | pyli "sum(len(l) + 1 for l in lines)" # wc -m

And now some more complicated examples:

log | pyli "str(time.time()) + ' ' + line" # time stamping a line
cat file.txt | pyli "set(w for s in nltk.sent_tokenize(contents) for w in nltk.word_tokenize)" # bag of words a file
cat file.json | pyli "pickle.dumps(json.loads(conts))" >file.pickle
cat space_sep.dat | pyli "json.dumps(dict(parts))" >file.json

Maybe it makes sense to separate commands:

cat index.html | pyli "for l in [a.get('href') for a in bs4.BeautifulSoup(cs).find_all('a')]: print l" | pyli --text='something' "r = requests.get(li); li if text in r.text else None"
cat index.html | pyli "hashlib.sha1(cs).hexdigest()" | pyli "encryptedfile.EncryptedFile(stdout, getpass.getpass()).write(cs)"

Perhaps you want to keep it a one liner, but Python is too opinionated to let you do that:

pyli -f "`ls -a`" "for l in f.split('\n'):" "    if '.git' == l: print 'git'"

pyli

Features:

  • Automatically import referred packages
  • Populate special CLI oriented variables
    • line (li, l): Gives you access to each line
    • lines (lis, ls): Access to the line generator
    • contents (cont, cs): Gives you access to all of stdin in one string
    • part, (p): Gives you access to the different fields of a space-separated line
    • parts, (ps): Access to the part generator
    • stdin, stdout, stderr: A shortcut to sys.std* streams
    • Accept arbitrary GNU style arguments (-c, --blah), and make them available
    • Print last statement; if an assignment, print the value assigned to variable(s)
    • If we are using line/part, then print the last statement for each line

Do note that you should only access one of these special variables at a time: no work has been put into combining these into something that makes sense, so if you want multiple variables, you'll have to do the legwork yourself.

See the issue tracker.

Related Projects

In alphabetical order:

LICENSE

Copyright (c) <2014>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release files for pyli 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyli 2.0.2
File Size Uploaded
pyli-2.0.2.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyli 2.0.2
File Interpreter ABI Platform
pyli-2.0.2-py3-none-any.whl Python 3 none any Details

Total release size:36.2 kB

Release files / pyli-2.0.2.tar.gz

Download URL pyli-2.0.2.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c5f412ae55298efb41858e7f9f821a98125e758065a88b44ea106a38746aae79
BLAKE2b-256 checksum
How to use checksums
bced70f8f02c9bc3e09582a2141331466e7e12338a5e5fd1ee9507427add1f0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.6

Release files / pyli-2.0.2-py3-none-any.whl

Download URL pyli-2.0.2-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abb984a2ef4b1e14c8bfed16b61ee786cd67fdc90c0dedfacd153ddc6f19e50f
BLAKE2b-256 checksum
How to use checksums
cebb9edb24aaccf66250e5cbfdecb20621781c06360c951e9dcaafcd9b6a03b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.6

Release history Release notifications | RSS feed

This release

2.0.2 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.4.1

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.5

1 release file

0.5.3

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page