Skip to main content
https://travis-ci.org/stovorov/IteratorDecorator.svg?branch=master https://codecov.io/gh/stovorov/IteratorDecorator/branch/master/graph/badge.svg

IteratorDecorator

Implementing iterator in Python is nothing complicated though what’s missing is possibility to make it even easier. This small library adds iter_attribute decorator allowing to quickly choose iterable for which iterator would be implemented.

Requirements

Python3.5+

Example

from IteratorDecorator import iter_attribute


@iter_attribute('number')
class CaseClass:
    def __init__(self):
        self.number = [1, 2, 3, 4]
        self.attr = ['attr1', 'attr2', 'attr3']

obj = CaseClass()

for num in obj:
    print(num)

Installing

In your virtualenv just call:

$ pip install IteratorDecorator

Warning

When using PyCharm or MYPY you’ll probably see issues with decorated class not being recognized as Iterator. That’s an issue which I could not overcome yet, it’s probably due to the fact that interpretation of object is being done statically rather than dynamically. MYPY checks for definition of methods in class code which changes at runtime. Since __iter__ and __next__ are added dynamically MYPY cannot find those defined in objects before object of a class is created. Possible workarounds for this issue are:

  1. Define __iter__ method in class:

@iter_attribute('attr')
class Test:
    def __init__(self) -> None:
        self.attr = [1, 2, 3]

    def __iter__(self) -> 'Test':
        return self

Actually it does not have to be “real” __iter__ since it’ll be replaced by decorator implementation, but the definition is only needed for static checkers.

  1. After creating object use cast or assert function denoting that particular instance inherits

from collections.Iterator:

assert isinstance(my_object, collections.Iterator)

Metadata

Release files for IteratorDecorator 0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for IteratorDecorator 0.11
File Size Uploaded
IteratorDecorator-0.11.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for IteratorDecorator 0.11
File Interpreter ABI Platform
IteratorDecorator-0.11-py3-none-any.whl Python 3 none any Details

Total release size: 7.5 kB

Release files / IteratorDecorator-0.11.tar.gz

Download URL IteratorDecorator-0.11.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1f46752b92746eef61185af7fa02e8d57e8ef96482305a54de8fdd9cec2ee8a9
BLAKE2b-256 checksum
How to use checksums
a0a938fed639505851e777d0ed0072c26d10158ebbb4ef21a3789daf74fd0bd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Release files / IteratorDecorator-0.11-py3-none-any.whl

Download URL IteratorDecorator-0.11-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4fe827b0c309e78ad54a767186f37136cc811f54e39b3f75805b876570fbfc75
BLAKE2b-256 checksum
How to use checksums
ef265cb3626b80f11df14439744dfe4b65d8d18d55ca5a5bd578806b1ab69d16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page