Eight “No-Code” Features In Python
You don’t have to write code to use Python, sometimes.
One of the reasons why Python become popular is that we can write relatively less code to achieve complex features. The Python developers’ community welcomes libraries that encapsulate complicated implementations with simple interfaces exposed for use.
However, that’s even not the simplest. Can you believe that we can even use Python without any code?
In this article, I’ll introduce 8 Python built-in features that we can leverage without writing any code.
0. Python CLI “-m”
Let’s start with Python CLI (Command-Line Interface) before everything. Although we don’t have to write code to use the features that will be introduced later on, we still need to let Python know what we want to execute. To do this, we can use Python CLI.
As long as we have Python installed on our machine, we can display all the supported arguments in Python CLI.
$ python --help
The screenshot shows only part of the help output because it is too long. The one I want to emphasise is the -m
option. It will run a library module as a script. Therefore, if the module is implemented to support CLI, we will be able to use it directly.
Now we should begin :)
1. Testing Port Number without Telnet
Sometimes, we would like to test the outbound network traffic to an IP:Port. Usually, telnet is a not bad choice, especially on the Windows platform. However, it is often not installed by default. We have to download and install it before use, which could be a waste if we just want a simple test and then dispose of it.
However, if you have Python installed, you don’t have to download telnet because Python already has it. Let’s try an IP of Google Search for its 443 port.
python -m telnetlib -d 142.250.70.174 443
As the screenshot is shown, the traffic is OK and we even received an empty string from Google. If we try to telnet a random port that we are not supposed to access, an error will be thrown.
python -m telnetlib -d 142.250.70.174 999
2. Start a Web Server
If you don’t know this before, it could be surprised I guess. Yes, we can spin up a web server using Python without writing any code. Just run the command as follows.
python -m http.server
After we run it, it shows that the server is listening to the localhost on port 8000. Then, we can try to access http://localhost:8000/
from our browser.
The web server will render our local file system using whichever path we started the server as root. In other words, we won’t be able to access any directories above the root.
Are you asking what is this used for? For example, if we have many text/PDF/image files in the directory or any sub-directories, we can very easily and quickly access them.
If you want to know more interesting usage, please check out this article.
If you follow the article above and turn this into a “low-code” solution, you may be able to add more customised features to it.
3. Validating and Beautifying JSON
If you have a long JSON string without formatting, it could be difficult to read. Usually, I would prefer to use text editors such as Sublime or VS code with JSON plugins to make the string pretty. However, if we don’t have these tools handy, Python will be able to help temporarily.
Suppose we have such a JSON string. I’ll use a short one for demonstrating purposes.
echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}'
Our operating systems do not recognise it, so the String will be displayed as-is. However, if we add the Python json.tool
as the magic, it will be well formatted.
echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}' | python -m json.tool
Oops! The JSON string is not valid, and the json.tool
helped us to identify the issue. We missed a comma after the name object. Let me add the comma to make it valid.
echo '{"name": {"first_name":"Chris", "last_name":"Tao"}, "age":33}' | python -m json.tool
Now, it is output with perfect indentation! Easy to read now.
4. Creating a Text Editor
Yes, we can use Python to “create” a text editor. Of course, it is not very powerful, but it will be convenient if you don’t have a better one installed. Also, it won’t be more powerful than Vim and Nanos, but it is totally UI-based rather than a command-line text editor. This editor is created by the idlelib
with Tkinter, so it is even cross-platform.
Suppose we are going to write a simple Python app to display the current time. We want to quickly write the code but don’t want to download and install a code editor. Now, let’s run this command.
mkdir get_time_app
python -m idlelib get_time_app/print_time.py
idlelib
cannot create the directory if it doesn’t exist, so we need to create one if we need it. After we run this command, the print_time.py
will NOT be created until we save it.
The editor should pop up now. We can write some code in it. The code can even be syntax-coloured.
Now, we can press ctrl+s to save it and then close it. Let’s go back to the command line and display the content of the file, the code is definitely there.
cat get_time_app/print_time.py
5. Creating an Executable Application
If we just want to create a simple application like the one we’ve just written above, we don’t need any 3rd party libraries such as PyInstaller. Python built-in Zipapp can help.
Suppose we want to package the “Get Time” App, we can run the command below.
python -m zipapp get_time_app -m "print_time:main"
In the command, we just need to let zipapp
knows the name of the app get_time_app
, the name of the Python file that will be used as the entry point print_time
and the main method main
.
The one with .pyz
extension is the app that we just created. We can distribute it as a single file rather than a folder.
To use the packaged app, simply use python to call it.
python get_time_app.pyz
6. Encoding and Decoding a String or File
With Python CLI, we can encrypt a string or a file. Let’s start with an interesting one. Rot 13 is an encryption method that simply moves any English letters by 13 positions to the right. For example, an “a” (position: 1) will become an “n” (position: 14).
We can use encodings.rot_13
to encrypt a string as follows.
echo "I am Chris" | python -m encodings.rot_13
Of course, don’t use this for any real confidential stuff. Because there are 26 letters in English, so we can decipher the string very easily by running this algorithm again :)
echo 'V nz Puevf' | python -m encodings.rot_13
Now, let’s have a look at a more useful one, base64. We can encode a string with base64 format as follows.
echo "I am Chris" | python -m base64
Then, we can add a flag -d
to decode it.
echo "SSBhbSBDaHJpcwo=" | python -m base64 -d
This will be pretty useful if we have an image file to be encoded on the fly. We can encode a file as follows.
python -m base64 get_time_app/print_time.py
It’s pretty interesting, the decoded Python script file can be executed on the fly.
echo "ZnJvbSBkYXRldGltZSBpbXBvcnQgZGF0ZXRpbWUKCgpkZWYgbWFpbigpOgogICAgY3VycmVudF90aW1lID0gZGF0ZXRpbWUubm93KCkKICAgIHByaW50KGYnQ3VycmVudCB0aW1lIGlzIHtjdXJyZW50X3RpbWV9LicpCgoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIG1haW4oKQo=" | python -m base64 -d | python
7. Get System Metadata
If we want to get the current system information, Python provides an easy way to do so. We can run the command below.
python -m sysconfig
All the system configuration is displayed such as the paths and environment variables. There is much more stuff displayed, the screenshot only shows a portion of it.
If we just want to display the paths and the current working directory, we can also run this command.
python -m site
8. Compressing files
We can also use Python to compress files without the need to download tar/zip/gzip. For example, if we want to compress the app we’ve just written in section 4, we can run the following command to add the folder to a zip file. In the command, the option -c
stands for “create”.
python -m zipfile -c get_time_app.zip get_time_app
Of course, we can extract it as well. Let’s extract the folder and put it into a new directory so that it can be separated from the original one. In the command below, the option -e
stands for “extract”.
python -m zipfile -e get_time_app.zip get_time_app_extracted
Then, we can verify it.
ls get_time_app_extracted
cat get_time_app_extracted/get_time_app/print_time.py
I use zip for demonstration, while Python also supports tar and gzip.
Summary
In this article, I have introduced a way to use Python built-in libraries without writing any code. It really provides lots of conveniences if we do remember to use them. Hope you enjoyed reading this article and the content would help!
If you feel my articles are helpful, please consider joining Medium Membership to support me and thousands of other writers! (Click the link above)