~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerbot/utils/lazydict.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/utils/lazydict.py
 
2
#
 
3
# thnx to maze
 
4
 
 
5
""" a lazydict allows dotted access to a dict .. dict.key """
 
6
 
 
7
class LazyDict(dict):
 
8
 
 
9
    """ Lazy dict allows dotted access to a dict """
 
10
 
 
11
 
 
12
    def __getattr__(self, attr, default={}):
 
13
 
 
14
        """ get attribute .. if not available init to default. """
 
15
 
 
16
        if not self.has_key(attr):
 
17
            self[attr] = default
 
18
 
 
19
        return self[attr]
 
20
 
 
21
    def __setattr__(self, attr, value):
 
22
 
 
23
        """ set attribute. """
 
24
 
 
25
        self[attr] = value
 
26
 
 
27
    def __str__(self):
 
28
 
 
29
        """ return a string representation of the dict """
 
30
 
 
31
        res = ""
 
32
        cp = dict(self)
 
33
        for item, value in cp.iteritems():
 
34
            res += "%r=%r " % (item, value)
 
35
 
 
36
        return res