-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
241 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
from itertools import filterfalse | ||
# from more_itertools 9.0 | ||
def only(iterable, default=None, too_long=None): | ||
"""If *iterable* has only one item, return it. | ||
If it has zero items, return *default*. | ||
If it has more than one item, raise the exception given by *too_long*, | ||
which is ``ValueError`` by default. | ||
>>> only([], default='missing') | ||
'missing' | ||
>>> only([1]) | ||
1 | ||
>>> only([1, 2]) # doctest: +IGNORE_EXCEPTION_DETAIL | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Expected exactly one item in iterable, but got 1, 2, | ||
and perhaps more.' | ||
>>> only([1, 2], too_long=TypeError) # doctest: +IGNORE_EXCEPTION_DETAIL | ||
Traceback (most recent call last): | ||
... | ||
TypeError | ||
Note that :func:`only` attempts to advance *iterable* twice to ensure there | ||
is only one item. See :func:`spy` or :func:`peekable` to check | ||
iterable contents less destructively. | ||
""" | ||
it = iter(iterable) | ||
first_value = next(it, default) | ||
|
||
from typing import ( | ||
Callable, | ||
Iterable, | ||
Iterator, | ||
Optional, | ||
Set, | ||
TypeVar, | ||
Union, | ||
) | ||
|
||
# Type and type variable definitions | ||
_T = TypeVar('_T') | ||
_U = TypeVar('_U') | ||
|
||
|
||
def unique_everseen( | ||
iterable: Iterable[_T], key: Optional[Callable[[_T], _U]] = None | ||
) -> Iterator[_T]: | ||
"List unique elements, preserving order. Remember all elements ever seen." | ||
# unique_everseen('AAAABBBCCDAABBB') --> A B C D | ||
# unique_everseen('ABBCcAD', str.lower) --> A B C D | ||
seen: Set[Union[_T, _U]] = set() | ||
seen_add = seen.add | ||
if key is None: | ||
for element in filterfalse(seen.__contains__, iterable): | ||
seen_add(element) | ||
yield element | ||
try: | ||
second_value = next(it) | ||
except StopIteration: | ||
pass | ||
else: | ||
for element in iterable: | ||
k = key(element) | ||
if k not in seen: | ||
seen_add(k) | ||
yield element | ||
msg = ( | ||
'Expected exactly one item in iterable, but got {!r}, {!r}, ' | ||
'and perhaps more.'.format(first_value, second_value) | ||
) | ||
raise too_long or ValueError(msg) | ||
|
||
return first_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Lib/test/test_importlib/resources/data02/subdirectory/subsubdir/resource.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a resource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
import contextlib | ||
import pathlib | ||
|
||
from test.support import os_helper | ||
|
||
from importlib import resources | ||
from importlib.resources.abc import TraversableResources, ResourceReader | ||
from . import util | ||
|
||
|
||
class SimpleLoader: | ||
""" | ||
A simple loader that only implements a resource reader. | ||
""" | ||
|
||
def __init__(self, reader: ResourceReader): | ||
self.reader = reader | ||
|
||
def get_resource_reader(self, package): | ||
return self.reader | ||
|
||
|
||
class MagicResources(TraversableResources): | ||
""" | ||
Magically returns the resources at path. | ||
""" | ||
|
||
def __init__(self, path: pathlib.Path): | ||
self.path = path | ||
|
||
def files(self): | ||
return self.path | ||
|
||
|
||
class CustomTraversableResourcesTests(unittest.TestCase): | ||
def setUp(self): | ||
self.fixtures = contextlib.ExitStack() | ||
self.addCleanup(self.fixtures.close) | ||
|
||
def test_custom_loader(self): | ||
temp_dir = self.fixtures.enter_context(os_helper.temp_dir()) | ||
loader = SimpleLoader(MagicResources(temp_dir)) | ||
pkg = util.create_package_from_loader(loader) | ||
files = resources.files(pkg) | ||
assert files is temp_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.