Code Snippet Reference
Python is a fairly convenient language, and specific problems often have relatively routine handling methods.
Here are some common code processing 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, it should be noted that overly complex processing logic can make the code difficult to read.
Therefore, for complex logic, it is recommended to directly use a for
loop for processing.
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 function for processing lists.
For simple logic, you can use lambda
expressions;
For complex logic, you can first define a function and then pass it as a parameter.
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 Levels in JSON Data
Sometimes functions will receive JSON with multiple nested levels, and need to retrieve values from a certain level.
You can use the try
statement to quickly retrieve them.
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 other unrelated code inside the try
block, otherwise exceptions that should be thrown may be skipped.
4. Using the arrow
Library to Properly Handle Time
The built-in date processing module in Python has a certain level of complexity, and its support for time zones is not good.
When dealing with time, it is recommended to use the third-party arrow
module.
The arrow
module is already 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
processing module in Python has a certain level of complexity.
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 via 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 includes relevant security verification processing; for more details, please refer to the official documentation.
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 into the function need to be used as parameters in SQL statements, it is important 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 examples:
Python | |
---|---|
1 2 |
|
The actual usage methods of different types of connector operation objects may vary slightly; please refer to the "Operating Connectors DFF.CONN(...)
" chapter.
Wikipedia entry on "SQL Injection": https://baike.baidu.com/item/sql%E6%B3%A8%E5%85%A5
W3Cschool "MySQL and SQL Injection" section: https://www.w3cschool.cn/mysql/mysql-sql-injection.html