You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider the following script that runs fine on my PC (with no output):
for lname in ['LZMA', 'BZ2', 'Gzip']:
lib = __import__(lname.lower())
cls = getattr(lib, lname + 'File')
# cls = getattr(lib, 'open') # uncomment to see that this also works
example_payload = b'a'
with cls('a.bin', 'w') as f:
f.write(example_payload)
with cls('a.bin', 'r') as f:
if f.read() != example_payload:
print('WTF!')
All three compression libraries in the Python standard library follow a pattern: there's an SomethingFile object that supports reading and writing just as if it was a regular file. Perhaps it makes sense to add such a class to python-zstandard as well?
The text was updated successfully, but these errors were encountered:
d33tah
changed the title
Add ZstandardFile class that supports 'r' and 'w', just like in Python's zstd
Add ZstandardFile class that supports 'r' and 'w', just like in Python's stdlib
Jul 31, 2020
d33tah
changed the title
Add ZstandardFile class that supports 'r' and 'w', just like in Python's stdlib
Add ZstandardFile class (and "open" method) that supports 'r' and 'w', just like in Python's stdlib
Jul 31, 2020
d33tah
changed the title
Add ZstandardFile class (and "open" method) that supports 'r' and 'w', just like in Python's stdlib
Add ZstandardFile class (and "open" method) that supports 'r' and 'w' modes, just like in Python's stdlib
Jul 31, 2020
import zstandard
import contextlib
old_open = open
@contextlib.contextmanager
def open(*, fileobj=None, file=None, mode='r'):
if mode not in ('r', 'w'):
raise ValueError('Mode should be "r" or "w")')
if not fileobj and not file:
raise ValueError('either fileobj or file must be provided')
if mode == "r":
cctx = zstandard.ZstdDecompressor()
if file:
with old_open(file, 'rb') as fh, cctx.stream_reader(fh) as reader:
yield reader
else:
yield cctx.stream_reader(fileobj)
elif mode == "w":
cctx = zstandard.ZstdCompressor()
if file:
with old_open(file, 'wb') as fh, cctx.stream_writer(fh) as writer:
yield writer
else:
yield cctx.stream_writer(fileobj)
Consider the following script that runs fine on my PC (with no output):
All three compression libraries in the Python standard library follow a pattern: there's an SomethingFile object that supports reading and writing just as if it was a regular file. Perhaps it makes sense to add such a class to python-zstandard as well?
The text was updated successfully, but these errors were encountered: