circuit

10 Powerful Python One-Liners

Python one-liners can be just as powerful as a long and tedious program written in another language designed to do the same thing."




image

Here are 10 powerful Python one-liners that I think you will find useful.

1. Share your Wi-Fi password By Printing it as QR Code

import wifi_qrcode_generator as qr
qr.wifi_qrcode('wifi name ', False, 'WPA', 'password')

2. CSV to JSON

import csv,json
print (json.dumps(list(csv.reader(open('csv_file.csv')))))

3. Apply regular expression to lines from stdin

import sys,re
[sys.stdout.write(re.sub('PATTERN', 'SUBSTITUTION', line)) for line in sys.stdin]

4. Profile a Python script

python3 -m cProfile foo.py

5. Start a Webserver on your current directory

python -m SimpleHTTPServer 8000

6. Finding all subsets of a set in one line

**from** itertools **import** combinations
print(list(combinations([1, 2, 3, 4], 2)))

7. Decode a base64 encoded file

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

8. Display List of all users on Unix-like systems

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

9. Largest 8-Bytes Number

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

10. Retrieve content text from HTTP data

import sys
print sys.stdin.read().replace('\r','').split('\n\n',2)[1]

Final Thoughts

Well, here are my 10 Powerful Python One-Liners that every developer must-have.

Python one-liners can be just as powerful as a long and tedious program written in another language designed to do the same thing. Python one-liners are also a great way to learn Python. They are short and concise, and you can learn a lot from them.

I hope you find this article helpful and learned some new things. Share this article with your developer friends or who are just starting development.

Have a good day!




Continue Learning