views
Project description
views – efficient tools for generators and sequences
The views module provides additions to the existing python comprehensions and generator expressions. (Chained) sequence views can be made using seq and generators can be chained with gen, as shown in the examples below.
Any feedback or suggestions are very welcome.
Email: koos.zevenhoven@aalto.fi
Twitter: @k7hoven
Getting Started
Requirements
Python 3.6+
Installation
The package can be installed with pip (make sure you have it installed):
pip3 install git+http://github.com/k7hoven/views
Or if your default python is Python 3:
pip install git+http://github.com/k7hoven/views
Basic Usage
Basic introductory examples here, but they should get you started.
Sequence view comprehension syntax
You can change chain single objects and sequences into one sequence view. Use :: just like you would use * in tuple (un)packing. The resulting object supports slicing and indexing.
Example:
>>> from views import seq
>>> seq[::range(3), None, ::"abc", "Hi!"]
<sequence view 8: [0, 1, 2, None, 'a', 'b', 'c', 'Hi!'] >
>>> seq[::range(100)]
<sequence view 100: [0, 1, 2, 3, 4, ..., 96, 97, 98, 99] >
Generator comprehension syntax
Use like seq. The resulting object is a generator.
Example:
>>> from views import gen
>>> list(gen[::range(3), 3, 4, ::range(5,7), 7])
[0, 1, 2, 3, 4, 5, 6, 7]
Chaining sequences and generators/iterables
You can chain an arbitrary number of sequences with seq.chain(*sequences) and of generators with gen.chain(*iterables). The latter is equivalent to itertools.chain(*iterables).
Example:
>>> from views import seq, gen
>>> seq.chain([1, 2, 3], [4, 5, 6])
<sequence view 6: [1, 2, 3, 4, 5, 6] >
>>> list(gen.chain([1, 2, 3], [4, 5, 6]))
[1, 2, 3, 4, 5, 6]
Have fun!
Changelog
0.3.0 (2017-10-10)
Add seq.chain(*sequences) and gen.chain(*iterables)
Some speed optimizations, especially to seq.
0.2.0 (2017-09-11)
LengthChangedError is now a subclass of RuntimeError.
Minor optimizations to seq and gen.
0.1.0 (2017-06-06)
First version
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.