Skip to content

Commit f4063c1

Browse files
authored
Merge pull request #3 from real-easypy/collection-methods
Collection methods
2 parents 7784f58 + 3ef20f3 commit f4063c1

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ python:
44
- '3.7'
55
- '3.6'
66
- '3.5'
7+
- 3.9-dev
78
- 3.8-dev
89
- 3.7-dev
910
- 3.6-dev
@@ -23,10 +24,12 @@ notifications:
2324
email:
2425
on_success: change
2526
on_failure: change
27+
if: branch != master
2628
jobs:
2729
include:
2830
- stage: deploy
2931
python: 3.8
32+
if: branch = master
3033
deploy:
3134
provider: pypi
3235
user: __token__

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.4.3] - 2020-04-28
8+
9+
### Added
10+
- collections: make `separate`, `grouped`, `chunkify` and `takesome` methods of the ObjectCollection classes
11+
12+
### Fixed
13+
- support for gevent 1.5
14+
715
## [0.4.2] - 2020-04-28
816

917
### Added

easypy/collections.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ def __add__(self, collection):
444444
return AggregateCollection([self, collection])
445445

446446

447+
def ObjectCollectionBaseMethod(func):
448+
setattr(ObjectCollectionBase, func.__name__, func)
449+
return func
450+
451+
447452
class AggregateCollection(ObjectCollectionBase):
448453
"""
449454
Dynamically aggregate other collections into one chained collection
@@ -790,6 +795,7 @@ def iter_filtered(self, *preds, **filters):
790795
return filtered(objects, preds, filters)
791796

792797

798+
@ObjectCollectionBaseMethod
793799
def grouped(sequence, key=None, transform=None):
794800
"""
795801
Parse the sequence into groups, according to key:
@@ -812,6 +818,7 @@ def grouped(sequence, key=None, transform=None):
812818
return groups
813819

814820

821+
@ObjectCollectionBaseMethod
815822
def separate(sequence, key=None):
816823
"""
817824
Partition the sequence into items that match and items that don't:
@@ -860,6 +867,7 @@ def listify(obj):
860867
return list(ilistify(obj))
861868

862869

870+
@ObjectCollectionBaseMethod
863871
def chunkify(sequence, size):
864872
"""
865873
Chunk a sequence into equal-size chunks (except for the last chunk):
@@ -984,6 +992,7 @@ def inner(*args, **kwargs):
984992
return inner
985993

986994

995+
@ObjectCollectionBaseMethod
987996
def takesome(generator, max=None, min=0):
988997
"""
989998
Take between ``min`` and ``max`` items from the generator.
@@ -1007,19 +1016,22 @@ def takesome(generator, max=None, min=0):
10071016
if max is not None and max < min:
10081017
raise ValueError("'max' must be great than 'min'")
10091018

1010-
items = [p for p, _ in zip(generator, range(min))]
1019+
generator = iter(generator) # just in case it ain't a generator
1020+
1021+
min_items = [p for _, p in zip(range(min), generator)]
10111022

1012-
if len(items) < min:
1013-
raise ValueError("Not enough items in sequence (wanted {}, got {})".format(min, len(items)))
1023+
if len(min_items) < min:
1024+
raise ValueError("Not enough items in sequence (wanted {}, got {})".format(min, len(min_items)))
10141025

1015-
yield from items
1026+
yield from min_items
10161027

10171028
if max is None:
1029+
# yield the rest
10181030
yield from generator
10191031
return
10201032

10211033
remain = max - min
1022-
for p, _ in zip(generator, range(remain)):
1034+
for _, p in zip(range(remain), generator):
10231035
yield p
10241036

10251037

0 commit comments

Comments
 (0)