How to loop extract element from dynamodb recursively

家住魔仙堡 提交于 2020-12-15 01:41:06

问题


1 I am trying to extract element from dynamdb recurselvely

2 I have dynamodb table with below attributes

`id, masterId, masterName`

3 I need to go to each id in the dynamodb check whether 'masterId' != '' and 'masterName' != 'Reserved'

4 if 3rd one got success then I need to extract ``'masterId' and 'masterName' tototallist `

  1. ** Now pass masterId to same function as id this will keep on running till 'masterId' == '' and 'masterName' == 'Reserved'
  2. else: add to newlist_dict

Basically for the incoming id check the masterID is null or not if not null put into totallist else put in to newlist_dict

Below is the Code

def recursion(singledict, table_name):
    totallist = []
    newlist_dict = {}
    if singledict.get('masterId') != '' and singledict.get('masterName') != 'Reserved':
        singledict_response = client.get_item(
            TableName=table_name,
            Key={
                'id': {'S': singledict['masterId']
                       }})
        totallist_dict = {}
        totallist_dict['id'] = singledict_response['Item']['masterId']['S']
        totallist_dict['name'] = singledict_response['Item']['masterName']['S']
        totallist .append(totallist_dict)

        if singledict_response['Item']['masterId']['S'] != '' and \
            singledict_response['Item']['masterName']['S'] != 'Reserved':
            recursion(singledict, table_name)
        else:
            newlist_dict['id'] = singledict_response['Item']['id']['S']

return totallist,newlist_dict

I got error in maximum recursion dept

The error is at the recursion(singledict, table_name) My code is running fine without this but single value only i m able to extract

example is below dynamodb details

id,masterId,masterName
100,1,A
1,2,B
2,3,D
3,'',''

singledict={'id':100}

Expected out

totallist = [{id:1,name:A}, {id:2,name:B}]
newlistdict = {id:3}

来源:https://stackoverflow.com/questions/64598874/how-to-loop-extract-element-from-dynamodb-recursively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!