fabiocaccamo / python-benedict

:blue_book: dict subclass with keylist/keypath support, built-in I/O operations (base64, csv, html, ini, json, pickle, plist, query-string, toml, xls, xml, yaml), s3 support and many utilities.
MIT License
1.51k stars 48 forks source link

Add `freeze` method (to make the dict immutable). #71

Open fabiocaccamo opened 2 years ago

fabiocaccamo commented 2 years ago

Fund with Polar

thesayfulla commented 4 months ago

Hello, is it possible to add an attribute to a benedict? I wanted to add an is_frozen attribute, but I encountered an error like this:

    raise AttributeError(attr_message) from None
AttributeError: 'benedict' object has no attribute 'is_frozen'

I've written:

class benedict(KeyattrDict, KeypathDict, IODict, ParseDict):
    def __init__(self, *args, **kwargs):
        ....
        self.is_frozen = False

    def freeze(self):
        self.is_frozen = True

    def __setitem__(self, key, value):
        if self.is_frozen:
            raise TypeError("This dictionary is immutable")
        return super().__setitem__(key, self._cast(value))