Skip to content

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
# 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 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
# 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 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
# 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 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
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 non-standard time string and treat it 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 arithmetic: 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 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
import requests

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

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

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

Incorrect example:

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