Skip to content

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
# Generate dps data by time
import time
dps = [ [(int(time.time()) + i) * 1000, i ** 2] for i in range(5) ]
dps = list(dps)
print(dps)
# [[1583172382000, 0], [1583172383000, 1], [1583172384000, 4], [1583172385000, 9], [1583172386000, 16]]

# Square the input dps
dps = [
    [1583172563000, 0],
    [1583172564000, 1],
    [1583172565000, 2],
    [1583172566000, 3],
    [1583172567000, 4]
]
dps = [ [d[0], d[1] ** 2] for d in dps ]
dps = list(dps)
print(dps)
# [[1583172563000, 0], [1583172564000, 1], [1583172565000, 4], [1583172566000, 9], [1583172567000, 16]]

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
# Use lambda expression to square the input dps
dps = [
    [1583172563000, 0],
    [1583172564000, 1],
    [1583172565000, 2],
    [1583172566000, 3],
    [1583172567000, 4]
]
dps = map(lambda x: [x[0], x[1] ** 2], dps)
dps = list(dps)
print(dps)
# [[1583172563000, 0], [1583172564000, 1], [1583172565000, 4], [1583172566000, 9], [1583172567000, 16]]

# Use passed-in function to square the input dps
dps = [
    [1583172563000, 0],
    [1583172564000, 1],
    [1583172565000, 2],
    [1583172566000, 3],
    [1583172567000, 4]
]
def get_pow(x):
    timestamp = x[0]
    value     = x[1] ** 2
    return [timestamp, value]

dps = map(get_pow, dps)
dps = list(dps)
print(dps)
# [[1583172563000, 0], [1583172564000, 1], [1583172565000, 4], [1583172566000, 9], [1583172567000, 16]]

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
# Get the value of level3
input_data = {
    'level1': {
        'level2': {
            'level3': 'value'
        }
    }
}
value = None
try:
    value = input_data['level1']['level2']['level3']
except Exception as e:
    pass

print(value)
# value

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
import arrow

# Get current Unix timestamp
print(arrow.utcnow().timestamp)
# 1583174345

# Get current Beijing time string
print(arrow.now('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss'))
# 2020-03-03 02:39:05

# Get current Beijing time ISO8601 format string
print(arrow.now('Asia/Shanghai').isoformat())
# 2020-03-03T02:39:05.013290+08:00

# Parse from Unix timestamp and output Beijing time string
print(arrow.get(1577808000).to('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss'))
# 2020-01-01 00:00:00

# Parse from ISO8601 time string and output Beijing time string
print(arrow.get('2019-12-31T16:00:00Z').to('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss'))
# 2020-01-01 00:00:00

# Parse from non-standard time string and as Beijing time string
print(arrow.get('2020-01-01 00:00:00', 'YYYY-MM-DD HH:mm:ss').replace(tzinfo='Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss'))
# 2020-01-01 00:00:00

# Time calculation: Get the previous day and output Beijing time string
print(arrow.get('2019-12-31T16:00:00Z').shift(days=-1).to('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss'))
# 2019-12-31 00:00:00

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
import requests

# Send GET request
r = requests.get('https://api.github.com/')
print(r.status_code)
print(r.json())

# Send POST request in form format
r = requests.post('https://httpbin.org/post', data={'key':'value'})
print(r.status_code)
print(r.json())

# Send POST request in json format
r = requests.post('https://httpbin.org/post', json={'key':'value'})
print(r.status_code)
print(r.json())

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
import requests

url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx'
body = {
    'msgtype': 'text',
    'text': {
        'content': 'DingTalk bot notification @180xxxx0000'
    },
    'at': {
        'atMobiles': [
            '180xxxx0000'
        ]
    }
}
requests.post(url, json=body)

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
helper.query('SELECT * FROM table WHERE id = ?', sql_params=[target_id])

Incorrect examples:

Python
1
2
helper.query("SELECT * FROM table WHERE id = '{}'".format(target_id))
helper.query("SELECT * FROM table WHERE id = '%s'" % target_id)

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