Beispiel #1
0
 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))))
Beispiel #2
0
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
Beispiel #3
0
 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)
Beispiel #4
0
 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), "")
Beispiel #5
0
 def test_head(self):
     ed = Editor("hello\nworld")
     f = io.StringIO()
     with redirect_stdout(f):
         ed.head(1)
     self.assertEqual("hello", f.getvalue())
Beispiel #6
0
 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")
Beispiel #7
0
 def test_delete_lines(self):
     ed = Editor("hello\nworld")
     ed.delete_lines([0])
     self.assertEqual(str(ed), "world")
Beispiel #8
0
 def test_insert(self):
     ed = Editor("world", ignore=True)
     ed.insert({0: "hello"})
     self.assertEqual(str(ed), "hello\nworld")
Beispiel #9
0
 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"])))
Beispiel #10
0
 def test_replace(self):
     ed = Editor("hello world", ignore=True)
     ed.replace("world", "universe")
     self.assertEqual(str(ed), "hello universe")
Beispiel #11
0
 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))
Beispiel #12
0
 def test_preappend(self):
     ed = Editor("world", ignore=True)
     ed.prepend("hello")
     self.assertEqual("hello\nworld", str(ed))