Archive for February 14th, 2014

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)

(more…)

The people at Packt Publishing recently sent me an ebook copy of Raúl Garreta and Guillermo Moncecchi’s book, Learning scikit-learn: Machine Learning in Python to review. Machine learning isn’t a topic I’m very familiar with, but I gave the book a shot as it sounded interesting. I’ll start off with my quick review for those of you with little time:

Quick Review

  • Why I picked it up: This book was a review copy, but I’m actually interested in just about any Python programming book
  • Why I finished it: The book is well written and while I don’t understand a lot of it, the gist of the text was interesting…and I skimmed a lot.
  • I’d give it to: Someone who uses Python for scientific purposes or machine learning

If you found that intriguing, then feel free to read the full review! (more…)