forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into sqlite-serialize
Sync with main bco. pythonGH-27431
- Loading branch information
Showing
29 changed files
with
814 additions
and
388 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from itertools import filterfalse | ||
|
||
|
||
def unique_everseen(iterable, key=None): | ||
"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() | ||
seen_add = seen.add | ||
if key is None: | ||
for element in filterfalse(seen.__contains__, iterable): | ||
seen_add(element) | ||
yield element | ||
else: | ||
for element in iterable: | ||
k = key(element) | ||
if k not in seen: | ||
seen_add(k) | ||
yield element |
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,84 @@ | ||
import os | ||
import pathlib | ||
import types | ||
|
||
from typing import Union, Iterable, ContextManager, BinaryIO, TextIO | ||
|
||
from . import _common | ||
|
||
Package = Union[types.ModuleType, str] | ||
Resource = Union[str, os.PathLike] | ||
|
||
|
||
def open_binary(package: Package, resource: Resource) -> BinaryIO: | ||
"""Return a file-like object opened for binary reading of the resource.""" | ||
return (_common.files(package) / _common.normalize_path(resource)).open('rb') | ||
|
||
|
||
def read_binary(package: Package, resource: Resource) -> bytes: | ||
"""Return the binary contents of the resource.""" | ||
return (_common.files(package) / _common.normalize_path(resource)).read_bytes() | ||
|
||
|
||
def open_text( | ||
package: Package, | ||
resource: Resource, | ||
encoding: str = 'utf-8', | ||
errors: str = 'strict', | ||
) -> TextIO: | ||
"""Return a file-like object opened for text reading of the resource.""" | ||
return (_common.files(package) / _common.normalize_path(resource)).open( | ||
'r', encoding=encoding, errors=errors | ||
) | ||
|
||
|
||
def read_text( | ||
package: Package, | ||
resource: Resource, | ||
encoding: str = 'utf-8', | ||
errors: str = 'strict', | ||
) -> str: | ||
"""Return the decoded string of the resource. | ||
The decoding-related arguments have the same semantics as those of | ||
bytes.decode(). | ||
""" | ||
with open_text(package, resource, encoding, errors) as fp: | ||
return fp.read() | ||
|
||
|
||
def contents(package: Package) -> Iterable[str]: | ||
"""Return an iterable of entries in `package`. | ||
Note that not all entries are resources. Specifically, directories are | ||
not considered resources. Use `is_resource()` on each entry returned here | ||
to check if it is a resource or not. | ||
""" | ||
return [path.name for path in _common.files(package).iterdir()] | ||
|
||
|
||
def is_resource(package: Package, name: str) -> bool: | ||
"""True if `name` is a resource inside `package`. | ||
Directories are *not* resources. | ||
""" | ||
resource = _common.normalize_path(name) | ||
return any( | ||
traversable.name == resource and traversable.is_file() | ||
for traversable in _common.files(package).iterdir() | ||
) | ||
|
||
|
||
def path( | ||
package: Package, | ||
resource: Resource, | ||
) -> ContextManager[pathlib.Path]: | ||
"""A context manager providing a file path object to the resource. | ||
If the resource does not already exist on its own on the file system, | ||
a temporary file will be created. If the file was created, the file | ||
will be deleted upon exiting the context manager (no exception is | ||
raised if the file was deleted prior to the context manager | ||
exiting). | ||
""" | ||
return _common.as_file(_common.files(package) / _common.normalize_path(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
Oops, something went wrong.