Python



Python common links

  • Python official

    Advantages of Python

    Concise and easy-to-learn syntax

    Python's syntax is simple and close to natural language. Even novice programmers can get started quickly, lowering the learning threshold.

    Rich standard library and third-party resources

    Python provides a wide range of standard function libraries, covering many aspects such as networking, data processing, and graphical interfaces. In addition, the powerful third-party ecosystem such as NumPy, Pandas, and TensorFlow makes Python a versatile development tool.

    Cross-platform features

    Python is a cross-platform language. Whether it is Windows, macOS or Linux, the same Python program can be executed, which greatly improves the flexibility of development.

    Widely used in many fields

    Python plays an important role in multiple fields such as data science, artificial intelligence, web development, automation scripting, game development, etc., allowing developers to handle multiple needs with one language.

    Active community support

    Python has a large global community, and both beginners and experienced developers can easily find educational resources, discussion groups, and technical support.

    Efficient development speed

    Python provides intuitive syntax and powerful tools, allowing developers to implement programming faster and shorten the product development cycle.



    Python development environment

    Anaconda

    What is Anaconda?

    Anaconda is an open source Python and R programming platform designed for scientific computing, including applications such as data science, machine learning, artificial intelligence, and big data analytics.

    Main functions

    Suitable for objects

    Anaconda is suitable for users in the following fields:

    How to install Anaconda?

    1. accessAnaconda official website
    2. Select the appropriate operating system version and download the corresponding installation file.
    3. Follow the installation wizard to complete the installation and configure environment variables (optional).

    FAQ

    The following are problems commonly encountered by users:



    Anaconda environment

    What is the Anaconda environment

    Anaconda provides a virtual environment (Environment) function that allows users to create multiple independent Python execution environments on the same computer. Each environment can have different Python versions and packages to avoid dependency conflicts between different projects.

    Establish environment

    # Create an environment named myenv and specify the Python version
    conda create -n myenv python=3.10

    Starting and switching environments

    # Start environment
    conda activate myenv
    
    #Exit the environment
    conda deactivate

    View environment

    # List all environments
    conda env list
    # or
    conda info --envs

    Export and restore environments

    # Export environment configuration to YAML file
    conda env export > environment.yml
    
    # Create environment from YAML file
    conda env create -f environment.yml

    Delete environment

    # Delete the specified environment
    conda remove -n myenv --all


    Jupyter

    What is Jupyter?

    Jupyter is an open source interactive computing environment that supports multiple programming languages ​​and is mainly used for data science, machine learning, and academic research.

  • Jupyter

    Core features

    Main components

    Application scope

    Jupyter is widely used in the following fields:

    How to use Jupyter?

    1. Install Anaconda or install Jupyter standalone.
    2. Enter in the terminaljupyter notebookStart Jupyter Notebook.
    3. Enter the editing interface through the browser to create and run notebooks.

    Advantages and Challenges



    VS Code Python development environment

    Install Visual Studio Code

    Go toVisual Studio Code official website, download and install the version appropriate for your operating system.

    Install Python extension

    In Visual Studio Code, install the Python extension by following these steps:

    1. Click the extension icon on the left.
    2. Search for "Python".
    3. Select the Python extension provided by Microsoft and click "Install".

    Install Python

    Make sure Python is installed on your system. Can be obtained fromPython official websiteDownload and install.

    After the installation is complete, enter the following command in the command line to confirm that the installation is successful:

    python --version
    # or
    python3 --version

    Setting up the Python interpreter

    Open your Python project or file, click the "Python" status bar in the lower right corner of Visual Studio Code, and select the appropriate Python interpreter.

    Execute Python program

    Open the Python file in the editor and execute the program using the following method:

    1. Right-click the file content and select "Run Python File in Terminal".
    2. Or use shortcut keysCtrl + Shift + P, search for "Run Python File" and execute it.

    Install necessary packages

    If you need to install a third-party package, you can use the built-in terminal to enter:

    pip install package name

    Enable autocompletion and debugging

    Enjoy auto-completion and powerful debugging tools through the functionality provided by Python extensions:

    1. Click the debug icon on the left.
    2. Select "Create a launch.json file" and select Python.
    3. After the settings are completed, press F5 to enable debug mode.

    Commonly used shortcut keys

    Here are some commonly used shortcut keys:



    VS Code

  • vscode/Vidual Studio Code

    VS Code sets Python execution parameters

    Modify launch.json

    If you need to pass parameters when executing a Python program, you can setlaunch.jsonFinish:

    1. Click the "Run and Debug" icon on the left.
    2. Click "create a launch.json file" or "Add Configuration".
    3. Select "Python" as the environment.
    4. in generatedlaunch.jsonModify relevant settings in the file.

    Set program and args parameters

    The following is an example configuration, including the program path and execution parameters:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Run with Arguments",
                "type": "python",
                "request": "launch",
                "program": "${workspaceFolder}/main.py", // program path
                "console": "integratedTerminal", // terminal type
                "args": ["arg1", "arg2", "--option", "value"] // Pass parameters
            }
        ]
    }

    The purpose of args

    existargsCommand line parameters can be passed in, for example:

    Read parameters in the program

    usesys.argvTo read the parameters passed from the command line:

    importsys
    
    print("All parameters:", sys.argv)
    if len(sys.argv) > 1:
        print("First parameter:", sys.argv[1])
        print("Second parameter:", sys.argv[2])

    Execution example

    Suppose the program is:

    python main.py arg1 arg2 --option value

    Execution result:

    All parameters: ['main.py', 'arg1', 'arg2', '--option', 'value']
    First parameter: arg1
    Second parameter: arg2


    VS Code Python Debug mode

    Enable Debug mode

    1. InstallationPython ExtensionExtension.

    2. Start your Python project in VS Code.

    3. PressF5Or click on the activity bar on the leftDebugicon.

    Setting launch.json

    1. Click "Add Configuration" in the Debug panel.

    2. SelectPython, the system will automatically generate alaunch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Current File",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal"
            }
        ]
    }
    

    Set breakpoint

    1. Click next to the code line number to add a break point.

    2. Conditional break points can be used: right-click the break point and select "Edit Condition".

    Debugging function

    Check variables

    1. View the current variable status in the "Variables" area of ​​the Debug panel.

    2. You can manually add specific expressions in the "Monitor" area.

    Using the Debug Console

    1. Enter Python commands in the Debug Console to check the program status in real time.

    2. Can perform operations such as variable query and function calling.



    VS Code sets Python path

    Step 1: Install Python and VS Code

    Make sure Python is installed and added to your system environment variables, then download and install Visual Studio Code.

    Step 2: Install the Python extension

    Open Visual Studio Code, click the Extensions icon on the left, and searchPython, and then install the Python extensions provided by Microsoft.

    Step 3: Check Python installation path

    Enter the following command in the terminal to confirm the installation path of Python:

    which python

    Or (Windows system):

    where python

    Step 4: Set Python path

    In Visual Studio Code, pressCtrl + Shift + P, enter and selectPython: Select Interpreter

    Select the correct Python path in the manifest. If it doesn't appear, enter the full path manually.

    Step 5: Confirm settings

    Open the terminal and executepython --versionto confirm that the correct version of the Python interpreter is selected.

    Additional information

    If you need the Python path of a specific project, you can add it in the project root directory.vscode/settings.jsonfile and add the following content:

    {
      "python.pythonPath": "Your full path to Python"
    }

    replaceYour Python full pathis the actual path.



    VS Code Python environment switching

    When developing in VS Code, I decide to use Anaconda or other environments, mainly through the underlying interpreter (Interpreter) settings. This ensures that your package dependencies are completely isolated from the program execution environment.


    1. Steps to choose a Python environment


    2. How to decide to use Anaconda or other environments

    environment type Applicable situations Main advantages
    Anaconda Data Science, Machine Learning, Deep Learning It is pre-installed with a large number of scientific computing libraries and has strong management of underlying binary files (such as DLL).
    Venv General web development, automation scripts Lightweight, fast to start, and contains only the necessary packages for execution.

    3. Manage and switch environments in Conda

    In addition to the graphical interface, you can also use commands in the built-in terminal of VS Code to manage the environment:


    4. Frequently Asked Questions and Solutions



    Execute Python on your Chromebook

    Method 1: Using Linux (Crostini)

    1. Turn on "Linux (Beta)" or "Developer → Linux Development Environment" in Chromebook settings.
    2. Start a Linux terminal and enter:
      
      sudo apt update
      sudo apt install python3 python3-pip -y
          
    3. enterpython3Enter the Python interactive environment.
    4. To install additional modules, availablepip3 install module name

    Method 2: Use an online compiler

    Method 3: Install Android App

    Method 4: Use VS Code

    1. Install VS Code in Linux (Crostini) environment:
      
      sudo apt install wget gpg -y
      wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
      sudo install -o root -g root -m 644 packages.microsoft.gpg /usr/share/keyrings/
      sudo sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/packages.microsoft.gpg] \
      https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list'
      sudo apt update
      sudo apt install code -y
          
    2. Install the Python plug-in in VS Code to develop programs.

    in conclusion



    pip usage guide

    1. What is pip?

    2. Basic operations of pip

    3. Advanced functions

    4. Frequently Asked Questions and Solutions

    5. Best practices for pip



    pip/cache/http-v2 folder

    1. What is the pip/cache/http-v2 folder?

    2. Purpose of http-v2 folder

    3. Management of http-v2 folders

    4. Precautions



    Python basic data structure

    Check variable type

    Use type()

    type()The function can return the type of the object.

    
    x = 10
    print(type(x))   # <class 'int'>
    
    y = "hello"
    print(type(y))   # <class 'str'>
    

    Use isinstance()

    isinstance()Used to check whether a variable belongs to a certain type and supports multi-type checking.

    
    x = 10
    
    print(isinstance(x, int))         # True
    print(isinstance(x, str))         # False
    print(isinstance(x, (int, float))) # True
    

    difference



    Python Boolean data type

    Boolean in Python is the basic data type in programming that represents two logical states: True or False. It is the basis for all conditional judgments and process control.

    1. Boolean values ​​and types

    a = True
    b = False
    print(type(a)) # Output: <class 'bool'>

    2. The relationship between Bollinger and numbers

    Inside Python,boolis an integerintsubcategories of , so they can participate in numerical operations:

    print(True + 1) # Output: 2 (1 + 1)
    print(False * 5) # Output: 0 (0 * 5)

    3. Core use: comparison operator

    Comparison operators compare two values ​​and return a Boolean result.

    operator describe example result
    == equal 10 == 10 True
    != not equal to 5 != 10 True
    > greater than 10 > 5 True
    <= less than or equal to 5 <= 5 True

    4. Logical operators

    Logical operators are used to combine or modify Boolean values ​​or Boolean expressions.

    print(True and False) # Output: False
    print(True or False) # Output: True
    print(not True) # Output: False

    5. Truth Value Testing

    In Python, all objects have a truth value. When an object is used for conditional judgment, Python will convert it toTrueorFalse

    if []: # Empty lists are treated as False
        print("This will not be executed")
    else:
        print("The list is empty")
    
    if "hello": # Non-empty strings are considered True
        print("String is not empty")


    Python array

    What is an array?

    In Python, an array is a data structure used to store multiple elements of the same type. Although Python itself does not have a built-in array type, you can uselistorarrayModules to achieve similar functionality.

    Use List as Array

    listIt is Python's built-in data structure that can store many types of data, but can also be used to simulate arrays.

    my_list = [1, 2, 3, 4, 5]
    print(my_list[0]) # Output: 1

    Using the array module

    If you need a true array (all elements must be of the same type) you can usearrayMods.

    import array
    
    my_array = array.array('i', [1, 2, 3, 4, 5])
    print(my_array[0]) # Output: 1

    here,'i'Indicates that the elements in the array are integers.

    Basic operations of array module

    Here are some basic operations:

    Arrays and NumPy

    For situations where numerical operations are required,numpyProvides more powerful array support.

    import numpy as np
    
    my_numpy_array = np.array([1, 2, 3, 4, 5])
    print(my_numpy_array[0]) # Output: 1

    NumPy arrays support multidimensional data and vectorized operations, making them ideal for processing large amounts of data.

    in conclusion

    Python provides many ways to implement array functions,listApplicable to general situations,arrayModules are suitable for situations where the same type of elements is required, whereasnumpyIt is the tool of choice for scientific computing.



    Determine whether the list is empty

    for variablesret_value = [], there are several simple and efficient ways to determine whether it is an empty list in the Python language. Since an empty list is treated asFalse, the following are several commonly used judgment methods.

    1. Use Boolean evaluation (the most recommended Pythonic way)

    This is the most popular method, checking the boolean value of a list directly. If the list is empty,notoperator will make its condition evaluate to true (True)。

    ret_value = []
    
    if not ret_value:
        #Execute here when the list is empty
        print("ret_value is an empty list")
    else:
        #Execute here when the list is not empty
        print("ret_value is not an empty list")

    2. Uselen()function

    Checks whether the length of the list is equal to 0.

    ret_value = []
    
    if len(ret_value) == 0:
        print("ret_value is an empty list")
    else:
        print("ret_value is not an empty list")

    3. Use equivalent comparisons== []

    Directly assign variables to an empty list literal[]Make a comparison.

    ret_value = []
    
    if ret_value == []:
        print("ret_value is an empty list")
    else:
        print("ret_value is not an empty list")


    Dynamic increase and decrease of array

    Using Lists for Dynamic Operations

    In Python,listIt is a dynamic data structure that can easily add and remove elements.

    Add new element

    New elements can be added using the following methods:

    # Example of new element
    my_list = [1, 2, 3]
    my_list.append(4) # [1, 2, 3, 4]
    my_list.insert(1, 10) # [1, 10, 2, 3, 4]
    my_list.extend([5, 6]) # [1, 10, 2, 3, 4, 5, 6]

    Remove element

    Elements can be removed using:

    # Remove element example
    my_list = [1, 2, 3, 4, 5]
    my_list.pop() # [1, 2, 3, 4]
    my_list.remove(2) # [1, 3, 4]
    my_list.clear() # []

    Dynamic operations using the array module

    For cases where you need elements of the same type, you can usearrayMods.

    Add new element

    append()andextend()Method applies toarrayMods.

    import array
    my_array = array.array('i', [1, 2, 3])
    my_array.append(4)  # [1, 2, 3, 4]
    my_array.extend([5, 6])  # [1, 2, 3, 4, 5, 6]
    

    Remove element

    remove()andpop()Methods can be usedarrayMods.

    # Remove element example
    my_array = array.array('i', [1, 2, 3, 4])
    my_array.remove(2) # [1, 3, 4]
    my_array.pop() # [1, 3]

    in conclusion

    Python provides a variety of methods to dynamically increase and decrease arrays.listandarrayThe modules are suitable for different needs. For more functional requirements, you can also consider usingnumpy



    Python array counting

    In Python,list.count()It is a tool specially used for statisticsThe number of occurrences of a specific elementmethod. It is the same as getting the length of the listlen()Different, a parameter must be passed in as the comparison object.


    1. Basic syntax of list.count()

    The syntax islist.count(value), which returns the integer number of times the value appears in the list. If the value does not exist, 0 is returned.

    fruits = ['apple', 'banana', 'apple', 'orange', 'apple']
    
    # Count the number of occurrences of 'apple'
    apple_count = fruits.count('apple')
    print(apple_count) # Output: 3
    
    # Count non-existent elements
    grape_count = fruits.count('grape')
    print(grape_count) # Output: 0

    2. Comparison of common counting requirements and tools

    Depending on your needs (whether you are looking for a single element, multiple elements, or all statistics), there are different best practices:

    need Recommended method Sample code
    Count a single specific element list.count() arr.count(10)
    Get the total length of the list len() len(arr)
    Count the frequency of all elements collections.Counter Counter(arr)
    Condition count (eg greater than 5) generator expression sum(1 for x in arr if x > 5)

    3. Advanced tools: collections.Counter

    If you need to know how many times "all elements" appear in the list at once, useCounterWill run many timescount()Much more efficient (O(n) vs O(n^2)).

    from collections import Counter
    
    data = [1, 2, 2, 3, 3, 3, 4]
    counts = Counter(data)
    
    print(counts) # Output: Counter({3: 3, 2: 2, 1: 1, 4: 1})
    print(counts[3]) # Get the number of times 3: 3
    print(counts.most_common(1)) # Get the element with the most occurrences

    4. Easily confusing usage reminders


    5. Practical skills: conditional counting

    If you want to count the number of elements that conform to a certain logic (for example, a string with a length greater than 3), you can combinesum()

    words = ['hi', 'hello', 'python', 'a', 'code']
    
    # Count the number of words with length > 3
    big_word_count = sum(1 for w in words if len(w) > 3)
    print(big_word_count) # Output: 3


    Merge 2D lists

    Requirements description

    Sample program

    list1 = [
        ["a", 1],
        ["b", 2],
        ["c", 3]
    ]
    
    list2 = [
        ["c", 30],
        ["a", 10],
        ["d", 40]
    ]
    
    #Convert list1 to dict
    dict1 = {k: v for k, v in list1}
    dict2 = {k: v for k, v in list2}
    
    # Find all keys
    all_keys = sorted(set(dict1.keys()) | set(dict2.keys()))
    
    # Merge results
    merged = []
    for k in all_keys:
        v1 = dict1.get(k)
        v2 = dict2.get(k)
        merged.append([k, v1, v2])
    
    for row in merged:
        print(row)

    Output results

    
    ['a', 1, 10]
    ['b', 2, None]
    ['c', 3, 30]
    ['d', None, 40]
    

    in conclusion



    Loop two lists at the same time

    In Python, if you have two lists (e.g.selected_fields_listandaSqlValuesStr_list), and need to iterate over them simultaneously, the most common and Pythonic way is to use the built-inzip()function.

    1. Use zip() function

    zip()The function packs multiple iterable objects (Iterable) into a sequence of tuples. Each iteration of the loop will remove the element at the corresponding position from each list.

    import re
    
    # Assume these are the two lists you get after re.split()
    selected_fields = "name,age,city"
    aSqlValuesStr = "Alice,25,Taipei"
    
    selected_fields_list = re.split(r',', selected_fields)
    aSqlValuesStr_list = re.split(r',', aSqlValuesStr)
    
    # Execute for loop
    for field, value in zip(selected_fields_list, aSqlValuesStr_list):
        print(f"Field: {field}, value: {value}")

    2. Sample output

    Field: name, value: Alice
    Field: age, value: 25
    Field: city, value: Taipei

    3. Important Notes on zip()

    When you usezip()When using functions, you need to pay attention to the following points:

    #Example: List lengths are inconsistent
    list1 = [1, 2, 3]
    list2 = ['a', 'b']
    
    for item1, item2 in zip(list1, list2):
        print(item1, item2)
    #The output will only be:
    # 1a
    #2b


    Align display of 2D array

    Align fields using format

    data = [
        [1, 23, 456],
        [7890, 12, 3],
        [45, 678, 9]
    ]
    
    # Calculate the maximum width of each column
    col_widths = [max(len(str(row[i])) for row in data) for i in range(len(data[0]))]
    
    # Format output
    for row in data:
        print(" ".join(str(val).rjust(col_widths[i]) for i, val in enumerate(row)))

    Output results

    
       1   23  456
    7890   12    3
      45  678    9
    

    Using the tabulate module

    Third-party packagestabulateAligned tables can be output directly.

    
    from tabulate import tabulate
    
    data = [
        [1, 23, 456],
        [7890, 12, 3],
        [45, 678, 9]
    ]
    
    print(tabulate(data, tablefmt="grid"))
    

    Output results

    
    +------+-----+-----+
    |    1 |  23 | 456 |
    | 7890 |  12 |   3 |
    |   45 | 678 |   9 |
    +------+-----+-----+
    

    in conclusion



    Aligned output of two-dimensional array floating point numbers

    illustrate

    The following example demonstrates how to print a two-dimensional list. When the element isfloatFixed display of two decimal places, other elements are output as they are, while maintaining field alignment.

    Program example

    data = [
        [1, 23.456, 456],
        [7890.1, 12, 3.5],
        [45, 678.9, 9]
    ]
    
    # Format each element string
    formatted_data = []
    for row in data:
        new_row = []
        for val in row:
            if isinstance(val, float):
                s = f"{val:.2f}" # float -> two decimal places
            else:
                s = str(val)
            new_row.append(s)
        formatted_data.append(new_row)
    
    # Calculate the maximum width of each column
    col_widths = [max(len(row[i]) for row in formatted_data) for i in range(len(formatted_data[0]))]
    
    # Format output
    for row in formatted_data:
        print(" ".join(val.rjust(col_widths[i]) for i, val in enumerate(row)))

    Output results

    
        1  23.46  456
    7890.10     12  3.50
       45  678.90     9
    

    focus



    Extract all values ​​from dictionary list

    The data structure you provide is a list containing multiple dictionaries, where each dictionary represents a time series record (such as K-line data).

    data = [
        {'time': 1759028400000, 'open': '109398.3', 'close': '109364.8', 'high': '109489.2', 'low': '109364.8', 'volume': '518.7594'},
        {'time': 1759024800000, 'open': '109305.6', 'close': '109398.3', 'high': '109496.4', 'low': '109296.0', 'volume': '757.0290'},
        # ...
    ]

    If you want to get all values ​​in all records (1759028400000, '109398.3', '109364.8'etc.) and collect them in a list, you can use Nested List Comprehension.

    1. Extract all values ​​into a single list (flattening)

    Use a two-level list comprehension to iterate through each dictionary in the list, and then call.values()Method gets the values ​​and finally collects all the values ​​into a flat list.

    data = [
        {'time': 1759028400000, 'open': '109398.3', 'close': '109364.8', 'high': '109489.2', 'low': '109364.8', 'volume': '518.7594'},
        {'time': 1759024800000, 'open': '109305.6', 'close': '109398.3', 'high': '109496.4', 'low': '109296.0', 'volume': '757.0290'}
    ]
    
    all_values = [
        value
        for record in data # Traverse each dictionary record in the external list
        for value in record.values() # Traverse all values in the dictionary record
    ]
    
    print(all_values)

    2. Output results

    This results in a single list containing all values:

    [1759028400000, '109398.3', '109364.8', '109489.2', '109364.8', '518.7594', 1759024800000, '109305.6', '109398.3', '109496.4', '109296.0', '757.0290']

    3. Extract the value of a specific field

    If you only need to extract specific fields (e.g. just allopenprice), you can use a single-level list comprehension:

    data = [
        {'time': 1759028400000, 'open': '109398.3', ...},
        {'time': 1759024800000, 'open': '109305.6', ...}
    ]
    
    open_prices = [record['open'] for record in data]
    
    print(open_prices)

    Output result:

    ['109398.3', '109305.6']


    Extract the value of a specific key from a dictionary list

    1. Use List Comprehension (Recommended) ✅

    List comprehensions are a concise, one-line syntax for creating new lists from existing lists.

    Python implementation example

    from datetime import datetime
    
    data_list = [
        {'videoid': 'b5HxsaM_E2Y', 'publishedat': datetime(2025, 12, 7, 3, 0, 53), 'rankno': 7, 'viewcount': 913053, 'query': 'baseball'},
        {'videoid': 'FEbMCBxsoWI', 'publishedat': datetime(2025, 11, 25, 5, 28, 6), 'rankno': 13, 'viewcount': 754598, 'query': 'baseball'},
        {'videoid': 'nOJUI0PGB68', 'publishedat': datetime(2025, 12, 7, 3, 7, 46), 'rankno': 14, 'viewcount': 748349, 'query': 'baseball'},
        {'videoid': 'uMHXIudw_w0', 'publishedat': datetime(2025, 12, 2, 10, 1, 38), 'rankno': 8, 'viewcount': 687949, 'query': 'baseball'}
    ]
    
    target_key = 'videoid'
    
    # Use list comprehension: for each dictionary item in data_list, take out item[target_key]
    video_ids = [item[target_key] for item in data_list]
    
    print(f"Extracted key: {target_key}")
    print("All videoid values:")
    print(video_ids)

    Output results

    ['b5HxsaM_E2Y', 'FEbMCBxsoWI', 'nOJUI0PGB68', 'uMHXIudw_w0']

    2. Handle the situation where the key may not exist (safe access)

    If some dictionaries in the list may be missing the target key (e.g. some dictionaries don't have the `videoid` key), using `item[target_key]` directly will throw a `KeyError` error. You can use the dictionary's `.get()` method or a conditional to handle it safely.

    Python implementation example (secure access)

    data_with_missing_key = [
        {'videoid': 'A1', 'query': 'football'},
        {'query': 'basketball'}, # Missing 'videoid' key
        {'videoid': 'C3', 'query': 'Volleyball'}
    ]
    
    target_key = 'videoid'
    
    # Option A: Use .get() to set the default value to None (or any other value)
    safe_video_ids_A = [item.get(target_key) for item in data_with_missing_key]
    # Output: ['A1', None, 'C3']
    
    # Option B: Extract only values with that key
    safe_video_ids_B = [item[target_key] for item in data_with_missing_key if target_key in item]
    # Output: ['A1', 'C3']
    
    print(f"\nSafe extraction results (Plan B): {safe_video_ids_B}")


    Convert dict values ​​to list

    you fromonesymbollist.values()The result obtained is a Python dictionary view objectdict_values. Although it looks like a list, it is a dynamic view and not a standard list that can be indexed or modified.

    dict_values([1763510400000, '0.00015218', '0.00015336', '0.00015415', '0.00015067', '1634523'])

    Do not showdict_values()To get the content of a tag, the simplest and most common way is to use the built-inlist()The function casts it to a list.

    1. Convert to list (list)

    Willdict_valuespassed tolist()Function that will immediately copy all elements in the view object to a new standard list.

    # Assume this is the dict_values view object you obtained
    dict_values_object = your_dictionary.values() # Assume onesymbollist is a dictionary
    
    # Step: Use list() function to convert
    result_list = list(dict_values_object)
    
    print(result_list)

    Output results

    [1763510400000, '0.00015218', '0.00015336', '0.00015415', '0.00015067', '1634523']

    2. Convert to tuple

    If your goal is to create an immutable sequence, you can usetuple()The function converts it into a tuple.

    result_tuple = tuple(dict_values_object)
    
    print(result_tuple)

    Output results

    (1763510400000, '0.00015218', '0.00015336', '0.00015415', '0.00015067', '1634523')

    3. Direct iteration (no conversion required)

    Note that if you just want to process the values ​​one by one in a loop, you don't need to explicitly convert it to a list.dict_valuesItself is an iterable object (Iterable).

    # Assume this is the dict_values view object you obtained
    dict_values_object = your_dictionary.values()
    
    print("Output elements one by one:")
    for value in dict_values_object:
        print(value)

    In summary, uselist(your_dict.values())is the most common way to get a clean, actionable list of values.



    Convert dictionary values ​​to comma separated string

    to start fromdict_valuesExtracts all values ​​from the view object and combines them into a single string separated only by commas and spaces (for example:1763510400000, '0.00015218', ...), you need to combine the following steps:

    1. get from dictionarydict_valuesView object.
    2. Converts all elements in this view object to string types.
    3. Using Python", ".join()method to concatenate these strings.

    1. Python implementation

    Assume your original dictionary is namedonesymbol_dict, and you need to convert non-string values ​​(such as timestamps) to strings to use.join()Make a connection.

    onesymbol_dict = {
        'time': 1763510400000,
        'open': '0.00015218',
        'close': '0.00015336',
        'high': '0.00015415',
        'low': '0.00015067',
        'volume': '1634523'
    }
    
    # Step 1 & 2: Get values and convert all values to strings
    # Use list comprehensions to ensure all elements are strings
    values_as_strings = [str(v) for v in onesymbol_dict.values()]
    
    # Step 3: Use ', '.join() to join these strings
    values_string = ", ".join(values_as_strings)
    
    print(values_string)

    2. Output results

    This will produce a single string whose content is only the value, without any outer brackets ordict_valuesLabel:

    1763510400000, 0.00015218, 0.00015336, 0.00015415, 0.00015067, 1634523

    3. Notes on string quotation marks

    Note that in the example output you expect, values ​​other than timestamps have single quotes:1763510400000, '0.00015218', ...

    If your goal is to have string values ​​retain single quotes in the final output, you will need to add quotes manually during concatenation. This is usually required when preparing strings for SQL statements or specific formats.

    # Extra step: Handle quotes manually, assuming non-integer values require quotes
    quoted_values = []
    for v in onesymbol_dict.values():
        if isinstance(v, (str, float)) or (isinstance(v, int) and v< 1000000000000): # 假設小數字串需要引號
            quoted_values.append(f"'{v}'")
        else:
            quoted_values.append(str(v))
    
    final_quoted_string = ", ".join(quoted_values)
    
    print(final_quoted_string)

    This will give you the format you expect (assumingtimeNo quotes required, other numeric strings do):

    1763510400000, '0.00015218', '0.00015336', '0.00015415', '0.00015067', '1634523'


    Determine whether a string is a number

    Use str.isdigit()

    isdigit()Method can be used to check whether a string contains only numeric characters.

    # Example
    string = "12345"
    if string.isdigit():
        print("is a number")
    else:
        print("Not a number")

    Notice:isdigit()Cannot handle decimal points or negative signs.

    Use str.replace() to handle decimals

    If you need to check a string with a decimal point, you can remove the decimal point before usingisdigit()

    # Example
    string = "123.45"
    if string.replace(".", "").isdigit():
        print("is a number")
    else:
        print("Not a number")

    This method does not work with negative numbers.

    Convert to number using try-except

    The most common approach is to try to convert the string to a float or integer and catch an exception if the conversion fails.

    # Example
    string = "-123.45"
    try:
        float(string) # You can use int(string) instead to check for integers
        print("is a number")
    except ValueError:
        print("Not a number")

    Use regular expressions

    Regular expressions can match numbers exactly, including integers, decimals, and negative numbers.

    # Example
    import re
    
    string = "-123.45"
    pattern = r"^-?\d+(\.\d+)?$"
    if re.match(pattern, string):
        print("is a number")
    else:
        print("Not a number")

    in conclusion

    For simple cases, useisdigit(). For more complex cases (like dealing with decimals or negative numbers) it is recommended to usetry-exceptor regular expression.



    Python f-string

    f-string (formatted string literal) is a powerful and efficient string formatting method introduced in Python 3.6. It provides a concise and highly readable way to embed the values ​​of variables and expressions into strings.

    The core feature of f-string is: using prefixes at the beginning of the stringforF, and use braces{}to contain what is to be calculated and displayed.

    1. Basic usage: Embed variables

    You can put any variable name directly inside curly braces.

    name = "Alice"
    age=30
    message = f"Hello, my name is {name} and I am {age} years old."
    # Output: Hello, my name is Alice and I am 30 years old.

    2. Embedded expressions

    The power of f-string is that you can put any valid Python expression inside the curly braces and it will be evaluated at runtime.

    price = 19.99
    tax_rate = 0.05
    total = price * (1 + tax_rate)
    
    # Perform calculations within f-string
    result = f"The total price including tax is: {price * (1 + tax_rate):.2f} yuan."
    # Output: The total price including tax is: 20.99 yuan.
    
    # Call function
    def get_status():
        return "OK"
    
    status_msg = f"System status: {get_status()}"
    # Output: System status: OK

    3. String formatting and alignment

    f-string supports and.format()Same method as Format Specifier Mini-Language, using colon:to separate expressions and format specifiers.

    format code use example output
    :.2f Floating point number with two decimal places f"{3.14159:.2f}" 3.14
    : <10 Align left, width 10 f"{'Name':<10}" Name
    : >10 Align right, width 10 f"{'Value':>10}" Value
    :^10 Center aligned, width 10 f"{'Hi':^10}" Hi
    :, Numeric thousands separator f"{1000000:,}" 1,000,000

    4. Debug F-strings

    Starting with Python 3.8, f-string introduces a convenient debugging feature that allows you to add an equal sign after the variable.=, automatically displays variable names and their values.

    user_id = 42
    is_active = True
    
    debug_output = f"User ID is {user_id=}, Status: {is_active=}"
    # Output: User ID is user_id=42, Status: is_active=True

    5. Precautions



    f-string does not display floating point decimal points

    1. Solution 1: Integer conversion within f-string (recommended)

    Braces in f-string{}Internally, you can directly useint()The function converts the variable back to an integer type. This is the clearest and most direct method.

    Python implementation example

    inta = 12 # assuming raw integer
    float_a = inta * 2.0 # The result is 24.0 (floating point number)
    
    # Convert floating point number back to integer directly in f-string
    result_str = f"abc def {int(float_a)}"
    
    print(f"Original value (float_a): {float_a}")
    print(f"Formatted result: {result_str}")

    2. Option 2: Use f-string format specifier (Format Specifier)

    You can use the integer format codes from the formatting mini-languagedor.0fto control the output format.

    A. Use integer format codes:d

    use:dPython is asked to treat the value as an integer when displayed. if variableais a floating point number, Python will automatically round it to the nearest integer before displaying it (if your calculation results in $24.0$, it will display $24$).

    float_a = 24.0
    result_d = f"abc def {float_a:d}"
    # Output: abc def 24

    B. Use floating point format code:.0f

    use:.0fmeans format the value as a floating point number, but require 0 digits after the decimal point. This will also cause the results to be displayed rounded.

    float_a = 24.0
    result_0f = f"abc def {float_a:.0f}"
    # Output: abc def 24

    Summary and suggestions

    If your purpose is to ensure that the result is an absolute integer without a decimal point, it is recommended to use option 1:

    str = f"abc def {int(a)}"


    Python re.split function

    In Python,re.split()The function isreA powerful tool in the (regular expression) module, used to split strings according to the delimiters (patterns) defined in regular expressions and return the results as a list (list).

    1. Function syntax

    re.split(pattern, string, maxsplit=0, flags=0)

    2. Basic usage

    Use regular expressions to define multiple or complex delimiters.

    import re
    
    text = "apple,banana;orange-grape"
    # Use comma, semicolon or hyphen as delimiter
    result = re.split(r'[;,-]', text)
    
    print(result)
    # Output: ['apple', 'banana', 'orange', 'grape']

    3. Handle multiple spaces (a common use)

    with standard stringssplit()different,re.split()Multiple consecutive delimiters (e.g. multiple spaces) can be easily handled and ignored.

    text = "Word1 Word2 Word3"
    # Use \s+ to match one or more whitespace characters as delimiters
    result = re.split(r'\s+', text)
    
    print(result)
    # Output: ['Word1', 'Word2', 'Word3']

    4. Use maxsplit to limit the number of cuts

    If setmaxsplit, the cutting operation will only be performed a specified number of times, and the remaining parts will be retained in the list as the last element.

    text = "one:two:three:four"
    # Only cut once
    result = re.split(r':', text, maxsplit=1)
    
    print(result)
    # Output: ['one', 'two:three:four']

    5. Keep delimiters

    If you place the delimiter pattern inside the brackets(), the delimiter itself will also be included between the elements of the resulting list.

    text = "2025-01-15"
    # Put hyphens in parentheses so they are preserved
    result = re.split(r'(-)', text)
    
    print(result)
    # Output: ['2025', '-', '01', '-', '15']


    Determine the beginning of a string startswith

    use

    startswith()is a Python string (str) object method, used to determine whether the string begins with the specified substring. If it matches, returnTrue; Otherwise returnFalse

    grammar

    
    str.startswith(prefix[, start[, end]])
    

    Parameter description

    return value

    Boolean value: If the string starts with the specified prefix, returnTrue,otherwiseFalse

    example

    text = "Python Programming"
    
    #Basic usage
    print(text.startswith("Py")) # True
    print(text.startswith("Java")) # False
    
    #Specify range
    print(text.startswith("thon", 2)) # True (starting at index 2 is "thon")
    
    # multiple alignments
    print(text.startswith(("Py", "Java", "C"))) # True, because any matching
    
    # Not case sensitive (can be converted to lowercase first)
    print(text.lower().startswith("py")) # True

    Common applications

    extend

    To determine whether a string "ends" with a certain text, you can useendswith()methods, syntax andstartswith()same.

    filename = "report.pdf"
    if filename.endswith(".pdf"):
        print("This is a PDF file")


    Delete the last character of the string

    illustrate

    In Python, strings are immutable objects. To remove the last character, string slicing is usually used to create a new string.

    example

    text = "Hello!"
    
    #Method 1: Use slicing
    new_text = text[:-1]
    print(new_text) #Output: Hello
    
    #Method 2: Use rstrip() to remove specific ending characters
    text2 = "Hello!!!"
    new_text2 = text2.rstrip("!")
    print(new_text2) #Output: Hello
    
    #Method 3: Make sure it is not empty and then delete the last character
    if text:
        text = text[:-1]
    print(text)

    Output results

    
    Hello
    Hello
    Hello
    

    illustrate

    extend

    To remove the leading characters, use:

    
    text = text[1:]
    

    Summarize



    Find the content before the substring in the string

    Problem description

    Given a stringstr1, we hope to find instrAorstrBThe part that comes before. For example:

    str1 = "Hello World, this is a test. Stop here or continue."
    strA = "Stop"
    strB = "continue"
    

    The goal is to obtain"Hello World, this is a test. "

    Use re.split()

    re.split()You can split a string based on multiple keywords and take the first part:

    import re
    
    def get_substring_before(text, strA, strB):
        result = re.split(f"{re.escape(strA)}|{re.escape(strB)}", text, maxsplit=1)[0]
        return result
    
    str1 = "Hello World, this is a test. Stop here or continue."
    strA = "Stop"
    strB = "continue"
    
    print(get_substring_before(str1, strA, strB))  # "Hello World, this is a test. "
    

    Use re.search()

    re.search()can be used to matchstrAorstrB, and get the content before matching:

    import re
    
    def get_substring_before(text, strA, strB):
        match = re.search(f"{re.escape(strA)}|{re.escape(strB)}", text)
        return text[:match.start()] if match else text
    
    str1 = "Hello World, this is a test. Stop here or continue."
    print(get_substring_before(str1, "Stop", "continue"))  # "Hello World, this is a test. "
    

    Use find() method

    find()You can manually search for the earlieststrAorstrB, and then retrieve the corresponding part:

    def get_substring_before(text, strA, strB):
        indexA = text.find(strA)
        indexB = text.find(strB)
        
        indices = [i for i in [indexA, indexB] if i != -1]
        first_index = min(indices, default=len(text))
        
        return text[:first_index]
    
    str1 = "Hello World, this is a test. Stop here or continue."
    print(get_substring_before(str1, "Stop", "continue"))  # "Hello World, this is a test. "
    

    in conclusion



    Concatenate multiple values ​​into a string

    Use join (you need to convert everything into strings first)

    values = ["str1", "str2", 123, "str3", 456]
    
    s = ", ".join(str(v) for v in values)
    print(s) # Output: str1, str2, 123, str3, 456

    Automatically handle mixed strings and integers

    def join_values(*args, sep=", "):
        return sep.join(str(v) for v in args)
    
    print(join_values("str1", "str2", 88, "str3"))
    # Output: str1, str2, 88, str3

    If the source is dict, sort by key and then concatenate

    data = {
        "str1": "hello",
        "str2": "world",
        "int1": 123,
        "str3": "ok"
    }
    
    # Sort by key and then join
    s = ", ".join(str(data[k]) for k in sorted(data.keys()))
    print(s) # hello, world, 123, ok

    To join key + value together

    s = ", ".join(f"{k}={v}" for k, v in data.items())
    print(s)
    # Output: str1=hello, str2=world, int1=123, str3=ok

    The source is a list of dict, each dict is joined into one column

    rows = [
        {"str1": "A", "int1": 10},
        {"str1": "B", "int1": 20},
    ]
    
    for row in rows:
        print(", ".join(str(v) for v in row.values()))
    # A, 10
    # B, 20
    

    Universal function: switch between "with quotes" or "without quotes" mode

    def join_values(values, sep=", ", quoted=False):
        if quoted:
            # Use full single quotes ‘ ’
            return sep.join(f"‘{v}’" for v in values)
        else:
            return sep.join(str(v) for v in values)
    
    values = ["str1", "str2", 88, "str3"]
    
    print(join_values(values, quoted=False))
    # Output: str1, str2, 88, str3
    
    print(join_values(values, quoted=True))
    # Output: ‘str1’, ‘str2’, ‘88’, ‘str3’

    Can support *args input version

    def join_args(*args, sep=", ", quoted=False):
        if quoted:
            return sep.join(f"‘{v}’" for v in args)
        return sep.join(str(v) for v in args)
    
    print(join_args("str1", "str2", 88, "str3", quoted=True))
    # ‘str1’, ‘str2’, ‘88’, ‘str3’
    

    Support dict (only output value)

    data = {"str1": "hello", "str2": "world", "int1": 123}
    
    print(join_values(data.values(), quoted=True))
    # ‘hello’, ‘world’, ‘123’
    

    Support key=value format

    def join_key_value(d, sep=", ", quoted=False):
        if quoted:
            return sep.join(f"{k}=‘{v}’" for k, v in d.items())
        return sep.join(f"{k}={v}" for k, v in d.items())
    
    print(join_key_value(data, quoted=True))
    # str1=‘hello’, str2=‘world’, int1=‘123’
    


    re.match()

    re.match()

    Pythonre.matchIs a function in the regular expression module, used to match from the beginning of the string. If the match is successful, return aMatchobject; otherwise returnNone

    grammar

    re.match(pattern, string, flags=0)

    Parameter description:

    Common properties and methods

    Usage examples

    import re
    
    #Define a string
    text = "123 Hello World!"
    
    # Use re.match to match numbers from the beginning
    match = re.match(r"(\d+)\s+(.*)", text)
    
    if match:
        print(f"Entire matching result: {match.group(0)}") # 123 Hello World!
        print(f"Number part: {match.group(1)}") # 123
        print(f"Text part: {match.group(2)}") # Hello World!
    else:
        print("match failed")

    Output results

    The entire matching result: 123 Hello World!
    Numeric part: 123
    Text part: Hello World!

    Things to note

    regular expression

    Regular Expression (Regex for short) is a syntax used to describe string matching rules. It is often used to search, replace or verify strings. in PythonreIn the module,patternThat's the core part of defining these rules.

    basic syntax elements

    Advanced usage

    example

    import re
    
    # Example 1: Match content starting with a number
    pattern = r"^\d+"
    text = "123abc"
    match = re.match(pattern, text)
    if match:
        print(f"Matching result: {match.group()}") # Output: 123
    
    #Example 2: Match text after numbers
    pattern = r"(\d+)\s+(.*)"
    text = "123 Hello World"
    match = re.match(pattern, text)
    if match:
        print(f"Number part: {match.group(1)}") # Output: 123
        print(f"Text part: {match.group(2)}") # Output: Hello World

    Application scenarios of regular expressions



    Application of re.search()

    Basic usage

    re.search()Used to search for the first match of a regular expression in a string and return itMatchObject, returned if there is no matchNone

    import re
    
    text = "Hello 2024!"
    match = re.search(r"\d+", text)
    
    if match:
        print("Number found:", match.group()) # 2024

    Return Match object

    whenre.search()When a match is found, it returnsMatchObjects can access information through the following methods:

    import re
    
    text = "Python 3.10 is great!"
    match = re.search(r"\d+\.\d+", text)
    
    if match:
        print("Match content:", match.group()) # 3.10
        print("Start index:", match.start()) # 7
        print("End index:", match.end()) # 11
        print("range:", match.span()) # (7, 11)

    Use group matching

    through brackets()to create a group and usegroup(n)to extract the corresponding matching content.

    import re
    
    text = "John Doe, Age: 25"
    match = re.search(r"(\w+) (\w+), Age: (\d+)", text)
    
    if match:
        print("Last name:", match.group(1)) # John
        print("name:", match.group(2)) # Doe
        print("Age:", match.group(3)) # 25

    Comparison with re.findall()

    re.search()Only the first matching result is returned, whereasre.findall()All matching results will be returned.

    import re
    
    text = "Price: $10, Discount: $2, Tax: $1"
    
    match = re.search(r"\$\d+", text)
    print("re.search:", match.group())  # $10
    
    matches = re.findall(r"\$\d+", text)
    print("re.findall:", matches)  # ['$10', '$2', '$1']
    

    in conclusion

    re.search()Suitable for finding the first matching result, and can be used toMatchObject to get detailed information. For multiple matching results, usere.findall()



    Non-capturing groups of regular expressions

    Improve matching performance

    In a regular expression,(...)Matching content will be captured and stored ingroup(n), but not the capture group(?:...)It is only used for organizational structure and will not affect the group number, so matching is faster.

    Avoid affecting group index

    If used in regular expression()to organize matching conditions, which will affectgroup(n)number. use(?:...)This ensures that the group index remains unchanged.

    import re
    
    text = "2024-03-12"
    pattern = r"(\d{4})-(?:\d{2})-(\d{2})"
    
    match = re.search(pattern, text)
    print(match.group(1))  # 2024
    print(match.group(2))  # 12
    

    Paired with OR operator

    use(?:...|...)can let|The operator affects matching content but not group access.

    import re
    
    text = "bar123"
    pattern = r"(?:foo|bar|baz)\d+"
    
    match = re.search(pattern, text)
    print(match.group())  # bar123
    

    Applies to --user-data-dir parsing

    When parsing Chrome parameters, use(?:...)This ensures that the matching format does not affect the group number.

    import re
    
    cmdline = '--user-data-dir="C:\\Users\\moirg\\AppData\\Local\\Google\\Chrome\\User Data"'
    
    match = re.search(r'--user-data-dir=(?:"([^"]+)"|(\S+))', cmdline)
    user_data_dir = match.group(1) or match.group(2)
    
    print(user_data_dir)  # C:\Users\moirg\AppData\Local\Google\Chrome\User Data
    

    in conclusion

    (?:...)It can improve performance in regular expressions, avoid affecting group index, and is suitable for|Operations and specific condition matching make the code more efficient and clear.



    Python datetime

    Import module

    import datetime

    Get current time

    now = datetime.datetime.now()
    print(now)

    Create specified time

    dt = datetime.datetime(2025, 7, 2, 14, 30, 0)
    print(dt)

    Format time string

    now = datetime.datetime.now()
    formatted = now.strftime("%Y-%m-%d %H:%M:%S")
    print(formatted)

    Parse time string

    dt_str = "2025-07-02 14:30:00"
    parsed = datetime.datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S")
    print(parsed)

    Time addition and subtraction

    now = datetime.datetime.now()
    delta = datetime.timedelta(days=7)
    next_week = now + delta
    print(next_week)

    Get today's date

    today = datetime.date.today()
    print(today)

    Compare dates

    dt1 = datetime.datetime(2025, 7, 1)
    dt2 = datetime.datetime(2025, 7, 2)
    print(dt1 < dt2)

    Get time difference

    dt1 = datetime.datetime(2025, 7, 1, 12, 0, 0)
    dt2 = datetime.datetime(2025, 7, 2, 14, 30, 0)
    diff = dt2 - dt1
    print(diff)
    print(diff.total_seconds())

    Get day of week

    today = datetime.date.today()
    print(today.weekday()) # 0 = Monday, 6 = Sunday


    datetime time zone issue

    offset-naive and offset-aware datetime subtraction error

    When one datetime object has no time zone (naive) and the other has a time zone (aware) and is subtracted, it will produce:

    TypeError: can't subtract offset-naive and offset-aware datetimes
    

    Check if datetime is naive or aware

    from datetime import datetime
    
    def is_aware(dt):
        return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None
    
    def is_naive(dt):
        return not is_aware(dt)
    
    dt1 = datetime.now()                 # naive
    dt2 = datetime.now().astimezone()    # aware
    
    print(is_naive(dt1), is_aware(dt1))
    print(is_naive(dt2), is_aware(dt2))
    

    Solution A: Convert to aware (recommended)

    Suitable for situations across time zones or when precise time calculation is required.

    from datetime import datetime, timezone
    
    sql_dt = sql_dt.replace(tzinfo=timezone.utc) # SQL data transfer aware
    now_dt = datetime.now(timezone.utc) # Use aware for the current time
    
    diff = now_dt - sql_dt
    print(diff.total_seconds())

    Solution B: Convert to naive (not recommended, time zone difference will be ignored)

    sql_dt = sql_dt.replace(tzinfo=None)
    now_dt = datetime.now()
    
    diff = now_dt - sql_dt
    

    Summarize



    Calculate the standard deviation of a series

    illustrate

    In Python, you can use the built-in modulesstatisticsofstdev()orpstdev()Calculate the sample standard deviation or population standard deviation.

    example

    import statistics
    
    ratios = [2.3, 2.8, 3.1, 2.5, 3.0]
    
    # average
    average = sum(ratios) / len(ratios)
    
    #Sample standard deviation (n-1)
    std_sample = statistics.stdev(ratios)
    
    #Matrix standard deviation (n)
    std_population = statistics.pstdev(ratios)
    
    print(f"Average: {average:.2f}")
    print(f"Sample standard deviation: {std_sample:.3f}")
    print(f"Population standard deviation: {std_population:.3f}")

    Output results

    Average: 2.74
    Sample standard deviation: 0.303
    Population standard deviation: 0.271

    Formula method (without using mods)

    If you don't want to rely onstatisticsModule, you can calculate it yourself using mathematical formulas:

    ratios = [2.3, 2.8, 3.1, 2.5, 3.0]
    average = sum(ratios) / len(ratios)
    
    # standard deviation (parent)
    variance = sum((x - average) ** 2 for x in ratios) / len(ratios)
    std_dev = variance ** 0.5
    
    print(f"Standard deviation: {std_dev:.3f}")

    Output results

    Standard deviation: 0.271

    Difference description

    Summarize



    Python displays ANSI color string

    basic example

    # ANSI color code example
    print("\033[31m red text\033[0m")
    print("\033[32m green text\033[0m")
    print("\033[33myellow text\033[0m")
    print("\033[34m blue text\033[0m")
    print("\033[35m purple text\033[0m")
    print("\033[36m cyan text\033[0m")
    print("\033[37m white text\033[0m")

    Bold and background color

    print("\033[1;31m bold red text\033[0m")
    print("\033[42m green background text\033[0m")

    Custom color combination

    # Format: \033[style; foreground color; background color m
    # Style: 0=Default, 1=Bold, 4=Underline
    # Foreground color: 30~37
    # Background color: 40~47
    
    print("\033[1;33;44m bold yellow text + blue background\033[0m")

    Can be encapsulated into functions

    def color_text(text, color_code):
        return f"\033[{color_code}m{text}\033[0m"
    
    print(color_text("Warning!", "1;31")) # Bold red
    print(color_text("Success!", "1;32")) # Bold green


    Check whether the current terminal supports ANSI colors

    Method 1: Check sys.stdout.isatty()

    importsys
    
    if sys.stdout.isatty():
        print("Terminal may support ANSI colors")
    else:
        print("It may be a file or an output environment that does not support color")

    Method 2: Use colorama (cross-platform solution)

    import colorama
    colorama.init()
    
    print("\033[32mThis text should be green\033[0m")

    Method 3: Actual test output

    def supports_ansi():
        try:
            print("\033[31m test red\033[0m")
            return True
        except:
            return False
    
    print("ANSI is supported" if supports_ansi() else "ANSI is not supported")

    Replenish



    Python input supports autocomplete

    illustrate

    In Python,input()The function itself does not supportTabCompleted automatically. To achieve this function, you can combinereadlineModule that allows users to useTabDo autocompletion (similar to Bash or IPython).

    basic example

    import readline
    
    # Define a list of strings that can be completed
    WORDS = ['apple', 'banana', 'cherry', 'grape', 'orange', 'watermelon']
    
    def completer(text, state):
        """Autocomplete function: compare available strings based on input prefixes"""
        options = [w for w in WORDS if w.startswith(text)]
        if state < len(options):
            return options[state]
        else:
            return None
    
    # Enable completion function
    readline.set_completer(completer)
    readline.parse_and_bind('tab: complete')
    
    # User input (supports Tab)
    user_input = input("Enter the name of the fruit (you can press Tab to complete): ")
    print(f"What you entered is: {user_input}")

    Operating Instructions

    Advanced: Dynamic completion

    You can also update the completion list based on the current context or dynamic content:

    import readline
    
    def dynamic_completer(text, state):
        current_words = ['cat', 'car', 'dog', 'duck', 'deer']
        options = [w for w in current_words if w.startswith(text)]
        if state < len(options):
            return options[state]
        return None
    
    readline.set_completer(dynamic_completer)
    readline.parse_and_bind('tab: complete')
    
    command = input("Enter the animal name: ")
    print("You enter:", command)

    Things to note

    Summarize



    Python process

    Python loop

    for with range()

    The most common loop is used to run a fixed number of times or sequence.

    # from 0 to 4
    for i in range(5):
        print(i) #0,1,2,3,4
    
    #Specify the starting point, end point and step length
    for i in range(2, 10, 2):
        print(i) #2,4,6,8

    for with iterable objects

    Directly iterate lists, strings, dictionaries, etc.

    fruits = ["apple", "banana", "cherry"]
    
    for fruit in fruits:
        print(fruit)
    
    for ch in "hello":
        print(ch)
    
    #Iterate over dictionary
    person = {"name": "Tom", "age": 25}
    for key, value in person.items():
        print(key, value)

    while loop

    It will always be executed when the condition is True.

    
    count = 0
    while count < 5:
        print(count)
        count += 1
    

    break and continue

    Control the loop process.

    for i in range(10):
        if i == 3:
            continue # skip this time
        if i == 7:
            break # End early
        print(i)

    nested loop

    Go back into the circle and put it back into the circle.

    
    for i in range(3):
        for j in range(2):
            print(f"i={i}, j={j}")
    

    Loop matching else

    for or while can be addedelse, only "normal completion (no break)" will be executed.

    for i in range(5):
        print(i)
    else:
        print("Loop ends normally")

    List Comprehension

    Concise writing method can complete the loop and generate the list in one line.

    
    squares = [x**2 for x in range(5)]
    print(squares)  # [0,1,4,9,16]
    

    in conclusion



    Python exception handling

    basic grammatical structure

    Python uses the try...except statement to intercept and handle errors that occur during program execution to prevent program crashes.

    try:
        # Code that may cause exceptions
        result=10/0
    except ZeroDivisionError:
        # Code executed when a specific exception occurs
        print("The divisor cannot be zero")
    except Exception as e:
        #Catch all other types of exceptions
        print(f"An error occurred: {e}")
    else:
        # Execute if no exception occurs in the try block
        print("operating normally")
    finally:
        # Will be executed regardless of whether an exception occurs
        print("Clean resources or close files")

    Block function description

    Catching multiple exceptions

    You can use tuples in an except to handle multiple error types at the same time.

    try:
        # Perform operations
        pass
    except (ValueError, TypeError):
        print("The entered data type or value is incorrect")

    Actively throw an exception

    Use the raise keyword to manually trigger exceptions based on logical requirements.

    age = -1
    if age < 0:
        raise ValueError("年齡數值不可為負數")

    best practices



    Python categories

    1. Basic category concepts

    Python classes are structures used to encapsulate data and behavior. Categories are used to create objects, which are instances of categories. For example:
    classMyClass:
        def __init__(self, value):
            self.value = value
    
        def display(self):
            print(f"Value: {self.value}")
    
    obj = MyClass(10)
    obj.display() # Output: Value: 10

    2. Static Method

    Static methods are defined using the `@staticmethod` decorator and have nothing to do with categories and objects. They cannot access category properties or object properties. Applies to some instrumental functions:

    classMyClass:
        @staticmethod
        def add(a, b):
            return a + b
    
    result = MyClass.add(5, 3)
    print(result) # Output: 8

    3. Class Method

    Class methods are defined using the `@classmethod` decorator, the first parameter is the class itself (usually named `cls`), and class attributes can be accessed:

    classMyClass:
        count = 0
    
        @classmethod
        def increment_count(cls):
            cls.count += 1
    
    MyClass.increment_count()
    print(MyClass.count) # Output: 1

    4. Inheritance and polytype

    Python supports class inheritance. Subclasses can inherit the attributes and methods of the parent class and override the parent class methods:

    class Parent:
        def greet(self):
            print("Hello from Parent!")
    
    class Child(Parent):
        def greet(self):
            print("Hello from Child!")
    
    obj = Child()
    obj.greet() # Output: Hello from Child!

    5. Category attributes and object attributes

    Category attributes belong to the entire category and are shared by all objects; object attributes belong to each object:

    classMyClass:
        class_attr = "I am a class attribute"
    
        def __init__(self, value):
            self.instance_attr = value
    
    obj1 = MyClass(10)
    obj2 = MyClass(20)
    
    print(MyClass.class_attr) # Output: I am a class attribute
    print(obj1.instance_attr) # Output: 10
    print(obj2.instance_attr) # Output: 20

    6. Use object as base class

    All classes in Python inherit from `object` by default, which is a built-in base class that provides some basic methods, such as `__str__` and `__eq__`:

    class MyClass(object):
        def __init__(self, value):
            self.value = value
    
        def __str__(self):
            return f"MyClass with value {self.value}"
    
    obj = MyClass(5)
    print(obj) # Output: MyClass with value 5

    7. Summary

    - **Static Method**: Independent of categories, mainly used for tool functions. - **Class Method**: Manipulate data at the category level. - **Object Method (Instance Method)**: Manipulate object-level data. - **Inheritance and polytype**: Support code reuse and flexible design. - **object base class**: Provides basic methods so that all categories have consistent behavior.

    Class inheritance

    1. Basic inheritance concepts

    In Python, class inheritance allows subclasses (Derived Class) to inherit the properties and methods of the parent class (Base Class) to achieve code reuse. For example:

    class Parent:
        def greet(self):
            print("Hello from Parent!")
    
    class Child(Parent):
        pass
    
    c = Child()
    c.greet() # Output: Hello from Parent!

    2. Subclass overrides parent class method

    Subclasses can override (Override) the methods of the parent class and rewrite its functions:

    class Parent:
        def greet(self):
            print("Hello from Parent!")
    
    class Child(Parent):
        def greet(self):
            print("Hello from Child!")
    
    c = Child()
    c.greet() # Output: Hello from Child!

    3. Use super() to call the parent class method

    In the subclass, you can call the method of the parent class through `super()` and extend the behavior of the parent class:

    class Parent:
        def greet(self):
            print("Hello from Parent!")
    
    class Child(Parent):
        def greet(self):
            super().greet()
            print("Hello from Child!")
    
    c = Child()
    c.greet()
    # Output:
    # Hello from Parent!
    # Hello from Child!

    4. Multiple inheritance

    Python supports multiple inheritance, and subclasses can inherit multiple parent classes at the same time:

    class Parent1:
        def greet(self):
            print("Hello from Parent1!")
    
    class Parent2:
        def greet(self):
            print("Hello from Parent2!")
    
    class Child(Parent1, Parent2):
        pass
    
    c = Child()
    c.greet() # Output: Hello from Parent1! (according to inheritance order)

    5. Method resolution order (MRO)

    Multiple inheritance uses MRO (Method Resolution Order) to determine the resolution order of methods. This can be checked using the `__mro__` attribute:

    print(Child.__mro__)
    # Output: (, , , )
    

    6. Abstract base class

    Use the `abc` module to define an Abstract Base Class to force subclasses to implement specific methods:

    from abc import ABC, abstractmethod
    
    class AbstractParent(ABC):
        @abstractmethod
        def greet(self):
            pass
    
    class Child(AbstractParent):
        def greet(self):
            print("Hello from Child!")
    
    c = Child()
    c.greet() # Output: Hello from Child!

    7. Summary

    - Inheritance makes class code more reusable and extensible. - Subclasses can override parent class methods and call parent class methods using `super()`. - Supports multiple inheritance, but you need to pay attention to the method resolution order (MRO). - Abstract base classes can be used to force subclasses to implement specific methods, which is suitable for interface design.

    Create a temporary class that inherits ClassB

    Sample code

    classClassB:
        def greet(self):
            print("Hello from ClassB!")
    
    # Dynamically create a temporary class inherited from ClassB
    TempClass = type('TempClass', (ClassB,), {
        'greet': lambda self: (print("Hello from TempClass!"), super(TempClass, self).greet())[0]
    })
    
    #Create instance and test
    temp = TempClass()
    temp.greet()

    explain

    1. type() function:
      type('TempClass', (ClassB,), {...})
      - 'TempClass': New category name.
      - (ClassB,): Tuple of basic categories, here there is only ClassB.
      - {...}: New attributes or methods.
    2. Lambda functions are used to override methods:
      - CustomizegreetThe method is to print out the new message first, and then passsuper()call parent categorygreet

    Output results

    Hello from TempClass!
    Hello from ClassB!


    Switch between using different categories with the same interface

    illustrate

    Suppose there are two categoriesAClassandBClass, they have functions with the same name and parameters. We want to be able to easily switch which category to use without having to modify the main program logic.

    Sample program

    class AClass:
        def process(self, data):
            print(f"AClass processing: {data}")
    
        def result(self):
            return "result from AClass"
    
    
    class BClass:
        def process(self, data):
            print(f"BClass processing: {data}")
    
        def result(self):
            return "result from BClass"
    
    
    # You can control which category to use through settings
    USE_A = True
    
    #Dynamic selection of categories
    SelectedClass = AClass if USE_A else BClass
    
    # Create an instance and use it
    obj = SelectedClass()
    obj.process("Test data")
    print(obj.result())

    Output results (USE_A=True)

    AClass processing: test data
    Results from AClass

    Output results (USE_A=False)

    BClass processing: test data
    Results from BClass

    More advanced way of writing: using factory function

    def get_class(name):
        mapping = {
            "A": AClass,
            "B": BClass
        }
        return mapping.get(name, AClass) # Default is AClass
    
    # Dynamic selection
    cls = get_class("B")
    obj = cls()
    obj.process("Test data")

    in conclusion



    Python uses abstract categories

    short answer

    In Python,Not necessarily requiredWritten like C++ or Javaabstract class. Python adopts "Duck Typing". As long as objects have the same method names and behaviors, they can be considered compatible.

    duck typing example

    class AClass:
        def process(self, data):
            print(f"AClass processing: {data}")
    
    class BClass:
        def process(self, data):
            print(f"BClass processing: {data}")
    
    def run(obj):
        obj.process("data") # No need to specify the type, as long as there is this method
    
    run(AClass())
    run(BClass())

    Output results

    AClass Processing: Data
    BClass Processing: Data

    Situations using abstract categories (ABC)

    Although Python does not enforce types, if you want to clearly define the interface in team development or large projects, you can useabcMods.

    from abc import ABC, abstractmethod
    
    class BaseClass(ABC):
        @abstractmethod
        def process(self, data):
            pass
    
    class AClass(BaseClass):
        def process(self, data):
            print(f"AClass processing: {data}")
    
    class BClass(BaseClass):
        def process(self, data):
            print(f"BClass processing: {data}")
    
    # BaseClass() will report an error because the abstract method is not implemented

    in conclusion



    Python modules

    definition

    In Python,Moduleis a file containing program code, usually with the file extension.pyThe end. Modules can define functions, categories, variables, and can also contain executable code to facilitate reuse and program structuring.

    use

    Usage

    Python viaimportKeyword to introduce the module, for example:

    import math
    
    print(math.sqrt(16)) # Output 4.0

    Custom modules

    Developers can create their own mods. For example, create amymodule.py

    
    # mymodule.py
    def greet(name):
        return f"Hello, {name}!"
    

    Use in another program file:

    
    import mymodule
    
    print(mymodule.greet("Alice"))
    

    Common built-in groups



    Python suite

    definition

    In Python,PackageIt is a way of organizing modules. A package is a directory containing multiple modules, which makes the program easier to manage and maintain through a hierarchical structure.

    feature

    basic example

    Create a file calledmypackageKit of:

    
    mypackage/
    │── __init__.py
    │── module1.py
    └── module2.py
    

    module1.pyexample:

    
    def add(a, b):
        return a + b
    

    module2.pyexample:

    
    def multiply(a, b):
        return a * b
    

    Use the kit

    import mypackage.module1
    import mypackage.module2
    
    print(mypackage.module1.add(2, 3)) # Output 5
    print(mypackage.module2.multiply(2, 3)) # Output 6

    Import specific projects from a package

    from mypackage.module1 import add
    from mypackage.module2 import multiply
    
    print(add(10, 5)) # Output 15
    print(multiply(10, 5)) # Output 50

    Common uses



    Get the current package name

    illustrate

    In Python, to get the current module'sPackage name, you can use special variables__package__. This and__module__The concept of getting the current module name is similar.

    Example: Module and Kit Structure

    
    mypackage/
    │── __init__.py
    └── submodule.py
    

    submodule.pycontent:

    
    print("__name__:", __name__)
    print("__package__:", __package__)
    print("__module__:", __module__)
    

    Execution result

    If used in other programs asimport mypackage.submoduleImport, the output is roughly as follows:

    
    __name__: mypackage.submodule
    __package__: mypackage
    __module__: __main__
    

    explain

    Application scenarios



    Python package version

    There are two main ways to check the Python package version: through the terminal (command line) or by running it in Python code. This can help you confirm whether the environment meets project requirements.


    1. Check via terminal (Command Line)

    This is the fastest method and does not require entering the Python interactive environment.


    2. Check in Python code

    If you need to determine the version when the program is executed, you can use the following two methods:


    3. Inspection method comparison table

    method Instructions/Code Applicable situations
    Pip command pip show View detailed information such as installation path, author, dependencies, etc.
    Pip list pip list Get a quick overview of all packages and versions in your current environment.
    internal properties .__version__ Make logical judgments while the script is running.
    Metadata version() Standardized way of checking without loading the entire suite.

    4. Check Python’s own version

    Sometimes the problem isn't with the suite, but with the Python interpreter itself:


    5. Frequently Asked Questions and Reminders



    Get package name from category and module object

    Class object

    In Python, class objects can be accessed viacls.__module__Find the name of the module that defines the category and then usesys.modulesGet the module object and finally read it__package__Property to get the package name.

    importsys
    
    # Assume a category is defined in mypackage.submodule
    classMyClass:
        pass
    
    # Get the name of the module to which the category belongs
    module_name = MyClass.__module__
    print("Module name:", module_name)
    
    # Get module object
    mod = sys.modules[module_name]
    
    # Get the package name from the module object
    print("Package name:", mod.__package__)

    Module object

    The module itself is an object and can be accessed directly__package__property.

    import math
    import mypackage.submodule as sub
    
    # math is a standard function library module, there is no package, so __package__ is an empty string
    print("math.__package__:", math.__package__)
    
    # Modules for custom kits
    print("sub.__package__:", sub.__package__)

    Example of execution result

    Module name: mypackage.submodule
    Package name: mypackage
    math.__package__:
    sub.__package__: mypackage

    in conclusion



    inspect.getfile

    When a Python file is executed directly, its module name is__main__. This makes using onlycls.__module__Unable to obtain original file name and path.

    Assume your project structure is:

    /project
      |-- test_runner.py <-- The file you execute directly (will be treated as __main__)
      |-- test/
            |-- db_test.py <-- File that defines DbCmdAgent

    If you execute the following code in `test_runner.py`:

    from test.db_test import DbCmdAgent
    agent_obj = DbCmdAgent(...) # Instantiation
    
    # At this time cls.__module__ is still 'test.db_test' (correct module name)

    However, if you define categories in `test_runner.py`:

    # Contents of test_runner.py
    class DbCmdAgent:
        pass
    
    agent_obj = DbCmdAgent()
    #At this time cls.__module__ == '__main__' (wrong module name)

    This indicates that the class you are getting information for is defined in a file that is executed as `__main__`.

    useinspectThe module can directly obtain the file path

    Regardless of whether the category is defined in `__main__`, you can bypass the `__module__` attribute and useinspectmodule to obtain the source code file path corresponding to this category. This is a more reliable and general method.

    Python corrected implementation

    import inspect
    import os
    
    # --- Simulation situation: categories defined in __main__ (currently executing script) ---
    
    class DbCmdAgent:
        """This category is defined in the currently executing main script"""
        def __init__(self, data):
            self.data = data
    
    agent_obj = DbCmdAgent("some_data")
    
    def get_class_location_robust(obj):
        """
        Use inspect.getfile to bypass the __module__ == '__main__' problem.
        """
        cls = type(obj)
        
        # 1. Use inspect.getfile() to get the file path that defines the category
        try:
            file_path = inspect.getfile(cls)
            
            # 2. Get the file name and directory
            file_name = os.path.basename(file_path)
            directory = os.path.dirname(file_path)
            py_name = os.path.splitext(file_name)[0]
            
            # 3. If __module__ is __main__, replace it with the archive name to provide more context
            module_name = cls.__module__
            if module_name == '__main__':
                module_name = py_name # Use db_test or test_runner as context
                
        exceptTypeError:
            # Handle built-in types
            file_path = "N/A (Built-in or C extension)"
            file_name = "N/A"
            directory = "N/A"
            py_name = "N/A"
            module_name = cls.__module__
            
        return {
            "module_name_or_main": module_name,
            "py_name_no_ext": py_name,
            "directory": directory,
            "file_path": file_path,
        }
    
    # Execute and view the results
    location_info = get_class_location_robust(agent_obj)
    
    print("--- Category definition file information ---")
    print(f"File name (.py name): {location_info['py_name_no_ext']}")
    print(f"Directory path (Package): {location_info['directory']}")
    print(f"Full file path: {location_info['file_path']}")

    4. Summary of key points



    Dynamic import module

    Function description

    This method usesimportlib.import_moduleTry to import the specified module. If you encounterModuleNotFoundError, will try to import its submodules from the currently loaded package.

    Program example

    import importlib
    importsys
    
    def safe_import(module_name):
        try:
            # Try to import directly
            return importlib.import_module(module_name)
        except ModuleNotFoundError:
            # If failed, try to import the submodule from a known package
            for pkg in list(sys.modules.keys()):
                if pkg and not pkg.startswith("_"):
                    try:
                        return importlib.import_module(f"{pkg}.{module_name}")
                    except ModuleNotFoundError:
                        continue
            raise # If still not found, throw an exception

    Usage examples

    import numpy
    
    mod1 = safe_import("random") # It can succeed directly because it is a standard library
    mod2 = safe_import("linalg") # Will try numpy.linalg
    print(mod2.__name__) # Output numpy.linalg

    illustrate



    Python module search path: composition of sys.path

    In Python,sys.pathis a list containing all directory paths that the Python interpreter will search in order when trying to import a module. When you executeimport some_module, Python will check in ordersys.patheach directory in the list until you find a directory namedsome_modulefiles (e.g.some_module.pysome_module/__init__.pywait).

    The three main components of sys.path

    sys.pathLists usually consist of the following three parts, and are searched in the following order:

    1. The entry directory (entry point) of the program code

    2. PYTHONPATH environment variable

    3. Standard function library and installation directory

    ---

    System variables related to sys.path

    In addition to the abovePYTHONPATHIn addition, there are several environment variables related to the Python execution environment, which affect the behavior of the interpreter and path finding, but affectsys.pathThe main variables that make up thePYTHONPATH

    System variables Function description Relationship to sys.path
    PYTHONPATH Defines additional directories to be added to the mod search path. direct impactsys.pathcomposition.
    PYTHONHOME Used to set an alternative path to the Python installation directory, especially for embedded systems. Indirectly affects the standard library andsite-packageslocation.
    PATH Used by the operating system to find executable files (e.g.python.exe) path. does not directly affectsys.path, but affects which Python interpreter is executed.
    VIRTUAL_ENV When you are in a virtual environment, this variable points to the root directory of the virtual environment. indirect impactsys.path, as it ensuressite-packagesIt comes from the virtual environment rather than the entire system.

    How to modify sys.path

    becausesys.pathis an ordinary Python list that you can dynamically modify while the program is running, but such modifications only take effect within the current interpreter session:

    importsys
    import os
    
    # Add the parent directory to the search path (commonly used for testing or internal project reference)
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


    Mod Shadow

    Module Shadowing (or Name Shadowing) is a common error or programming problem in Python. It occurs when you accidentally create a code file or variable name that conflicts with the name of a built-in module or third-party library that you are trying to import or use.

    1. Definition and causes of occurrence

    The result of this is that your local file "covers" or "overrides" the standard module that should be loaded, making your code unable to access standard module functionality (such assocket.AF_UNSPEC) will fail because the local file you loaded does not have these properties.

    2. Common examples

    Standard module name The local file name that caused the shadow as a result of
    socket socket.py Unable to use standardsocketThe network constants of the module (such asAF_INET, AI_PASSIVE)。
    json json.py Unable to use standardjsonModularloadsordumpsfunction.
    test(Project name) test.py In a unit testing environment, there may be conflicts with the internal logic of the testing framework.

    3. Solution

    1. Rename the archive: This is the simplest and most effective solution. Simply rename local files or directories that conflict with standard module or library names. For example, changesocket.pyrenamednetwork_handler.py
    2. Check sys.path: do this in your codeimport sys; print(sys.path)Check Python's search path to see if it loads your local files in preference to the standard library directory.
    3. Use a virtual environment: While a virtual environment by itself does not prevent local archive shadowing, it can ensure that third-party packages you install do not conflict with other environments.


    Get the current entry .py name

    illustrate

    To get the current execution of the Python program__main__The name of the .py file it belongs to, which can be used__main__.__file__orsys.argv[0]. But in interactive mode, Jupyter, or-cMay not exist at execution time and therefore require safe handling.

    Get the current main .py file name (safe version)

    import os
    importsys
    import __main__
    
    def get_main_py_path():
        """Returns the absolute path of .py to which main belongs. If it cannot be found, it returns None"""
    
        # Case 1: Normal execution .py
        main_file = getattr(__main__, "__file__", None)
        if main_file:
            return os.path.abspath(main_file)
    
        # Case 2: Judgment from sys.argv[0]
        if len(sys.argv) > 0:
            argv0 = sys.argv[0]
            if argv0 not in ("", "-c", ""):
                candidate = os.path.abspath(argv0)
                if os.path.exists(candidate):
                    return candidate
    
        # Case 3: Interactive mode, Jupyter, embed etc.
        return None
    
    # Example
    path = get_main_py_path()
    if path:
        print("main path:", path)
        print("main filename:", os.path.basename(path))
    else:
        print("main.py not found (maybe in interactive environment or not executed from archive)")

    Just the file name (basename)

    path = get_main_py_path()
    filename = os.path.basename(path) if path else None
    print(filename)

    Summarize



    Get the parameter names and values ​​of the current function

    To get the parameter names and their corresponding values of a function in Python, you can use the `inspect` module, which provides introspection utilities. Specifically, `inspect.signature()` can help you retrieve the names of the parameters, and you can pass the current frame's local variables to get their values.
    
    Here is an example that demonstrates how to get the function name, parameter names, and their values:
    
    ```python
    import inspect
    
    # Sample function
    def my_function(a, b, c=5):
        # Get the current frame
        frame = inspect.currentframe()
        
        # Get the function name
        func_name = frame.f_code.co_name
        print(f"Function name: {func_name}")
        
        # Get the parameter names and their values
        args, _, _, values = inspect.getargvalues(frame)
        
        # Print parameter names and values
        for arg in args:
            print(f"Parameter name: {arg}, Value: {values[arg]}")
    
    # Call the function
    my_function(1, 2)
    ```
    
    ### Output:
    ```
    Function name: my_function
    Parameter name: a, Value: 1
    Parameter name: b, Value: 2
    Parameter name: c, Value: 5
    ```
    
    ### Explanation:
    1. **`inspect.currentframe()`**: Retrieves the current execution frame.
    2. **`frame.f_code.co_name`**: Extracts the name of the current function.
    3. **`inspect.getargvalues(frame)`**: Gets the argument names and their corresponding values from the frame. This function returns a tuple containing:
       - `args`: List of argument names.
       - `_`: Placeholder for unused information.
       - `values`: Dictionary containing argument names as keys and their values.
    
    This allows you to print both the names of the function's parameters and their values at runtime.
    


    Get the type of function parameter

    Using the inspect module

    In Python, you can useinspect.signature()Get function parameter information, and further fromParameter.annotationThe property gets the type annotation (type hint) for each parameter.

    import inspect
    
    def my_function(a: int, b: str, c: float = 3.14) -> bool:
        return str(a) == b
    
    sig = inspect.signature(my_function)
    
    for name, param in sig.parameters.items():
        print(f"Parameter name: {name}")
        print(f"Default value: {param.default}")
        print(f"Type annotation: {param.annotation}")
        print()

    Output results

    Parameter name: a
      Default: <class 'inspect._empty'>
      Type annotation: <class 'int'>
    
    Parameter name: b
      Default: <class 'inspect._empty'>
      Type annotation: <class 'str'>
    
    Parameter name: c
      Default: 3.14
      Type annotation: <class 'float'>

    illustrate

    Use get_type_hints

    This method will automatically resolve forward references (types marked as strings).

    
    from typing import get_type_hints
    
    hints = get_type_hints(my_function)
    print(hints)
    

    Output results

    
    {'a': <class 'int'>, 'b': <class 'str'>, 'c': <class 'float'>, 'return': <class 'bool'>}
    

    Summarize



    Get the class name of the object

    illustrate

    In Python, you can use the object's__class__attribute ortype()function, Get the category (class) it belongs to, and then get the category name.

    example

    class Animal:
        pass
    
    class Dog(Animal):
        pass
    
    obj = Dog()
    
    #Method 1: Use __class__.__name__
    print(obj.__class__.__name__) # Output: Dog
    
    #Method 2: Use type()
    print(type(obj).__name__) # Output: Dog
    
    #Method 3: Get the complete module and category name
    print(obj.__class__) # Output: <class '__main__.Dog'>
    print(obj.__class__.__module__) # Output: __main__

    Output results

    
    Dog
    Dog
    <class '__main__.Dog'>
    __main__
    

    illustrate

    Advanced applications

    To get the complete "module + category name" at the same time, you can write:

    
    cls = type(obj)
    full_name = f"{cls.__module__}.{cls.__name__}"
    print(full_name)
    

    Output results

    
    __main__.Dog
    

    Summarize



    Detect the category to which the attribute belongs

    The following is sample code that uses Python to determine which inheritance class a property belongs to:

    Sample code

    import inspect
    
    class BaseClass:
        base_attr = "I am an attribute from BaseClass"
    
    class SubClass(BaseClass):
        sub_attr = "I am an attribute from SubClass"
    
    # Define a function to find the category to which an attribute belongs
    def find_attribute_owner(cls, attr_name):
        for base in inspect.getmro(cls): # Get MRO (method resolution order)
            if attr_name in base.__dict__:
                return base
        return None
    
    # test
    sub_obj = SubClass()
    attributes = sub_obj.__class__.__dict__.items() # Get all attributes at the category level
    for name, value in attributes:
        owner = find_attribute_owner(sub_obj.__class__, name)
        print(f"Attribute '{name}' belongs to category: {owner.__name__}")

    Program Description

    Execution result

    For the categories in the example, the execution results are as follows:

    Attribute '__module__' belongs to class: SubClass
    Attribute 'sub_attr' belongs to class: SubClass
    Attribute '__doc__' belongs to class: SubClass
    Attribute 'base_attr' belongs to class: BaseClass


    Get function annotation (Docstring)

    Use __doc__

    class MyClass:
        def fun1(self):
            '''
            Fun1 comment
            '''
            pass
    
    obj = MyClass()
    print(obj.fun1.__doc__)
    

    Using the inspect module

    import inspect
    
    class MyClass:
        def fun1(self):
            '''
            Fun1 comment
            '''
            pass
    
    print(inspect.getdoc(MyClass.fun1))
    


    Function marked as deprecated

    Using warnings module

    In Python, you can use the built-inwarningsModules that alert users at execution time (rather than compile time) that a function has been deprecated and suggest new alternatives.

    import warnings
    
    def old_function(x, y):
        warnings.warn(
            "Function old_function() is deprecated, please use new_function(x, y) instead.",
            category=DeprecationWarning,
            stacklevel=2
        )
        return x + y
    
    def new_function(x, y):
        return x + y

    Execution example

    
    result = old_function(3, 4)
    print(result)
    

    Output results

    DeprecationWarning: Function old_function() is deprecated, please use new_function(x, y) instead.
      result = old_function(3, 4)
    7

    Additional information

    Advanced: Create a decorator to automatically mark deprecated functions

    Decorators can be used to allow multiple old functions to share the same deprecation prompt logic:

    import warnings
    from functools import wraps
    
    def deprecated(new_func_name):
        def decorator(func):
            @wraps(func)
            def wrapper(*args, **kwargs):
                warnings.warn(
                    f"Function {func.__name__}() is deprecated, please use {new_func_name}() instead.",
                    category=DeprecationWarning,
                    stacklevel=2
                )
                return func(*args, **kwargs)
            return wrapper
        return decorator
    
    @deprecated("new_function")
    def old_function(x, y):
        return x + y


    The difference between @staticmethod and @classmethod

    In Python,@staticmethodand@classmethodBoth decorators can define methods that can be called without instantiating the class, but their purpose and behavior differ.

    @staticmethod

    @staticmethod example:

    classMyClass:
        @staticmethod
        def static_method(x, y):
            return x + y
    
    # Static methods can be called without creating an instance
    result = MyClass.static_method(5, 10) # Result: 15

    focus@staticmethodCategory cannot be accessed (cls) or instance (self)。

    @classmethod

    @classmethod example:

    classMyClass:
        class_variable = 0
    
        def __init__(self, value):
            self.value = value
            MyClass.class_variable += 1
    
        @classmethod
        def get_class_variable(cls):
            return cls.class_variable
    
    #Create instance
    obj1 = MyClass(10)
    obj2 = MyClass(20)
    
    # Call category method
    print(MyClass.get_class_variable()) # Result: 2

    focus@classmethodCan access category-level status (cls)。

    Summarize

    feature @staticmethod @classmethod
    first parameter No implicit first argument cls(category itself)
    access instance none none
    Access Category none have
    usage Utility functions that are related to categories but do not require instances or categories Need to manipulate category-level data or provide alternative constructors


    Static class performs initialization

    Python itself does not provide a "default static method" or a "default class method", that is, the function of automatically executing a method the first time any static or class method is called. But we can achieve similar behavior through lazy loading techniques.

    Solution: Use static variables and lazy loading

    You can define a static variable in the class to track the initialized state, and then execute the initialization logic when the static or class method is called for the first time.

    example:

    classMyClass:
        initialized = False # Static variable, tracking whether it has been initialized
    
        @staticmethod
        def init_once():
            if not MyClass.initialized:
                print("Initialization logic execution...")
                MyClass.initialized = True
    
        @classmethod
        def class_method(cls):
            cls.init_once()
            print("Call class method")
    
        @staticmethod
        def static_method():
            MyClass.init_once()
            print("Call static method")
    
    # Call the category method for the first time to trigger initialization
    MyClass.class_method() # Output: Initialization logic execution...Call class method
    
    # Call the category method for the second time and no longer perform initialization
    MyClass.class_method() # Output: Call class method
    
    # The first time the static method is called, no initialization is performed because it has already been initialized.
    MyClass.static_method() # Output: Call static method

    How it works:

    Summarize

    Although Python does not have a built-in "default static method" or "default class method", by using static variables and lazy loading techniques, you can automatically execute initialization logic when the static or class method is called for the first time, and ensure that this logic will only be executed once.



    Python thread

    In Python, thread is a mechanism used to implement concurrency. It allows a program to perform multiple tasks simultaneously within a single process (Process). This is very useful for performing I/O-intensive operations (such as network communication, file reading and writing), and can prevent the program from blocking (Blocking) waiting for external operations to complete.

    1. Why use threads?

    2. Python thread restrictions: GIL (Global Interpreter Lock)

    In the standard CPython interpreter, there is a "Global Interpreter Lock" (Global Interpreter Lock, GIL). The GIL ensures that only one thread can execute Python bitcode at any given time. This means:

    3. Execution thread module:threading

    Python uses the standard function librarythreadingModule to handle threads of execution. There are two main methods of thread creation:

    Method 1: Pass function as target (Target Function)

    This is the simplest and most common usage.

    import threading
    import time
    
    def task(name, delay):
        """Task function to be executed by the thread"""
        print(f"Thread {name}: Starting...")
        time.sleep(delay) # Simulate time-consuming I/O operations
        print(f"Thread {name}: Task completed.")
    
    #Create execution thread
    thread1 = threading.Thread(target=task, args=("T1", 2))
    thread2 = threading.Thread(target=task, args=("T2", 4))
    
    # Start the thread
    thread1.start()
    thread2.start()
    
    # Wait for all threads to complete (block the main thread until they finish)
    thread1.join()
    thread2.join()
    
    print("All execution threads have completed. The main program exits.")

    Method 2: Inheritancethreading.Threadcategory

    Suitable for more complex scenarios, encapsulating the logic of the execution thread in a category.

    import threading
    import time
    
    class MyThread(threading.Thread):
        def __init__(self, name, delay):
            super().__init__()
            self.name = name
            self.delay = delay
    
        def run(self):
            """
            When the thread starts, the run() method is automatically called.
            Here you define the tasks to be performed by the thread.
            """
            print(f"Thread {self.name}: Starting...")
            time.sleep(self.delay)
            print(f"Thread {self.name}: Task completed.")
    
    #Create and start the thread
    thread3 = MyThread("T3", 3)
    thread3.start()
    thread3.join()
    
    print("Custom execution thread has been completed.")

    4. Thread synchronization and data sharing

    When multiple threads access and modify shared data, race conditions may occur. You need to use a synchronization mechanism to protect your data:

    Using Lock Example

    import threading
    
    # Shared resources
    counter = 0
    # Create lock
    lock = threading.Lock()
    
    def increment_counter():
        global counter
        # Obtain the lock to ensure that only one thread can execute this block at the same time
        lock.acquire()
        try:
            # Competition section
            current_value = counter
            time.sleep(0.001) # Simulate switching
            counter = current_value + 1
        finally:
            # Release lock
            lock.release()
    
    threads = []
    for i in range(100):
        t = threading.Thread(target=increment_counter)
        threads.append(t)
        t.start()
    
    for t in threads:
        t.join()
    
    print(f"Final counter value: {counter}") # If there is no lock, this value may not be 100


    Starting, synchronizing and stopping Python threads

    PythonthreadingThe module provides the function of creating and managing execution threads, but due to operating system limitations and design philosophy, Python does not provide a safe, direct, and forced method to stop (Kill) external execution threads. Forced stopping may result in resource leakage or data corruption.

    Therefore, stopping the execution thread must be achieved through **Cooperative Mechanism****, that is, letting the execution thread check a stop flag by itself and exit gracefully.

    1. Thread stopping mechanism: using flags (Flag)

    This is the safest and most recommended method of thread stopping. It requires the thread to periodically check an external variable (flag) in the loop of executing the task.

    Python implementation example

    import threading
    import time
    
    # Shared stop flag
    stop_flag = threading.Event()
    
    def monitored_task(name, delay):
        """
        A task function that periodically checks the stop flag
        """
        print(f"Thread {name}: Starting...")
        i = 0
        while not stop_flag.is_set(): # Check whether the flag is set
            i += 1
            print(f"Thread {name}: Execution step {i}")
            
            # Simulate time-consuming operations and check regularly
            time.sleep(delay)
            
            # Here you can set a limit on the number of executions to ensure that there is no infinite loop
            if i >= 5:
                break
                
        print(f"Execution thread {name}: received a stop signal or the task ended, and exited gracefully.")
    
    # --- Main program control block ---
    
    #Create execution thread
    worker_thread = threading.Thread(target=monitored_task, args=("Worker-1", 1))
    
    # Start the thread
    worker_thread.start()
    
    print("\nMain program: The execution thread has been started, wait 3 seconds...\n")
    time.sleep(3) # Let the thread run for a while
    
    # Send stop signal
    print("\nMain program: Set stop flag...\n")
    stop_flag.set() # Set Event and let is_set() return True
    
    # Wait for the thread to finish gracefully and exit (usually quickly)
    worker_thread.join()
    
    print("\nMain program: The thread has been safely stopped and joined. The program exited.")

    2. Description of key components

    3. Other stopping mechanisms (forced stopping is not recommended)

    Although there are some experimental or unsafe ways to force a stop, such as using `_thread.stop()` or throwing an exception, these methods can cause:

    Therefore, in Python, you should always stick to using a coordinated flagging mechanism to stop threads.



    Solution to multi-thread shared object conflict

    1. The most commonly used solution: Thread-Local Storage

    This is the most recommended approach in multi-threaded environments. Instead of having all threads grab the same object, each thread has an independent copy of the object. In Python, you can usethreading.local()to achieve.

    import threading
    
    # Create a thread area to store objects
    thread_data = threading.local()
    
    def get_service():
        # If the current thread does not have its own service, create one
        if not hasattr(thread_data, 'service'):
            print(f"Create a new connection for thread {threading.current_thread().name}")
            thread_data.service = create_new_connection()
        return thread_data.service
    
    def task():
        service = get_service()
        # Perform operations...

    2. The second most commonly used method: locking mechanism (Locking)

    If the object must be the same (such as writing to the same file or operating on the same global counter), you must useLock. This ensures that only one thread can access the object at the same time, avoiding race conditions.

    lock = threading.Lock()
    
    def safe_task():
        with lock:
            # Within this block, other threads must wait
            shared_object.do_something()
    ---

    Alternatives to Multithreading: Asynchronous and Multiprocessing

    If you want to avoid the risk of lock contention or crashes that come with multiple threads, there are two main alternatives to consider:

    1. Asynchronous coroutines (Asyncio) - suitable for I/O-intensive tasks

    This is currently the most popular approach in Python (such as the core principle of FastAPI). It runs within a single thread and waits for I/O (such as API requests, database queries) by switching tasks.

    import asyncio
    
    async def fetch_api(url):
        # Use asynchronous libraries such as aiohttp
        response = await call_api(url)
        return response
    
    async def main():
        # Execute multiple tasks at the same time, but switch within a single thread
        results = await asyncio.gather(fetch_api("url1"), fetch_api("url2"))

    2. Multiprocessing - suitable for computationally intensive tasks

    Python's execution thread is limited by GIL (Global Interpreter Lock) and cannot truly parallelize operations.multiprocessingMultiple independent Python interpreter instances will be opened.

    from multiprocessing import Process
    
    def task(name):
        print(f"Process {name} is executing")
    
    if __name__ == "__main__":
        p = Process(target=task, args=('A',))
        p.start()
        p.join()

    3. Task Queue - suitable for distributed processing

    If you want to completely decouple tasks, you can use Celery or Redis Queue. Throw the task into the queue and let the back-end Worker (which may be multiple processes or multiple machines) pick up and execute it.

    ---

    Summary suggestions

    plan Solution Applicable situations
    Thread-Local Each thread gets a copy API Service, database connection
    Asyncio Single thread switching (non-synchronous) High concurrent network requests (recommended)
    Multiprocessing independent memory space CPU computing, completely avoiding sharing conflicts


    Balance between thread area storage and information sharing

    core concepts

    When you use Thread-Local Storage (TLS), the purpose is to protect those "non-thread-safe" objects (such as API Service, database connections). But if data needs to be exchanged between threads (for example, the results downloaded by thread A need to be processed by thread B), you need to establish a special "communication channel".

    1. Use thread-safe queues (Queue) - most recommended

    Pythonqueue.QueueIs thread safe. This is the most standard and safest way to transfer information between threads. It has already processed all lock logic internally.

    import threading
    import queue
    
    # Create a global queue that all execution threads can access
    task_queue = queue.Queue()
    
    def producer():
        # Produce data and put them in the queue
        data = {"video_id": "abc", "status": "pending"}
        task_queue.put(data)
    
    def consumer():
        # Get data from the queue
        data = task_queue.get()
        # Process data...
        task_queue.task_done()

    2. Use thread security variables (such as shared list locking)

    If you need to share a large list or dictionary, you can use general global variables, but they must be accessed with matchingthreading.Lock

    shared_results = []
    results_lock = threading.Lock()
    
    def task():
        result = "Some operation results"
        
        #Lock before accessing shared resources
        with results_lock:
            shared_results.append(result)
        # Automatically unlock after leaving the with block

    3. Use Event or Condition objects (signal synchronization)

    Sometimes you don't want to share "data" but "state" (for example: telling other threads that the API has been initialized).

    api_ready = threading.Event()
    
    def initializer():
        #Perform initialization
        api_ready.set() # Send signal
    
    def worker():
        api_ready.wait() # Wait for the signal until the initializer calls set()
        print("Start working")

    Summary: Regional storage vs shared information

    Content type Storage location management style
    Tool objects(API, DB connection) Thread-Local (region) Each has a copy to avoid crashes.
    Mission information(ID, parameter) Queue (global) Use thread-safe queue passing.
    Calculation result(statistics) Global List/Dict (global) Must matchthreading.Lock

    To put it simply: **"Get the private tools (connections) by yourself, and get the public information (data) in queue (Queue/Lock)."**



    thread lock

    Basic concepts

    In a multi-thread environment, when multiple threads try to modify the same global variable or shared resource (such as a file, database connection, global list) at the same time, an error occurs.Race Condition, leading to data confusion.threading.LockIs a synchronization primitive that ensures that only one thread can enter a protected block of code at a time.

    1. Standard usage

    The safest and recommended way is to matchwithUse narrative sentences. This ensures that even if an exception occurs within the block, the lock will be released correctly to avoid deadlock.

    import threading
    
    # 1. Create a lock object
    my_lock = threading.Lock()
    shared_counter = 0
    
    def increment_task():
        global shared_counter
        # 2. Use with to automatically manage acquire() and release()
        with my_lock:
            # The code in this block can only be executed by one thread at a time
            temp = shared_counter
            temp += 1
            shared_counter = temp
    
    # Start multiple thread tests
    threads = [threading.Thread(target=increment_task) for _ in range(100)]
    for t in threads: t.start()
    for t in threads: t.join()
    
    print(f"Final count: {shared_counter}")

    2. Manual control method

    Although not recommended, sometimes finer control is needed. You must manually callacquire()acquire the lock andfinallycall in blockrelease()

    lock = threading.Lock()
    
    def manual_task():
        lock.acquire() #Acquire the lock. If the lock is already occupied, it will block (wait) here.
        try:
            # Execute tasks
            pass
        finally:
            lock.release() # Must be released, otherwise other threads will never be able to execute

    3. Lock characteristics: non-reentrancy

    threading.LockIt is not reentrant. This means that if the same thread requests the same lock again when it already holds the lock, it will "lock" itself (deadlock).

    4. When should you use Lock?

    Performance considerations

    Excessive use of locks will cause program performance to decrease because multiple threads will become "queued for execution." If possible, give priority to usingqueue.Queueor what we discussed earlierThread-Local Storage, these methods are usually more efficient and less error-prone than frequent locking.



    Python asynchronous programming

    In Python,async defandawaitis realizedAsynchronous Programmingcore syntax. They prevent the program from getting stuck while waiting for I/O tasks (such as network requests, reading files) and can instead handle other tasks, greatly improving performance.


    1. async def: define coroutine function

    When you prepend a function definition withasync, the function will become aCoroutine Function. When you call it, it will not execute the content immediately, but will return a "coroutine object".

    async def fetch_data():
        print("Start fetching data...")
        # Simulate time-consuming tasks
        return {"data": "success"}
    
    # A direct call will only get the coroutine object and will not execute print
    result = fetch_data()
    print(result) # Output: <coroutine object fetch_data at ...>

    2. await: suspend and wait

    awaitcan only beasync defFor internal use. Its function is to "temporarily suspend the current coroutine, wait for the subsequent tasks to complete, and obtain the return value." While waiting, the system can perform other asynchronous tasks.

    import asyncio
    
    async def main():
        # Use await to execute the coroutine and get the results
        data = await fetch_data()
        print(f"Capture results: {data}")
    
    # Start the entry for asynchronous programs
    asyncio.run(main())

    3. Key comparison table

    grammar Function description Things to note
    async def Declare an asynchronous function What is returned is the coroutine object, not the execution result.
    await Wait for asynchronous tasks to complete Can only be written inside async functions.
    asyncio.run() Start the outermost asynchronous entry A program usually only needs to be called once.

    4. Common errors and corrections


    5. Why use asynchronous?

    Imagine you are cooking:



    Complete the coroutine and return the result

    loop.run_until_complete()yesasyncioThe lower-level method in the module is used to execute the coroutine until it is completed and return the result. After Python 3.7, although it is officially recommended to useasyncio.run(), but you still need to use this method in some specific situations (such as when you need to reuse event loops or customize startup logic).

    1. Basic usage process

    userun_until_completeYou must first obtain or create an event loop object, and then pass the coroutine to it.

    import asyncio
    
    async def my_task():
        await asyncio.sleep(1)
        return "Task completed"
    
    # 1. Get event loop
    loop = asyncio.get_event_loop()
    
    # 2. Execute the coroutine until completion and obtain the return value directly
    result = loop.run_until_complete(my_task())
    
    print(result) #Output: Task completed

    2. Differences from asyncio.run()

    Both can get the return value, but manage the life cycle differently:

    characteristic asyncio.run() (recommended) loop.run_until_complete()
    degree of automation high. Automatically create, close loops and clean up tasks. Low. The life cycle of the loop needs to be managed manually.
    reusability Low. Each call creates a new loop. high. Multiple tasks can be executed in the same loop.
    Usage restrictions Cannot be used within an already running loop. More flexible and often used in legacy code or testing environments.

    3. Get results in an existing loop

    If you are in a script that is already running and want to ensure that a coroutine is executed and the value is obtained, you can use this method:

    import asyncio
    
    async def add(a, b):
        return a + b
    
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    
    try:
        # Execute the first task
        val1 = loop.run_until_complete(add(10, 20))
        #Execute the second task
        val2 = loop.run_until_complete(add(val1, 5))
        print(f"Final result: {val2}")
    finally:
        # Must be closed manually
        loop.close()

    4. Get the results of multiple tasks

    If you want multiple coroutines to execute in parallel and retrieve the return value uniformly, you need to matchasyncio.gather

    async def task(id):
        return f"result {id}"
    
    loop = asyncio.get_event_loop()
    # gather will wrap multiple coroutines into one task, and run_until_complete will wait for all to be completed.
    results = loop.run_until_complete(asyncio.gather(task(1), task(2), task(3)))
    print(results) #Output: ['Result 1', 'Result 2', 'Result 3']

    5. Common precautions



    asyncio.run()It is a high-level API introduced since Python 3.7. It is the most recommended way to start asynchronous programs. it will automatically take care ofEstablish an event loop, execute the coroutine, and close the loop after completion. The most important thing is that it will be passed directly back to the coroutinereturnvalue.

    1. Basic acquisition methods

    You just need toasync defThe function call passed toasyncio.run(), it will return the result like a normal synchronous function.

    import asyncio
    
    async def calculate_score(name):
        print(f"Calculating the score of {name}...")
        await asyncio.sleep(1) # Simulate time-consuming tasks
        return 95
    
    # Get the result of return directly
    final_score = asyncio.run(calculate_score("Zhang Xiaoming"))
    
    print(f"The final score is: {final_score}") # Output: 95

    2. Process the results of multiple tasks

    Usually we will define amain()function as the entry point, and obtain the results of all subtasks inside, and finally byasyncio.run(main())Unified output.

    async def task_a():
        return "apple"
    
    async def task_b():
        return "banana"
    
    async def main():
        # Use gather in main to execute simultaneously
        results = await asyncio.gather(task_a(), task_b())
        return results # Return a list
    
    # Get the return value of main through asyncio.run
    all_fruits = asyncio.run(main())
    print(all_fruits) # Output: ['apple', 'banana']

    3. Execution rules of asyncio.run()

    Rule items illustrate
    single entrance In an execution continuation, usually only called onceasyncio.run()
    Automatic cleaning It automatically cancels all remaining tasks and closes the thread pool, which is very safe.
    Nesting limit cannotis alreadyasync deffunction internal callasyncio.run()

    4. Common errors: RuntimeError

    If you are inside an asynchronous function trying to get the results of another asynchronous function, useawait, instead ofasyncio.run()

    # Error demonstration
    async def sub_task():
        return 10
    
    async def main():
        # An error will occur here: RuntimeError: asyncio.run() cannot be called from a running event loop
        res = asyncio.run(sub_task())
        
    # Correct correction
    async def main():
        res = await sub_task() # Please use await in an asynchronous environment

    5. Practical suggestions



    Python data analysis

    Python scientific operations NumPy

    NumPy(Numerical Python) is the most important scientific computing library in Python. It provides efficient multi-dimensional array objectsndarray, and a large library of mathematical functions for manipulating these arrays. It is the underlying pillar of data science, machine learning (such as Pandas, Scikit-learn, TensorFlow) and other fields.


    1. Why choose NumPy instead of native List?


    2. Core object: ndarray basic operations

    import numpy as np
    
    # Create one-dimensional and two-dimensional arrays
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([[1, 2], [3, 4]])
    
    # Quickly create a specific array
    zeros = np.zeros((3, 3)) # 3x3 matrix with all zeros
    ones = np.ones((2, 4)) # 2x4 matrix with all 1’s
    eye = np.eye(3) # 3x3 identity matrix
    range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]

    3. Commonly used array operations and attributes

    Function Code example illustrate
    Shape check arr.shape Return the size of each dimension (such as (3, 2)).
    change shape arr.reshape(1, 6) Change dimensions without changing the data.
    Matrix multiplication np.dot(a, b)ora @ b Perform matrix multiplication in linear algebra.
    statistical function np.mean(), np.std() Calculate the mean, standard deviation, maximum and minimum values.

    4. Slicing & Indexing

    NumPy's slicing syntax is similar to Python List, but more powerful and supports multi-dimensional simultaneous slicing:

    arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    
    # Get the second column (index 1)
    print(arr[1, :]) # [4, 5, 6]
    
    # Get the 2x2 sub-matrix in the lower right corner
    print(arr[1:, 1:]) # [[5, 6], [8, 9]]

    5. Installation and version checking


    6. Use with Numba

    As mentioned earlier,NumbaIt can perfectly recognize the array structure of NumPy and further compile complex NumPy operations into machine code, reaching the execution limit close to the native C language.



    Python efficiently processes large amounts of data Numba

    NumbaIt is an open source JIT (Just-In-Time) compiler specifically designed to accelerate Python code that processes large amounts of data. It can translate Python functions into machine code with execution speed comparable to C, C++ or Fortran, and is especially suitable forNumPyArray operations.


    1. Core functions and advantages


    2. Basic usage examples

    The easiest way to use Numba is to add@jitor@njitDecorator.

    from numba import njit
    import numpy as np
    
    # @njit represents "nopython" mode, which ensures not to enter the Python parser and is the fastest
    @njit
    def fast_function(n):
        total=0
        for i in range(n):
            total += i
        return total
    
    # The first call will compile, the second call will directly execute the machine code
    print(fast_function(10000000))

    3. Pattern comparison: nopython vs object

    model Decorator illustrate
    nopython mode @njit Recommended.Completely separated from the Python interpreter and directly compiled into machine code. If the program code contains parts that cannot be compiled, an error will be reported.
    object mode @jit If it cannot be compiled, it will fall back to the Python parser for execution. Performance improvements are usually limited.

    4. Commonly used techniques: parallel operations

    To take advantage of your CPU's multi-core capabilities, simply turn onparallel=Trueand useprange

    from numba import njit, prange
    
    @njit(parallel=True)
    def parallel_sum(A):
        s = 0
        # prange will automatically allocate loops to different CPU cores
        for i in prange(A.shape[0]):
            s += A[i]
        return s

    5. Installation and dependency checking


    6. Usage restrictions



    Pandas data analysis tool

    What is pandas?

    Pandas is a Python-based data analysis and manipulation tool designed for processing structured data, such as tabular data or time series data.

    Pandas core data structures

    Main features of Pandas

    Usage examples

    import pandas as pd
    
    # Create DataFrame
    data = {'Name': ['Alice', 'Bob', 'Charlie'],
            'Age': [25, 30, 35],
            'City': ['Taipei', 'Taichung', 'Kaohsiung']}
    df = pd.DataFrame(data)
    
    # View data
    print(df)
    
    # Filter information older than 28
    filtered_df = df[df['age'] > 28]
    print(filtered_df)

    Applicable scenarios

    Why choose Pandas?

    Pandas provides efficient, flexible and intuitive operation methods, which is particularly suitable for data analysis and processing. It is one of the indispensable tools in data science and machine learning.

    in conclusion

    Pandas is a powerful data analysis tool, and both beginners and advanced users can benefit from its easy-to-use design and extensive functionality.



    Python Googletrans

    Install Googletrans

    First you need to installgoogletranskit. Enter the following command at the command line:

    pip install googletrans==4.0.0-rc1

    Note: Please confirm the version when installing4.0.0-rc1, as older versions may no longer work.

    Basic usage examples

    Here is an example of translating English into Traditional Chinese:

    from googletrans import Translator
    
    #Initialize the Translator object
    translator = Translator()
    
    # Translate text
    text = "Hello, how are you?"
    result = translator.translate(text, src="en", dest="zh-tw")
    
    # Output translation results
    print("original text:", text)
    print("Translation:", result.text)

    Supported language codes

    You can translate multiple languages, here are the common language codes:

    Things to note

    Googletrans is an unofficial Google Translate API and may stop working due to changes on Google's end. If you find that the translation function is not working, please consider using other translation APIs, such as Google's official Cloud Translation API.



    Python other translation kits

    DeepL Translator

    DeepL provides high-accuracy translation services, but requires an API key to use its developer API.

    Microsoft Translator

    The translation tool provided by Microsoft supports multi-language translation, but requires the use of Azure API key settings.

    Amazon Translate

    The translation service provided by Amazon Web Services (AWS) provides efficient translation of multilingual texts and needs to be accessed through the API key provided by AWS.

    LibreTranslate

    LibreTranslate is an open source translation tool that can set up its own server and does not require an API key. Some third-party public servers also offer the option of using them without an API key.

    TextBlob

    TextBlob is a natural language processing-based tool with built-in Google Translate functionality. However, older versions do not require an API key and may need to pay attention to version support.

    MyMemory

    MyMemory provides memory-based translation. Some functions do not require an API key, but advanced use may require application.

    in conclusion

    Among Googletrans' competitors, LibreTranslate and some versions of TextBlob offer options that don't require an API key. If you need a tool that is completely free and requires no additional setup, consider these options.



    OpenCC Chinese conversion

    OpenCC(Open Chinese Convert) is an open source project dedicated to the conversion of Simplified Chinese to Traditional Chinese. It is not just a simple word-to-word conversion, but more importantly, it handlesvocabulary levelconversion and differences in word usage habits in different regions (Mainland China, Taiwan, Hong Kong).


    1. Core advantages of OpenCC