Package: pythonp
What is pythonp?pythonpis a simple utility script that helps you using python on the
command line. Basically, it's apython -ccommand with a handy print
functionp. See examples below to see how convenient it can be.By design, it avoids adding much sugar to the goold oldpython -c. It introduces no magic except
for a few preprocessings and handy global variables.
The goal of this project is to deliver seamless experience to
python users and become a part of some major python
implementations in the end, without remaining as a standalone package.DisclaimerThere are already several projects that have
smilar goals with this project such aspyp,pythonpyand others.
Particularlypythonpyis super
popular. I think they are all amazing projects and I don't mean to assert that
every aspect ofpythonpis breakingly new.But there are fundamental differences betweenpythonpand others. Notably,pythonphas been designed
to be able to runany python programs, not just
single statements. Any
valid python code should be able to be run withpythonpand only
(almost valid) python code should.How to installYou can install it via pippython-mpipinstallpythonpor you can simply download this repository and copy__main__.pyto
one of your$PATHlocationscppythonp/__main__.py...../pythonpHandy global variables definedpA handy print function with commandline usage in mind. It has the
similar interface to the built-inprintwith some exceptions.It specially handles a single iterable as an argument,
in which case it prints as many
times as the number of elements in the iterable. Giving extra positional
arguments along with an iterable is not allowed.linesStandard input lines. You can think of it assys.stdinexcept that
each line of it doesn't end with a newline character. Also note that it's
subscriptable and allows a one-time random access, which means you
can do something likelines[3], lines[10:].llis a line from the standard input. It also doesn't end with a new
line character.
Without-eoption,lis the first line from the standard input.
With-eoption, it represents each line
of the standard input. See the feature explanation below to learn-eoption._linesLazy evaluted non-stream-like version oflines.
Becuase it's acollections.abc.Sequence, you can access its
lines multiple times, reverse it, do inclusion test on it,
and so forth. The lines are not prepared until you actually
use it to save up memory.FeaturesThe last expression is automatically printed withpfunction if your
code dind't write anything tosys.stdoutand the last expression does
not evalute toNone. If you don't want this feature you can put
something like;passor;Nonein the end of your code.If-eoption is given, your code is applied to each linelof the
stanard input, not the
entire lineslinesor_lines. The nameslinesand_lineswill
disappear and can not be used. Note that in the current implementation,
globals are shared during continued executions of the code
and there could be some side effects.
This is an intended behavior but can change in the future.Automatic importing is supported.pythonpautomatically tries to
import a name for you when it encounters an unseen one.Backtick(`) in code is replaced with"""so that you can have
one more way to make string literals. In python 3.6 or abovefprefix
is also added to make the enclosed section af-string.
For example, you can do
something like this.$echo91/seoul/bombs|pythonp"`name='{l.split('/')[2]}'`"# python3.6+name='bombs'ExamplesMake commands that you want on the fly and and run them with your shell# Remove extensions of .txt files$ls|pythonp-e"if l.endswith('.txt'): p('mv', l, l[:-4])"|shRandomly sample N lines from a large number of lines# choose n files randomlyls|pythonp"random.sample(_lines, 3)"item_1443
item_6360
item_7285Concatenate lines$ls|pythonp"','.join(l.strip() for l in lines if not 'bombs' in l)"LICENSE,README.md,pythonp,setup.pyDo something foreach line# A web crawler one-liner$caturls.txt|pythonp-e'p(requests.get(l)); time.sleep(1)'>outputSplit a long line and output the nth chunk# Get the 4th column from the current processs status$ps|tail-n+1|pythonp-e"l.split()[3]"/usr/local/bin/fish
-fish
python3
ssh# Only using only pythonp$ps|pythonp"lines[1:]"|pythonp-e"l.split()[3]"Others# Use it to solve some weird quiz$pythonp"now=datetime.datetime.now();(now.year+now.day)%10"# Make at most 5 random names$pythonp"'\n'*(5-1)"|pythonp-e"''.join(random.sample(string.ascii_letters, 7))"|xargstouchMiscIf you want a shorter name forpythonpyou can do something like this.mv$(whichpythonp)$(dirname$(whichpythonp))/py# rename pythonp to pyBoth python2 and python3 are supported.Refer to python official docs to learn useful string manipulating functionshttps://docs.python.org/3/library/string.htmlIt is a good idea to use generator expressions or list comprehensions
with pythonphttps://docs.python.org/3/howto/functional.htmlIf you want some other features, you are always welcome to make an issue
at the issue tab on the top menu.