Skip to main content

Better python CLI integration

Project description

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 via pip:

pip install pyli

Or directly via git and setup.py:

git clone https://github.com/thenoviceoof/pyli.git
cd pyli
sudo python setup.py

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.

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

pyli-2.0.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

pyli-2.0.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file pyli-2.0.0.tar.gz.

File metadata

  • Download URL: pyli-2.0.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for pyli-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0815ea7c29bbc0f1e77c9f678066182105761e94081a1b609f2f216d4936a319
MD5 87d067fd23e423ce8064fbbbf74da20c
BLAKE2b-256 cf48105c3d9e0cbb241aad4afb9d0bb43e9a49ddcc7318a8d44e6d6368772ced

See more details on using hashes here.

File details

Details for the file pyli-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyli-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for pyli-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf18ec73e39d56fa9ab6339eef7d92f64899e3e644f926aeae98271fc1aac550
MD5 0e71e1ab5a404092e2160fb08982bb0e
BLAKE2b-256 1a6a02c58cbfde97d4dc88c15aba2cb2b8c167cc38c08f5fba48d4a874c94fd2

See more details on using hashes here.

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