Package: rstructs
Reactive structures - data structures with eventsProvides several data structures with different events such as reactive list or reactive dict.Both ordinary and async functions are supported.Examplefromrstructsimportrlistsubscriptions=rlist()@subscriptions.addeddefon_user_subscribed(index,user):ifindex==0:print(f'The first user{user}has just subscribed! ')else:print(f'{user}has just subscribed. ')subscriptions.add('Bob')subscriptions.add('Alice')The first user Bob has just subscribed!Alice has just subscribed.How to subscribe to events?To subscribe to an event just decorate with it event handler.@subscriptions.addeddefon_user_subscribed(index,user):...Moreover, a time to handle parameter can be used.@subscriptions.added(2)# handle only for the first two timesdefon_user_subscribed(index,user):...APICommon predefined eventsconstructed(initial_data)- The structure was constructed and initialized.added(key, item)- Added new item into the structure.before_added(key_or_item)- Just before added new item into the structure.filled(key, item)- The first item was added to the empty structure.before_filled(key_or_item)- Just before the first item was added to the empty structure.changed(key, item)- Changed existing item.before_changed(key, item)- Just before changed existing item.updated(key, item)- Added new item or changed existing one in the structure.before_updated(key_or_item)- Just before added new item or changed existing one in the structure.removed(key, item)- Removed existing item.before_removed(key_or_item)- Just before removed existing item.emptied()- The last item was removed from the structure.before_emptied()- Just before the last item was removed from the structure.Additional predefined rlist eventssorted()- The list sorted inplace.before_sorted()- Just before the list sorted inplace.Common predefined methodskey_or_item in ...- Checks whether key or item is contained by data.len(...)- Returns data length.iter(...)- Returns data iterator if available....[key_or_item]- alias toget(key_or_item)....[key] = value- alias toupdate(key, value).del ...[key]- alias to remove_key(key).add(key)->key, itemoradd(key, item)->key, item- Add new item into the structure.change(key, item)- Change existing item.update(key)orupdate(key, item)- Add new item or change existing one in the structure. Firstly tries to change item and in case of ElementError adds as a new one.remove_key(key)->key, item- Remove existing item.remove_item(item)->key, item- Remove existing item.get(key_or_item, default=None)- Get existing item. If default value is defined return it when ElementError occurs.clear()- Remove all items from the structure. Derived classes should use remove methods per each item.index(item)->key- Try to get key of item or rise ElementError.Additional predefined rlist methodssort(*, key=None, reverse=False)- This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state)....[key] = value- alias tochange(key, value).Additional predefined rdict methodskeys()->list[keys]- get list of keys.values()->list[values]- get list of keys.items()->list[tuple[key,value]]- get list of tuples of keys and values.How to create your own reactive structure?To create new reactive class you need to derive fromIReactiveStructureand implement abstract methods (and optionally override other predefined methods).More detailsThis library is made on top ofeventsystem.