22
22
Module description
23
23
==================
24
24
25
- This simple module provides a basic interface to input/output operations on
26
- files. Its key design feature is the ability to work with both uncompressed
27
- and compressed files through a unified interface, thus, making it easier
28
- for a user to deal with various types of compressed files. The compression
29
- types supported include gzip, bzip2, and lzma (xz).
25
+ This simple module provides a basic interface to input/output operations
26
+ on files. Its key design feature is the ability to work with both
27
+ uncompressed and compressed files through a unified interface, thus,
28
+ making it easier for a user to deal with various types of compressed
29
+ files. The compression types supported include gzip, bzip2, lzma (xz), and
30
+ zstandard (zstd).
30
31
31
32
The module is supposed to be mainly used by :mod:`pysat.formula`.
32
33
66
67
except ImportError : # for Python2 without lzma
67
68
lzma_present = False
68
69
70
+ zstd_present = True
71
+ try :
72
+ from compression import zstd
73
+ except ImportError : # zstandard is introduced in Python 3.14
74
+ zstd_present = False
75
+
76
+
69
77
70
78
#
71
79
#==============================================================================
@@ -75,9 +83,10 @@ class FileObject(object):
75
83
open files creating standard file pointers and closing them. The class
76
84
is used when opening DIMACS files for reading and writing. Supports
77
85
both uncompressed and compressed files. Compression algorithms
78
- supported are ``gzip``, ``bzip2``, and ``lzma``. Algorithm ``lzma`` can
79
- be used in Python 3 by default and also in Python 2 if the
80
- ``backports.lzma`` package is installed.
86
+ supported are ``gzip``, ``bzip2``, ``lzma``, and ``zstd``. Algorithm
87
+ ``lzma`` can be used in Python 3 by default and also in Python 2 if
88
+ the ``backports.lzma`` package is installed. Algorithm ``zstandard``
89
+ can be used in Python 3.14 and later.
81
90
82
91
Note that the class opens a file in text mode.
83
92
@@ -90,8 +99,9 @@ class FileObject(object):
90
99
:type compression: str
91
100
92
101
Compression type can be ``None``, ``'gzip'``, ``'bzip2'``, ``'lzma'``,
93
- as well as ``'use_ext'``. If ``'use_ext'`` is specified, compression
94
- algorithm is defined by the extension of the given file name.
102
+ ``'zstd'``, as well as ``'use_ext'``. If ``'use_ext'`` is specified,
103
+ compression algorithm is defined by the extension of the given file
104
+ name.
95
105
"""
96
106
97
107
def __init__ (self , name , mode = 'r' , compression = None ):
@@ -109,9 +119,9 @@ def __init__(self, name, mode='r', compression=None):
109
119
110
120
def open (self , name , mode = 'r' , compression = None ):
111
121
"""
112
- Open a file pointer. Note that a file is *always* opened in text
113
- mode. The method inherits its input parameters from the constructor
114
- of :class:`FileObject`.
122
+ Open a file pointer. Note that a file is *always* opened in
123
+ text mode. The method inherits its input parameters from the
124
+ constructor of :class:`FileObject`.
115
125
"""
116
126
117
127
if compression == 'use_ext' :
@@ -138,11 +148,15 @@ def open(self, name, mode='r', compression=None):
138
148
self .fp = codecs .getreader ('ascii' )(self .fp_extra )
139
149
else : # mode == 'w'
140
150
self .fp = codecs .getwriter ('ascii' )(self .fp_extra )
141
- else : # self.ctype == 'lzma'
151
+ elif self .ctype == 'lzma' :
142
152
# LZMA is available in Python 2 only if backports.lzma is installed
143
153
# Python 3 supports it by default
144
154
assert lzma_present , 'LZMA compression is unavailable.'
145
155
self .fp = lzma .open (name , mode = mode + 't' )
156
+ else : # self.ctype == 'zstd'
157
+ # Zstandard is available only in Python 3.14 and later
158
+ assert zstd_present , 'Zstandard compression is unavailable.'
159
+ self .fp = zstd .open (name , mode = mode + 't' )
146
160
147
161
def close (self ):
148
162
"""
@@ -175,6 +189,8 @@ def get_compression_type(self, file_name):
175
189
self .ctype = 'bzip2'
176
190
elif ext in ('.xz' , '.lzma' ):
177
191
self .ctype = 'lzma'
192
+ elif ext == 'zst' :
193
+ self .ctype = 'zstd'
178
194
else :
179
195
self .ctype = None
180
196
0 commit comments