Skip to main content

python -c, with tab completion and shorthand

Project description

Installation
------------

::

sudo pip install pythonpy
# restart your shell for tab completion to take effect

::

py 'expression' ≅ python -c 'print(expression)'
-----------------------------------------------

Float Arithmetic
~~~~~~~~~~~~~~~~

::

$ py '3 * 1.5'
4.5

::

Access imports directly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

$ 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.


::

Lists are printed row by row
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

$ py 'range(3)'
0
1
2

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

::

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

::

Append ".txt" to each line of input
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

$ py 'range(3)' | py -x 'x + ".txt"'
0.txt
1.txt
2.txt

::

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

::

Get only even numbers
~~~~~~~~~~~~~~~~~~~~~

::

$ py 'range(8)' | py -x 'x if int(x)%2 == 0 else None'
0
2
4
6

::

py -fx 'predicate(x)' filters rows satisfying a condition
---------------------------------------------------------

Get only odd numbers
~~~~~~~~~~~~~~~~~~~~

::

$ py 'range(8)' | py -fx 'int(x)%2 == 1'
1
3
5
7

::

Get words starting with "and"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

$ cat /usr/share/dict/words | py -fx 're.match(r"and", x)' | head -5
and
andante
andante's
andantes
andiron

::

Get verbs starting with ba
~~~~~~~~~~~~~~~~~~~~~~~~~~

::

$ cat /usr/share/dict/words | py -fx 're.match(r"ba.*ing$", x)' | head -5
baaing
babbling
babying
babysitting
backbiting

::

Get long palindromes
~~~~~~~~~~~~~~~~~~~~

::

$ cat /usr/share/dict/words | py -fx 'x==x[::-1] and len(x) >= 5' | head -5
civic
deified
kayak
level
ma'am

::

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

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

::

Count the lines of input
~~~~~~~~~~~~~~~~~~~~~~~~

::

$ py 'range(17)' | py -l 'len(l)'
17

::

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)

::

If you haven't had enough yet, check out the `wiki <http://github.com/Russell91/pythonpy/wiki>`__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

pythonwpy-0.3.15.tar.gz (8.2 kB view details)

Uploaded Source

File details

Details for the file pythonwpy-0.3.15.tar.gz.

File metadata

  • Download URL: pythonwpy-0.3.15.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pythonwpy-0.3.15.tar.gz
Algorithm Hash digest
SHA256 8da4fca593dcbcfcd9862f6039bc58aff5f53ea82510d76fd705b6e03a64be09
MD5 55a324839a29b26ab894403b26c2060b
BLAKE2b-256 69b2586c416b7b7ada607affcf59545a7043d1011f90735a4c258317c1ce8c46

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