Name | chitter JSON |
Version |
0.2.2
JSON |
| download |
home_page | |
Summary | Fork of 'iterchain', Chitter allows you to chain iters. |
upload_time | 2023-12-10 02:23:36 |
maintainer | |
docs_url | None |
author | Bruno Fauth |
requires_python | >=3.11,<4.0 |
license | GPLv3 |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<!--
vim: nospell
-->
<!-- [![Documentation
Status](https://readthedocs.org/projects/iterchain/badge/?version=latest)](https://iterchain.readthedocs.io/en/latest/?badge=latest)
-->
# Chitter: my take on iterator chaining for Python
## Introduction
Chitter is a fork Evelyn H's
[iterchain](https://github.com/Evelyn-H/iterchain.git) package, but made to be
mine. Anyway, this package makes working with iterators in python more
ergonomic by allowing you to chain methods on them (that's easier to read).
Similarly to Evelyn's this package's design is inspired by the [Rust
iterators](https://doc.rust-lang.org/std/iter/index.html), stuff from python's
own standard library
([itertools](https://docs.python.org/3/library/itertools.html)).
## Installation
With Poetry:
poetry add chitter
With Pip:
pip install chitter
For the development version:
git clone https://github.com/brunofauth/chitter.git
cd ./chitter/
poetry install
***Everything below this line comes straight from `iterchain`'s docs, so keep that in mind while reading...***
## Why would I need this?
Say we want to know the sum of all the squares of even numbers up to 100.
How can we do this?
Let's try some straightforward, procedural Python:
```python
>>> total = 0
>>> for i in range(100):
... if i % 2 is 0:
... total += i ** 2
...
>>> total
161700
```
This works, but if you read this for the first time it can take a bit of effort
to figure out what's happening, especially in slightly less trivial cases.
So, how about we use iterators instead?
Well, let's see:
```python
>>> sum(i**2 for i in range(100) if i % 2 is 0)
161700
```
That's pretty nice! Much shorter, and much easier to understand. But there's a
problem, this pattern only works for relatively simple manipulations. In those
cases you could try using the python `map` and `filter` builtins (and the
slightly more hidden `functools.reduce`). They let you construct more complex
processing chains.
Let's rewrite our iterator to use those functions instead:
```python
>>> sum(map(lambda x: x**2, filter(lambda x: x % 2 is 0, range(100))))
161700
```
Okay, now _that_ is a mess... I don't know about you, but it would take me
quite a while to unravel what's happening here. The problem is that the whole
expression is inside out. The `filter` gets applied first, but it's hidden in
the middle of the expression, and the `sum` gets applied last but it is all the
way in the front. Makes no sense...
So, how can we improve on this? `iterchain` of course!
(you probably saw this coming already)
So, let's see how it looks using `iterchain`:
```python
>>> import iterchain
>>> (iterchain.count(stop=100)
... .filter(lambda x: x % 2 is 0)
... .map(lambda x: x**2)
... .sum())
161700
```
Isn't this much better? The operations are listed in the order that they're
executed, are clearly separated, and you can have as few or as many operations
as you want. This is why you should use `iterchain`!
## Generators
`iterchain` also provides handy methods that let you build new `Iterator`
instances from scratch. These are contained in the `iterchain.generators`
sub-module, but they're also accessible directly from the `iterchain` module,
which is the preferred way of using them.
For example:
```
>>> import iterchain
>>> iterchain.count().take(4).map(lambda x: x**2).to_list()
[0, 1, 4, 9]
```
Raw data
{
"_id": null,
"home_page": "",
"name": "chitter",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Bruno Fauth",
"author_email": "149593@upf.br",
"download_url": "https://files.pythonhosted.org/packages/d2/05/79a3fdd3d56e5acebfd10639e789e465affae8cd3fe9b2a2da6032b3a147/chitter-0.2.2.tar.gz",
"platform": null,
"description": "<!--\n vim: nospell\n-->\n\n<!-- [![Documentation \nStatus](https://readthedocs.org/projects/iterchain/badge/?version=latest)](https://iterchain.readthedocs.io/en/latest/?badge=latest) \n-->\n\n# Chitter: my take on iterator chaining for Python\n\n## Introduction\n\nChitter is a fork Evelyn H's \n[iterchain](https://github.com/Evelyn-H/iterchain.git) package, but made to be \nmine. Anyway, this package makes working with iterators in python more \nergonomic by allowing you to chain methods on them (that's easier to read).\n\nSimilarly to Evelyn's this package's design is inspired by the [Rust \niterators](https://doc.rust-lang.org/std/iter/index.html), stuff from python's \nown standard library \n([itertools](https://docs.python.org/3/library/itertools.html)).\n\n## Installation\n\nWith Poetry:\n\n poetry add chitter\n\nWith Pip:\n\n pip install chitter\n\nFor the development version:\n\n git clone https://github.com/brunofauth/chitter.git\n cd ./chitter/\n poetry install\n\n***Everything below this line comes straight from `iterchain`'s docs, so keep that in mind while reading...***\n\n## Why would I need this?\n\nSay we want to know the sum of all the squares of even numbers up to 100. \nHow can we do this?\n\nLet's try some straightforward, procedural Python:\n```python\n>>> total = 0\n>>> for i in range(100):\n... if i % 2 is 0:\n... total += i ** 2\n...\n>>> total\n161700\n```\n\nThis works, but if you read this for the first time it can take a bit of effort \nto figure out what's happening, especially in slightly less trivial cases.\nSo, how about we use iterators instead?\n\nWell, let's see:\n```python\n>>> sum(i**2 for i in range(100) if i % 2 is 0)\n161700\n```\n\nThat's pretty nice! Much shorter, and much easier to understand. But there's a \nproblem, this pattern only works for relatively simple manipulations. In those \ncases you could try using the python `map` and `filter` builtins (and the \nslightly more hidden `functools.reduce`). They let you construct more complex \nprocessing chains.\n\nLet's rewrite our iterator to use those functions instead:\n```python\n>>> sum(map(lambda x: x**2, filter(lambda x: x % 2 is 0, range(100))))\n161700\n```\n\nOkay, now _that_ is a mess... I don't know about you, but it would take me \nquite a while to unravel what's happening here. The problem is that the whole \nexpression is inside out. The `filter` gets applied first, but it's hidden in \nthe middle of the expression, and the `sum` gets applied last but it is all the \nway in the front. Makes no sense...\n\nSo, how can we improve on this? `iterchain` of course! \n(you probably saw this coming already)\n\nSo, let's see how it looks using `iterchain`:\n```python\n>>> import iterchain\n>>> (iterchain.count(stop=100)\n... .filter(lambda x: x % 2 is 0)\n... .map(lambda x: x**2)\n... .sum())\n161700\n```\n\nIsn't this much better? The operations are listed in the order that they're \nexecuted, are clearly separated, and you can have as few or as many operations \nas you want. This is why you should use `iterchain`!\n\n\n## Generators\n\n`iterchain` also provides handy methods that let you build new `Iterator` \ninstances from scratch. These are contained in the `iterchain.generators` \nsub-module, but they're also accessible directly from the `iterchain` module, \nwhich is the preferred way of using them.\n\nFor example:\n```\n >>> import iterchain\n >>> iterchain.count().take(4).map(lambda x: x**2).to_list()\n [0, 1, 4, 9]\n```\n\n",
"bugtrack_url": null,
"license": "GPLv3",
"summary": "Fork of 'iterchain', Chitter allows you to chain iters.",
"version": "0.2.2",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d20579a3fdd3d56e5acebfd10639e789e465affae8cd3fe9b2a2da6032b3a147",
"md5": "c599285aeb6652f4992abee6ddac9f6c",
"sha256": "41127a48875c5ffca8e579611bf3d064dfe2877a8d0ed63958ff7fd982efaf3a"
},
"downloads": -1,
"filename": "chitter-0.2.2.tar.gz",
"has_sig": false,
"md5_digest": "c599285aeb6652f4992abee6ddac9f6c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11,<4.0",
"size": 17410,
"upload_time": "2023-12-10T02:23:36",
"upload_time_iso_8601": "2023-12-10T02:23:36.638468Z",
"url": "https://files.pythonhosted.org/packages/d2/05/79a3fdd3d56e5acebfd10639e789e465affae8cd3fe9b2a2da6032b3a147/chitter-0.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-10 02:23:36",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "chitter"
}