def test_SList(): sl = text.SList(["a 11", "b 1", "a 2"]) assert sl.n == "a 11\nb 1\na 2" assert sl.s == "a 11 b 1 a 2" assert sl.grep(lambda x: x.startswith("a")) == text.SList(["a 11", "a 2"]) assert sl.fields(0) == text.SList(["a", "b", "a"]) assert sl.sort(field=1, nums=True) == text.SList(["b 1", "a 2", "a 11"])
def test_SList(): sl = text.SList(['a 11', 'b 1', 'a 2']) nt.assert_equal(sl.n, 'a 11\nb 1\na 2') nt.assert_equal(sl.s, 'a 11 b 1 a 2') nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2'])) nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a'])) nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11']))
def test_SList(): sl = text.SList(["a 11", "b 1", "a 2"]) nt.assert_equal(sl.n, "a 11\nb 1\na 2") nt.assert_equal(sl.s, "a 11 b 1 a 2") nt.assert_equal(sl.grep(lambda x: x.startswith("a")), text.SList(["a 11", "a 2"])) nt.assert_equal(sl.fields(0), text.SList(["a", "b", "a"])) nt.assert_equal(sl.sort(field=1, nums=True), text.SList(["b 1", "a 2", "a 11"]))
def _getoutput_compat(shell, cmd, split=True, depth=0): """Compatibility function for IPython's built-in getoutput command. The getoutput command has the following semantics: * Returns a SList containing an array of output * SList items are of type "str". In Python 2, the str object is utf-8 encoded. In Python 3, the "str" type already supports Unicode. * The _exit_code attribute is not set * If the process was interrupted, "^C" is printed. Args: shell: An InteractiveShell instance. cmd: Command to execute. This is the same as the corresponding argument to InteractiveShell.getoutput. split: Same as the corresponding argument to InteractiveShell.getoutput. depth: Same as the corresponding argument to InteractiveShell.getoutput. Returns: The output as a SList if split was true, otherwise an LSString. """ # We set a higher depth than the IPython system command since a shell object # is expected to call this function, thus adding one level of nesting to the # stack. result = _run_command(shell.var_expand(cmd, depth=depth + 2), clear_streamed_output=True) if -result.returncode in _INTERRUPTED_SIGNALS: print('^C') output = result.output if six.PY2: # Backwards compatibility. Python 2 getoutput() expects the result as a # str, not a unicode. output = output.encode(_ENCODING) if split: return text.SList(output.splitlines()) else: return text.LSString(output)