Tuesday, July 3, 2012

Override Python dictionary to easily access dictionary entries as an attribute

Accessing python dictionary by attribute instead of [key] is easy if you override the dictionary class and provide the __getattr__ and __setattr__ methods as shown in MyDict class.


Before
old = {}
old['Name'] = 'Daffy'
old['Age'] = 30

After
new = MyDict()
new.Name = 'Micky'
new.Age = 30

print values['Name'], values.Name

print len(values.keys())

class MyDict(dict):
    def __init__(self):
        pass


    def __getattr__(self, name):
        return = self.get(name, '')


    def __setattr__(self, name, value):
        self[name] = value


    def __delattr__(self, name):
        if name in self:
            del self[name]

Python Cheat Sheet

Python Cheat Sheet



Case Insensitive Dictionary search:

1. First create a set with lower case values

dict(zip(map(string.lower,a.keys()),a.values()))







Sunday, July 1, 2012

Never buying another Mac

Man what a crappy OS.  I have never use an OS that just shows the busy icon for no reason.

Mac Book Pro - 15 " Mid 2009
2.53 GB Intel Core Duo
500 GB hard-drive
8GB of RAM

I think I am going to install Windows on it.
1. Spinning pinwheel of death pops up way to much.
2. Takes forever to launch apps, I even upgraded the ram to 8 GB, and it still takes forever
3. Running multiple apps at the same time rarely works
4. The damm keyboard is crazy.  Command, Option, Alt, Shift.  It's enuf to make you crazy.