In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about what Python built-in workers have about improving work efficiency, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Introduces the gadgets provided by the four Python interpreters themselves.
These gadgets are often used in the author's daily work.
Reduce the waste of all kinds of time
However, it is easy to be ignored.
Whenever a new colleague sees me using it like this,
Can't help sighing.
It turns out that Python hides such an easy-to-use function.
Let's take a look at some of the gadgets that come with Python.
Start a download server in one or one second
In our work, there is a need from time to time to pass on documents to other colleagues.
Sending files to colleagues is not a tedious task in itself, and today's chat tools support file transfer.
However, there is another situation where it will be more troublesome to operate if we need to transfer more files.
In addition, if the file is uploaded to your colleague on a remote server
You need to download the files from the remote server locally, and then send them to your colleagues through chat.
Or, you don't know exactly which documents to send to your colleagues.
So, you need to communicate back and forth.
The time cost of communication is relatively high, which will reduce the efficiency of work.
At this point, you need a more efficient approach.
This time,
If you know that Python has a built-in download server, it can significantly improve efficiency.
For example:
Your colleague wants the files you send to be located in a directory.
So, you can enter this directory.
Then execute the following command to start a download server:
In Python 3
Due to the rearrangement of the system library
As a result, it will be used differently:
Executing the above command will start a file download server in the current directory
Port 8000 is open by default.
This time,
All you have to do is tell your colleagues the IP and port.
Just let colleagues do it on their own.
Very convenient and efficient.
For example:
The author is in the `~ / temp` directory on his own server.
Start a download server:
Use a browser to access the download server started by Python
You can see an interface similar to FTP download.
This time,
Click on the file to download.
Reduce the cost of communication.
From the point of view of working principle, execute the above command
Just started a web server built into Python.
If there is a file named index.html in the current directory
The contents of the file are displayed by default
If such a file does not exist in the current directory
The list of files in the current directory is displayed by default
That's what you see as the download server.
Second, convert string to JSON
JSON is a lightweight data exchange format
It is easy for humans to read and write, and it is also easy for machines to parse and generate.
Because of its many advantages, JSON has been widely used in various systems.
therefore,
We often have the need to convert JSON strings to JSON objects.
For example:
During the author's work, our system will call the API of the underlying service.
The API of the underlying service is generally returned in the format of JSON
In order to facilitate problem tracking
We will convert the JSON returned by API into a string and record it in the log file.
When you need to analyze a problem
It may be necessary to deduct the JSON string from the log file for analysis.
This time,
You need to convert a JSON string to a JSON object for easy reading.
This requirement is so common.
So much so that you use a search engine to search for "JSON".
The * * item in the search results is the "online JSON formatting tool".
In addition to opening the browser and using the online JSON formatting tool
We can also use the Python interpreter of the command line terminal to parse the JSON string
As follows:
It is very convenient to parse the JSON string using the command line interpreter
And, for ease of reading,
The tool also automatically aligns and formats the results of the conversion.
As follows:
Third, check whether the third-party library is installed correctly
After installing the third-party libraries of Python
How do I make sure that the library is installed correctly?
The answer to this question is simple.
Just try to import import
If there are no errors in the import
The installation is considered to be successful.
Verify that the third-party libraries of Python have been installed successfully
It's a very simple thing in itself.
However, if we use scripts to automate the deployment of large numbers of servers
How do you verify that third-party libraries are installed successfully?
It is absolutely impossible to log in to every server for authentication.
This time,
We can use the `- c` parameter of the Python interpreter to execute import statements quickly.
As follows:
Use this authentication method
Not only is it more efficient than interactive verification
More importantly,
You can validate the remote server in a script.
Use Python to extract the zip package.
If you are an engineer with work experience
You must have encountered the need to extract the zip format package under Linux.
Under Linux, you usually use tar command to create and read compressed packages.
However, the tar command does not support compressed packages in zip format.
To read compressed packages in zip format, you need to use unzip
Most operating systems do not have unzip installed.
Therefore, it will be troublesome every time you need to extract a compressed package in zip format under Linux.
In addition to installing unzip tools and writing Python scripts to extract compressed packages in zip format
You can also use the zipfile module to provide a command line interface.
You can go under Linux in the future.
Use the Python command to create and extract compressed packages in zip format.
The command line interface provided by the zipfile module includes the following options:
-l displays a list of files in the zip format package
-c create a zip format package
-e extract zip format compression package
-t verify that the file is a valid zip format package
The following command uses the command line interface provided by the zipfile module of Python
Create, view, and extract zip format packages:
With these Python built-in gadgets, you can double your productivity
After reading the above, do you have any further understanding of the productivity-enhancing Python built-in workers? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.