def test_loaders(self): """ The editor provides there different methods for instantiation; from a file on disk, from a file stream, or from a string. """ fl = Editor.from_file(self.path) with open(self.path) as f: tm = Editor.from_stream(f) f.seek(0) tr = Editor.from_string(f.read()) self.assertTrue(len(self.lines) == len(fl) == len(tm) == len(tr)) self.assertTrue(all(fl[i] == tm[i] == tr[i] for i in range(len(self.lines))))
def parse_symbols_from_input(path): """ The only way to get the symbols, in the correct order, is by parsing them from an input file. """ inp = Editor(path) found = inp.regex("atomic_positions", "nat", flags=re.I) nat = int(found['nat'][0][1].split("=")[-1]) start = found['atomic_positions'][0][0] length = "Angstrom" if "bohr" in inp[start].lower(): length = "au" xyz = pd.read_csv(StringIO("\n".join(inp[start + 1:start + 1 + nat])), names=("symbol", "x", "y", "z"), delim_whitespace=True) for q in ("x", "y", "z"): xyz[q] = Length[length, 'au'] * xyz[q].astype(float) return xyz
def setUpClass(cls): """ Generate the file path to the exatomic.exa.core.editor module (which will be used as the test for the :class:`~exatomic.exa.core.editor.Editor` class that it provides). """ cls.path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "editor.py")) with open(cls.path) as f: cls.lines = f.readlines() cls.fl = Editor.from_file(cls.path)
def test_dunder(self): ed = Editor("hello world", ignore=True) self.assertEqual(str(ed), "hello world") self.assertEqual(len(ed), 1) self.assertTrue("hello" in ed) self.assertTrue(callable(ed["find"])) ed[0] = "hi" self.assertEqual(str(ed), "hi") for line in ed: pass self.assertEqual(line, str(ed)) del ed[0] self.assertEqual(str(ed), "")
def test_head(self): ed = Editor("hello\nworld") f = io.StringIO() with redirect_stdout(f): ed.head(1) self.assertEqual("hello", f.getvalue())
def test_format_inplace(self): ed = Editor("hello {name}", ignore=True) self.assertEqual(str(ed), "hello {name}") self.assertEqual(ed.variables, ['{name}']) ed.format(inplace=True, name="world") self.assertEqual(str(ed), "hello world")
def test_delete_lines(self): ed = Editor("hello\nworld") ed.delete_lines([0]) self.assertEqual(str(ed), "world")
def test_insert(self): ed = Editor("world", ignore=True) ed.insert({0: "hello"}) self.assertEqual(str(ed), "hello\nworld")
def test_pandas_dataframe(self): ed = Editor("hello\nworld") df = ed.pandas_dataframe(0, len(ed), 1) self.assertTrue(df.equals(pd.DataFrame([["hello"], ["world"]]))) df = ed.pandas_dataframe(0, len(ed), ["text"]) self.assertTrue(df.equals(pd.DataFrame([["hello"], ["world"]], columns=["text"])))
def test_replace(self): ed = Editor("hello world", ignore=True) ed.replace("world", "universe") self.assertEqual(str(ed), "hello universe")
def test_remove_blank_lines(self): ed = Editor("hello\n\nworld") self.assertEqual("hello\n\nworld", str(ed)) ed.remove_blank_lines() self.assertEqual("hello\nworld", str(ed))
def test_preappend(self): ed = Editor("world", ignore=True) ed.prepend("hello") self.assertEqual("hello\nworld", str(ed))