Name | lazy-freeze JSON |
Version |
0.1.0
JSON |
| download |
home_page | None |
Summary | A decorator that makes objects immutable after their hash is calculated |
upload_time | 2025-08-09 18:05:13 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.7 |
license | MIT License
Copyright (c) 2025 Ofek Shilon
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.
|
keywords |
hash
immutable
dictionary
frozen
decorator
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# lazy-freeze
A Python decorator that makes objects immutable after their hash is calculated.
## Overview
`lazy-freeze` provides a simple solution to a common problem in Python: ensuring immutability of objects after they're used as dictionary keys. This is implemented as a decorator that makes objects behave normally until their hash is calculated, at which point they become immutable.
## Installation
Clone this repository:
```bash
git clone https://github.com/username/lazy-freeze.git
cd lazy-freeze
```
## Usage
### Basic Usage
```python
from lazy_freeze import lazy_freeze
@lazy_freeze
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __hash__(self):
return hash((self.name, self.age))
def __eq__(self, other):
if not isinstance(other, Person):
return False
return self.name == other.name and self.age == other.age
# Create a person
p = Person("Alice", 30)
# Modify before hash - this works fine
p.age = 31
# Take the hash - this freezes the object
h = hash(p)
# Try to modify after hash - this raises TypeError
try:
p.age = 32
except TypeError as e:
print(f"Error: {e}") # Error: Cannot modify Person after its hash has been taken
```
### Debug Mode
Enable debug mode to capture stack traces when an object's hash is taken:
```python
@lazy_freeze(debug=True)
class DebugPerson:
def __init__(self, name, age):
self.name = name
self.age = age
def __hash__(self):
return hash((self.name, self.age))
# Create and hash a person
p = DebugPerson("Alice", 30)
h = hash(p)
# Attempting to modify will show where the hash was calculated
try:
p.age = 32
except TypeError as e:
print(f"Error:\n{e}")
# Output will include the stack trace from when hash(p) was called
```
### Selective Attribute Freeze
If your `__hash__` implementation only depends on certain attributes, you can selectively freeze only those attributes:
```python
@lazy_freeze(freeze_attrs=["name", "age"])
class PartiallyFrozenPerson:
def __init__(self, name, age, description):
self.name = name
self.age = age
self.description = description # Not used in hash
def __hash__(self):
return hash((self.name, self.age)) # Only uses name and age
def __eq__(self, other):
if not isinstance(other, PartiallyFrozenPerson):
return False
return self.name == other.name and self.age == other.age
# Create and hash a person
p = PartiallyFrozenPerson("Alice", 30, "Software Engineer")
h = hash(p)
# Frozen attributes cannot be modified
try:
p.name = "Bob" # This will raise TypeError
except TypeError as e:
print(f"Error: {e}")
# Non-frozen attributes can still be modified
p.description = "Senior Engineer" # This works fine
```
Raw data
{
"_id": null,
"home_page": null,
"name": "lazy-freeze",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "hash, immutable, dictionary, frozen, decorator",
"author": null,
"author_email": "Ofek Shilon <ofekshilon@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/77/7b/01a30d2c1da1af1e0b699fc658f15dd26bde5704f6f0bbed311903cba1af/lazy_freeze-0.1.0.tar.gz",
"platform": null,
"description": "# lazy-freeze\n\nA Python decorator that makes objects immutable after their hash is calculated.\n\n## Overview\n\n`lazy-freeze` provides a simple solution to a common problem in Python: ensuring immutability of objects after they're used as dictionary keys. This is implemented as a decorator that makes objects behave normally until their hash is calculated, at which point they become immutable.\n\n## Installation\n\nClone this repository:\n\n```bash\ngit clone https://github.com/username/lazy-freeze.git\ncd lazy-freeze\n```\n\n## Usage\n\n### Basic Usage\n\n```python\nfrom lazy_freeze import lazy_freeze\n\n@lazy_freeze\nclass Person:\n def __init__(self, name, age):\n self.name = name\n self.age = age\n \n def __hash__(self):\n return hash((self.name, self.age))\n \n def __eq__(self, other):\n if not isinstance(other, Person):\n return False\n return self.name == other.name and self.age == other.age\n\n# Create a person\np = Person(\"Alice\", 30)\n\n# Modify before hash - this works fine\np.age = 31\n\n# Take the hash - this freezes the object\nh = hash(p)\n\n# Try to modify after hash - this raises TypeError\ntry:\n p.age = 32\nexcept TypeError as e:\n print(f\"Error: {e}\") # Error: Cannot modify Person after its hash has been taken\n```\n\n### Debug Mode\n\nEnable debug mode to capture stack traces when an object's hash is taken:\n\n```python\n@lazy_freeze(debug=True)\nclass DebugPerson:\n def __init__(self, name, age):\n self.name = name\n self.age = age\n \n def __hash__(self):\n return hash((self.name, self.age))\n\n# Create and hash a person\np = DebugPerson(\"Alice\", 30)\nh = hash(p)\n\n# Attempting to modify will show where the hash was calculated\ntry:\n p.age = 32\nexcept TypeError as e:\n print(f\"Error:\\n{e}\")\n # Output will include the stack trace from when hash(p) was called\n```\n\n### Selective Attribute Freeze\n\nIf your `__hash__` implementation only depends on certain attributes, you can selectively freeze only those attributes:\n\n```python\n@lazy_freeze(freeze_attrs=[\"name\", \"age\"])\nclass PartiallyFrozenPerson:\n def __init__(self, name, age, description):\n self.name = name\n self.age = age\n self.description = description # Not used in hash\n \n def __hash__(self):\n return hash((self.name, self.age)) # Only uses name and age\n \n def __eq__(self, other):\n if not isinstance(other, PartiallyFrozenPerson):\n return False\n return self.name == other.name and self.age == other.age\n\n# Create and hash a person\np = PartiallyFrozenPerson(\"Alice\", 30, \"Software Engineer\")\nh = hash(p)\n\n# Frozen attributes cannot be modified\ntry:\n p.name = \"Bob\" # This will raise TypeError\nexcept TypeError as e:\n print(f\"Error: {e}\")\n\n# Non-frozen attributes can still be modified\np.description = \"Senior Engineer\" # This works fine\n```\n",
"bugtrack_url": null,
"license": "MIT License\n \n Copyright (c) 2025 Ofek Shilon\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n ",
"summary": "A decorator that makes objects immutable after their hash is calculated",
"version": "0.1.0",
"project_urls": {
"Bug Tracker": "https://github.com/ofekshilon/lazy-freeze/issues",
"Homepage": "https://github.com/ofekshilon/lazy-freeze"
},
"split_keywords": [
"hash",
" immutable",
" dictionary",
" frozen",
" decorator"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "186e3baf607efa47299af8d3c829af74094a2bf2ee34dd21f0b15e2b81dcc3e6",
"md5": "e14f0e68e6a89eb0f79ff2c0b62a1446",
"sha256": "168a8508f488e1f82f2fed7cca71e8d03dd409820f28b58cedb8647c241bdf82"
},
"downloads": -1,
"filename": "lazy_freeze-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e14f0e68e6a89eb0f79ff2c0b62a1446",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 6492,
"upload_time": "2025-08-09T18:05:12",
"upload_time_iso_8601": "2025-08-09T18:05:12.749903Z",
"url": "https://files.pythonhosted.org/packages/18/6e/3baf607efa47299af8d3c829af74094a2bf2ee34dd21f0b15e2b81dcc3e6/lazy_freeze-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "777b01a30d2c1da1af1e0b699fc658f15dd26bde5704f6f0bbed311903cba1af",
"md5": "f9e1edcd39062e6b86cacf71f77deab6",
"sha256": "f047bb34376c8369098194bd194e5759b934103455e0e79ce90e3d8a5e9d51c8"
},
"downloads": -1,
"filename": "lazy_freeze-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "f9e1edcd39062e6b86cacf71f77deab6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 6793,
"upload_time": "2025-08-09T18:05:13",
"upload_time_iso_8601": "2025-08-09T18:05:13.933340Z",
"url": "https://files.pythonhosted.org/packages/77/7b/01a30d2c1da1af1e0b699fc658f15dd26bde5704f6f0bbed311903cba1af/lazy_freeze-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-09 18:05:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ofekshilon",
"github_project": "lazy-freeze",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "lazy-freeze"
}