Code Snippet Reference
Python is a rather convenient language, and specific problems often have fairly routine handling methods.
The following are some common code handling methods:
1. Using List Comprehension Syntax
Python's list comprehension syntax can quickly generate lists, which is very suitable when the logic is relatively simple.
However, overly complex processing logic can make the code difficult to read.
Therefore, for complex logic, it is recommended to directly use for
loops.
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
2. Using Built-in map
Function to Process Lists
Python's built-in map
function is another convenient way to handle lists.
For simple logic, you can use lambda
expressions;
For complex logic, you can first define a function and then pass it as an argument.
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
3. Getting Values from Multiple Nested Layers in JSON Data
Sometimes functions receive a JSON with many nested layers, and need to retrieve a value at a certain level.
You can use try
syntax to quickly retrieve it.
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
When using this method, do not add any unrelated code inside the try
block, otherwise exceptions that should be thrown may be skipped
4. Properly Handling Time with arrow
Library
The built-in date handling module in Python has a certain complexity in usage and does not support time zones well.
When handling time, it is recommended to use the third-party arrow
module.
arrow
module is built-in and can be used directly after import
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
For more detailed content, please refer to the official arrow
documentation: https://arrow.readthedocs.io/
5. Sending External HTTP Requests
The built-in http
handling module in Python has a certain complexity in usage.
When sending http requests, it is recommended to use the third-party requests
module.
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
For more detailed content, please refer to the official requests
documentation:
https://requests.readthedocs.io/
6. Sending DingTalk Bot Messages
DingTalk custom bots can simply push messages to groups through POST requests, making them an excellent choice for message notifications.
Since only HTTP requests need to be sent, the requests
module can be used directly.
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The new version of DingTalk group bots adds relevant security verification handling, please refer to the official documentation for details
For more detailed content, please refer to the official DingTalk bot documentation: https://developers.dingtalk.com/document/app/custom-robot-access
7. Avoiding SQL Injection
When the parameters passed to a function need to be used as SQL statement parameters, care must be taken to avoid SQL injection issues.
You should always use the sql_params
parameter provided by the built-in connector operation object instead of directly concatenating SQL statement strings.
Correct example:
Python | |
---|---|
1 |
|
Incorrect example:
Python | |
---|---|
1 2 |
|
The actual usage of various types of connector operation objects may differ slightly; please refer to the chapter on "Operating Connector DFF.CONN(...)
".
Wikipedia entry on "SQL Injection": https://en.wikipedia.org/wiki/SQL_injection
W3Cschool "MySQL and SQL Injection" section: https://www.w3cschool.cn/mysql/mysql-sql-injection.html