iterproxy


Nameiterproxy JSON
Version 0.3.1 PyPI version JSON
download
home_pagehttps://github.com/MacHu-GWU/iterproxy-project
SummaryGive any iterable object capability to use .one(), .one_or_none(), .many(k), .skip(k), .all() API.
upload_time2023-11-02 17:54:26
maintainerUnknown
docs_urlNone
authorSanhe Hu
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            
.. .. image:: https://readthedocs.org/projects/iterproxy/badge/?version=latest
    :target: https://iterproxy.readthedocs.io/index.html
    :alt: Documentation Status

.. image:: https://github.com/MacHu-GWU/iterproxy-project/workflows/CI/badge.svg
    :target: https://github.com/MacHu-GWU/iterproxy-project/actions?query=workflow:CI

.. image:: https://codecov.io/gh/MacHu-GWU/iterproxy-project/branch/main/graph/badge.svg
    :target: https://codecov.io/gh/MacHu-GWU/iterproxy-project

.. image:: https://img.shields.io/pypi/v/iterproxy.svg
    :target: https://pypi.python.org/pypi/iterproxy

.. image:: https://img.shields.io/pypi/l/iterproxy.svg
    :target: https://pypi.python.org/pypi/iterproxy

.. image:: https://img.shields.io/pypi/pyversions/iterproxy.svg
    :target: https://pypi.python.org/pypi/iterproxy

.. image:: https://img.shields.io/badge/Release_History!--None.svg?style=social
    :target: https://github.com/MacHu-GWU/iterproxy-project/blob/main/release-history.rst

.. image:: https://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social
    :target: https://github.com/MacHu-GWU/iterproxy-project

------


.. .. image:: https://img.shields.io/badge/Link-Document-blue.svg
    :target: https://iterproxy.readthedocs.io/index.html

.. .. image:: https://img.shields.io/badge/Link-API-blue.svg
    :target: https://iterproxy.readthedocs.io/py-modindex.html

.. .. image:: https://img.shields.io/badge/Link-Source_Code-blue.svg
    :target: https://iterproxy.readthedocs.io/py-modindex.html

.. image:: https://img.shields.io/badge/Link-Install-blue.svg
    :target: `install`_

.. image:: https://img.shields.io/badge/Link-GitHub-blue.svg
    :target: https://github.com/MacHu-GWU/iterproxy-project

.. image:: https://img.shields.io/badge/Link-Submit_Issue-blue.svg
    :target: https://github.com/MacHu-GWU/iterproxy-project/issues

.. image:: https://img.shields.io/badge/Link-Request_Feature-blue.svg
    :target: https://github.com/MacHu-GWU/iterproxy-project/issues

.. image:: https://img.shields.io/badge/Link-Download-blue.svg
    :target: https://pypi.org/pypi/iterproxy#files


⏩ Welcome to ``iterproxy`` Documentation
==============================================================================
You may seen the following code style in many ORM framework, this pattern provides a user friendly API to access items from the iterator object:

.. code-block:: python

    query(...).one()
    query(...).one_or_none()
    query(...).many(3)
    query(...).all(5)
    query(...).skip(5).many(3)

`iterproxy <https://github.com/MacHu-GWU/iterproxy-project>`_ library can give any iterable object similar capabilities.


Usage Example
------------------------------------------------------------------------------
Convert any iterable object to a ``IterProxy`` object:

.. code-block:: python

    from iterproxy import IterProxy

    # Suppose you have an iterable object
    iterator = range(10)

    # Convert it to a IterProxy object
    proxy = IterProxy(iterator)

Access items from the ``IterProxy`` object:

.. code-block:: python

    proxy = IterProxy(range(10))

    proxy.one() # it will return 0
    proxy.many(3) # it will return [1, 2, 3]
    proxy.skip(2).many(2) # it will skip [4, 5] and return [6, 7]
    proxy.all() # it will return the rest [8, 9]
    proxy.one_or_none() # it will return None

``IterProxy.iter_chunks`` can group items into chunks having K items, the last chunk may have less items than K:

.. code-block:: python

    proxy = IterProxy(range(3))
    list(proxy.iter_chunks(2)) # it will return [[0, 1], [2]]

Another example:

.. code-block:: python

    proxy = IterProxy(range(10))
    proxy.all() # it will return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Of course the ``IterProxy`` itself is a iterator:

.. code-block:: python

    for i in IterProxy(range(10)): # 0, 1, 2, ...
        ...

You can use custom filter function to filter the result. Other than the nesting style in built-in ``filter`` function, it use chain pattern.

.. code-block:: python

    def is_odd(x):
        return x % 2 == 1

    def gte_5(x):
        return x >= 5

    # with IterProxy, you can chain them
    # it returns you [5, 7, 9]
    for i in IterProxy(range(10)).filter(is_odd).filter(gte_5):
        print(i)

    # or put them together, by default, it is logic and
    for i in IterProxy(range(10)).filter(is_odd, gte_5):
        print(i)

    # with the built-in filter, this is not that intuitive
    for i in filter(gte_5, filter(is_odd, range(10))):
        ...

You can also use compound logic ``and_``, ``or_``, ``not_``:

.. code-block:: python

    def is_odd(i):
        return i % 2

    def is_even(i):
        return not (i % 2)

    def lte_3(i):
        return i <= 3

    def gte_4(i):
        return i >= 4

    def lte_6(i):
        return i <= 6

    def gte_7(i):
        return i >= 7

    IterProxy(range(10)).filter(and_(gte_4, lte_6)).all() # [4, 5, 6]
    IterProxy(range(10)).filter(or_(lte_3, gte_7)).all() # [0, 1, 2, 3, 7, 8, 9]
    IterProxy(range(10)).filter(not_(is_odd)).all() # [0, 2, 4, 6, 8]

    # of course you can nest and_, or_, not_
    IterProxy(range(10)).filter(not_(and_(is_odd, or_(lte_3, gte_7)))).all() # [0, 2, 4, 5, 6, 8]

(Advanced) In order to enable type hint, you can do:

.. code-block:: python

    from iterproxy import IterProxy

    class Dog:
        def bark(self):
            pass

    class DogIterProxy(IterProxy[Dog]): # subclass from IterProxy[${YourTypeHint}]
        pass

    many_dogs = [Dog(),]*10

    proxy = DogIterProxy(many_dogs)

    proxy.one_or_none().bark()
    for dog in proxy.many(2):
        dog.bark()
    for dog in proxy.skip(1).many(2):
        dog.bark()
    for dog in proxy.all():
        dog.bark()

    filtered_proxy = DogIterProxy(many_dogs).filter(lambda dog: True)
    filtered_proxy.one_or_none().bark()
    for dog in filtered_proxy.many(2):
        dog.bark()
    for dog in filtered_proxy.skip(1).many(2):
        dog.bark()
    for dog in filtered_proxy.all():
        dog.bark()


.. _install:

Install
------------------------------------------------------------------------------

``iterproxy`` is released on PyPI, so all you need is:

.. code-block:: console

    $ pip install iterproxy

To upgrade to latest version:

.. code-block:: console

    $ pip install --upgrade iterproxy

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/MacHu-GWU/iterproxy-project",
    "name": "iterproxy",
    "maintainer": "Unknown",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Sanhe Hu",
    "author_email": "husanhe@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/be/c8/6dddbff88a70d9c8d389b1a0d0670a357c1bf3a151c0b0c7cd24b8fb535a/iterproxy-0.3.1.tar.gz",
    "platform": "Windows",
    "description": "\n.. .. image:: https://readthedocs.org/projects/iterproxy/badge/?version=latest\n    :target: https://iterproxy.readthedocs.io/index.html\n    :alt: Documentation Status\n\n.. image:: https://github.com/MacHu-GWU/iterproxy-project/workflows/CI/badge.svg\n    :target: https://github.com/MacHu-GWU/iterproxy-project/actions?query=workflow:CI\n\n.. image:: https://codecov.io/gh/MacHu-GWU/iterproxy-project/branch/main/graph/badge.svg\n    :target: https://codecov.io/gh/MacHu-GWU/iterproxy-project\n\n.. image:: https://img.shields.io/pypi/v/iterproxy.svg\n    :target: https://pypi.python.org/pypi/iterproxy\n\n.. image:: https://img.shields.io/pypi/l/iterproxy.svg\n    :target: https://pypi.python.org/pypi/iterproxy\n\n.. image:: https://img.shields.io/pypi/pyversions/iterproxy.svg\n    :target: https://pypi.python.org/pypi/iterproxy\n\n.. image:: https://img.shields.io/badge/Release_History!--None.svg?style=social\n    :target: https://github.com/MacHu-GWU/iterproxy-project/blob/main/release-history.rst\n\n.. image:: https://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social\n    :target: https://github.com/MacHu-GWU/iterproxy-project\n\n------\n\n\n.. .. image:: https://img.shields.io/badge/Link-Document-blue.svg\n    :target: https://iterproxy.readthedocs.io/index.html\n\n.. .. image:: https://img.shields.io/badge/Link-API-blue.svg\n    :target: https://iterproxy.readthedocs.io/py-modindex.html\n\n.. .. image:: https://img.shields.io/badge/Link-Source_Code-blue.svg\n    :target: https://iterproxy.readthedocs.io/py-modindex.html\n\n.. image:: https://img.shields.io/badge/Link-Install-blue.svg\n    :target: `install`_\n\n.. image:: https://img.shields.io/badge/Link-GitHub-blue.svg\n    :target: https://github.com/MacHu-GWU/iterproxy-project\n\n.. image:: https://img.shields.io/badge/Link-Submit_Issue-blue.svg\n    :target: https://github.com/MacHu-GWU/iterproxy-project/issues\n\n.. image:: https://img.shields.io/badge/Link-Request_Feature-blue.svg\n    :target: https://github.com/MacHu-GWU/iterproxy-project/issues\n\n.. image:: https://img.shields.io/badge/Link-Download-blue.svg\n    :target: https://pypi.org/pypi/iterproxy#files\n\n\n\u23e9 Welcome to ``iterproxy`` Documentation\n==============================================================================\nYou may seen the following code style in many ORM framework, this pattern provides a user friendly API to access items from the iterator object:\n\n.. code-block:: python\n\n    query(...).one()\n    query(...).one_or_none()\n    query(...).many(3)\n    query(...).all(5)\n    query(...).skip(5).many(3)\n\n`iterproxy <https://github.com/MacHu-GWU/iterproxy-project>`_ library can give any iterable object similar capabilities.\n\n\nUsage Example\n------------------------------------------------------------------------------\nConvert any iterable object to a ``IterProxy`` object:\n\n.. code-block:: python\n\n    from iterproxy import IterProxy\n\n    # Suppose you have an iterable object\n    iterator = range(10)\n\n    # Convert it to a IterProxy object\n    proxy = IterProxy(iterator)\n\nAccess items from the ``IterProxy`` object:\n\n.. code-block:: python\n\n    proxy = IterProxy(range(10))\n\n    proxy.one() # it will return 0\n    proxy.many(3) # it will return [1, 2, 3]\n    proxy.skip(2).many(2) # it will skip [4, 5] and return [6, 7]\n    proxy.all() # it will return the rest [8, 9]\n    proxy.one_or_none() # it will return None\n\n``IterProxy.iter_chunks`` can group items into chunks having K items, the last chunk may have less items than K:\n\n.. code-block:: python\n\n    proxy = IterProxy(range(3))\n    list(proxy.iter_chunks(2)) # it will return [[0, 1], [2]]\n\nAnother example:\n\n.. code-block:: python\n\n    proxy = IterProxy(range(10))\n    proxy.all() # it will return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nOf course the ``IterProxy`` itself is a iterator:\n\n.. code-block:: python\n\n    for i in IterProxy(range(10)): # 0, 1, 2, ...\n        ...\n\nYou can use custom filter function to filter the result. Other than the nesting style in built-in ``filter`` function, it use chain pattern.\n\n.. code-block:: python\n\n    def is_odd(x):\n        return x % 2 == 1\n\n    def gte_5(x):\n        return x >= 5\n\n    # with IterProxy, you can chain them\n    # it returns you [5, 7, 9]\n    for i in IterProxy(range(10)).filter(is_odd).filter(gte_5):\n        print(i)\n\n    # or put them together, by default, it is logic and\n    for i in IterProxy(range(10)).filter(is_odd, gte_5):\n        print(i)\n\n    # with the built-in filter, this is not that intuitive\n    for i in filter(gte_5, filter(is_odd, range(10))):\n        ...\n\nYou can also use compound logic ``and_``, ``or_``, ``not_``:\n\n.. code-block:: python\n\n    def is_odd(i):\n        return i % 2\n\n    def is_even(i):\n        return not (i % 2)\n\n    def lte_3(i):\n        return i <= 3\n\n    def gte_4(i):\n        return i >= 4\n\n    def lte_6(i):\n        return i <= 6\n\n    def gte_7(i):\n        return i >= 7\n\n    IterProxy(range(10)).filter(and_(gte_4, lte_6)).all() # [4, 5, 6]\n    IterProxy(range(10)).filter(or_(lte_3, gte_7)).all() # [0, 1, 2, 3, 7, 8, 9]\n    IterProxy(range(10)).filter(not_(is_odd)).all() # [0, 2, 4, 6, 8]\n\n    # of course you can nest and_, or_, not_\n    IterProxy(range(10)).filter(not_(and_(is_odd, or_(lte_3, gte_7)))).all() # [0, 2, 4, 5, 6, 8]\n\n(Advanced) In order to enable type hint, you can do:\n\n.. code-block:: python\n\n    from iterproxy import IterProxy\n\n    class Dog:\n        def bark(self):\n            pass\n\n    class DogIterProxy(IterProxy[Dog]): # subclass from IterProxy[${YourTypeHint}]\n        pass\n\n    many_dogs = [Dog(),]*10\n\n    proxy = DogIterProxy(many_dogs)\n\n    proxy.one_or_none().bark()\n    for dog in proxy.many(2):\n        dog.bark()\n    for dog in proxy.skip(1).many(2):\n        dog.bark()\n    for dog in proxy.all():\n        dog.bark()\n\n    filtered_proxy = DogIterProxy(many_dogs).filter(lambda dog: True)\n    filtered_proxy.one_or_none().bark()\n    for dog in filtered_proxy.many(2):\n        dog.bark()\n    for dog in filtered_proxy.skip(1).many(2):\n        dog.bark()\n    for dog in filtered_proxy.all():\n        dog.bark()\n\n\n.. _install:\n\nInstall\n------------------------------------------------------------------------------\n\n``iterproxy`` is released on PyPI, so all you need is:\n\n.. code-block:: console\n\n    $ pip install iterproxy\n\nTo upgrade to latest version:\n\n.. code-block:: console\n\n    $ pip install --upgrade iterproxy\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Give any iterable object capability to use .one(), .one_or_none(), .many(k), .skip(k), .all() API.",
    "version": "0.3.1",
    "project_urls": {
        "Download": "https://pypi.python.org/pypi/iterproxy/0.3.1#downloads",
        "Homepage": "https://github.com/MacHu-GWU/iterproxy-project"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c51a0076370bd1c7f16bab017378d5258b7396a70d5552296399e644da2b7421",
                "md5": "b197d07c20292b0f3eb86ebc8653730f",
                "sha256": "bf52ad378298db94866631ad78cc3e0332c897ba5f2cd53a02ba5a900a932f70"
            },
            "downloads": -1,
            "filename": "iterproxy-0.3.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b197d07c20292b0f3eb86ebc8653730f",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 8247,
            "upload_time": "2023-11-02T17:54:24",
            "upload_time_iso_8601": "2023-11-02T17:54:24.939635Z",
            "url": "https://files.pythonhosted.org/packages/c5/1a/0076370bd1c7f16bab017378d5258b7396a70d5552296399e644da2b7421/iterproxy-0.3.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bec86dddbff88a70d9c8d389b1a0d0670a357c1bf3a151c0b0c7cd24b8fb535a",
                "md5": "9358ac9f58f264e2c06fa46d05275682",
                "sha256": "a832d71822e5f658cb3e9c7624496383b57c43c7f51a3aa4eb1f4b986fdd1317"
            },
            "downloads": -1,
            "filename": "iterproxy-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "9358ac9f58f264e2c06fa46d05275682",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 11509,
            "upload_time": "2023-11-02T17:54:26",
            "upload_time_iso_8601": "2023-11-02T17:54:26.779314Z",
            "url": "https://files.pythonhosted.org/packages/be/c8/6dddbff88a70d9c8d389b1a0d0670a357c1bf3a151c0b0c7cd24b8fb535a/iterproxy-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-02 17:54:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "MacHu-GWU",
    "github_project": "iterproxy-project",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "iterproxy"
}
        
Elapsed time: 1.05194s