PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 26, 2022

[FIXED] How to check if an instance of a class exists based on its name

 October 26, 2022     dictionary, oop, python, python-class     No comments   

Issue

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?


Solution

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.



Answered By - Barmar
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing