Adds dynamically interface of iterator to class
Project description
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:
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.
After creating object use cast or assert function denoting that particular instance inherits
from collections.Iterator:
assert isinstance(my_object, collections.Iterator)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file IteratorDecorator-0.11.tar.gz.
File metadata
- Download URL: IteratorDecorator-0.11.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f46752b92746eef61185af7fa02e8d57e8ef96482305a54de8fdd9cec2ee8a9
|
|
| MD5 |
51c2ecd907bae07f2600a45852eb2b15
|
|
| BLAKE2b-256 |
a0a938fed639505851e777d0ed0072c26d10158ebbb4ef21a3789daf74fd0bd2
|
File details
Details for the file IteratorDecorator-0.11-py3-none-any.whl.
File metadata
- Download URL: IteratorDecorator-0.11-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fe827b0c309e78ad54a767186f37136cc811f54e39b3f75805b876570fbfc75
|
|
| MD5 |
365b51beee2fb16d9678e0652d7077d8
|
|
| BLAKE2b-256 |
ef265cb3626b80f11df14439744dfe4b65d8d18d55ca5a5bd578806b1ab69d16
|