Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init(self):
"""Does the class initialize correctly?"""
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout")
data = cclib.io.ccread(fpath)
cjson = cclib.io.cjsonwriter.CJSON(data)
# The object should keep the ccData instance passed to its constructor.
self.assertEqual(cjson.ccdata, data)
def test_cjson_read(self):
"""File->ccData->CJSON->attribute_dict, the attributes within ccData and attribute_dict
should be the same."""
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout")
data = cclib.io.ccread(fpath)
self.assertIsNotNone(data, "The logfileparser failed to parse the output file")
cjson_obj = cclib.io.cjsonwriter.CJSON(data, terse=True)
self.assertEqual(cjson_obj.ccdata, data, "The ccData instance within the CJSON class should be the same as the "
"one generated by the logfileparsers")
# Generate the CJSON object to be written into a file.
cjson_data = cjson_obj.generate_repr()
with tempfile.NamedTemporaryFile(mode='w') as fp:
fp.write(cjson_data)
fp.flush()
cjson_Reader = cclib.io.cjsonreader.CJSON(fp.name)
read_cjson_data = cjson_Reader.read_cjson()
self.assertIsNotNone(read_cjson_data, "The CJSON reader failed to read attributes")
# The attribute values read by the CJSON reader will be a subset of the total attributes
# stored by the logfileparser in the ccData object.
ccdata_dict = data.getattributes()
def test_missing_dipole_moment(self):
"""Does the CJSON writer handle missing properties correctly?"""
fpath = os.path.join(__datadir__, "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out")
data = cclib.io.ccopen(fpath).parse()
del data.moments
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr()
json_data = json.loads(cjson)
self.assertFalse("total dipole moment" in json_data["properties"])
def test_zero_dipole_moment(self):
"""Does the CJSON writer handle zero dipole moment correctly?"""
fpath = os.path.join(__datadir__, "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out")
data = cclib.io.ccopen(fpath).parse()
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr()
json_data = json.loads(cjson)
self.assertAlmostEqual(json_data["properties"]['total dipole moment'], 0.0)
def test_cjson_generation(self):
"""Does the CJSON format get generated properly?"""
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/NH3.adfout")
data = cclib.io.ccread(fpath)
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr()
# The data available in the cjson and ccdata objects should be equal.
json_data = json.loads(cjson)
number_of_atoms = json_data['properties']['number of atoms']
self.assertEqual(number_of_atoms, data.natom)
dipole_moment = json_data['properties']['total dipole moment']
self.assertAlmostEqual(
dipole_moment,
sqrt(sum(data.moments[1] ** 2))
)
# Ensure the bond connectivity index starts from 0
bonds = json_data.get('bonds', None)
self.assertIsNotNone(bonds)
indices = bonds['connections']['index']
def read():
# Pass the standard input to ccopen:
log = ccopen(sys.stdin)
ccdata = log.parse()
output_obj = CJSON(ccdata, terse=True)
output = output_obj.generate_repr()
return output
def __init__(self, ccdata, terse=False, *args, **kwargs):
"""Initialize the chemical JSON writer object.
Inputs:
ccdata - An instance of ccData, parsed from a logfile.
"""
super(CJSON, self).__init__(ccdata, terse=terse, *args, **kwargs)