Having moved to Python this was more of a challenge, but I managed to throw something together using the "vars()" local variable ability.
<><><>
if ap_members.count((packet.info, packet.addr2)) == 0: # If not found before add and setup storage
ap_members.append((packet.info, packet.addr2))
vars()[packet.info + "_" + packet.addr2 + "_working"] = deque(maxlen=2)
vars()[packet.info + "_" + packet.addr2 + "_list"] = 0
vars()[packet.info + "_" + packet.addr2 + "_average"] = make_av() # Stats
vars()[packet.info + "_" + packet.addr2 + "_power"] = make_av()
vars()[packet.info + "_" + packet.addr2 + "_list"] += 1 # Tracks num of beacons per AP
<><><>
This is just a little code example of the principle which is implemented continually through the program I am working on. Here I have a stream of packets coming in and I want to create a series of variables every time a new link is found, i.e. every time a client and an Access Point talk to each other. If the "packet.info" (SSID) and "packet.addr2" (destination address) has not been seen before then it is added to the list of known associations and has a deque, a list and two stats instances created.
Using this approach any time a new variable needs created I jut append with a different "_x" term and define it as I see fit. Now any time the "packet.info" and "packet.addr2" fields are fulfilled the information is added for that combination and that combination alone.
I have since been told that my solution would have been better utilising a dictionary and a series of lists but I disagree. Once you realise what this code is doing it is very very easy to start adapting it and adding new variables. The description of what each variable does is clear as day and you can guarantee it is going to be created irrespective of how many times it is called. Yes it clutters the screen somewhat, but I think it's a fair trade off.
No comments:
Post a Comment