Advertisement
treyhunner

read_chunks on Path 2

Apr 5th, 2019
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import os
  2. import pathlib
  3.  
  4.  
  5. class Path(pathlib.Path):
  6.     def __new__(cls, *args, **kwargs):
  7.         if cls is Path:
  8.             cls = WindowsPath if os.name == 'nt' else PosixPath
  9.         self = cls._from_parts(args, init=False)
  10.         if not self._flavour.is_supported:
  11.             raise NotImplementedError("cannot instantiate %r on your system"
  12.                                       % (cls.__name__,))
  13.         self._init()
  14.         return self
  15.     def read_chunks(self, size=1024, *, mode='rb'):
  16.         with self.open(mode) as f:
  17.             sentinel = b'' if 'b' in mode else ''
  18.             for chunk in iter(lambda: f.read(size), sentinel):
  19.                 yield chunk
  20.  
  21.  
  22. class PosixPath(Path, pathlib.PurePosixPath):
  23.     __slots__ = ()
  24.  
  25.  
  26. class WindowsPath(Path, pathlib.PureWindowsPath):
  27.     __slots__ = ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement