From 36213e67c56968bef51b9e41fd697f53f6259043 Mon Sep 17 00:00:00 2001 From: Santiago Lezica Date: Fri, 7 Dec 2012 19:42:20 -0300 Subject: [PATCH] Initial commit --- LICENSE.txt | 7 +++++++ README.txt | 0 frozendict/__init__.py | 30 ++++++++++++++++++++++++++++++ setup.py | 9 +++++++++ 4 files changed, 46 insertions(+) create mode 100644 LICENSE.txt create mode 100644 README.txt create mode 100644 frozendict/__init__.py create mode 100644 setup.py diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..a51531d --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +Copyright (c) 2012 Santiago Lezica + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..e69de29 diff --git a/frozendict/__init__.py b/frozendict/__init__.py new file mode 100644 index 0000000..627bc0c --- /dev/null +++ b/frozendict/__init__.py @@ -0,0 +1,30 @@ +import collections, operator + +class frozendict(collections.Mapping): + + def __init__(self, *args, **kwargs): + self.__dict = dict(*args, **kwargs) + self.__hash = None + + def __getitem__(self, key): + return self.__dict[key] + + def copy(self, **add_or_replace): + new = frozendict(self) + new.__dict.update(add_or_replace) # Feels like cheating + return new + + def __iter__(self): + return iter(self.__dict) + + def __len__(self): + return len(self.__dict) + + def __repr__(self): + return '' % repr(self.__dict) + + def __hash__(self): + if self.__hash is None: + self.__hash = reduce(operator.xor, map(hash, self.iteritems()), 0) + + return self.__hash diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1171caa --- /dev/null +++ b/setup.py @@ -0,0 +1,9 @@ +from distutils.core import setup + +setup( + name = 'frozendict', + version = '0.1', + packages = ['frozendict'], + license = 'MIT License', + long_description = open('README.txt').read() +)