跳转至

Record Repository

controller.infrastructure.persistence.record_repository

记录数据持久化仓库

RecordRepository

RecordRepository(file_path: str = 'teach_record.json')

示教记录持久化仓库 - Infrastructure层。

职责: - 从JSON文件加载记录数据 - 保存记录数据到JSON文件 - 管理记录文件的读写

属性:

名称 类型 描述
file_path str

记录文件路径。

初始化记录仓库。

参数:

名称 类型 描述 默认
file_path str

记录文件路径. Defaults to "teach_record.json".

'teach_record.json'
源代码位于: src/controller/controller/infrastructure/persistence/record_repository.py
21
22
23
24
25
26
27
def __init__(self, file_path: str = "teach_record.json"):
    """初始化记录仓库。

    Args:
        file_path (str, optional): 记录文件路径. Defaults to "teach_record.json".
    """
    self.file_path = file_path

load_records

load_records() -> Dict[str, List[List[float]]]

从文件加载记录数据。

返回:

类型 描述
Dict[str, List[List[float]]]

Dict[str, List[List[float]]]: 记录数据字典 {记录名: 角度列表}。

源代码位于: src/controller/controller/infrastructure/persistence/record_repository.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def load_records(self) -> Dict[str, List[List[float]]]:
    """从文件加载记录数据。

    Returns:
        Dict[str, List[List[float]]]: 记录数据字典 {记录名: 角度列表}。
    """
    try:
        if os.path.exists(self.file_path):
            with open(self.file_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
            return data
        else:
            return {}
    except Exception as e:
        return {}

save_records

save_records(records: Dict[str, List[List[float]]]) -> bool

保存记录数据到文件。

参数:

名称 类型 描述 默认
records Dict[str, List[List[float]]]

记录数据字典。

必需

返回:

名称 类型 描述
bool bool

是否保存成功。

源代码位于: src/controller/controller/infrastructure/persistence/record_repository.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def save_records(self, records: Dict[str, List[List[float]]]) -> bool:
    """保存记录数据到文件。

    Args:
        records (Dict[str, List[List[float]]]): 记录数据字典。

    Returns:
        bool: 是否保存成功。
    """
    try:
        with open(self.file_path, 'w', encoding='utf-8') as f:
            json.dump(records, f, ensure_ascii=False, indent=2)
        return True
    except Exception as e:
        return False

delete_file

delete_file() -> bool

删除记录文件。

返回:

名称 类型 描述
bool bool

是否删除成功。

源代码位于: src/controller/controller/infrastructure/persistence/record_repository.py
61
62
63
64
65
66
67
68
69
70
71
72
73
def delete_file(self) -> bool:
    """删除记录文件。

    Returns:
        bool: 是否删除成功。
    """
    try:
        if os.path.exists(self.file_path):
            os.remove(self.file_path)
            return True
        return False
    except Exception as e:
        return False