/usr/share/psi/python/qcdb/psiutil.py is in psi4-data 1:0.3-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | #
#@BEGIN LICENSE
#
# PSI4: an ab initio quantum chemistry software package
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#@END LICENSE
#
r"""Stuff stolen from psi. Should import or not as necessary
or some better way. Apologies to the coders.
"""
import sys
import math
import re
import os
import string
from vecutil import *
from p4xcpt import *
def _success(label):
"""Function to print a '*label*...PASSED' line to screen.
Used by :py:func:`util.compare_values` family when functions pass.
"""
print('\t{0:.<66}PASSED'.format(label))
sys.stdout.flush()
def compare_values(expected, computed, digits, label):
"""Function to compare two values. Prints :py:func:`util.success`
when value *computed* matches value *expected* to number of *digits*
(or to *digits* itself when *digits* > 1 e.g. digits=0.04). Performs
a system exit on failure. Used in input files in the test suite.
"""
thresh = 10 ** -digits if digits > 1 else digits
if abs(expected - computed) > thresh:
message = ("\t%s: computed value (%f) does not match (%f) to %f digits." % (label, computed, expected, digits))
raise ValidationError(message)
if math.isnan(computed):
message = ("\t%s: computed value (%f) does not match (%f)\n\tprobably because the computed value is nan." % (label, computed, expected))
raise ValidationError(message)
_success(label)
def compare_integers(expected, computed, label):
"""Function to compare two integers. Prints :py:func:`util.success`
when value *computed* matches value *expected*.
Performs a system exit on failure. Used in input files in the test suite.
"""
if (expected != computed):
message = ("\t%s: computed value (%d) does not match (%d)." % (label, computed, expected))
raise ValidationError(message)
_success(label)
def compare_strings(expected, computed, label):
"""Function to compare two strings. Prints :py:func:`util.success`
when string *computed* exactly matches string *expected*.
Performs a system exit on failure. Used in input files in the test suite.
"""
if(expected != computed):
message = ("\t%s: computed value (%s) does not match (%s)." % (label, computed, expected))
raise ValidationError(message)
_success(label)
def compare_matrices(expected, computed, digits, label):
"""Function to compare two matrices. Prints :py:func:`util.success`
when elements of matrix *computed* match elements of matrix *expected* to
number of *digits*. Performs a system exit on failure to match symmetry
structure, dimensions, or element values. Used in input files in the test suite.
"""
rows = len(expected)
cols = len(expected[0])
failed = 0
for row in range(rows):
for col in range(cols):
if abs(expected[row][col] - computed[row][col]) > 10 ** (-digits):
print("\t%s: computed value (%s) does not match (%s)." % (label, expected[row][col], computed[row][col]))
failed = 1
break
if failed:
print("The Failed Test Matrices\n")
show(computed)
print('\n')
show(expected)
raise ValidationError("\n")
_success(label)
def query_yes_no(question, default=True):
"""Ask a yes/no question via raw_input() and return their answer.
*question* is a string that is presented to the user.
*default* is the presumed answer if the user just hits <Enter>.
It must be yes (the default), no or None (meaning
an answer is required of the user).
The return value is one of True or False.
"""
yes = re.compile(r'^(y|yes|true|on|1)', re.IGNORECASE)
no = re.compile(r'^(n|no|false|off|0)', re.IGNORECASE)
if default == None:
prompt = " [y/n] "
elif default == True:
prompt = " [Y/n] "
elif default == False:
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = raw_input().strip().lower()
if default is not None and choice == "":
return default
elif yes.match(choice):
return True
elif no.match(choice):
return False
else:
sys.stdout.write(" Please respond with 'yes' or 'no'.\n")
## {{{ http://code.activestate.com/recipes/52224/ (r1)
def search_file(filename, search_path):
"""Given an os.pathsep divided *search_path*, find first occurance of
*filename*. Returns full path to file if found or None if unfound.
"""
file_found = False
paths = string.split(search_path, os.pathsep)
for path in paths:
if os.path.exists(os.path.join(path, filename)):
file_found = True
break
if file_found:
return os.path.abspath(os.path.join(path, filename))
else:
return None
## end of http://code.activestate.com/recipes/52224/ }}}
def drop_duplicates(seq):
"""Function that given an array *seq*, returns an array without any duplicate
entries. There is no guarantee of which duplicate entry is dropped.
"""
#noDupes = []
#[noDupes.append(i) for i in seq if not noDupes.count(i)]
#return noDupes
noDupes = []
seq2 = sum(seq, [])
[noDupes.append(i) for i in seq2 if not noDupes.count(i)]
return noDupes
def all_casings(input_string):
"""Function to return a generator of all lettercase permutations
of *input_string*.
"""
if not input_string:
yield ""
else:
first = input_string[:1]
if first.lower() == first.upper():
for sub_casing in all_casings(input_string[1:]):
yield first + sub_casing
else:
for sub_casing in all_casings(input_string[1:]):
yield first.lower() + sub_casing
yield first.upper() + sub_casing
def getattr_ignorecase(module, attr):
"""Function to extract attribute *attr* from *module* if *attr*
is available in any possible lettercase permutation. Returns
attribute if available, None if not.
"""
array = None
for per in list(all_casings(attr)):
try:
getattr(module, per)
except AttributeError:
pass
else:
array = getattr(module, per)
break
return array
def import_ignorecase(module):
"""Function to import *module* in any possible lettercase
permutation. Returns module object if available, None if not.
"""
modobj = None
for per in list(all_casings(module)):
try:
modobj = __import__(per)
except ImportError:
pass
else:
break
return modobj
|