File: db.py

package info (click to toggle)
python-ase 3.26.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (40 lines) | stat: -rw-r--r-- 1,153 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
import ase.db
from ase.io.formats import string2index


def read_db(filename, index, **kwargs):
    with ase.db.connect(filename, serial=True, **kwargs) as db:
        if isinstance(index, str):
            try:
                index = string2index(index)
            except ValueError:
                pass

        if isinstance(index, int):
            index = slice(index, index + 1 or None)

        if isinstance(index, str):
            # index is a database query string:
            for row in db.select(index):
                yield row.toatoms()
        else:
            start, stop, step = index.indices(db.count())
            if start == stop:
                return
            assert step == 1
            for row in db.select(offset=start, limit=stop - start):
                yield row.toatoms()


def write_db(filename, images, append=False, **kwargs):
    with ase.db.connect(filename, serial=True, append=append, **kwargs) as con:
        for atoms in images:
            con.write(atoms)


read_json = read_db
write_json = write_db
read_postgresql = read_db
write_postgresql = write_db
read_mysql = read_db
write_mysql = write_db