Skip to content

Commit

Permalink
skip encoding for strings with len > 4
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed Dec 21, 2018
1 parent fa2f66d commit 13404bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
18 changes: 17 additions & 1 deletion simplemmtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,22 @@ def _PackedIntBufStrategy(nbytes=1, dectype='i4'):
########## MEDIUM LEVEL ARRAY ENCODE/DECODE API #################


def check_encodable(arr, codec, param=0):
'''Check if an array is encodable with the requested strategy.
Example of type 5 "fixed-length string array" with string length 4:
>>> check_encodable(["ABCD"], 5, 4)
True
>>> check_encodable(["ABCDE"], 5, 4)
False
'''
if codec == 5:
return all(len(s) <= param for s in arr)

return True


def encode_array(arr, codec, param=0):
strategy = strategies[codec](param)

Expand Down Expand Up @@ -757,7 +773,7 @@ def set(self, key, value, codec=-1, param=0):
if codec == -1:
codec, param = encodingrules.get(key, (0, 0))

if codec != 0:
if codec != 0 and check_encodable(value, codec, param):
value = encode_array(value, codec, param)

self._data[mmtfstr(key)] = value
Expand Down
23 changes: 23 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,34 @@ def test_encode_recode():
raise UserWarning(key)


def test_encodable():
atoms = [{
u'atomName': u'N',
u'chainId': u'A',
u'coords': (12.284, 42.763, 10.037),
u'element': u'N',
u'groupName': u'MET',
}]

# len('A') <= 4: encodable -> binary
d = simplemmtf.from_atoms(atoms)
assert d.get(u'chainIdList') == ['A']
assert isinstance(d._data[u'chainIdList'], bytes)

atoms[0][u'chainId'] = u'ABCDE'

# len('ABCDE') > 4: not encodable -> array
d = simplemmtf.from_atoms(atoms)
assert d.get(u'chainIdList') == ['ABCDE']
assert isinstance(d._data[u'chainIdList'], list)


def test():
for fn in sys.argv[1:]:
test_file(fn)

test_encode_recode()
test_encodable()


if __name__ == '__main__':
Expand Down

0 comments on commit 13404bc

Please sign in to comment.