current position:Home>Eight practical "no code" features of Python

Eight practical "no code" features of Python

2022-05-15 04:48:45Game programming

developer (KaiFaX)

Developers for full stack Engineers Focus on the front end 、Java/Python/Go/PHP Our technology community

In recent years Python Language is popular , Because we can use it to write less code to implement complex functions .Python Developers welcome toolkits that encapsulate complex implementations but are user-friendly .
However ,Python The simplicity of is more than that . You can believe that we can use... Without writing any code Python Do you ? In the following article , We will introduce 8 One can be used without writing any code Python Examples of built-in functions .

0. Python CLI “-m” Parameters

We start with Python CLI( Command line interface ) Start talking about . Although we don't have to write code to use the functions described later , But in order to make Python Know what we're going to do , We need to use Python From the command line .
As long as we have installed Python Environmental Science , We can do that Python Command line interface input python --help Display all supported parameters .

Python Eight practical “ There is no code ” characteristic - The first 2 Zhang

Because the output of the command is too long , The figure above shows only part of the content . What I want to emphasize most here is -m mod Parameters , It will be Python The module runs as a script . therefore , If the implementation of this module supports command line operation , We can use it directly on the command line . Next, let's experience :)

1. Service port test

occasionally , We want to test ip Outbound network traffic of port , Usually telnet Command is a good choice . stay Windows The platform is not installed by default telnet Software , Manual installation is required before use . If it's just a simple test , There are not many usage scenarios in the future , Installing it can be a waste of resources .
however , If installed Python, Then you don't have to download and install telnet, because Python Built in telnet Corresponding module . We can Google Search site 443 Port to test .

python -m telnetlib -d 142.250.70.174 443
Python Eight practical “ There is no code ” characteristic - The first 3 Zhang

As shown in the figure above , The network traffic display is normal , We even received from Google Response to null characters . If we try to visit ip A random port , An error will be thrown , As shown in the figure below .

python -m telnetlib -d 142.250.70.174 999
Python Eight practical “ There is no code ” characteristic - The first 4 Zhang

2. Local boot web service

quite a lot Python Users don't know this , You'll be surprised when you hear it for the first time . Yes , We can use Python start-up web service , Without writing any code , Just execute the following command on the command line as follows .

python -m http.server
Python Eight practical “ There is no code ” characteristic - The first 5 Zhang

After operation , It shows that the service listens to local 8000 port , then , We can try to access from the browser http://localhost:8000/.

Python Eight practical “ There is no code ” characteristic - The first 6 Zhang

The web The service will show the local file system under the command startup path in the form of root directory , let me put it another way , We can't access its parent directory .
You may ask , What are the usage scenarios of this function . For example , If you want to share many texts in a directory on your computer with your good friends /PDF/ image file / Subdirectory files, etc , Then it's very easy to share using this method .

Python Eight practical “ There is no code ” characteristic - The first 7 Zhang

If you want to know more about this topic , You can refer to 3 Lines of Python Code to Write A Web Server This article . If you implement one according to the above article “ Low code ” Solutions for , Then you can add more customization to it .

3. Validation and formatting JSON character string

If you have a very long and unformatted JSON character string , Then it will be very difficult to read . Usually , I will use some with JSON Plug in text editor , such as Sublime perhaps VS Code, Formatting JSON character string . however , If you don't have these tools at hand ,Python It can be used temporarily . For example, the following will be a short JSON String display .

echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}'
Python Eight practical “ There is no code ” characteristic - The first 8 Zhang

You can see , The command line tools of the current operating system can only be displayed in the original format of the original string . however , If with the help of Python Of json.tool Tools ,JSON The string will be well formatted .

echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}' | python -m json.tool
Python Eight practical “ There is no code ” characteristic - The first 9 Zhang

Oops!JSON Invalid string , also json.tool Helped us locate the problem . We missed a comma after the name object . So add a comma to make this JSON Legal and effective .

echo '{"name": {"first_name":"Chris", "last_name":"Tao"}, "age":33}' | python -m json.tool
Python Eight practical “ There is no code ” characteristic - The first 10 Zhang

Now? ,JSON The string has perfectly indented formatted output ! It's easier to read .

4. Create a text editor

You read that right , We can use Python Come on ” establish ” A text editor . Of course , Its functions are very limited , But if there is no better choice at present , It will be much more convenient to use . in addition , It's definitely not functionally compatible with Vim and Nanos comparison , But it's based entirely on UI Editor instead of command line text . This editor is based on Tkinter Realized idlelib Module creation , So it can run across platforms .
Suppose we want to write a simple Python Program to display the current time , I want to write code quickly instead of downloading and installing huge code editing tools . Now let's run the following command .

mkdir get_time_apppython -m idlelib get_time_app/print_time.py

If the file directory does not exist , idlelib Will not be able to create , So if necessary , We need to create one . After we run this command ,print_time.py Only when saving is executed will it be created locally . The editor should now pop up , We can write some code in it , You can see that the code supports syntax highlighting .

Python Eight practical “ There is no code ” characteristic - The first 11 Zhang

Now we use ctrl+s Shortcut keys to save the edited code , And close the edit window . Next, use the command line to view the edited code file for verification , No problem .

cat get_time_app/print_time.py
Python Eight practical “ There is no code ” characteristic - The first 12 Zhang

5. Create an executable application

If we want to create a simple application , For example, the previously written application to obtain the current time , We don't have to be like PyInstaller Such a third-party toolkit ,Python Built in Zipapp You can do that . Suppose we want to package it into a "Get Time" Application , We can run the following commands on the command line .

python -m zipapp get_time_app -m "print_time:main"

In this order , We just need to give zipapp Set up get_time_app name , Appoint Python The entry file of the program and its entry function . With .pyz The file with the extension is the application we created , At this point, we can distribute the project as a single file instead of a folder .

Python Eight practical “ There is no code ” characteristic - The first 13 Zhang

The startup mode of the program is also very simple , Use it directly Python Call .

python get_time_app.pyz
Python Eight practical “ There is no code ” characteristic - The first 14 Zhang

6. Encode and decode strings or files

adopt Python CLI, We can encrypt strings or files . We have fun ROT13 The encryption algorithm is shown as an example .ROT13 Is an offset 13 Bit Caesar code , Its encryption principle is shown in the figure below .

Python Eight practical “ There is no code ” characteristic - The first 15 Zhang

We can use encodings.rot_13 To encrypt a string , The order is as follows .

echo "I am Chris" | python -m encodings.rot_13
Python Eight practical “ There is no code ” characteristic - The first 16 Zhang

Bear in mind , Don't use it for any real encrypted content . Because English has 26 Letters , So running this algorithm again, we can easily decipher the encrypted string :)

echo 'V nz Puevf' | python -m encodings.rot_13
Python Eight practical “ There is no code ” characteristic - The first 17 Zhang

Now let's try a more common scenario ——base64 code . We can change the string base64 code , As shown below .

echo "I am Chris" | python -m base64
Python Eight practical “ There is no code ” characteristic - The first 18 Zhang

Next , We can also use -d Parameter to decode the encrypted string .

echo "SSBhbSBDaHJpcwo=" | python -m base64 -d
Python Eight practical “ There is no code ” characteristic - The first 19 Zhang

base64 It is also often used to encode and decode image files . We can also encode the file as follows .

python -m base64 get_time_app/print_time.py
Python Eight practical “ There is no code ” characteristic - The first 20 Zhang

It's very interesting , After decoding Python Scripts can be executed immediately , No mistake. ..

echo "ZnJvbSBkYXRldGltZSBpbXBvcnQgZGF0ZXRpbWUKCgpkZWYgbWFpbigpOgogICAgY3VycmVudF90aW1lID0gZGF0ZXRpbWUubm93KCkKICAgIHByaW50KGYnQ3VycmVudCB0aW1lIGlzIHtjdXJyZW50X3RpbWV9LicpCgoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIG1haW4oKQo=" | python -m base64 -d | python
Python Eight practical “ There is no code ” characteristic - The first 21 Zhang

7. Get system metadata

If we want to get the current system information ,Python Provides a very simple method . We just need to run the following command .

python -m sysconfig
Python Eight practical “ There is no code ” characteristic - The first 22 Zhang

You can see , After this command is executed, all system configuration information will be displayed , such as Python Environment path and environment variables, etc . The screenshot above shows only part of the content , The content actually displayed will be very rich . If we just want to show Python Environment path and current working path , We can execute the following command .

python -m site
Python Eight practical “ There is no code ” characteristic - The first 23 Zhang

8. File compression

We can use Python To compress the file , Without having to download tar/zip/gzip Tools such as . for instance , If we want to compress, we were just in the first 4 The application written in Section , We can run the following command to compress the folder to zip In file . In the order , Options -c It stands for “create” That is, the meaning of creation .

python -m zipfile -c get_time_app.zip get_time_app
Python Eight practical “ There is no code ” characteristic - The first 24 Zhang

Of course , We can also decompress the compressed file . Next to the above operation , We unzip the folder and put it in a new directory , This will not conflict with the original directory . In the following order , Options -e representative “extract” That is, the meaning of decompression .

python -m zipfile -e get_time_app.zip get_time_app_extracted

If you are not at ease , We can test .

ls get_time_app_extractedcat get_time_app_extracted/get_time_app/print_time.py
Python Eight practical “ There is no code ” characteristic - The first 25 Zhang

We just started with zip The file is shown as an example ,Python In addition to supporting zip In addition to the decompression of the format , And support tar and gzip Decompression of .

summary

This article introduces a method that can be used without writing any code Python Method of built-in Library . If you can think of using these methods in some scenarios , There is no doubt that it can provide us with a lot of convenience . I hope this article can give you inspiration and help .

author :Christopher Tao
translator : Wang Kunxiang @InfoQ
original text :Eight “No-Code” Features In Python (https://towardsdatascience.com/eight-no-code-features-in-python-15744e8c01f4)


  1. reply “m” You can view history ;
  2. reply “h” perhaps “ help ”, view help ;
    Developers have opened multiple technologies * Stream learning , Please add Ruofei wechat :1321113940 ( Code k) Enter the development group to learn and exchange
    explain : We are all developers . Videos or articles come from the Internet , If copyright or error is involved , Please fly with you (1321113940) contact , Will be deleted or modified at the first time , thank you !

    Python Eight practical “ There is no code ” characteristic - The first 1 Zhang

    open Hair person : KaiFaX Developers for full stack Engineers Focus on the front end 、Java/Python/Go/PHP Our technology community

author : Ordinary netizens

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .

copyright notice
author[Game programming],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/135/202205142211295415.html

Random recommended