Skip to content

Commit

Permalink
Remove more-itertools dependency (#183)
Browse files Browse the repository at this point in the history
* edit dependencies

* implement own ichunked
  • Loading branch information
dholth authored Sep 5, 2024
1 parent bc60f48 commit f0d579a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
14 changes: 12 additions & 2 deletions conda_index/index/convert_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import os.path
import re
import sqlite3

from more_itertools import ichunked
from itertools import chain, islice

from . import common

Expand Down Expand Up @@ -196,6 +195,17 @@ def db_path(match, override_channel=None):
return f"{match['basename']}"


def ichunked(iterable, n):
"""
Break iterable into n-item iterables.
Lazier than Python 3.12 batched().
"""
source = iter(iterable)
for item in source:
yield islice(chain([item], source), n)


def convert_cache(conn, cache_generator):
"""
Convert old style `conda index` cache to sqlite cache.
Expand Down
19 changes: 19 additions & 0 deletions news/126-less-itertools
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* Remove `more-itertools` dependency. (#127)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dependencies = [
"conda-package-streaming >=0.7.0",
"filelock",
"jinja2",
"more-itertools",
"ruamel.yaml",
"zstandard",
]
Expand Down
1 change: 0 additions & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ requirements:
- conda-package-streaming
- filelock
- jinja2
- more-itertools
- ruamel.yaml

test:
Expand Down
49 changes: 49 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pathlib
import tempfile

from conda_index.index.convert_cache import ichunked
from conda_index.utils import file_contents_match


Expand All @@ -25,3 +26,51 @@ def test_file_contents_match():

assert file_contents_match(c, c)
assert file_contents_match(c, d)


def test_ichunked():
"""
Test laziness of our version of ichunked.
"""
CHUNK_SIZE = 5 # not divisible into total
TOTAL = 32
REMAINDER = TOTAL - (TOTAL // CHUNK_SIZE) * CHUNK_SIZE

consumed = -1
generated = 0

def counter():
nonlocal consumed
consumed += 1
return consumed

def counters():
nonlocal generated
for i in range(TOTAL):
generated = i
yield i, counter

print("More lazy version")
for chunk in ichunked(counters(), CHUNK_SIZE):
print("Batch")
chunk_size = 0
for i, c in chunk:
chunk_size += 1
count = c()
print(i, generated, count)
assert i == generated == count
assert chunk_size == CHUNK_SIZE or chunk_size == REMAINDER

try:
from itertools import batched
except ImportError:
return

# demonstrate that generated is sometimes greater than i, c() in
# gathers-into-tuples implementation
print("Less lazy version")
consumed = -1
for chunk in batched(counters(), CHUNK_SIZE):
print("Batch")
for i, c in chunk:
print(i, generated, c())

0 comments on commit f0d579a

Please sign in to comment.