10
0
mirror of https://github.com/LCPQ/EMSL_Basis_Set_Exchange_Local synced 2024-10-31 19:23:42 +01:00

Clean up and save option

This commit is contained in:
Thomas Applencourt 2015-01-08 16:12:15 +01:00
parent d66bedc716
commit 44bf2f0384
2 changed files with 108 additions and 42 deletions

View File

@ -4,11 +4,18 @@
"""EMSL Api.
Usage:
EMSL_api.py get_list_basis <db_path> [<elts>...]
EMSL_api.py get_list_elements <db_path> <basis_name>
EMSL_api.py get_basis_data <db_path> <basis_name> <elts>...
EMSL_api.py list_basis [--ele=element_name...]
[--db_path=db_path]
EMSL_api.py list_elements --basis=basis_name
[--db_path=db_path]
EMSL_api.py get_basis_data --basis=basis_name
[--ele=element_name...]
[--db_path=db_path]
[(--save [--path=path])]
EMSL_api.py get_list_formats
EMSL_api.py create_db <db_path> <format> [--no-contraction]
EMSL_api.py create_db --db_path=db_path
--format
[--no-contraction]
EMSL_api.py (-h | --help)
EMSL_api.py --version
@ -18,9 +25,10 @@ Options:
--no-contraction Basis functions are not contracted
<db_path> is the path to the SQLite3 file containing the Basis sets.
By default is $EMSL_API_ROOT/db/Gausian_uk.db
"""
version = "0.1.2"
version = "0.2.0"
import sys
@ -29,7 +37,6 @@ try:
from EMSL_utility import EMSL_dump
from EMSL_utility import format_dict
from EMSL_utility import EMSL_local
except:
print "Run setup.py then source EMSL_api.rc"
sys.exit(1)
@ -38,50 +45,92 @@ if __name__ == '__main__':
arguments = docopt(__doc__, version='EMSL Api ' + version)
if arguments["get_list_basis"]:
db_path = arguments["<db_path>"]
elts = arguments["<elts>"]
if arguments["--db_path"]:
db_path = arguments["--db_path"]
else:
import os
db_path = os.path.dirname(sys.argv[0]) + "/db/Gausian_uk.db"
# _ _ _ ______ _
#| | (_) | | | ___ \ (_)
#| | _ ___| |_ | |_/ / __ _ ___ _ ___
#| | | / __| __| | ___ \/ _` / __| / __|
#| |___| \__ \ |_ | |_/ / (_| \__ \ \__ \
#\_____/_|___/\__| \____/ \__,_|___/_|___/
#
if arguments["list_basis"]:
e = EMSL_local(db_path=db_path)
elts = arguments["--ele"]
l = e.get_list_basis_available(elts)
for i in l:
print i
elif arguments["get_list_elements"]:
db_path = arguments["<db_path>"]
basis_name = arguments["<basis_name>"]
for name, des in l:
print name, "|", des
# _ _ _ _____ _ _
#| | (_) | | | ___| | | |
#| | _ ___| |_ | |__ | | ___ _ __ ___ ___ _ __ | |_ ___
#| | | / __| __| | __|| |/ _ \ '_ ` _ \ / _ \ '_ \| __/ __|
#| |___| \__ \ |_ | |___| | __/ | | | | | __/ | | | |_\__ \
#\_____/_|___/\__| \____/|_|\___|_| |_| |_|\___|_| |_|\__|___/
if arguments["list_elements"]:
e = EMSL_local(db_path=db_path)
basis_name = arguments["--basis"]
l = e.get_list_element_available(basis_name)
for i in l:
print i
elif arguments["get_basis_data"]:
db_path = arguments["<db_path>"]
basis_name = arguments["<basis_name>"]
elts = arguments["<elts>"]
print ", ".join(l)
#______ _ _ _
#| ___ \ (_) | | | |
#| |_/ / __ _ ___ _ ___ __| | __ _| |_ __ _
#| ___ \/ _` / __| / __| / _` |/ _` | __/ _` |
#| |_/ / (_| \__ \ \__ \ | (_| | (_| | || (_| |
#\____/ \__,_|___/_|___/ \__,_|\__,_|\__\__,_|
if arguments["get_basis_data"]:
e = EMSL_local(db_path=db_path)
l = e.get_basis(basis_name, elts)
for i in l:
print i, '\n'
l = e.get_basis(arguments["--basis"], arguments["--ele"])
str_ = "\n\n".join(l) + "\n"
elif arguments["get_list_formats"]:
if arguments["--save"]:
if arguments["--path"]:
path = arguments["--path"]
else:
path = "/tmp/" + "_".join([basis_name, ".".join(elts)])
with open(path, 'w') as f:
f.write(str_ + "\n")
print path
else:
print str_
# _ _ _ __ _
#| | (_) | | / _| | |
#| | _ ___| |_ | |_ ___ _ __ _ __ ___ __ _| |_ ___
#| | | / __| __| | _/ _ \| '__| '_ ` _ \ / _` | __/ __|
#| |___| \__ \ |_ | || (_) | | | | | | | | (_| | |_\__ \
#\_____/_|___/\__| |_| \___/|_| |_| |_| |_|\__,_|\__|___/
if arguments["get_list_formats"]:
for i in format_dict:
print i
elif arguments["create_db"]:
db_path = arguments["<db_path>"]
format = arguments["<format>"]
# _____ _ _ _
#/ __ \ | | | | |
#| / \/_ __ ___ __ _| |_ ___ __| | |__
#| | | '__/ _ \/ _` | __/ _ \ / _` | '_ \
#| \__/\ | | __/ (_| | || __/ | (_| | |_) |
# \____/_| \___|\__,_|\__\___| \__,_|_.__/
if arguments["create_db"]:
db_path = arguments["--db_path"]
format = arguments["--format"]
if format not in format_dict:
print "Format %s doesn't exist. Run get_list_formats to get the list of formats." % (format)
sys.exit(1)
contraction = not arguments["--no-contraction"]
e = EMSL_dump(
db_path=db_path, format=format_dict[format], contraction=contraction)
db_path=db_path,
format=format_dict[format],
contraction=contraction)
e.new_db()

View File

@ -9,6 +9,16 @@ import time
debug = False
def cond_sql_or(table_name, l_value):
l = []
dmy = " OR ".join(['%s = "%s"' % (table_name, i) for i in l_value])
if dmy:
l.append("(%s)" % dmy)
return l
class EMSL_dump:
def __init__(self, db_path=None, format="GAMESS-US", contraction="True"):
@ -106,7 +116,7 @@ class EMSL_dump:
if (b == -1 or data.find("$DATA$END") != -1):
if debug:
print data
raise StandardError("WARNING not DATA")
raise Exception("WARNING not DATA")
else:
data = data[b + 5:e].split('\n\n')
for (elt, data_elt) in zip(elts, data):
@ -213,7 +223,8 @@ class EMSL_local:
data = c.fetchall()
else:
cmd = ["SELECT name,description FROM all_value WHERE elt=?"] * len(elts)
cmd = [
"SELECT name,description FROM all_value WHERE elt=?"] * len(elts)
cmd = " INTERSECT ".join(cmd) + ";"
c.execute(cmd, elts)
@ -229,8 +240,9 @@ class EMSL_local:
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
c.execute("SELECT DISTINCT elt from all_value WHERE name=:name_us COLLATE NOCASE",
{"name_us": basis_name})
c.execute(
"SELECT DISTINCT elt from all_value WHERE name=:name_us COLLATE NOCASE", {
"name_us": basis_name})
data = c.fetchall()
@ -246,13 +258,18 @@ class EMSL_local:
d = []
for elt in elts:
c.execute("SELECT DISTINCT data from all_value WHERE name=:name_cur COLLATE NOCASE AND elt=:elt_cur COLLATE NOCASE",
{"name_cur": basis_name,
"elt_cur": elt})
if elts:
cmd_ele = "AND " + " ".join(cond_sql_or("elt", elts))
else:
cmd_ele = ""
data = c.fetchone()
d.append(data[0])
c.execute('''SELECT DISTINCT data from all_value
WHERE name="{basis_name}" COLLATE NOCASE
{cmd_ele}'''.format(basis_name=basis_name,
cmd_ele=cmd_ele))
for data in c.fetchall():
d.append(data[0].strip())
conn.close()
return d