Skip to content

Instantly share code, notes, and snippets.

View lgr's full-sized avatar

Łukasz Grądzki lgr

View GitHub Profile
@lgr
lgr / read_only_class.py
Created October 25, 2012 10:54
A class with the attributes that are prevented from being overwritten
class ReadOnlyAttr:
""" A class with the attributes that are defined on the instance
creation and cannot be modified later. """
# A tuple of attributes that should remain read-only
READ_ONLY_ATTR = ('x', 'y')
def __init__(self, *args, **kwargs):
""" Takes a list of read-only attributes: %s """
for attr in self.READ_ONLY_ATTR:
@lgr
lgr / attributed_dict.py
Created October 25, 2012 10:19
A dictionary which keys can be accessed and added as object attributes.
class AttributedDict(dict):
"""
A class extending the standard dict so that its keys can be accessed
as an object attributes (as long as the are strings).
Notice: this class is not pickable, so for example an attempt to save
its instance in Django session causes an error.
"""
__getattr__ = dict.__getitem__