def test_parse_header(): r"""Test parsing header.""" header = ["# name\tnumber\tvalue\tcomplex\n", "# n/a\tg\tcm\tn/a\n", "# %5s\t%ld\t%lf\t%g%+gj\n"] res = dict(delimiter='\t', comment='# ', newline='\n', format_str="%5s\t%ld\t%lf\t%g%+gj\n", fmts=['%5s', '%ld', '%lf', '%g%+gj'], field_names=['name', 'number', 'value', 'complex'], field_units=['n/a', 'g', 'cm', 'n/a']) for i in range(len(header)): header[i] = backwards.unicode2bytes(header[i]) for k, v in res.items(): if isinstance(v, str): res[k] = backwards.unicode2bytes(v) elif isinstance(v, list): res[k] = [backwards.unicode2bytes(s) for s in v] nt.assert_equal(serialize.parse_header(header), res) nt.assert_equal(serialize.parse_header(header[::-1]), res) _empty = backwards.unicode2bytes('') nt.assert_equal(serialize.parse_header(_empty.join(header)), res) # Test without formats header2 = header[:2] res2 = dict(**res) del res2['format_str'] res2['fmts'] = [] nt.assert_equal(serialize.parse_header(header2), res2) # Test with explicit line numbers nt.assert_equal(serialize.parse_header(header, lineno_names=0, lineno_units=1), res) # Test errors header3 = [header[0], header[0]] nt.assert_raises(RuntimeError, serialize.parse_header, header3) header4 = [header[1], header[1]] nt.assert_raises(RuntimeError, serialize.parse_header, header4)
def test_guess_serializer(): r"""Test guess_serializer.""" nele = 5 names = ["name", "number", "value", "complex"] field_names = [backwards.unicode2bytes(n) for n in names] dtypes = ['S5', 'i8', 'f8', 'c16'] dtype = np.dtype([(n, f) for n, f in zip(names, dtypes)]) arr_mix = np.zeros(nele, dtype) arr_mix['name'][0] = 'hello' fmt_arr = serialize._default_delimiter.join( serialize.nptype2cformat(arr_mix.dtype, asbytes=True)) fmt_arr += serialize._default_newline if platform._is_win: # pragma: windows x = arr_mix[0].tolist() for i in x: print(type(i)) if backwards.PY2: # pragma: Python 2 # tolist maps to long on python 2, but int on python 3?! fmt = backwards.unicode2bytes('%s\t%l64d\t%g\t%g%+gj\n') else: # pragma: Python 3 fmt = backwards.unicode2bytes('%s\t%d\t%g\t%g%+gj\n') else: fmt = backwards.unicode2bytes('%s\t%ld\t%g\t%g%+gj\n') test_list = [(arr_mix, dict(field_names=field_names, format_str=fmt_arr, stype=2, as_array=1)), (arr_mix[0].tolist(), dict(format_str=fmt, stype=1)), ('hello', dict(stype=0))] for obj, sinfo_ans in test_list: sinfo_res = serialize.guess_serializer(obj) s = serialize.get_serializer(**sinfo_res) nt.assert_equal(s.serializer_info, sinfo_ans)
def __init__(self, format_str=None, as_array=False, field_names=None, field_units=None, func_serialize=None, func_deserialize=None, **kwargs): self.format_str = format_str self.as_array = as_array self._alias = None if field_names is not None: field_names = [backwards.unicode2bytes(n) for n in field_names] self.field_names = field_names if field_units is not None: field_units = [backwards.unicode2bytes(n) for n in field_units] self.field_units = field_units if isinstance(func_serialize, DefaultSerialize): self._func_serialize = func_serialize.func_serialize else: self._func_serialize = func_serialize if isinstance(func_deserialize, DefaultSerialize): self._func_deserialize = func_deserialize.func_deserialize else: self._func_deserialize = func_deserialize
def parse_header(self, msg): r"""Extract header info from a message. Args: msg (str): Message to extract header from. Returns: dict: Message properties. """ if CIS_MSG_HEAD not in msg: out = dict(body=msg, size=len(msg)) return out _, header, body = msg.split(CIS_MSG_HEAD) out = dict(body=body) for x in header.split(HEAD_KEY_SEP): k, v = x.split(HEAD_VAL_SEP) out[backwards.bytes2unicode(k)] = backwards.bytes2unicode(v) for k in ['size', 'as_array', 'stype']: if k in out: out[k] = int(float(out[k])) for k in ['format_str', 'field_names', 'field_units']: if k in out: out[k] = backwards.unicode2bytes(out[k]) if k in ['field_names', 'field_units']: out[k] = out[k].split(backwards.unicode2bytes(',')) # for k in ['format_str']: # if k in out: # out[k] = backwards.unicode2bytes(out[k]) return out
def test_format_header(): r"""Test formatting header.""" kws_all = dict( field_names=['name', 'number', 'value', 'complex'], field_units=['n/a', 'g', 'cm', 'n/a']) res_all = dict( names="# name\tnumber\tvalue\tcomplex\n", units="# n/a\tg\tcm\tn/a\n") if platform._is_win: # pragma: windows kws_all['format_str'] = "%5s\t%l64d\t%g\t%g%+gj\n" res_all['format'] = "# " + kws_all['format_str'] else: kws_all['format_str'] = "%5s\t%ld\t%g\t%g%+gj\n" res_all['format'] = "# " + kws_all['format_str'] kws_all['dtype'] = serialize.cformat2nptype(kws_all['format_str'], names=kws_all['field_names']) for x in [kws_all, res_all]: for k, v in x.items(): if isinstance(v, str): x[k] = backwards.unicode2bytes(v) elif isinstance(v, list): x[k] = [backwards.unicode2bytes(iv) for iv in v] test_list = [(['format_str', 'field_names', 'field_units'], ['names', 'units', 'format']), (['field_names', 'field_units', 'dtype'], ['names', 'units', 'format']), (['field_units', 'dtype'], ['names', 'units', 'format']), (['field_names'], ['names']), (['field_units'], ['units'])] for kws_keys, res_keys in test_list: kws = {k: kws_all[k] for k in kws_keys} res = backwards.unicode2bytes('').join([res_all[k] for k in res_keys]) nt.assert_equal(serialize.format_header(**kws), res) nt.assert_raises(ValueError, serialize.format_header)
def guess_serializer(msg, **kwargs): r"""Guess the type of serializer required based on the message object. Args: msg (obj): Python object that needs to be serialized. **kwargs: Additional keyword arguments are passed to the serializer. Returns: dict: Extracted keyword arguments for creating a serializer. """ sinfo = dict(**kwargs) kws_fmt = {k: kwargs.get(k, None) for k in ['delimiter', 'newline']} kws_fmt['comment'] = backwards.unicode2bytes('') if isinstance(msg, np.ndarray): names = [backwards.unicode2bytes(n) for n in msg.dtype.names] sinfo.setdefault('field_names', names) sinfo.setdefault('format_str', table2format(msg.dtype, **kws_fmt)) sinfo.setdefault('as_array', True) elif isinstance(msg, (list, tuple)): typ_list = [] for x in msg: if isinstance(x, backwards.string_types): typ_list.append(np.dtype('S1')) else: typ_list.append(np.dtype(type(x))) new_type = dict(formats=typ_list, names=['f%d' % i for i in range(len(typ_list))]) row_dtype = np.dtype(new_type) format_str = table2format(row_dtype, **kws_fmt) format_str = format_str.replace(backwards.unicode2bytes('%1s'), backwards.unicode2bytes('%s')) sinfo.setdefault('format_str', format_str) return sinfo
def test_extract_formats(): r"""Test extract_formats.""" test_str = ['%10s\t%5.2f\t%4d\t%g%+gj'] test_fmt = [['%10s', '%5.2f', '%4d', '%g%+gj']] for s, f in zip(test_str, test_fmt): nt.assert_equal(serialize.extract_formats(s), f) nt.assert_equal(serialize.extract_formats(backwards.unicode2bytes(s)), [backwards.unicode2bytes(i) for i in f])
def __init__(self, *args, **kwargs): super(TestDefaultSerialize, self).__init__(*args, **kwargs) self._cls = 'DefaultSerialize' self._empty_msg = backwards.unicode2bytes('') self._empty_obj = backwards.unicode2bytes('') self._header_info = dict(arg1='1', arg2='two') self._objects = self.file_lines self.attr_list += ['format_str', 'as_array', 'field_names', 'field_units', 'nfields', 'field_formats', 'numpy_dtype', 'scanf_format_str', 'serializer_info']
def test_array_to_table(): r"""Test conversion of arrays to ASCII table and back.""" flist = [backwards.unicode2bytes("%5s\t%ld\t%lf\t%g%+gj\n")] for use_astropy in [False, True]: for f in flist: dtype = serialize.cformat2nptype(f) arr0 = np.ones(5, dtype) arr0['f0'][0] = backwards.unicode2bytes('hello') tab = serialize.array_to_table(arr0, f, use_astropy=use_astropy) arr1 = serialize.table_to_array(tab, f, use_astropy=use_astropy) np.testing.assert_array_equal(arr1, arr0)
def format_str(self): if not hasattr(self, '_format_str'): if hasattr(self, '_dtype'): fmts = [ backwards.unicode2bytes(nptype2cformat(self.dtype[i])) for i in range(len(self.dtype)) ] self._format_str = backwards.unicode2bytes( self.column.join(fmts) + self.newline) else: # pragma: debug raise RuntimeError("Format string not set " + "and cannot be determined.") return self._format_str
def __init__(self, filepath, io_mode, comment=_default_args['comment'], newline=_default_args['newline'], open_as_binary=True): r"""Class for reading/writing an ASCII file. Args: filepath (str): Full path to the file that should be read from or written to. io_mode (str): Mode that should be used to open the file. Valid values include 'r', 'w', and None. None can be used to indicate an in memory table that will not be read from or written to a file. comment (str, optional): String that should be used to identify comments. Defaults to '#'. newline (str, optional): String that should be used to identify the end of a line. Defaults to '\n'. open_as_binary (bool, optional): If True, the file is opened in binary mode. Defaults to True. Attributes: filepath (str): Full path to the file that should be read from or written to. io_mode (str): Mode that should be used to open the file. comment (str): String that should be used to identify comments. newline (str): String that should be used to identify the end of a line. open_as_binary (bool): If True, the file is opened in binary mode. fd (file): File descriptor. Raises: TypeError: If filepath is not a string. ValueError: If io_mode is not one of the allowed values. ValueError: If filepath is not a valid path and io_mode is 'r'. """ if not isinstance(filepath, str): raise TypeError('File path must be provided as a string.') self.filepath = os.path.abspath(filepath) if io_mode not in ['r', 'w', 'a', None]: raise ValueError("IO specifier must be 'r' or 'w'.") self.io_mode = io_mode if self.io_mode == 'r' and not os.path.isfile(filepath): raise ValueError("File does not exist.") self.comment = backwards.unicode2bytes(comment) self.newline = backwards.unicode2bytes(newline) self.open_as_binary = open_as_binary self.fd = None
def __init__(self): self.field_names = ['name', 'count', 'size'] self.field_units = ['n/a', 'g', 'cm'] self.nfields = len(self.field_names) self.comment = backwards.unicode2bytes('# ') self.delimiter = backwards.unicode2bytes('\t') self.newline = backwards.unicode2bytes('\n') self.fmt_str = backwards.unicode2bytes('%5s\t%d\t%f\n') self.fmt_str_matlab = backwards.unicode2bytes('%5s\\t%d\\t%f\\n') self.field_formats = self.fmt_str.split(self.newline)[0].split( self.delimiter) self.fmt_str_line = backwards.unicode2bytes('# ') + self.fmt_str # self.file_cols = ['name', 'count', 'size'] self.file_dtype = np.dtype({ 'names': self.field_names, 'formats': ['%s5' % backwards.np_dtype_str, 'i4', 'f8'] }) self.field_names = [ backwards.unicode2bytes(x) for x in self.field_names ] self.field_units = [ backwards.unicode2bytes(x) for x in self.field_units ] self.field_names_line = (self.comment + self.delimiter.join(self.field_names) + self.newline) self.field_units_line = (self.comment + self.delimiter.join(self.field_units) + self.newline) self.file_elements = [('one', int(1), 1.0), ('two', int(2), 2.0), ('three', int(3), 3.0)]
def test_cformat2pyscanf(): r"""Test conversion of C format string to version for python scanf.""" for a, b in map_cformat2pyscanf: if isinstance(a, str): a = [a] for _ia in a: ia = backwards.unicode2bytes(_ia) ib = backwards.unicode2bytes(b) assert_equal(AsciiTable.cformat2pyscanf(ia), ib) assert_raises(TypeError, AsciiTable.cformat2pyscanf, 0) assert_raises(ValueError, AsciiTable.cformat2pyscanf, backwards.unicode2bytes('s')) assert_raises(ValueError, AsciiTable.cformat2pyscanf, backwards.unicode2bytes('%'))
def read_header(self): r"""Read header lines from the file and update serializer info.""" if self.header_was_read: return header_lines = [] header_size = 0 self.fd.seek(0) for line in self.fd: sline = backwards.unicode2bytes( line.replace(self.platform_newline, self.newline)) if not sline.startswith(self.comment): break header_size += len(line) header_lines.append(sline) # Parse header & set serializer attributes header = serialize.parse_header(header_lines) for k in ['format_str', 'field_names', 'field_units']: if header.get(k, False): setattr(self.serializer, k, header[k]) # Try to determine format from array without header str_fmt = backwards.unicode2bytes('%s') if (((self.serializer.format_str is None) or (str_fmt in self.serializer.format_str))): with open(self.address, self.open_mode) as fd: fd.seek(header_size) all_contents = fd.read() if len(all_contents) == 0: # pragma: debug return # In case the file has not been written arr = serialize.table_to_array(all_contents, names=self.serializer.field_names, comment=self.comment, delimiter=self.delimiter) self.serializer.field_names = arr.dtype.names if self.serializer.format_str is None: self.serializer.format_str = serialize.table2format( arr.dtype, delimiter=self.delimiter, comment=backwards.unicode2bytes(''), newline=self.newline) while str_fmt in self.serializer.format_str: ifld = self.serializer.field_formats.index(str_fmt) max_len = len( max(arr[self.serializer.field_names[ifld]], key=len)) new_str_fmt = backwards.unicode2bytes('%' + str(max_len) + 's') self.serializer.format_str = self.serializer.format_str.replace( str_fmt, new_str_fmt, 1) self.delimiter = self.serializer.table_info['delimiter'] # Seek to just after the header self.fd.seek(header_size) self.header_was_read = True
def _send_direct(self, msg, topic='', identity=None, **kwargs): r"""Send a message. Args: msg (str, bytes): Message to be sent. topic (str, optional): Filter that should be sent with the message for 'PUB' sockets. Defaults to ''. identity (str, optional): Identify of identified worker that should be sent for 'ROUTER' sockets. Defaults to self.dealer_identity. **kwargs: Additional keyword arguments are passed to socket send. Returns: bool: Success or failure of send. """ if not self.is_open_direct: # pragma: debug self.error("Socket closed") return False if identity is None: identity = self.dealer_identity topic = backwards.unicode2bytes(topic) identity = backwards.unicode2bytes(identity) if self.socket_type_name == 'PUB': total_msg = topic + _flag_zmq_filter + msg else: total_msg = msg total_msg = self.check_reply_socket_send(total_msg) kwargs.setdefault('flags', zmq.NOBLOCK) with self.socket_lock: try: if self.socket.closed: # pragma: debug self.error("Socket closed") return False self.debug("Sending %d bytes to %s", len(total_msg), self.address) if self.socket_type_name == 'ROUTER': self.socket.send(identity, zmq.SNDMORE) self.socket.send(total_msg, **kwargs) self.debug("Sent %d bytes to %s", len(total_msg), self.address) self._n_zmq_sent += 1 except zmq.ZMQError as e: # pragma: debug if e.errno == zmq.EAGAIN: raise AsyncComm.AsyncTryAgain("Socket not yet available.") else: self.special_debug("Socket could not send. (errno=%d)", e.errno) return False return True
def _recv(self, timeout=0): r"""Reads message from a file. Args: timeout (float, optional): Time in seconds to wait for a message. Defaults to self.recv_timeout. Unused. Returns: tuple (bool, str): Success or failure of reading from the file and the read messages as bytes. """ flag = True if self.read_meth == 'read': out = self.fd.read() elif self.read_meth == 'readline': out = self.fd.readline() if len(out) == 0: if self.advance_in_series(): self.debug("Advanced to %d", self._series_index) flag, out = self._recv() else: out = self.eof_msg else: out = out.replace(self.platform_newline, self.newline) if not self.open_as_binary: out = backwards.unicode2bytes(out) return (flag, out)
def format_header(self, header_info): r"""Format header info to form a string that should prepend a message. Args: header_info (dict): Properties that should be included in the header. Returns: str: Message with header in front. """ header = backwards.bytes2unicode(CIS_MSG_HEAD) header_str = {} for k, v in header_info.items(): if isinstance(v, list): header_str[k] = ','.join( [backwards.bytes2unicode(x) for x in v]) elif isinstance(v, backwards.string_types): header_str[k] = backwards.bytes2unicode(v) else: header_str[k] = str(v) header += backwards.bytes2unicode(HEAD_KEY_SEP).join([ '%s%s%s' % (backwards.bytes2unicode(k), backwards.bytes2unicode(HEAD_VAL_SEP), backwards.bytes2unicode(v)) for k, v in header_str.items() ]) header += backwards.bytes2unicode(CIS_MSG_HEAD) return backwards.unicode2bytes(header)
def _recv(self, timeout=0): r"""Reads message from a file. Args: timeout (float, optional): Time in seconds to wait for a message. Defaults to self.recv_timeout. Unused. Returns: tuple (bool, str): Success or failure of reading from the file and the read messages as bytes. """ if self.read_meth == 'read': out = self.fd.read() elif self.read_meth == 'readline': out = self.fd.readline() else: # pragma: debug self.error('Unsupported read_meth: %s', self.read_meth) out = '' if len(out) == 0: out = self.eof_msg else: out = out.replace(self.platform_newline, self.newline) if not self.open_as_binary: out = backwards.unicode2bytes(out) return (True, out)
def check_reply_socket_send(self, msg): r"""Append reply socket address if it Args: msg (str): Message that will be piggy backed on. Returns: str: Message with reply address if it has not been sent. """ if self.direction == 'recv': return msg # Create socket if self.reply_socket_send is None: self.reply_socket_send = self.context.socket(zmq.REP) self.reply_socket_send.setsockopt(zmq.LINGER, 0) address = format_address(_default_protocol, 'localhost') address = bind_socket(self.reply_socket_send, address) self.register_comm('REPLY_SEND_' + address, self.reply_socket_send) self.reply_socket_address = address self.debug("new send address: %s", address) new_msg = backwards.format_bytes( backwards.unicode2bytes(':%s:%s:%s:'), (_reply_msg, self.reply_socket_address, _reply_msg)) new_msg += msg return new_msg
def func_serialize(self, args): r"""Serialize a message. Args: args: List of arguments to be formatted or numpy array to be serialized. Returns: bytes, str: Serialized message. """ if self.format_str is None: # if self.as_array: # dtype = args.dtype # else: # dtype = np.dtype(names=self.field_names, # formats=[type(x) for x in args]) # self.format_str = serialize.table2format(dtype) raise RuntimeError("Format string is not defined.") if self.as_array: out = serialize.array_to_table(args, self.format_str, use_astropy=self.use_astropy) out = backwards.unicode2bytes(out) else: out = super(AsciiTableSerialize, self).func_serialize(args) return out
def check_reply_socket_recv(self, msg): r"""Check incoming message for reply address. Args: msg (str): Incoming message to check. Returns: str: Messages with reply address removed if present. """ if self.direction == 'send': return msg, None prefix = backwards.format_bytes(backwards.unicode2bytes(':%s:'), (_reply_msg, )) if msg.startswith(prefix): _, address, new_msg = msg.split(prefix) if address not in self.reply_socket_recv: self.reply_socket_recv[address] = self.context.socket(zmq.REQ) self.reply_socket_recv[address].setsockopt(zmq.LINGER, 0) self.reply_socket_recv[address].connect(address) self.register_comm( 'REPLY_RECV_' + backwards.bytes2unicode(address), self.reply_socket_recv[address]) self._n_reply_recv[address] = 0 self._n_zmq_recv[address] = 0 self.debug("new recv address: %s", address) else: # pragma: debug new_msg = msg raise Exception("No reply socket address attached.") return new_msg, address
def func_serialize(self, args): r"""Default method for serializing object into message. Args: args (obj): List of arguments to be formatted or a ready made message. Returns: bytes, str: Serialized message. Raises: Exception: If there is no format string and more than one argument is provided. """ if self._func_serialize is not None: # Return directly to check and raise TypeError return self._func_serialize(args) elif self.format_str is not None: if self.as_array: out = serialize.array_to_bytes(args, dtype=self.numpy_dtype, order='F') else: out = serialize.format_message(args, self.format_str) else: if isinstance(args, (list, tuple)): if len(args) != 1: raise Exception("No format string and more than one " + "argument provided.") out = args[0] else: out = args out = backwards.unicode2bytes(out) return out
def check_czmq(): r"""Determine if the necessary C/C++ libraries are installed for ZeroMQ. Returns: bool: True if the libraries are installed, False otherwise. """ # Check that the libraries are installed if platform._is_win: # pragma: windows opts = [ 'libzmq_include', 'libzmq_static', # 'libzmq_dynamic', 'czmq_include', 'czmq_static' ] # , 'czmq_dynamic'] for o in opts: if not cis_cfg.get('windows', o, None): # pragma: debug warnings.warn("Config option %s not set." % o) return False return True else: process = subprocess.Popen(['gcc', '-lzmq', '-lczmq'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) outs, errs = process.communicate() # Python 3 # try: # outs, errs = process.communicate(timeout=15) # except subprocess.TimeoutExpired: # process.kill() # outs, errs = process.communicate() return (backwards.unicode2bytes('zmq') not in errs)
def func_serialize(self, args): r"""Serialize a message. Args: args (obj): Python object to be serialized. Returns: bytes, str: Serialized message. """ fd = backwards.StringIO() if backwards.PY2: args_ = args else: # For Python 3 and higher, bytes need to be encoded args_ = copy.deepcopy(args) for c in args.columns: if isinstance(args_[c][0], backwards.bytes_type): args_[c] = args_[c].apply(lambda s: s.decode('utf-8')) if self.field_names is not None: args_.columns = [ backwards.bytes2unicode(n) for n in self.field_names ] args_.to_csv(fd, index=False, sep=self.delimiter, mode='wb', encoding='utf8', header=self.write_header) out = fd.getvalue() fd.close() return backwards.unicode2bytes(out)
def table2format(fmts=[], delimiter=None, newline=None, comment=None): r"""Create a format string from table information. Args: fmts (list, optional): List of format codes for each column. Defaults to []. delimiter (bytes, optional): String used to separate columns. Defaults to _default_delimiter. newline (bytes, optional): String used to indicate the end of a table line. Defaults to _default_newline. comment (bytes, optional): String that should be prepended to the format string to indicate a comment. Defaults to _default_comment. Returns: str, bytes: Table format string. """ if delimiter is None: delimiter = _default_delimiter if newline is None: newline = _default_newline if comment is None: comment = _default_comment if isinstance(fmts, np.dtype): fmts = nptype2cformat(fmts) bytes_fmts = [backwards.unicode2bytes(f) for f in fmts] fmt_str = comment + delimiter.join(bytes_fmts) + newline return fmt_str
def empty_msg(self): r"""obj: Object indicating empty message.""" stype = self.serializer_type if stype <= 0: out = backwards.unicode2bytes('') else: out = tuple() return out
def file_contents(self): r"""str: Complete contents of mock file.""" out = backwards.unicode2bytes('') for line in self.header_lines: out += line for line in self.file_lines: out += line return out
def on_message(self, ch, method, props, body): r"""Buffer received messages.""" if self.direction == 'send': # pragma: debug raise Exception("Send comm received a message.") # self.add_backlog_recv(backwards.unicode2bytes(body)) with self.rmq_lock: self._buffered_messages.append(backwards.unicode2bytes(body)) ch.basic_ack(delivery_tag=method.delivery_tag)
def test_format2table(): r"""Test getting table information from a format string.""" out = {'delimiter': backwards.unicode2bytes('\t'), 'newline': backwards.unicode2bytes('\n'), 'comment': backwards.unicode2bytes('# '), 'fmts': ["%5s", "%ld", "%lf", "%g%+gj"]} out['fmts'] = [backwards.unicode2bytes(f) for f in out['fmts']] sfmt = out['fmts'][0] sout = dict(**out) sout['fmts'] = [sfmt] del sout['newline'], sout['comment'] fmt = backwards.unicode2bytes("# %5s\t%ld\t%lf\t%g%+gj\n") nt.assert_equal(dict(fmts=[]), serialize.format2table('hello')) nt.assert_equal(sout, serialize.format2table(sfmt)) nt.assert_equal(fmt, serialize.table2format(**out)) nt.assert_equal(out, serialize.format2table(fmt)) nt.assert_equal(fmt, serialize.table2format(fmts=out['fmts'])) nt.assert_raises(RuntimeError, serialize.format2table, "%5s,%ld\t%g\n")
def test_format_bytes(): r"""Test formating of bytes string.""" s0 = "%s, %s" ans = "one, one" arg0 = "one" args = (backwards.unicode2bytes(arg0), backwards.bytes2unicode(arg0)) for cvt in [backwards.unicode2bytes, backwards.bytes2unicode]: res = backwards.format_bytes(cvt(s0), args) nt.assert_equal(res, cvt(ans))