Package: richset
richsetrichset interporlates with set, dict, and list.Motivations and Conceptsrichset provides useful functions for common use cases of set, dict, and list.builtin set, dict and list requires many boilerplate codes.Pure Python package.Fully typing supported.No magic. No meta-programming.Install$pipinstallrichsetUsagefromdataclassesimportdataclassfromrichsetimportRichSet@dataclass(frozen=True)classSomething:id:intname:strrichset=RichSet.from_list([Something(1,'one'),Something(2,'two'),Something(3,'three'),])Conversionsrichset.to_list()# => [Something(1, 'one'), Something(2, 'one'), Something(3, 'three')]richset.to_tuple()# => (Something(1, 'one'), Something(2, 'two'), Something(3, 'three'))richset.to_set()# => {Something(1, 'one'), Something(2, 'two'), Something(3, 'three')}richset.to_frozenset()# => frozenset({Something(1, 'one'), Something(2, 'two'), Something(3, 'three')})richset.to_dict(lambdas:s.id)# => {1: Something(1, 'one'), 2: Something(2, 'two'), 3: Something(3, 'three')}to_dict()takes second argumentduplicatedwhich is a choice of'error','first'or'last'.ifduplicatedis'error', thento_dict()raisesValueErrorif there is a duplicated key.ifduplicatedis'first', thento_dict()picks the first one of duplicated key.ifduplicatedis'last', thento_dict()picks the last one of duplicated key.to_dict_of_list()is similar toto_dict()but it returns a dict of list.richset.to_dict_of_list(lambdas:s.name)# => {'john': [Something(1, 'john'), Something(2, 'john')], 'jane': [Something(3, 'jane')]}List accessorsrichset.first()# => returns first item `Something(1, 'one')` or raise Error (if empty)richset.get_first()# => returns first item `Something(1, 'one')` or None (if empty)richset.last()# => returns last item `Something(3, 'three')` or raise Error (if empty)richset.get_last()# => returns last item `Something(3, 'three')` or None (if empty)richset.nth(2)# => returns 3rd item `Something(3, 'three')` or raise Error (if empty)richset.get_nth(2)# => returns 3rd item `Something(3, 'three')` or None (if empty)richset.one()# => returns one item `Something(1, 'one')` or raise Error (if empty)richset.get_one()# => returns one item `Something(1, 'one')` or None (if empty)Note:get_first,get_last,get_nthandget_oneacceptdefaultargument that returns specified value instead of None.richset.get_nth(100,default=Something(-1,'default'))# => Something(-1, 'default')List basic manipulationsrichset.pushed(Something(4,'four')).to_list()# => [Something(1, 'one'), Something(2, 'two'), Something(3, 'three'), Something(4, 'four')]richset.unshift(Something(4,'four')).to_list()# => [Something(4, 'four'), Something(1, 'one'), Something(2, 'two'), Something(3, 'three')]richset.popped()# => Something(3, 'three'), RichSet([Something(1, 'one'), Something(2, 'two')])richset.shift()# => Something(1, 'one'), RichSet([Something(2, 'two'), Something(3, 'three')])richset.slice(1,2).to_list()# => [Something(2, 'two')]richset.divide_at(1)# => RichSet([Something(1, 'one')]), RichSet([Something(2, 'two'), Something(3, 'three')])pushed_all()andunshift_all()are similar topushed()andunshift()but they accept multiple items.popped_n()andshift_n()are similar topopped()andshift()but they accept count of items.List functional manipulationsrichset.unique(lambdas:s.id)# => unique by idrichset.map(lambdas:s.id).to_list()# => [1, 2]richset.filter(lambdas:s.id>1).to_list()# => [Something(2, 'two'), Something(3, 'three')]Searchrichset.index(lambdas:s.id==2)# => 1richset.indices(lambdas:s.id==2)# => [1]richset.search_first(lambdas:s.id==2)# => Something(2, 'two')richset.search_last(lambdas:s.id==2)# => Something(2, 'two')richset.search_all(lambdas:s.id==2)# => [Something(2, 'two')]richset.contains(lambdas:s.id==2)# => Truerichset.has(Something(2,'two'))# => TrueSortsrichset.sorted(key=lambdas:s.name,reverse=True).to_list()# => [Something(2, 'two'), Something(3, 'three'), Something(1, 'one')]richset.reversed().to_list()# => [Something(3, 'three'), Something(2, 'two'), Something(1, 'one')]Statisticsrichset.is_empty()# => True if emptyrichset.is_not_empty()# => True if not emptyrichset.size()# => 3richset.count(lambdas:s.id>1)# => 2Set operationsrichset=RichSet.from_list([Something(3,'three'),Something(4,'four'),Something(5,'five'),])richset2=RichSet.from_list([Something(3,'three'),Something(4,'four'),Something(6,'six'),])richset.union(richset2).to_set()# => {Something(3, 'three'), Something(4, 'four'), Something(5, 'five'), Something(6, 'six')}richset.intersection(richset2).to_set()# => {Something(3, 'three'), Something(4, 'four')}richset.difference(richset2).to_set()# => {Something(5, 'five')}richset.symmetric_difference(richset2).to_set()# => {Something(5, 'five'), Something(6, 'six')}richset.cartesian_product(richset2).to_set()# => {(Something(3, 'three'), Something(3, 'three')), (Something(3, 'three'), Something(4, 'four')), (Something(3, 'three'), Something(6, 'six')), (Something(4, 'four'), Something(3, 'three')), (Something(4, 'four'), Something(4, 'four')), (Something(4, 'four'), Something(6, 'six')), (Something(5, 'five'), Something(3, 'three')), (Something(5, 'five'), Something(4, 'four')), (Something(5, 'five'), Something(6, 'six'))}richset.zip(richset2).to_set()# => {(Something(3, 'three'), Something(3, 'three')), (Something(4, 'four'), Something(4, 'four')), (Something(5, 'five'), Something(6, 'six')}Alsois_subset(),is_superset(),is_disjoint(),is_equal_as_set()andzip_longest()are available.Groupingrichset.group_by(lambdaitem:item.id%2)# => {1: RichSet(records=(Something(id=1, name='one'), Something(id=3, name='three'))), 0: RichSet(records=(Something(id=2, name='two'),))}richset.size_of_group_by(lambdaitem:item.id%2)# => {1: 2, 0: 1}richset.count_of_group_by(key=lambdaitem:item.id%2,predicate=lambdaitem:item.name.startswith('t'))# => {1: 1, 0: 1}richset.aggregate_by(key=lambdar:r.id%2,fn=lambdaa,b:a+b.name,initial='')# => {1: 'onethree', 0: 'two'}Pagingrichset.page(1,2).to_list()# => [Something(1, 'one'), Something(2, 'two')]richset.split_into_pages(2).to_list()# => [RichSet([Something(1, 'one'), Something(2, 'two')]), RichSet([Something(3, 'three')])]LICENSEThe 3-Clause BSD License. See also LICENSE file.