File: db.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 (41 lines) | stat: -rw-r--r-- 1,060 bytes parent folder | download | duplicates (3)
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
41
import ase.db
from ase.io.formats import string2index


def read_db(filename, index, **kwargs):
    db = ase.db.connect(filename, serial=True, **kwargs)

    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):
    con = ase.db.connect(filename, serial=True, append=append, **kwargs)
    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