dsacre/mididings — pull request #7 by SpotlightKid (2014-08-27 21:46:01+00:00)
Title:  Accept sysex strings in ascii hexadecimal notation with no white-space separation

... and comma/white space-separated single-digits bytes in `sysex_to_bytearray`.

Raise `ValueError` when both commas and white-space is used.

I.e. this is allowed:

sysex_data('F0040815162342F7')
sysex_data('f0040815162342f7')
sysex_data('f0 4 8 15162342f7')
sysex_data('f0,4,8,15,16,23,42,f7')

But not this:

sysex_data('f0,4,8 15162342f7')

The check in added line 288, searching for each white-space character, may be a bit excessive, esp. for long sysex strings, but the expression is ordered in such a way that the performance cost is only incurred when the sysex string contains a comma, which should be less common and even then it's still negligible, I think.

Instead, we could also only allow commas or spaces as separators:

if sysex.startswith('F0') or sysex.startswith('f0'):
if ',' in sysex and ' ' in sysex:
raise ValueError("mixed comma and space chars in sysex string")
result = bytearray()
for group in sysex.split(',' if ',' in sysex else ' '):
for hex in (group[i:i+2] for i in range(0, len(group), 2)):
result.append(int(hex, 16))
return result

(More than one consecutive space is handled by `range(0, len(group), 2)` yielding an empty sequence for empty string groups.)

But then other white-space characters may throw off the pair-wise conversion of hex chars to ints and produce hard to understand errors, e.g. this input:

sysex_data('f0,4,8\t\t15162342f7')