I have an application that reads bluetooth advertisements from sensors and does stuff.
Currently, I use a dict to manage instances of the sensors and attributes of them. I have a bunch of functions that I call to do stuff based on the details of the bluetooth advertisements.
I am interested in switching from a dict to creating a class called Sensors. Having functions/methods within the class itself will clean up my code a lot (make it more readable) and set me up better for adding new features.
When a new advertisement comes in, the first step I do is to check whether it's from a sensor I've seen before. This is simple with a dict using
newAdvertisement = {"name": "sensor1", "attribute": "value"}
sensor = newAdvertisement['name']
if sensor in sensorDict
...
I am having trouble figuring out how to do something similar with instances of a class. If I read a string value from the advertisement, how can I check if there is an instance of my class with that name?
I've tried to work out a way to use isInstance with something like this:
newAdvertisement = {"name": "sensor1", "attribute": "value"}
sensor = newAdvertisement['name']
if isInstance(sensor, Sensor):
...
But line 2 sets the variable 'sensor' to the string 'sensor1', so this checks if that string is an instance of Sensor (which it is of course, not). What I want is to check if there is an instance of Sensor called sensor1.
I've also tried to create a list of instances like so:
sensor1 = Sensor(name='sensor1', attribute='value')
sensor2 = Sensor(name='sensor2', attribute='value')
sensorList = [sensor1, sensor2]
newAdvertisement = {"name": "sensor1", "attribute": "value"}
sensor = newAdvertisement['name']
if sensor in sensorList:
...
But again, I'm comparing the string 'sensor' to a list of objects, so it's never going to be there.
Does it make sense to do this? Can I do it cleanly? Is it useful to have my own class in this code?
There's nothing that does this automatically.
Your class can maintain a dictionary of all the instances.
import weakref
class Sensor:
sensor_dict = weakref.WeakValueDictionary()
def __init__(self, name, attribute)
self.sensor_dict[name] = self
self.name = name
self.attribute = attribute
Then you can do
if sensor in Sensor.sensor_dict:
I've used a WeakValueDictionary
so the elements will be removed automatically when the Sensor
instances are GC'ed.