【问题标题】:Python: How do I compare two sets containing strings and replace one value with the other if they are not equal?Python:如何比较两个包含字符串的集合,如果它们不相等,则用另一个替换一个值?
【发布时间】:2020-02-18 13:09:23
【问题描述】:

我正在编写一个 python 脚本来创建/更新/删除 AWS SSM 参数存储中的值。

我现在的逻辑是:

def lambda_handler(event, context):
    logger.info(event)#removing tab
    files = get_local_files() #set of all application.properties files
    count = 0
    for file in files: #iterate in each application.properties file
        remote_key_vals = get_ssm_paramstore_values(file) #remote_key_vals is a set of keys from ssm
        local_key_vals = read_ssm_local_file(file) #local_key_vals is a set of keys from local file
        if(len(remote_key_vals)==0):
            create_ssm_paramstore(file)
            count=count+1
        elif(local_key_vals>remote_key_vals):
            diff = local_key_vals-remote_key_vals
            if(len(diff)>0):
                update_ssm_paramstore(file, diff)
                count=count+1
        elif(remote_key_vals>local_key_vals):
            diff=remote_key_vals - local_key_vals
            if(len(diff)>0):
                delete_ssm_paramstore(file, diff)
                count = count+1 
        else:
            logger.info(file+' already exist')
        logger.info(local_key_vals)
    try:
        req = requests.put(event['ResponseURL'],
                          data=getResponse(event, context,
                                            responseStatus))
        if req.status_code != 200:
            logger.info(req.text)
            raise Exception('Received non 200 response while sending response to CFN.'
                            )
    except requests.exceptions.RequestException, e:
        logger.info(e)
        raise
    return

此代码仅适用于 3 个条件:

  • 当 SSM 参数存储为空时,如果 remote_key_vals 集的长度为 0(因为不存在 ssm 参数),则将创建/加载本地 application.properties 文件。
  • 当 local_key_vals 中的值多于 remote_key_vals 时。我计算了两组的差异并更新了参数存储(application.properties 文件)中的差异
  • 如果本地 application.properties 文件不再具有该键/值,则从 SSM 参数存储中删除该值。

我需要处理两个集合大小相同的情况,本地 application.properties 文件中的键具有更新的名称,远程 ssm 需要使用此名称更新。

例如: 本地学生/application.properties 的密钥集:set(['APPLICATION_NAME_TEST', 'VERSION', 'S3_BUCKET'])

SSM 参数存储的密钥集(student/application.properties):set(['APPLICATION_NAME', 'VERSION', 'S3_BUCKET'])

如何将 SSM 参数存储中的 APPLICATION_NAME 键更新为 APPLICATION_NAME_TEST 键(存在于本地文件中)?

【问题讨论】:

  • 如果数据集很小,并且您总是想将本地“推送”到远程,那么您最好总是进行覆盖并丢弃所有比较逻辑? set 也没有顺序,您的示例显示了一个包含单个列表的集合 - 它是什么?
  • 问题是覆盖将清除分配给键的任何值,并且必须在 SSM 中手动添加这些值。因此,为了避免在每次部署后手动为键添加值,编写了比较逻辑。这是一个包含单个列表的集合,顺序无关紧要

标签: python-3.x set ssm


【解决方案1】:

这个解决方案对我有用。

local_key_set = set(['APPLICATION_NAME_TEST', 'VERSION']);
remote_key_set= set(['APPLICATION_NAME', 'VERSION']);

local_key_list = list(local_key_set);
remote_key_list= list(local_key_set);

for i in range(0,len(local_key_list)):
    if local_key_list[i] != remote_key_list[i]:
        remote_key_list[i] = local_key_list[i]

remote_key_set = set(remote_key_list);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多