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 array_to_table(arrs, fmt_str, use_astropy=False): r"""Serialize an array as an ASCII table. Args: arrs (np.ndarray, list, tuple): Structured array or list/tuple of arrays that contain table information. fmt_str (str, bytes): Format string that should be used to structure the ASCII array. use_astropy (bool, optional): If True, astropy will be used to format the table if it is installed. Defaults to False. Returns: bytes: ASCII table. """ if not _use_astropy: use_astropy = False dtype = cformat2nptype(fmt_str) info = format2table(fmt_str) arr1 = consolidate_array(arrs, dtype=dtype) if use_astropy: fd = backwards.StringIO() table = apy_Table(arr1) delimiter = info['delimiter'] delimiter = backwards.bytes2unicode(delimiter) apy_ascii.write(table, fd, delimiter=delimiter, format='no_header') out = backwards.unicode2bytes(fd.getvalue()) else: fd = backwards.BytesIO() for ele in arr1: line = format_message(ele.tolist(), fmt_str) fd.write(line) # fmt = fmt_str.split(info['newline'])[0] # np.savetxt(fd, arr1, # fmt=fmt, delimiter=info['delimiter'], # newline=info['newline'], header='') out = fd.getvalue() fd.close() return out
def load_yaml(fname): r"""Parse a yaml file defining a run. Args: fname (str): Path to the yaml file. Returns: dict: Contents of yaml file. """ fname = os.path.realpath(fname) if not os.path.isfile(fname): raise IOError("Unable locate yaml file %s" % fname) # Open file and parse yaml with open(fname, 'r') as f: # Mustache replace vars yamlparsed = f.read() yamlparsed = pystache.render( backwards.StringIO(yamlparsed).getvalue(), dict(os.environ)) yamlparsed = yaml.safe_load(yamlparsed) yamlparsed['working_dir'] = os.path.dirname(fname) return yamlparsed