def test_calls(): mkdir = cmd('mkdir foo', stderr=STDOUT) rmdir = cmd('rmdir foo', stderr=STDOUT) call(rmdir) check_call(mkdir) assert_raises(CalledProcessError, lambda: check_call(mkdir)) eq_(call(rmdir), 0) ok_(call(rmdir) != 0)
def _conll_eval(fp): """Evaluates a conll-style data file using the conll-2000 perl script. :param fp: file path :return: f1-score, precision, recall """ cwd = '.' try: os.mkdir('tmp') except OSError: pass fpres = 'tmp/results.%s' % random_str() fh_out = open(fpres, 'w') if '\t' in open(fp, 'r').readline(): warnings.warn('Wrong tab column separator. Use tabs (\\t).') try: check_call(cmd('perl prl/conll_eval.pl -l < {}', fp, cwd=cwd, stdout=fh_out)) except Exception: warnings.warn("Exception ocurred during Evaluation.") exc_type, exc_value, exc_traceback = sys.exc_info() print("*** print_tb:") traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) print("*** print_exception:") traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) res = AccuracyResults() res.parse_conll_eval_table(fpres) os.remove(fpres) return res['Total']['fscore'], res['Total']['precision'], \ res['Total']['recall']
def run_and_log_output(cmd_string, *args, **kwargs): """ Runs a command with iterpipes and logs the output """ c = iterpipes.cmd(cmd_string, bufsize=128, *args, **kwargs) logging.info('Running %s', iterpipes.format(cmd_string, args)) out = iterpipes.run(c) for line in out: logging.info(line)
def get_git_hash(): c = iterpipes.cmd('git rev-parse HEAD') out = iterpipes.run(c) return list(out)[0].strip()
def test_run_output(): eq_(''.join(run(cmd('echo foo'))), 'foo\n') eq_(run(compose(join, cmd('echo foo'))), 'foo\n') eq_(call(cmd('echo foo')), 0)
def test_or_operator(): eq_(run(Fun(cmd('echo foo')) | (lambda x: x) | join), 'foo\n') eq_(run(Fun(lambda _: ['a']) | cmd('tr a-z A-Z') | join), 'A')
def test_line_buffering(): eq_(list(cmd('echo "foo\nbar"', bufsize=1)([])), ['foo\n', 'bar\n'])
def test_cmd(): text = six.u('привет, λ!\nλx. x\nдо свидания\n') regexp = six.u('λ[a-zA-Z]\.') grep = compose(join, cmd('grep {}', regexp)) eq_(grep(text), six.u('λx. x\n'))
def test_dont_touch_stdout(): eq_(''.join(run(cmd('ls -d /', stdout=STDOUT_FILENO))), '')
def test_stdin_iterable_excpt(): class E(Exception): pass def g(): raise E('foo') yield 'bar' assert_raises(E, lambda: ''.join(run(cmd('grep foo'), g())))
def test_make_pipeline(): lines = ''.join(run(cmd(r'echo a b c b a | tr {} \\n | sort | uniq', ' '))) eq_(lines, 'a\nb\nc\n')
def test_huge_input(): wc = compose(join, cmd('wc -l')) eq_(wc('%d\n' % x for x in range(10000)), '10000\n')
def test_basic_many(): eq_(''.join(run(cmd('echo foo | wc -l'))), '1\n')
def test_basic_single(): eq_(''.join(run(cmd('ls -d /'))), '/\n')
def test_string_input(): eq_(''.join(run(cmd('tr t T'), 'input')), six.u('inpuT')) eq_(''.join(run(cmd('tr t T'), six.u('input'))), six.u('inpuT')) eq_(''.join(run(cmd('tr t T'), [six.u('input')])), six.u('inpuT'))