0

i have a question regarding adding dictionary key and value to method using loop

This is what i was thinking to write but it doesn't work how i want because it creates a packet just with one key/value every time

for key in packetData:
    for name in packetData[key]:
        packets = Ether()/IP()/UDP()/createsPacket(key, name=packetData[key][name])
        print ("as name " + name + " \n as value " + str(packetData[key][name]))

Instead of writing this manually like that :

packets1 = Ether()/IP()/UDP()/createsPacket("65", UserID = "name", Password = "pass123", ETX = 123)
packets2 = Ether()/IP()/UDP()/createsPacket("72", PriceID = 123, Side = 12, MaxAmount = 123, MinAmount = 123, Price = 123000)

    json then converted to dictionary in python , this is data that i want to pass in

    {
    "65":{
        "UserID":"vcjazfan",
        "Password":"ejujwlhk",
        "SessionID":115,
        "ETX":192
    },
    "66":{
        "UserID":"dzmtrssy",
        "SessionID":35,
        "Reason":"zbwivjcv",
        "ETX":43
    },
     "72":{
        "InstrumentIndex":171,
        "PriceID":217,
        "Side":226,
        "MaxAmount":210,
        "MinAmount":219,
        "Price":47,
        "PriceProvider":207,
        "ETX":78
    },

 

Made more generic for easier understanding, hoping it helps

Generic code

dictionary = {"65":{ "UserID":"vcjazfan", "Password":"ejujwlhk", "ETX":192} ,   "72":{ "InstrumentIndex":171,  "PriceID":217, } }


#This is what i was thinking to write but it doesn't work how i want because it creates a packet just with one key/value every time
for key in dictionary:
    for name in dictionary[key]:
        value=dictionary[key][name]
        packets = method(key, name=value) # in first iteration when key is 65  ,  name = "UserID" ,  value = "vcjazfan"
                                          # in second iteration when key is 65   ,  name = "Password" ,  value = "ejujwlhk"  


#Instead of writing this manually like that :

packets1 = method("65", UserID = "name", Password = "pass123", ETX = 123)
packets2 = method("72", InstrumentIndex = 123, PriceID = 12,)
  
2
  • import json; data = json.loads(packetData) docs.python.org/3/library/json.html Commented Oct 22, 2020 at 13:26
  • I wrote that already, its only snippets of code Commented Oct 22, 2020 at 14:37

1 Answer 1

0

This question solved my problem : How to pass dictionary items as function arguments in python?

solution to  my original code:
Allpackets= []
for key in packetData:
    Allpackets.append(packets/createsPacket(key, **packetData[key]))


Solution to generic one:

dictionary = {"65":{ "UserID":"vcjazfan", "Password":"ejujwlhk", "ETX":192} ,   "72":{ "InstrumentIndex":171,  "PriceID":217, } }

Allpackets = []
for key in dictionary:
    Allpackets.append( method(key, **dictionary))


#Instead of writing this manually like that :

packets1 = method("65", UserID = "name", Password = "pass123", ETX = 123)
packets2 = method("72", InstrumentIndex = 123, PriceID = 12,)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.