attrdict.py revision 6278:47e757f289e0
13101Sstever@eecs.umich.edu# Copyright (c) 2006 The Regents of The University of Michigan
27534Ssteve.reinhardt@amd.com# All rights reserved.
33101Sstever@eecs.umich.edu#
43101Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
53101Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
63101Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
73101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
83101Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
93101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
103101Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
113101Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
123101Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
133101Sstever@eecs.umich.edu# this software without specific prior written permission.
143101Sstever@eecs.umich.edu#
153101Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
163101Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
173101Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
183101Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
193101Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
203101Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
213101Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223101Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233101Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243101Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
253101Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263101Sstever@eecs.umich.edu#
273101Sstever@eecs.umich.edu# Authors: Nathan Binkert
283101Sstever@eecs.umich.edu
293101Sstever@eecs.umich.edu__all__ = [ 'attrdict', 'multiattrdict', 'optiondict' ]
303101Sstever@eecs.umich.edu
313101Sstever@eecs.umich.educlass attrdict(dict):
323101Sstever@eecs.umich.edu    """Wrap dict, so you can use attribute access to get/set elements"""
333101Sstever@eecs.umich.edu    def __getattr__(self, attr):
343101Sstever@eecs.umich.edu        if attr in self:
353101Sstever@eecs.umich.edu            return self.__getitem__(attr)
363101Sstever@eecs.umich.edu        return super(attrdict, self).__getattribute__(attr)
373101Sstever@eecs.umich.edu
383101Sstever@eecs.umich.edu    def __setattr__(self, attr, value):
393101Sstever@eecs.umich.edu        if attr in dir(self) or attr.startswith('_'):
403101Sstever@eecs.umich.edu            return super(attrdict, self).__setattr__(attr, value)
413101Sstever@eecs.umich.edu        return self.__setitem__(attr, value)
423101Sstever@eecs.umich.edu
433101Sstever@eecs.umich.edu    def __delattr__(self, attr):
443101Sstever@eecs.umich.edu        if attr in self:
453101Sstever@eecs.umich.edu            return self.__delitem__(attr)
463101Sstever@eecs.umich.edu        return super(attrdict, self).__delattr__(attr)
473101Sstever@eecs.umich.edu
483885Sbinkertn@umich.educlass multiattrdict(attrdict):
493885Sbinkertn@umich.edu    """Wrap attrdict so that nested attribute accesses automatically create
504762Snate@binkert.org    nested dictionaries."""
513885Sbinkertn@umich.edu    def __getattr__(self, attr):
523885Sbinkertn@umich.edu        try:
537528Ssteve.reinhardt@amd.com            return super(multiattrdict, self).__getattr__(attr)
543885Sbinkertn@umich.edu        except AttributeError:
554380Sbinkertn@umich.edu            d = optiondict()
564167Sbinkertn@umich.edu            setattr(self, attr, d)
573102Sstever@eecs.umich.edu            return d
583101Sstever@eecs.umich.edu
594762Snate@binkert.orgclass optiondict(attrdict):
604762Snate@binkert.org    """Modify attrdict so that a missing attribute just returns None"""
614762Snate@binkert.org    def __getattr__(self, attr):
624762Snate@binkert.org        try:
634762Snate@binkert.org            return super(optiondict, self).__getattr__(attr)
644762Snate@binkert.org        except AttributeError:
654762Snate@binkert.org            return None
664762Snate@binkert.org
674762Snate@binkert.orgif __name__ == '__main__':
685033Smilesck@eecs.umich.edu    x = attrdict()
695033Smilesck@eecs.umich.edu    x.y = 1
705033Smilesck@eecs.umich.edu    x['z'] = 2
715033Smilesck@eecs.umich.edu    print x['y'], x.y
725033Smilesck@eecs.umich.edu    print x['z'], x.z
735033Smilesck@eecs.umich.edu    print dir(x)
745033Smilesck@eecs.umich.edu    print x
755033Smilesck@eecs.umich.edu
765033Smilesck@eecs.umich.edu    print
775033Smilesck@eecs.umich.edu
783101Sstever@eecs.umich.edu    del x['y']
793101Sstever@eecs.umich.edu    del x.z
803101Sstever@eecs.umich.edu    print dir(x)
815033Smilesck@eecs.umich.edu    print(x)
823101Sstever@eecs.umich.edu
837673Snate@binkert.org    x = multiattrdict()
847673Snate@binkert.org    x.y.z = 9
857673Snate@binkert.org    print x
867673Snate@binkert.org    print x.y
877673Snate@binkert.org    print x.y.z
887673Snate@binkert.org