#!/usr/bin/env python # -*- coding: utf-8 -*- ''' # @Time : 2025/2/18 10:02 # @Author : hjw # @File : tool.py ''' import time def parse_config(file_path): config = {} current_level = [config] # 使用栈来跟踪当前层级的字典 indent_stack = [] # 记录缩进层级 with open(file_path, 'r', encoding='utf-8') as file: for line in file: line = line.rstrip() # 去掉行尾的换行符和空格 if not line or line.startswith('#'): # 跳过空行和注释 continue # 检测缩进层级 indent = len(line) - len(line.lstrip()) while indent_stack and indent <= indent_stack[-1]: current_level.pop() indent_stack.pop() key, value = line.split(':', 1) # 分割键和值 key = key.strip() value = value.strip() # 处理值 if value.startswith('[') and value.endswith(']'): # 列表 value = [int(x.strip()) for x in value[1:-1].split(',')] elif value.isdigit(): # 数字 value = int(value) elif value.replace('.', '', 1).isdigit(): # 浮点数 value = float(value) elif value.lower() in ('true', 'false'): # 布尔值 value = value.lower() == 'true' # 插入到当前层级的字典中 if value: # 如果有值,则直接赋值 current_level[-1][key] = value else: # 如果没有值,则创建一个新字典 new_dict = {} current_level[-1][key] = new_dict current_level.append(new_dict) indent_stack.append(indent) return config def precise_delay_us(us): """实现微秒级延时""" start_time = time.perf_counter() #print(time.perf_counter()) while (time.perf_counter() - start_time) * 1e6 < us: pass