Transforms between arrays containing bytes and character arrays.
| 152 | |
| 153 | |
| 154 | class CharacterArrayCoder(VariableCoder): |
| 155 | """Transforms between arrays containing bytes and character arrays.""" |
| 156 | |
| 157 | def encode(self, variable, name=None): |
| 158 | variable = ensure_fixed_length_bytes(variable) |
| 159 | |
| 160 | dims, data, attrs, encoding = unpack_for_encoding(variable) |
| 161 | if data.dtype.kind == "S" and encoding.get("dtype") is not str: |
| 162 | data = bytes_to_char(data) |
| 163 | char_dim_name = validate_char_dim_name(data.shape[-1], encoding, name) |
| 164 | dims = dims + (char_dim_name,) |
| 165 | return Variable(dims, data, attrs, encoding) |
| 166 | |
| 167 | def decode(self, variable, name=None): |
| 168 | dims, data, attrs, encoding = unpack_for_decoding(variable) |
| 169 | |
| 170 | if data.dtype == "S1" and dims: |
| 171 | encoding["char_dim_name"] = dims[-1] |
| 172 | dims = dims[:-1] |
| 173 | data = char_to_bytes(data) |
| 174 | return Variable(dims, data, attrs, encoding) |
| 175 | |
| 176 | |
| 177 | def bytes_to_char(arr): |
no outgoing calls
no test coverage detected
searching dependent graphs…