Home > Software design >  Adding a new key value pair to a list of AttributeDicts
Adding a new key value pair to a list of AttributeDicts

Time:03-02

I'm struggling with indexing and updating in a for loop.

I have the following list of AttributeDicts.

[
  {
    "blockHash": {},
    "blockNumber": 14262929,
    "contractAddress": null,
    "cumulativeGasUsed": 17784692,
    "effectiveGasPrice": 42175682624,
    "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
    "gasUsed": 266567,
    "logs": [
      {
        "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x000000000000000000000000000000000000000000000000a688906bd8b00000",
        "logIndex": 319,
        "removed": false,
        "topics": [
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      },
      {
        "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x",
        "logIndex": 321,
        "removed": false,
        "topics": [
          {},
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      }
    ],
    "logsBloom": {},
    "status": 1,
    "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
    "transactionHash": {},
    "transactionIndex": 453,
    "type": "0x2"
  },
  {
    "blockHash": {},
    "blockNumber": 14262929,
    "contractAddress": null,
    "cumulativeGasUsed": 17784692,
    "effectiveGasPrice": 42175682624,
    "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
    "gasUsed": 266567,
    "logs": [
      {
        "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x00000000000000000000000000000000000000000000000014d1120d7b160000",
        "logIndex": 320,
        "removed": false,
        "topics": [
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      },
      {
        "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x",
        "logIndex": 321,
        "removed": false,
        "topics": [
          {},
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      }
    ],
    "logsBloom": {},
    "status": 1,
    "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
    "transactionHash": {},
    "transactionIndex": 453,
    "type": "0x2"
  }
]

When data != 0x in the nested dictionary I'd like to add an additional key:value pair. To look like the following.

  {
    "blockHash": {},
    "blockNumber": 14262929,
    "contractAddress": null,
    "cumulativeGasUsed": 17784692,
    "effectiveGasPrice": 42175682624,
    "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
    "gasUsed": 266567,
    "logs": [
      {
        "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x00000000000000000000000000000000000000000000000014d1120d7b160000",
        "logIndex": 320,
        "removed": false,
        "topics": [
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453,
        "Value": Decimal('1.5')
      },
      {
        "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x",
        "logIndex": 321,
        "removed": false,
        "topics": [
          {},
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      }
    ],
    "logsBloom": {},
    "status": 1,
    "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
    "transactionHash": {},
    "transactionIndex": 453,
    "type": "0x2"
  }

In essence, adding the Value: Decimal(1.5) to the end of that first nested dictionary.

How do you replace (or update?) the original dictionary in nodeTxLog with the above dictionary?

My code

txDetails = []
for i in txHash:
      nodeTxLog = w3.eth.get_transaction_receipt(i)
      tx_log = json.dumps(nodeTxLog["logs"], sort_keys=True, indent=2, cls=TxEncoder)
      log_data = json.loads(tx_log)
      for d in log_data:
          if d["data"] != "0x":
              dl = d["data"]
              wei = w3.toInt(hexstr=dl)
              value = w3.fromWei(wei, 'ether')
              #How to replace (or update) the original Tx in nodeTxLog with d?
              d.update({"Value": value})
              #This doesn't work
              log_data.append(d)
              #This appends the original dict to the list, not the dict with updated key:value
              txDetails.append(nodeTxLog)

CodePudding user response:

It's not clear to me where your data comes from, but updating the already pasted data is pretty straightforward.
I'd like call it nested data structure, instead of dictionary that you named.
Note null and false should be changed to None and False to run here. I'm wondering how you got the long data structure, from python print(some_data), or copied directly from some files?

blocks = [
    {
        "blockHash": {},
        "blockNumber": 14262929,
        "contractAddress": None,
        "cumulativeGasUsed": 17784692,
        "effectiveGasPrice": 42175682624,
        "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
        "gasUsed": 266567,
        "logs": [
            {
                "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x000000000000000000000000000000000000000000000000a688906bd8b00000",
                "logIndex": 319,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            },
            {
                "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x",
                "logIndex": 321,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            }
        ],
        "logsBloom": {},
        "status": 1,
        "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
        "transactionHash": {},
        "transactionIndex": 453,
        "type": "0x2"
    },
    {
        "blockHash": {},
        "blockNumber": 14262929,
        "contractAddress": None,
        "cumulativeGasUsed": 17784692,
        "effectiveGasPrice": 42175682624,
        "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
        "gasUsed": 266567,
        "logs": [
            {
                "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x00000000000000000000000000000000000000000000000014d1120d7b160000",
                "logIndex": 320,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            },
            {
                "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x",
                "logIndex": 321,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            }
        ],
        "logsBloom": {},
        "status": 1,
        "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
        "transactionHash": {},
        "transactionIndex": 453,
        "type": "0x2"
    }
]
# note null and false should be changed to None and False to run here
for block in blocks:
    for log in block["logs"]:
        if log["data"] != "0x":
            log["Value"] = "value_you_got_some_where"
print(blocks)  # check the output
  • Related