首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较yaml中几种备忘录的内容?

如何比较yaml中几种备忘录的内容?
EN

Stack Overflow用户
提问于 2022-06-23 18:58:53
回答 1查看 36关注 0票数 1

我必须从yaml文件中检查以下几个条件:

代码语言:javascript
复制
provider: sockets
delayed_free_threads: 0
ATL_threads: 0
ATL_queue_size: 1000
ATL_data_size: 1024
Memservers:
  0:
    memory_type: volatile
    fam_path: /dev/shm/vol_path
    rpc_interface: fam5:8793
    libfabric_port: 7500
    if_device: eth0
  1:
    memory_type: volatile
    fam_path: /dev/shm/vol_path
    rpc_interface: fam4:8793
    libfabric_port: 7500
    if_device: eth1
  2:
    memory_type: volatile
    fam_path: /dev/shm/vol_path
    rpc_interface: fam3:8793
    libfabric_port: 7500
    if_device: eth1

下面是我要检查的条件:1.如果rpc_interface相同的话。2.如果rpc_interface是相同的,那么fam_path是一样的吗?3.如果rpc_interface是相同的,libabric_port是一样的吗?

下面是我尝试过的代码:

代码语言:javascript
复制
def compare():
    host_port = {}
    fam_path_list = {}
    libabric_port_list ={}
    file_in = Path(r'C:\Users\User\OneDrive\Desktop\toold\fam_memoryserver_config.yaml')
    yaml = ruamel.yaml.YAML(typ='safe')  
    data = yaml.load(file_in)
    print(data.values())
    for machine, config in data.values():
            if 'rpc_interface' not in config: 
                print(f'machine {machine} has no "rpc_interface"');
                sys.exit(1)
            else:
                host_port.setdefault(con['rpc_interface'], set()).add(machine)
                fam_path_list.setdefault(config['fam_path'], set()).add(machine)
                libabric_port_list.setdefault(config['libfabric_port'], set()).add(machine)
    # check if host_port has any values that have more than one machine
    for hp, machine_nrs in host_port.items():
        if len(machine_nrs) == 1:
            continue
        j=[str(x) for x in machine_nrs]
        print(f'Found {hp} in machines: {", ".join(j)}')
   
    for hp, machine_nrs in fam_path_list.items():
        if len(machine_nrs) == 1:
            continue
        k=[str(x) for x in machine_nrs]
        print(f'Found {hp} in machines: {", ".join(k)}')

    for hp, machine_nrs in libabric_port_list.items():
        if len(machine_nrs) == 1:
           continue
        l=[str(x) for x in machine_nrs]
        print(f'Found {hp} in machines: {", ".join([str(x) for x in machine_nrs])}')

    def removeElements(j, k):
        return ', '.join(map(str, j)) in ', '.join(map(str, k))

    if (removeElements(j, k)==True) :
        print(f'The memory servers {j}, have the same fam_path, hence Validation failed!')
    else:
        print('Only the rpc_interface of the machines are same, hence Validation failed!')
 

    def removeElements(j, l):
        return ', '.join(map(str, j)) in ', '.join(map(str, l))

    if (removeElements(j, l)==True) :
        print(f'The memory servers {j}, have the same libfabric_port, hence Validation failed!')
        
    else:
        print('Only the rpc_interface of the machines are same, hence Validation failed!')

compare()

但我所犯的错误是:

代码语言:javascript
复制
for machine, config in data.values():
ValueError: too many values to unpack (expected 2)

请告诉我怎么改正

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-23 19:16:11

您的data是一个dict (从YAML中的根级映射加载)。如果在data.values()上迭代,则只得到值,即sockets0等。you不能将该值分配给两个变量。

由于您正在rpc_interface上进行测试,所以您可能希望这样做:

代码语言:javascript
复制
for machine, config in data['Memservers'].items():
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72735319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档