Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /trisongz_lazyops /src /lzo /utils /filestream.py
| """ | |
| File Streaming Support | |
| """ | |
| import typing as t | |
| from io import IOBase, TextIOWrapper | |
| # from typing import Iterable, Any, Union, Type, Optional | |
| class FileLikeObject(IOBase): | |
| def __init__( | |
| self, | |
| data: t.Iterable[t.Union[bytes, t.Any]], | |
| base: t.Type = bytes, | |
| # async_enabled: Optional[bool] = False, | |
| **kwargs, | |
| ): | |
| super().__init__() | |
| self.base: t.Type[bytes] = base | |
| self.chunk = base() | |
| self.offset = 0 | |
| self.iterator = iter(data) | |
| def up_to_iter(self, size: int) -> t.Iterable[t.Union[bytes, t.Any]]: | |
| """ | |
| Yield up to size bytes from the iterator. | |
| """ | |
| while size: | |
| if self.offset == len(self.chunk): | |
| try: self.chunk = next(self.iterator) | |
| except StopIteration: break | |
| else: self.offset = 0 | |
| to_yield = min(size, len(self.chunk) - self.offset) | |
| self.offset = self.offset + to_yield | |
| size -= to_yield | |
| yield self.chunk[self.offset - to_yield : self.offset] | |
| def readable(self): | |
| """ | |
| Return True if the stream can be read from. If False, read() will raise | |
| """ | |
| return True | |
| def writable(self): | |
| """ | |
| Return True if the stream supports writing. If False, write() will raise | |
| """ | |
| return False | |
| def read(self, size: int = -1) -> bytes: | |
| """ | |
| Read and return up to size bytes. If the argument is omitted, None, or | |
| """ | |
| return self.base().join( | |
| self.up_to_iter(float('inf') if size is None or size < 0 else size) | |
| ) | |
| def read1(self, size: int = -1) -> bytes: | |
| """ | |
| Read and return up to size bytes, with at most one call to the underlying | |
| """ | |
| return self.read(size) | |
| def as_line_iterator(self, newline: t.Optional[str] = '', encoding: t.Optional[str] = 'utf-8', **kwargs) -> t.Iterable[str]: | |
| """ | |
| Return an iterator the yields lines from the stream. | |
| """ | |
| return TextIOWrapper(self, newline=newline, encoding=encoding, **kwargs) | |
| class AsyncFileLikeObject(IOBase): | |
| def __init__( | |
| self, | |
| data: t.Iterable[t.Union[bytes, t.Any]], | |
| base: t.Type = bytes, | |
| **kwargs, | |
| ): | |
| super().__init__() | |
| self.base: t.Type[bytes] = base | |
| self.chunk = base() | |
| self.offset = 0 | |
| self.iterator = aiter(data) | |
| async def up_to_iter(self, size: int) -> t.Iterable[t.Union[bytes, t.Any]]: | |
| """ | |
| Yield up to size bytes from the iterator. | |
| """ | |
| while size: | |
| if self.offset == len(self.chunk): | |
| try: self.chunk = await anext(self.iterator) | |
| except StopIteration: break | |
| else: self.offset = 0 | |
| to_yield = min(size, len(self.chunk) - self.offset) | |
| self.offset += to_yield | |
| size -= to_yield | |
| yield self.chunk[self.offset - to_yield : self.offset] | |
| def readable(self): | |
| """ | |
| Return True if the stream can be read from. If False, read() will raise | |
| """ | |
| return True | |
| def writable(self): | |
| """ | |
| Return True if the stream supports writing. If False, write() will raise | |
| """ | |
| return False | |
| async def read(self, size: int = -1) -> bytes: | |
| """ | |
| Read and return up to size bytes. If the argument is omitted, None, or | |
| """ | |
| b = [] | |
| async for chunk in self.up_to_iter(float('inf') if size is None or size < 0 else size): | |
| b += chunk | |
| return self.base().join(b) | |
Xet Storage Details
- Size:
- 3.69 kB
- Xet hash:
- ca94d0397454a7178eb80a3c156793172cd462660df2bbf59d53748e23bbea2f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.