def _exec(self, pms): params, stdin, timeout = pms # for multiproc exlst = [self._bin] + ([] if missing(params) else smap( params, lambda x: str(x).strip())) if self._shell: exlst = paste(smap(exlst, lambda x: x.replace(' ', r'\ ')), sep=' ') procs = Popen(exlst, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=self._shell) try: rvals = procs.communicate(input=stdin, timeout=timeout) rstrs = smap( rvals, lambda x: '' if x is None else x.decode('utf-8').strip()) rcode = procs.returncode except TimeoutExpired: procs.kill() rstrs = ['subprocess terminated as timeout expired', ''] rcode = 124 prstr = paste(rstrs, sep=' | ') if rcode in self._ncode: logging.log((logging.DEBUG if self._mute else logging.INFO), prstr) else: raise RuntimeError(f'execution failed [{rcode}]:\n{prstr}') return rcode, rstrs
def _fmt(mtx, rnam, cnam, ridx, cidx): nr, nc = mtx.shape if missing(rnam): rnam = smap(range(nr), lambda x: f'[{x}]') if missing(cnam): cnam = smap(range(nc), lambda x: f'[{x}]') _sln = lambda x,sr,hd,tl,rp: (smap(x[:hd],str) + [rp] + smap(x[tl:],str)) if sr else smap(x, str) _scol = lambda x: _sln(x, nc > strinkcols, 3, -1, ' ... ') _srow = lambda x: _sln(x, nr > strinkrows, 5, -3, '') slns = [_scol(cnam)] + \ ([_scol(ln) for ln in mtx] if nr <= strinkrows else ([_scol(ln) for ln in mtx[:5]] + [_scol([' ... ... '] + [''] * (nc-1))] + [_scol(ln) for ln in mtx[-3:]])) slns = [['#'] + slns[0]] + [[n] + ln for n,ln in zip(_srow(rnam), slns[1:])] nri = ridx.size if available(ridx) else 0 nci = cidx.size if available(cidx) else 0 if nci > 0: slns = [[f'<{k}>'] + _scol(cidx[k]) for k in cidx.names] + slns if nri > 0: sidx = [[''] * nci + [f'<{k}>'] + _srow(ridx[k]) for k in ridx.names] slns = [list(ix) + ln for ix,ln in zip(zip(*sidx), slns)] def _sfmt(lns, pos): size = max(collapse(smap(lns, lambda x: smap(x[pos], lambda v: len(v) if v not in (' ... ', ' ... ... ') else 0)))) + 1 for ln in lns: ln[pos] = smap(ln[pos], lambda x: '{0:>{1}s}'.format(x, size) if x != ' ... ' else x) return lns if nri > 0: slns = _sfmt(slns, slice(None,nri)) slns = _sfmt(slns, slice(nri,nri+1)) slns = _sfmt(slns, slice(nri+1,None)) return smap(slns, lambda ln: paste(ln, sep = delimiter))
def addColumn(self, tableName: str, colName: str, types: Optional[Iterable[str]] = None) -> SQLiteWrapper: tstr = paste(optional(types, []), sep=' ') self.execute( f"ALTER TABLE '{tableName}' ADD COLUMN '{colName}' {tstr}") return self
def __str__(self): if len(self._arrs) == 0: return '[ ]' nlen = max(smap(self._arrs.keys(), len)) olns = [ (('{' + f':{nlen}s' + '} : ').format(k) if i == 0 else (' ' * (nlen + 3))) + ln for k, v in zip(self._arrs.keys(), smap(self._arrs.values(), str)) for i, ln in enumerate(v.split('\n')) ] return paste(olns, sep='\n')
def tostring(self, delimiter: str = ',', transpose: bool = False, withindex: bool = False) -> str: rlns = self._tostrlns(delimiter = delimiter, transpose = transpose, withindex = withindex) rlns = ['[' + rlns[0]] + \ [' ' + ln for ln in rlns[1:]] return paste(rlns, sep = '\n') + ']'
def __repr__(self): rlns = self._tostrlns(delimiter = ',') rlns = ['Table([' + rlns[0]] + \ [' ' + ln for ln in rlns[1:]] return paste(rlns, sep = '\n') + f'], size = ({self.nrow}, {self.ncol}))'
def createTable( self, tableName: str, columns: Iterable[Iterable[str]] = ()) -> SQLiteWrapper: tcols = paste(smap(columns, lambda x: paste(x, sep=' ')), sep=', ') self.execute(f"CREATE TABLE '{tableName}'({tcols})") return self
def __repr__(self): rlns = str(self._names).split('\n') rlns = [f'NamedIndex({rlns[0]}'] + \ [f' {ln}' for ln in rlns[1:]] return paste(rlns, sep='\n') + f', size = {self.size})'
def __repr__(self): rlns = str(self).split('\n') rlns = ['StructuredArray(' + rlns[0]] + \ [' ' + ln for ln in rlns[1:]] return paste(rlns, sep='\n') + f', size = ({self.size}, {self.length}))'