7 ms·
I like the spirit, however a lot of these can be done with a simple one liner... Example for chunked iteration: ``` def chunk_iter(some_seq, n): return (
by lqdc13 12y ago
I like the spirit, however a lot of these can be done with a simple one liner...
Example for chunked iteration:
```
def chunk_iter(some_seq, n):
return (seq[pos:pos + n] for pos in range(0, len(seq), n))
```
example for multi-dict: (unless I'm missing something)
```
from collections import defaultdict
d = defaultdict(list)
d['a'].append('b')
d['a'].append('c')
dict(d)
{'a': ['b', 'c']}
```
- has2k1 12y agoWhile your version are usable. They differ in the requirements fulfilled. e.g. chunk_iter - iterables not just sequences multi-dict - usability Plus nice error messages. The goal is to be standard library quality.
- mhashemi 12y agoThat's right! Boltons are meant to replace those one liners, unless you really really think downstream customers are going to be impressed with code golf birdies. ;)
- father_of_two 12y agoHere's my own version of an iterating chunker (itertools needed imports omitted). def chunks_of(it, n): return takewhile(len, imap(list, imap(islice, repeat(it), repeat(n)))) But this library functions adds error checking and are more expressive (I wrote the above and always have to look at it lenghty to understand it). I've not paid much attention yet to the license scheme, but if it's friendly to be used on commercial projects, I might take the plunge.
- mhashemi 12y agoBSD, go nuts!