Name | richset JSON |
Version |
0.4.1
JSON |
| download |
home_page | None |
Summary | richset interpolate between list, dict, set and iterables. |
upload_time | 2025-01-31 18:12:34 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.9 |
license | Copyright 2022-2025 Kitsu Yui
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
|
# richset
[](https://badge.fury.io/py/richset)
[](https://pypi.python.org/pypi/richset/)
[](https://codecov.io/gh/kitsuyui/python-richset)
[](https://opensource.org/licenses/BSD-3-Clause)
richset interporlates with set, dict, and list.
## Motivations and Concepts
- richset 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
```sh
$ pip install richset
```
## Usage
```python
from dataclasses import dataclass
from richset import RichSet
@dataclass(frozen=True)
class Something:
id: int
name: str
richset = RichSet.from_list([
Something(1, 'one'),
Something(2, 'two'),
Something(3, 'three'),
])
```
### Conversions
```python
richset.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(lambda s: s.id) # => {1: Something(1, 'one'), 2: Something(2, 'two'), 3: Something(3, 'three')}
```
`to_dict()` takes second argument `duplicated` which is a choice of `'error'`, `'first'` or `'last'`.
- if `duplicated` is `'error'`, then `to_dict()` raises `ValueError` if there is a duplicated key.
- if `duplicated` is `'first'`, then `to_dict()` picks the first one of duplicated key.
- if `duplicated` is `'last'`, then `to_dict()` picks the last one of duplicated key.
`to_dict_of_list()` is similar to `to_dict()` but it returns a dict of list.
```python
richset.to_dict_of_list(lambda s: s.name) # => {'john': [Something(1, 'john'), Something(2, 'john')], 'jane': [Something(3, 'jane')]}
```
### List accessors
```python
richset.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_nth` and `get_one` accept `default` argument that returns specified value instead of None.
```python
richset.get_nth(100, default=Something(-1, 'default')) # => Something(-1, 'default')
```
### List basic manipulations
```python
richset.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()` and `unshift_all()` are similar to `pushed()` and `unshift()` but they accept multiple items.
- `popped_n()` and `shift_n()` are similar to `popped()` and `shift()` but they accept count of items.
### List functional manipulations
```python
richset.unique(lambda s: s.id) # => unique by id
richset.map(lambda s: s.id).to_list() # => [1, 2]
richset.filter(lambda s: s.id > 1).to_list() # => [Something(2, 'two'), Something(3, 'three')]
```
### Search
```python
richset.index(lambda s: s.id == 2) # => 1
richset.indices(lambda s: s.id == 2) # => [1]
richset.search_first(lambda s: s.id == 2) # => Something(2, 'two')
richset.search_last(lambda s: s.id == 2) # => Something(2, 'two')
richset.search_all(lambda s: s.id == 2) # => [Something(2, 'two')]
richset.contains(lambda s: s.id == 2) # => True
richset.has(Something(2, 'two')) # => True
```
### Sorts
```python
richset.sorted(key=lambda s: 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')]
```
### Statistics
```python
richset.is_empty() # => True if empty
richset.is_not_empty() # => True if not empty
richset.size() # => 3
richset.count(lambda s: s.id > 1) # => 2
```
### Set operations
```python
richset = 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'),
])
```
```python
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')}
```
Also `is_subset()`, `is_superset()`, `is_disjoint()`, `is_equal_as_set()` and `zip_longest()` are available.
### Grouping
```python
richset.group_by(lambda item: 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(lambda item: item.id % 2) # => {1: 2, 0: 1}
richset.count_of_group_by(key=lambda item: item.id % 2, predicate=lambda item: item.name.startswith('t')) # => {1: 1, 0: 1}
richset.aggregate_by(key=lambda r: r.id % 2, fn=lambda a, b: a + b.name, initial='') # => {1: 'onethree', 0: 'two'}
```
## Paging
```python
richset.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')])]
```
# LICENSE
The 3-Clause BSD License. See also LICENSE file.
Raw data
{
"_id": null,
"home_page": null,
"name": "richset",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Yui KITSU <kitsuyui+github@kitsuyui.com>",
"download_url": "https://files.pythonhosted.org/packages/2d/a8/4399b98afb0942d5686f5a7d4bfda5d017042158873a5be06964299e6f84/richset-0.4.1.tar.gz",
"platform": null,
"description": "# richset\n\n[](https://badge.fury.io/py/richset)\n[](https://pypi.python.org/pypi/richset/)\n[](https://codecov.io/gh/kitsuyui/python-richset)\n[](https://opensource.org/licenses/BSD-3-Clause)\n\nrichset interporlates with set, dict, and list.\n\n## Motivations and Concepts\n\n- richset provides useful functions for common use cases of set, dict, and list.\n- builtin set, dict and list requires many boilerplate codes.\n- Pure Python package.\n- Fully typing supported.\n- No magic. No meta-programming.\n\n## Install\n\n```sh\n$ pip install richset\n```\n\n## Usage\n\n```python\nfrom dataclasses import dataclass\nfrom richset import RichSet\n\n\n@dataclass(frozen=True)\nclass Something:\n id: int\n name: str\n\n\nrichset = RichSet.from_list([\n Something(1, 'one'),\n Something(2, 'two'),\n Something(3, 'three'),\n])\n```\n\n### Conversions\n\n```python\nrichset.to_list() # => [Something(1, 'one'), Something(2, 'one'), Something(3, 'three')]\nrichset.to_tuple() # => (Something(1, 'one'), Something(2, 'two'), Something(3, 'three'))\nrichset.to_set() # => {Something(1, 'one'), Something(2, 'two'), Something(3, 'three')}\nrichset.to_frozenset() # => frozenset({Something(1, 'one'), Something(2, 'two'), Something(3, 'three')})\nrichset.to_dict(lambda s: s.id) # => {1: Something(1, 'one'), 2: Something(2, 'two'), 3: Something(3, 'three')}\n```\n\n`to_dict()` takes second argument `duplicated` which is a choice of `'error'`, `'first'` or `'last'`.\n\n- if `duplicated` is `'error'`, then `to_dict()` raises `ValueError` if there is a duplicated key.\n- if `duplicated` is `'first'`, then `to_dict()` picks the first one of duplicated key.\n- if `duplicated` is `'last'`, then `to_dict()` picks the last one of duplicated key.\n\n`to_dict_of_list()` is similar to `to_dict()` but it returns a dict of list.\n\n```python\nrichset.to_dict_of_list(lambda s: s.name) # => {'john': [Something(1, 'john'), Something(2, 'john')], 'jane': [Something(3, 'jane')]}\n```\n\n### List accessors\n\n```python\nrichset.first() # => returns first item `Something(1, 'one')` or raise Error (if empty)\nrichset.get_first() # => returns first item `Something(1, 'one')` or None (if empty)\nrichset.last() # => returns last item `Something(3, 'three')` or raise Error (if empty)\nrichset.get_last() # => returns last item `Something(3, 'three')` or None (if empty)\nrichset.nth(2) # => returns 3rd item `Something(3, 'three')` or raise Error (if empty)\nrichset.get_nth(2) # => returns 3rd item `Something(3, 'three')` or None (if empty)\nrichset.one() # => returns one item `Something(1, 'one')` or raise Error (if empty)\nrichset.get_one() # => returns one item `Something(1, 'one')` or None (if empty)\n```\n\nNote: `get_first`, `get_last`, `get_nth` and `get_one` accept `default` argument that returns specified value instead of None.\n\n```python\nrichset.get_nth(100, default=Something(-1, 'default')) # => Something(-1, 'default')\n```\n\n### List basic manipulations\n\n```python\nrichset.pushed(Something(4, 'four')).to_list() # => [Something(1, 'one'), Something(2, 'two'), Something(3, 'three'), Something(4, 'four')]\nrichset.unshift(Something(4, 'four')).to_list() # => [Something(4, 'four'), Something(1, 'one'), Something(2, 'two'), Something(3, 'three')]\nrichset.popped() # => Something(3, 'three'), RichSet([Something(1, 'one'), Something(2, 'two')])\nrichset.shift() # => Something(1, 'one'), RichSet([Something(2, 'two'), Something(3, 'three')])\nrichset.slice(1, 2).to_list() # => [Something(2, 'two')]\nrichset.divide_at(1) # => RichSet([Something(1, 'one')]), RichSet([Something(2, 'two'), Something(3, 'three')])\n```\n\n- `pushed_all()` and `unshift_all()` are similar to `pushed()` and `unshift()` but they accept multiple items.\n- `popped_n()` and `shift_n()` are similar to `popped()` and `shift()` but they accept count of items.\n\n### List functional manipulations\n\n```python\nrichset.unique(lambda s: s.id) # => unique by id\nrichset.map(lambda s: s.id).to_list() # => [1, 2]\nrichset.filter(lambda s: s.id > 1).to_list() # => [Something(2, 'two'), Something(3, 'three')]\n```\n\n### Search\n\n```python\nrichset.index(lambda s: s.id == 2) # => 1\nrichset.indices(lambda s: s.id == 2) # => [1]\nrichset.search_first(lambda s: s.id == 2) # => Something(2, 'two')\nrichset.search_last(lambda s: s.id == 2) # => Something(2, 'two')\nrichset.search_all(lambda s: s.id == 2) # => [Something(2, 'two')]\nrichset.contains(lambda s: s.id == 2) # => True\nrichset.has(Something(2, 'two')) # => True\n```\n\n### Sorts\n\n```python\nrichset.sorted(key=lambda s: s.name, reverse=True).to_list() # => [Something(2, 'two'), Something(3, 'three'), Something(1, 'one')]\nrichset.reversed().to_list() # => [Something(3, 'three'), Something(2, 'two'), Something(1, 'one')]\n```\n\n### Statistics\n\n```python\nrichset.is_empty() # => True if empty\nrichset.is_not_empty() # => True if not empty\nrichset.size() # => 3\nrichset.count(lambda s: s.id > 1) # => 2\n```\n\n### Set operations\n\n```python\nrichset = RichSet.from_list([\n Something(3, 'three'),\n Something(4, 'four'),\n Something(5, 'five'),\n])\nrichset2 = RichSet.from_list([\n Something(3, 'three'),\n Something(4, 'four'),\n Something(6, 'six'),\n])\n```\n\n```python\nrichset.union(richset2).to_set() # => {Something(3, 'three'), Something(4, 'four'), Something(5, 'five'), Something(6, 'six')}\nrichset.intersection(richset2).to_set() # => {Something(3, 'three'), Something(4, 'four')}\nrichset.difference(richset2).to_set() # => {Something(5, 'five')}\nrichset.symmetric_difference(richset2).to_set() # => {Something(5, 'five'), Something(6, 'six')}\nrichset.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'))}\nrichset.zip(richset2).to_set() # => {(Something(3, 'three'), Something(3, 'three')), (Something(4, 'four'), Something(4, 'four')), (Something(5, 'five'), Something(6, 'six')}\n```\n\nAlso `is_subset()`, `is_superset()`, `is_disjoint()`, `is_equal_as_set()` and `zip_longest()` are available.\n\n### Grouping\n\n```python\nrichset.group_by(lambda item: item.id % 2) # => {1: RichSet(records=(Something(id=1, name='one'), Something(id=3, name='three'))), 0: RichSet(records=(Something(id=2, name='two'),))}\nrichset.size_of_group_by(lambda item: item.id % 2) # => {1: 2, 0: 1}\nrichset.count_of_group_by(key=lambda item: item.id % 2, predicate=lambda item: item.name.startswith('t')) # => {1: 1, 0: 1}\nrichset.aggregate_by(key=lambda r: r.id % 2, fn=lambda a, b: a + b.name, initial='') # => {1: 'onethree', 0: 'two'}\n```\n\n## Paging\n\n```python\nrichset.page(1, 2).to_list() # => [Something(1, 'one'), Something(2, 'two')]\nrichset.split_into_pages(2).to_list() # => [RichSet([Something(1, 'one'), Something(2, 'two')]), RichSet([Something(3, 'three')])]\n```\n\n# LICENSE\n\nThe 3-Clause BSD License. See also LICENSE file.\n",
"bugtrack_url": null,
"license": "Copyright 2022-2025 Kitsu Yui\n \n Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n \n 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n \n 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n \n 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n \n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n ",
"summary": "richset interpolate between list, dict, set and iterables.",
"version": "0.4.1",
"project_urls": {
"Homepage": "https://github.com/kitsuyui/python-richset"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b3de96e14779f7562aa8130bb49e2fd98e36369bc53c8bfe935804f8bca5f4d1",
"md5": "cd4f465dbf7d751a63adf08b3bd73b89",
"sha256": "298bd63e2884347a634cd22f5c1604a7f5ff58bf3bf4cb1f21ed9e1542775d79"
},
"downloads": -1,
"filename": "richset-0.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cd4f465dbf7d751a63adf08b3bd73b89",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 9364,
"upload_time": "2025-01-31T18:12:33",
"upload_time_iso_8601": "2025-01-31T18:12:33.286237Z",
"url": "https://files.pythonhosted.org/packages/b3/de/96e14779f7562aa8130bb49e2fd98e36369bc53c8bfe935804f8bca5f4d1/richset-0.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "2da84399b98afb0942d5686f5a7d4bfda5d017042158873a5be06964299e6f84",
"md5": "ef92fc2153ad9ee176682df9aa8c6959",
"sha256": "5e79709867b0db75da21fc14dc68ab6f4685e2223eb4c5f94845af67510d9d34"
},
"downloads": -1,
"filename": "richset-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "ef92fc2153ad9ee176682df9aa8c6959",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 40278,
"upload_time": "2025-01-31T18:12:34",
"upload_time_iso_8601": "2025-01-31T18:12:34.660558Z",
"url": "https://files.pythonhosted.org/packages/2d/a8/4399b98afb0942d5686f5a7d4bfda5d017042158873a5be06964299e6f84/richset-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-31 18:12:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kitsuyui",
"github_project": "python-richset",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "richset"
}