Pythongasm - Python One-Liners You Should Know

Copied to clipboard

Python One-Liners You Should Know

Jan 08, 2020 • 5 minutes • 285 views

Introduction

Python is known for its simplicity. At the same time, it’s fascinating to see how powerful one line of Python code can be sometimes. This article features 5 such one-liners that can do awesome things, or at least save you a few minutes.

JSON Formatter

Python has a built-in module for formatting JSON files.

Suppose you want to format the JSON file at this link.


Open Terminal, and run this command:


curl "https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02" | python -m json.tool

You can combine python -m json.tool with any UNIX command using the pipeline |. For example, to read and format a locally saved JSON file, you can run use it with the cat command: 



cat file.json | python -m json.tool

The output of command before the pipeline | is sent to the command following it.

Print

We know that Python 3 has the print() function. The complete function looks like this:

print(*objects
    , sep=' '
    , end='\n'
    , file=sys.stdout
    , flush=False)

You can change the default value of the file parameter to save the output as a file on the disk.

print("Hello, World!", file=open('file.txt', 'w'))

This is a quite handy way of writing (or appending) output to a file.

Note that it's quick but should not be seen as a replacement to Context Managers. It does the same thing but leaves the file open, and you won’t want too many files (especially large ones) open, and eating up RAM.

Hide User Input

A lot of the times you need to prompt the user for a confidential input (like keys or passwords). The input() would expose as the user types in a key.

Fortunately, you can use the getpass module (preinstalled) to hide user input.



import getpass

pwd = getpass.getpass("ENTER PASSWORD:" )

Use the Dictionary

No, I don’t mean a Python dictionary object. Literally the dictionary, the one that is used by your operating system as an English dictionary. Actually your system has this file called words.txt which contains around 235,000+ English words.

You can have a look at this file at usr/share/dict/words.txt in most of the UNIX based systems, or simply click here to open it in your browser.

This file can be used in a lot of ways — spotting misspellings in a large word document or simply verifying if an input entered by the user is a valid English word.

Let us check if “python” is a valid English word:

if "python".casefold() in open('/usr/share/dict/words/').read(): print("EXISTS")

macOS and many other UNIX based operating systems ship with this words.txt file. But if you don’t find this file in your system, you can install it with this command:

apt-get install --reinstall wamerican

Start a Local Server

This is perhaps the quickest way to start a local webserver. Just go the directory where you have saved your website and run this command:

python -m http.server 8000

Of course, you can run multiple local servers with this command as long they have different port numbers.

Conclusion

Python is great when it comes to doing more and writing less. If you are interested in more Python one-liners, do check out: Powerful Python One-Liners

Hope you enjoyed reading this article.


Permanent link to this article Read Next
pygs.me/003 Web Scraping Without Getting Blocked

Comments

styling code quotes links