def scanf_bytes(fmt, bytes_line): r"""Extract parameters from a bytes object using scanf.""" if PY2: # pragma: Python 2 out_byt = scanf(fmt, bytes_line) else: # pragma: Python 3 out_str = scanf(as_str(fmt), as_str(bytes_line)) out_byt = match_stype(bytes_line, out_str, recurse=True, allow_pass=True) return out_byt
def test_scanf(): r"""Test scanf.""" num_codes = 'dieEfgGouxX' fmt_list = [("%g%+gj", complex(1)), ("%s", "hello"), ("%5s", "the"), ("%5s\t%ld\t%lf\t%lf%+lfj\n", ("one", 1, 1.0, complex(1, 1)))] fmt_list += [("%" + s, 1) for s in num_codes] fmt_list += [("%5.2" + s, 1) for s in num_codes] fmt_list += [("%+5.2" + s, 1) for s in num_codes] fmt_list += [("%-5.2" + s, 1) for s in num_codes] fmt_list += [("% 5.2" + s, 1) for s in num_codes] fmt_list += [("%05.2" + s, 1) for s in num_codes] for fmt, val in fmt_list: if isinstance(val, tuple): val_tup = val else: val_tup = (val, ) new_tup = [] for v in val_tup: if isinstance(v, complex): new_tup += [v.real, v.imag] else: new_tup.append(v) val_str = fmt % tuple(new_tup) res = scanf.scanf(fmt, val_str) assert_equal(res, val_tup) # Test bytes version res = backwards.scanf_bytes(fmt, backwards.as_bytes(val_str)) assert_equal( res, backwards.as_bytes(val_tup, recurse=True, allow_pass=True)) # Test unicode version res = backwards.scanf_bytes(fmt, backwards.as_unicode(val_str)) assert_equal( res, backwards.as_unicode(val_tup, recurse=True, allow_pass=True))
def test_scanf(): r"""Test scanf.""" num_codes = 'dieEfgGouxX' fmt_list = [("%g%+gj", complex(1)), ("%s", "hello"), ("%5s", "the"), ("%5s\t%ld\t%lf\t%lf%+lfj\n", ("one", 1, 1.0, complex(1, 1)))] fmt_list += [("%" + s, 1) for s in num_codes] fmt_list += [("%5.2" + s, 1) for s in num_codes] fmt_list += [("%+5.2" + s, 1) for s in num_codes] fmt_list += [("%-5.2" + s, 1) for s in num_codes] fmt_list += [("% 5.2" + s, 1) for s in num_codes] fmt_list += [("%05.2" + s, 1) for s in num_codes] for fmt, val in fmt_list: if isinstance(val, tuple): val_tup = val else: val_tup = (val, ) new_tup = [] for v in val_tup: if isinstance(v, complex): new_tup += [v.real, v.imag] else: new_tup.append(v) val_str = fmt % tuple(new_tup) res = scanf.scanf(fmt, val_str) assert(res == val_tup)
def process_message(msg, fmt_str): r"""Extract python objects from a message using a format string. Args: msg (str, bytes): Message that should be parsed. fmt_str (str, bytes): Format string that should be used to parse the message using scanf. Returns: tuple: Variables extracted from the message. Raises: TypeError: If the message is not a string or bytes string type. ValueError: If the expected number of variables cannot be extracted from the message. """ if not isinstance(msg, (str, bytes)): raise TypeError("Message must be a string or bytes string type.") nfmt = len(extract_formats(fmt_str)) py_fmt_str = cformat2pyscanf(fmt_str) args = scanf.scanf(py_fmt_str, msg) if args is None: nargs = 0 else: nargs = len(args) if len(args) > 1: dtype = cformat2nptype(fmt_str) dtype_list = [dtype[i] for i in range(nargs)] args = tuple([np.array([a], idtype)[0] for a, idtype in zip(args, dtype_list)]) if nargs != nfmt: raise ValueError("%d arguments were extracted, " % nargs + "but format string expected %d." % nfmt) return args