设为首页 收藏本站
查看: 599|回复: 0

[经验分享] Powerful Python One-Liners

[复制链接]

尚未签到

发表于 2017-4-24 11:16:32 | 显示全部楼层 |阅读模式
  http://wiki.python.org/moin/Powerful%20Python%20One-Liners
  


Powerful Python One-Liners

This is a page that is devoted to short programs that can perform powerful operations. The ability to write short programs that are just as powerful as a program written in another language designed to do the same thing. However, it is sometimes
fun to try and write a program in Python that is only one line. In other languages this would be nearly
impossible, but in Python it is a lot easier to do. The trick is to think up something that will "do a lot with a little." I, personally, would love to see this page expanded to the point where it needs some sort of organization system.

Thanks for Your Code, JAM

Of course, there is debate on whether one-liners are even
Pythonic
.


Contributed Code




  • JAM/Code/PlatformFinder - This program tells you what platform you are using.



  • JAM/Code/ComPYiler - This program compiles every .py file in the Python directory.



  • Powerful Python One-Liners/Hostname - This programs tells you what your hostname is.




Some thoughts by ewo:



  • Want to know many bytes a terabyte is? If you know further abbreviations, you can extend the list.



import pprint;pprint.pprint(zip(('Byte', 'KByte', 'MByte', 'GByte', 'TByte'), (1 << 10*i for i in xrange(5))))


  • And what's the largest number that can be represented by 8 Byte?



print '\n'.join("%i Byte = %i Bit = largest number: %i" % (j, j*8, 256**j-1) for j in (1 << i for i in xrange(8)))

Cute, isn't it?


Set of all subsets



  • Function that returns the set of all subsets of its argument



f = lambda x: [[y for j, y in enumerate(set(x)) if (i >> j) & 1] for i in range(2**len(set(x)))]




>>>f([10,9,1,10,9,1,1,1,10,9,7])
[[], [9], [10], [9, 10], [7], [9, 7], [10, 7], [9, 10, 7], [1], [9, 1], [10, 1], [9, 10, 1], [7, 1], [9, 7, 1], [10, 7, 1], [9, 10, 7, 1]]

-RJW
Alternately (shorter, more functional version):


f = lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])



Decode a base64 encoded file




import base64, sys; base64.decode(open(sys.argv[1], "rb"), open(sys.argv[2], "wb"))



Editing a list of files in place


I came up with this one-liner in response to an
article that said it couldn't be done as an one-liner in Python.

What this does is replace the substring "at" by "op" on all lines of all files (in place) under the path specified (here, the current path).




  • Caution: Don't run this on your home directory or you're going to get
    all your text files edited.



切换行号显示
   1 import sys,os,re,fileinput;a=[i[2] for i in os.walk('.') if i[2]] [0];[sys.stdout.write(re.sub('at','op',j)) for j in fileinput.input(a,inplace=1)]






Clearer is: importos.path;a=[fforfinos.listdir('.')ifnotos.path.isdir(f)]



Reimplementing cut


Print every line from an input file but remove the first two fields.



python -c "import sys;[sys.stdout.write(' '.join(line.split(' ')[2:])) for line in sys.stdin]" < input.txt



Cramming Python into Makefiles


A related issue is embedding Python into a Makefile. I had a really long script
that I was trying to cram into a makefile so I automated the process:


import sys,re

def main():
    fh = open(sys.argv[1],'r')
    lines = fh.readlines()
    print '\tpython2.2 -c "`printf \\"if 1:\\n\\'
    for line in lines:
        line = re.sub('[\\\'\"()]','\\\g<0>',line)
        # grab leading white space (should be multiples of 4) and makes them into
        # tabs
        wh_spc_len = len(re.match('\s*',line).group())
        
        sys.stdout.write('\t')
        sys.stdout.write(wh_spc_len/4*'\\t'+line.rstrip().lstrip())
        sys.stdout.write('\\n\\\n')
    print '\t\\"`"'

if __name__=='__main__':
    main()

This script generates a "one-liner" from make's point of view.



echo unicode character:




python -c "print unichr(234)"

This script echos "ê"


Apply regular expression to lines from stdin




[another command] | python -c "import sys,re;[sys.stdout.write(re.sub('PATTERN', 'SUBSTITUTION', line)) for line in sys.stdin]"



Modify lines from stdin using map




python -c "import sys; tmp = lambda x: sys.stdout.write(x.split()[0]+'\t'+str(int(x.split()[1])+1)+'\n'); map(tmp, sys.stdin);"



Display List of all users on Unix-like systems




print '\n'.join(line.split(":",1)[0] for line in open("/etc/passwd"))



Compress CSS file




python -c 'import re,sys;print re.sub("\s*([{};,:])\s*", "\\1", re.sub("/\*.*?\*/", "", re.sub("\s+", " ", sys.stdin.read())))'



Decode string written in Hex




python -c "print ''.join(chr(int(''.join(i), 16)) for i in zip(*[iter('474e552773204e6f7420556e6978')]*2))"



Retrieve content text from HTTP data




python -c "import sys; print sys.stdin.read().replace('\r','').split('\n\n',2)[1]";



Prints file extension




print '~/python/one-liners.py'.split('.')[-1]



Escapes content from stdin


This can be used to convert a string into a "url safe" string



python -c "import urllib, sys ; print urllib.quote_plus(sys.stdin.read())";



Reverse lines in stdin




python -c "import sys; print '\n'.join(reversed(sys.stdin.read().split('\n')))"



Print top 10 lines of stdin




python -c "import sys; sys.stdout.write(''.join(sys.stdin.readlines()[:10]))" < /path/to/your/file




Sony's Open Source command line tool for performing python one liners using unix-like pipes



They call it "The Pyed Piper" or pyp. It's pretty similar to the -c way of executing python, but it imports common modules and has it's own preset variable that help with splitting/joining, line counter, etc. You use pipes to pass information
forward instead of nested parentheses, and then use your normal python string and list methods. Here is an example from the homepage:

Here, we take a linux long listing, capture every other of the 5th through the 10th lines, keep username and file name fields, replace "hello" with "goodbye", capitalize the first letter of every word, and then add the text "is splendid"
to the end:

ls -l | pyp "pp[5:11:2] | whitespace[2], w[-1] | p.replace('hello','goodbye') | p.title(),'is splendid'"

and the explanation:
This uses pyp's built-in string and list variables (p and pp), as well as the variable whitespace and it's shortcut w, which both represent a list based on splitting each line on whitespace (whitespace = w = p.split()).
The other functions and selection techniques are all standard python. Notice the pipes ("|") are inside the pyp command.


http://code.google.com/p/pyp/
http://opensource.imageworks.com/?p=pyp

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-368583-1-1.html 上篇帖子: Python模拟MapReduce的流程 下篇帖子: eclipse+python搭建方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表