Code Monkey home page Code Monkey logo

pythonpy's Introduction

Community Notice: We'll working on a new release of pythonpy2 that will break backwards compatibility. Check out the thread here if you want to be involved: https://github.com/Russell91/pythonpy/issues/86.

Installation

pip install pythonpy

Bonus: tab completion setup is explained at the end of the readme

Usage

Pythonpy will evaluate any python expression from the command line.

Float Arithmetic

$ py '3 * 1.5'
4.5

Import any module automatically

$ py 'math.exp(1)'
2.71828182846

$ py 'random.random()'
0.103173957713

$ py 'datetime.datetime.now?'
Help on built-in function now:

now(...)
      [tz] -> new datetime with tz's local day and time.

py -x 'foo(x)' will apply foo to each line of input

Multiply each line of input by 7

$ py 'range(3)' | py -x 'int(x)*7'
0
7
14

Grab the second column of a csv

$ echo $'a1,b1,c1\na2,b2,c2' | py -x 'x.split(",")[1]'
b1
b2

Append ".txt" to every file in the directory

$ ls | py -x '"mv `%s` `%s.txt`" % (x,x)' | sh
# sharp quotes are swapped out for single quotes
# single quotes handle spaces in filenames

Remove every file returned by the find command

$ find . -type f | py -x '"rm %s" % x' | sh

Get only 2 digit numbers

$ py 'range(14)' | py -x 'x if len(x) == 2 else None'
10
11
12
13

py -l will set l = list(sys.stdin)

Lists are printed row by row

$ py 'range(3)'
0
1
2

$ py '[range(3)]'
[0, 1, 2]

Reverse the input

$ py 'range(3)' | py -l 'l[::-1]'
2
1
0

Sum the input

$ py 'range(3)' | py -l 'sum(int(x) for x in l)'
3

Sort a csv by the second column

$ echo $'a,2\nb,1' | py -l 'sorted(l, key=lambda x: x.split(",")[1])'
b,1
a,2

Count words beginning with each letter

$ cat /usr/share/dict/words | py -x 'x[0].lower()' | py -l 'collections.Counter(l).most_common(5)'
('s', 11327)
('c', 9521)
('p', 7659)
('b', 6068)
('m', 5922)

For more examples, check out the wiki.

Pythonpy also supports ipython style tab completion, which you can enable by adding the following to your .bashrc

$ if command -v find_pycompletion.sh>/dev/null; then source `find_pycompletion.sh`; fi

The tab completion has a few quirks, all of which are necessary evils. If you find any regressions in behaviour, please report them though.

pythonpy's People

Contributors

russell92 avatar lowks avatar kwpolska avatar abarnert avatar finng avatar svisser avatar abhishekmukherg avatar tyilo avatar bm371613 avatar grzn avatar snahor avatar jhermann avatar razzius avatar myhro avatar yeondudad avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.