https://github.com/diegojromerolopez/gelidum Skip to content Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} diegojromerolopez / gelidum * Notifications * Star 1 * Fork 0 Freeze your objects in python MIT License 1 star 0 forks Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags 1 branch 5 tags Code Clone HTTPS GitHub CLI [https://github.com/d] Use Git or checkout with SVN using the web URL. [gh repo clone diegoj] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @diegojromerolopez diegojromerolopez update_on parameter on freeze method ... 5a5ed89 Jun 14, 2021 update_on parameter on freeze method 5a5ed89 Git stats * 65 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Restrict the publish of packages only when tagged commits May 31, 2021 gelidum update_on parameter on freeze method Jun 14, 2021 tests update_on parameter on freeze method Jun 14, 2021 .gitignore First commit May 29, 2021 LICENSE Initial commit May 29, 2021 README.md update_on parameter on freeze method Jun 14, 2021 requirements.txt Thread-safety Jun 12, 2021 setup.py Update version Jun 13, 2021 View code gelidum Introduction Major highlights How it works How to use it Freeze in the same object Freeze in a new object Basic use What to do when trying to update an attribute Freeze input params Check original (i.e. "hot") class Limitations Dependencies Roadmap Collaborations License README.md gelidum License Maintenance made-with-python PyPI pyversions PyPI version gelidum PyPI status Maintainability Test Coverage Freeze your objects in python. Latin English In winter the air is cold and frosty: Caelum est hieme frigidum et myrtles, olives and all other trees gelidum; myrtos oleas quaeque which require constant warmth for alia assiduo tepore laetantur, them to do well, the climate rejects aspernatur ac respuit; laurum and spurns, though it allows laurel tamen patitur atque etiam to grow, and even brings it to a nitidissimam profert, interdum luxuriant leaf. Occasionally, sed non saepius quam sub urbe however, it kills it, but that does nostra necat. not happen more frequently than in the neighbourhood of Rome. The Letters of the Younger Pliny, First Series -- Volume 1 by the Younger Pliny, translated to English by John Benjamin Firth. Introduction Inspired by the method freeze found in other languages like Javascript, this package tries to make immutable objects to make it easier avoid accidental modifications in your code. Major highlights * freeze method creates objects with the same attributes of inputs that cannot be expanded or modified. * Frozen object creation is thread-safe. How it works In case of the builtin types (int, float, str, etc) it makes nothing, as they are already immutable. For the list type, a tuple with frozen items is returned. Tuples are already immutable, so a new tuple with frozen items is returned. For sets, frozensets of frozen items are returned. For dicts, it creates a new frozendict with the keys and frozen values of the original dict. This package, change the methods __setattr__, __delattr__, __set__, __setitem__, __delitem__, and __reversed__. of the object argument and all of its attributed recursively, making them raise an exception if the developer tries to call them to modify the attributes of the instance. How to use it Freeze in the same object from gelidum import freeze your_frozen_object = freeze(your_object, inplace=True) assert(id(your_frozen_object), id(your_object)) # Both raise exception your_object.attr1 = new_value your_frozen_object.attr1 = new_value Freeze in a new object Basic use from gelidum import freeze # inplace=False by default your_frozen_object = freeze(your_object, inplace=False) # It doesn't raise an exception, mutable object your_object.attr1 = new_value # Raises exception, immutable object your_frozen_object.attr1 = new_value What to do when trying to update an attribute from gelidum import freeze class SharedState(object): def __init__(self, count: int): self.count = count shared_state = SharedState(1) # on_update="exception": raises an exception when an update is tried frozen_shared_state = freeze(shared_state, on_update="exception") frozen_shared_state.count = 4 # Raises exception # on_update="warning": shows a warning in console exception when an update is tried frozen_shared_state = freeze(shared_state, on_update="warning") frozen_shared_state.count = 4 # Shows a warning in console # on_update="nothing": does nothing when an update is tried frozen_shared_state = freeze(shared_state, on_update="nothing") frozen_shared_state.count = 4 # Does nothing, as this update did not exist Freeze input params Use the decorator freeze_params to freeze the input parameters and avoid non-intended modifications: from typing import List from gelidum import freeze_params @freeze_params() def append_to_list(a_list: List, new_item: int): a_list.append(new_item) If freeze_params is called without arguments, all input parameters will be frozen. Otherwise, passing a set of parameters will inform the decorator of which named parameters must be frozen. from typing import List from gelidum import freeze_params @freeze_params(params={"list1", "list2"}) def concat_lists_in(dest: List, list1: List, list2: List): dest = list1 + list2 # Freeze dest, list1 and list2 concat_lists_in([], list1=[1, 2, 3], list2=[4, 5, 6]) # Freeze list1 and list2 concat_lists_in(dest=[], list1=[1, 2, 3], list2=[4, 5, 6]) Always use kwargs unless you want to freeze the args params. A good way to enforce this is by making the function have keyword-only arguments: from typing import List from gelidum import freeze_params @freeze_params(params={"list1", "list2"}) def concat_lists_in(*, dest: List, list1: List, list2: List): dest = list1 + list2 Take in account that all freezing is done in a new object (i.e. freeze with inplace=False). It makes no sense to freeze a parameter of a function that could be used later, outside said function. Check original (i.e. "hot") class * get_gelidum_hot_class_name: returns the name of hot class. * get_gelidum_hot_class_module returns the module reference where the hot class was. Limitations * dict, list, tuple and set cannot be modified inplace although the flag inplace is set. * file handler attributes are not supported. An exception is raised when trying to freeze an object with them * frozen objects cannot be serialized with marshal. Dependencies Right now this package uses frozendict and Roadmap * [ ] Include timestamp when freezing objects. * [ ] Make some use-cases with threading module. * [ ] Add version of object when freezing. Collaborations This project is open to collaborations. Make a PR or an issue, and I'll take a look to it. License MIT About Freeze your objects in python Topics python immutable freeze object-freeze Resources Readme License MIT License Releases 5 tags Packages 0 No packages published Languages * Python 100.0% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.