Quantcast
Channel: Python Archives - Mouse Vs Python
Viewing all articles
Browse latest Browse all 498

Python 101: How to Change a Dict Into a Class

$
0
0

I work with a lot of dictionaries at my job. Sometimes the dictionaries get really complicated with lots of nested data structures embedded within them. Recently I got a little tired of trying to remember all the keys in my dictionaries so I decided to change one of my dictionaries into a class so I could access the keys as instance variables / attributes. If you’ve ever gotten sick

Here’s one simple way to do it:

########################################################################
class Dict2Obj(object):
    """
    Turns a dictionary into a class
    """
 
    #----------------------------------------------------------------------
    def __init__(self, dictionary):
        """Constructor"""
        for key in dictionary:
            setattr(self, key, dictionary[key])
 
 
#----------------------------------------------------------------------
if __name__ == "__main__":
    ball_dict = {"color":"blue",
                 "size":"8 inches",
                 "material":"rubber"}
    ball = Dict2Obj(ball_dict)

This code uses setattr to add each of the keys as attributes to the class. The following shows some examples of how it works:

>>> ball.color
'blue'
>>> ball.size
'8 inches'
>>> print ball
<__main__.Dict2Obj object at 0x028CD5B0>

When we print the ball object, we get a rather unhelpful string back from the class. Let’s override the __repr__ method of our class and make it print out something a little more useful:

########################################################################
class Dict2Obj(object):
    """
    Turns a dictionary into a class
    """
 
    #----------------------------------------------------------------------
    def __init__(self, dictionary):
        """Constructor"""
        for key in dictionary:
            setattr(self, key, dictionary[key])
 
    #----------------------------------------------------------------------
    def __repr__(self):
        """"""
        return "<Dict2Obj: %s>" % self.__dict__
 
#----------------------------------------------------------------------
if __name__ == "__main__":
    ball_dict = {"color":"blue",
                 "size":"8 inches",
                 "material":"rubber"}
    ball = Dict2Obj(ball_dict)

Now if we print out the ball object, we’ll get the following:

>>> print ball
<Dict2Obj: {'color': 'blue', 'material': 'rubber', 'size': '8 inches'}>

This is a little unintuitive in that it is printing out a dictionary using the class’s internal __dict__ rather than just the attribute names. This is more a matter of taste than anything, but let’s try to get just the method names:

########################################################################
class Dict2Obj(object):
    """
    Turns a dictionary into a class
    """
 
    #----------------------------------------------------------------------
    def __init__(self, dictionary):
        """Constructor"""
        for key in dictionary:
            setattr(self, key, dictionary[key])
 
    #----------------------------------------------------------------------
    def __repr__(self):
        """"""
        attrs = str([x for x in self.__dict__])
        return "<Dict2Obj: %s>" % attrs
 
#----------------------------------------------------------------------
if __name__ == "__main__":
    ball_dict = {"color":"blue",
                 "size":"8 inches",
                 "material":"rubber"}
    ball = Dict2Obj(ball_dict)

Here we just loop over the contents of __dict__ and return a string with just a list of the keys, which match up with the attribute names. You could have also done it like this:

attrs = str([x for x in dir(self) if "__" not in x])

I’m sure there are lots of other ways to accomplish this sort of thing as well. Regardless, I found this little piece of code helpful in some of my work. Hopefully you’ll find it useful too.


Viewing all articles
Browse latest Browse all 498

Latest Images

Trending Articles



Latest Images