File: test_fileobj.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (40 lines) | stat: -rw-r--r-- 1,300 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# test reading and writing a file descriptor using its name

import pytest
import ase.io
from ase.build import bulk
from ase.calculators.calculator import compare_atoms


@pytest.fixture
def at():
    return bulk('Si')


def test_write_to_obj_name(at):
    # compare writing to a file with a particular extension and writing to
    # a file object that has same extension
    ase.io.write('direct_to_file.xyz', at)
    with open('to_file_obj.xyz','w') as fout:
        ase.io.write(fout, at)

    with open('direct_to_file.xyz') as f1, open('to_file_obj.xyz') as f2:
        for l1, l2 in zip(f1, f2):
            assert l1 == l2

    # compare reading from a file with a particular extension and reading from
    # a file object that has same extension
    at1 = ase.io.read('direct_to_file.xyz')
    with open('to_file_obj.xyz') as fin:
        at2 = ase.io.read(fin)
    print('compare',compare_atoms(at1, at2, 1.0e-10))
    assert len(compare_atoms(at1, at2)) == 0

    # compare reading from a file with a particular extension and reading from
    # a file object that has same extension
    at1 = ase.io.iread('direct_to_file.xyz')
    with open('to_file_obj.xyz') as fin:
        at2 = ase.io.iread(fin)
        for a1, a2 in zip(at1, at2):
            assert len(compare_atoms(a1, a2)) == 0