lundi 17 mai 2021

python: For loop, while, else, repeat

I have a function that pulls in a value from another function. This function could either go one of two ways, it includes a dict key('collection_gurantees') or it doesn't.

I want to log the occurrence where it does not include the key, but only continue if it does which means I need to "re-run" the function to bring in a value. Ive been trying different combinations of for loops, whiles, elses, breaks etc and I may be at the point where im going backwards.

Here is the two different versions of the dict(block), one includes 'collection_gurantees' while the other doesnt.

here is my current code:

def block_by_height():
    for item in determine_block():
        block = protobuf_to_dict(item)
        for key in list(block.items()):    
            while key != 'collection_gurantees':
                flowtransactions.insert_one(block)
    else:
        collection_ids = block['block']['collection_guarantees']
        return collection_ids

if the key does not exist, I want to save the dict as is to the DB(insert_one), if the key does exists, I retrieve it and return it so it could be used in the next function. The function should only continue once the key exists and is returned

Aucun commentaire:

Enregistrer un commentaire