Skip to main content

Better python CLI integration

Project description

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? pyli aims to make it easier to use python and all it’s batteries in conjunction with your favorite hangout: 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

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 something more complicated:

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

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)

    • lines (lis, ls)

    • contents (cont, cs)

    • stdin, stdout, stderr

    • Accept arbitrary GNU style arguments (-c, –blah), and make them available

    • Print last statement; if an assignment, print the assigned to variables

    • If we are using line, then print the last statement for each line

TODO

  • Move the engine to the nice-looking ast library

  • Make sure it works in more snakes than just 2.7.3

Also see the issue tracker

LICENSE

“THE BEER-WARE LICENSE” (Revision 42):

<thenoviceoof> wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return

  • thenoviceoof

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-1.1.0.tar.gz (10.4 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pyli-1.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyli-1.1.0.tar.gz
Algorithm Hash digest
SHA256 04eeeb9aeccb421beef3a36347297ef17f175e69d969beb89884303c1451a97c
MD5 91ff1f94489e8efc4c71eb2304709322
BLAKE2b-256 c6af8a7e7a6d74c7363801faeae23ba7a7c6c9e7e9408414f84f4763f9e45d1b

See more details on using hashes here.

Supported by

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