def test_compare_values(self, category_data): dcU = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode']) dcM = DataCategory('A', category_data['attributeListMiss'], category_data['rowListUnicodeMiss']) na = dcU.getAttributeList() assert len(na) >= 1 tupL = dcU.cmpAttributeValues(dcU) for tup in tupL: assert tup[1] == True tupL = dcU.cmpAttributeValues(dcM) for tup in tupL: if tup[0] in ['colC', 'colD']: assert tup[1] == False else: assert tup[1] == True # dcX = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode']) assert dcX.setValue( u'134ĆćĈĉĊċČÄ�ÄŽÄ�Ä�Ä‘Ä’Ä“Ä Ä¡Ä¢Ä£Ä¤Ä¥Ä¦Ä§Ä¨xyz', attributeName='colD', rowIndex=dcX.getRowCount() - 2) tupL = dcU.cmpAttributeValues(dcX) for tup in tupL: if tup[0] in ['colD']: assert tup[1] == False else: assert tup[1] == True
def testCompareValues(self): """Test case - compare object values -""" try: dcU = DataCategory("A", self.__attributeList, self.__rowListUnicode) dcM = DataCategory("A", self.__attributeListMiss, self.__rowListUnicodeMiss) na = dcU.getAttributeList() self.assertGreaterEqual(len(na), 1) tupL = dcU.cmpAttributeValues(dcU) for tup in tupL: self.assertEqual(tup[1], True) tupL = dcU.cmpAttributeValues(dcM) for tup in tupL: if tup[0] in ["colC", "colD"]: self.assertEqual(tup[1], False) else: self.assertEqual(tup[1], True) # dcX = DataCategory("A", self.__attributeList, self.__rowListUnicode) self.assertTrue( dcX.setValue(u"134ĆćĈĉĊċČčĎďĐđĒēĠġĢģĤĥĦħĨxyz", attributeName="colD", rowIndex=dcX.getRowCount() - 2)) tupL = dcU.cmpAttributeValues(dcX) for tup in tupL: if tup[0] in ["colD"]: self.assertEqual(tup[1], False) else: self.assertEqual(tup[1], True) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_basic_ascii(self, category_data): dcA = DataCategory('A', category_data['attributeList'], category_data['rowListAsciiA']) dcB = DataCategory('A', category_data['attributeList'], category_data['rowListAsciiA']) assert dcA == dcA assert dcB == dcB assert dcA == dcB
def test_basic_unicode(self, category_data): dcA = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode']) dcB = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode']) assert dcA == dcA assert dcB == dcB assert dcA == dcB
def testBasicAscii(self): """Test case - subcclass instantiation with ascii data""" try: dcA = DataCategory("A", self.__attributeList, self.__rowListAsciiA) dcB = DataCategory("A", self.__attributeList, self.__rowListAsciiA) self.assertEqual(dcA, dcA) self.assertEqual(dcB, dcB) self.assertEqual(dcA, dcB) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def invokeMethods(self, fh=sys.stdout): _ = fh mI = self.__dApi.getMethodIndex() lenD = len(mI) i = 0 for k, mRefL in mI.items(): for mRef in mRefL: i += 1 mId = mRef.getId() mType = mRef.getType() categoryName = mRef.getCategoryName() attributeName = mRef.getAttributeName() # logger.debug("\n") logger.debug("++++++++++++++++++--------------------\n") logger.debug( "Invoking dictionary method on file object: %s (%d/%d)", k, i, lenD) logger.debug(" + Method id: %s", mId) logger.debug(" + Type: %s", mType) logger.debug(" + Category: %s", categoryName) logger.debug(" + Attribute: %s", attributeName) # if mType == "datablock": logger.debug("Invoke datablock method %s", mId) # self.invokeDataBlockMethod(type,self.__dApi.getMethod(id)) # continue # for db in self.__dataContainerList: if mType == "category": if not db.exists(categoryName): dc = DataCategory(categoryName) db.append(dc) dObj = db.getObj(categoryName) dObj.invokeCategoryMethod(mType, self.__dApi.getMethod(mId), db) elif mType == "attribute": if not db.exists(categoryName): dc = DataCategory(categoryName) db.append(dc) dObj = db.getObj(categoryName) # logger.debug("invoke - %r %r %r %r" % (attributeName, type, self.__dApi.getMethod(id), db)) dObj.invokeAttributeMethod(attributeName, mType, self.__dApi.getMethod(mId), db) elif mType == "datablock": logger.debug("Invoke datablock method %s", mId) db.invokeDataBlockMethod(mType, self.__dApi.getMethod(mId), db) else: pass
def _makeKeyItem(self, catName, attName, keyItemList, iDef): itemName = CifName.itemName(catName, attName) # dc = DataCategory("definition") dc.appendAttribute("id") dc.appendAttribute("scope") dc.appendAttribute("class") dc.appendAttribute("update") dc.append([itemName, "Item", "Single", "2013-08-22"]) iDef.append(dc) # dc = DataCategory("description") dc.appendAttribute("text") dc.append(['synthentic componsite key']) iDef.append(dc) # dc = DataCategory("name") dc.appendAttribute("category_id") dc.appendAttribute("object_id") dc.append([catName, attName]) iDef.append(dc) tcontainer = 'Set' purpose = 'Composite' source = 'Derived' contents = 'Name' dimension = '[%d]' % len(keyItemList) # dc = DataCategory("type") dc.appendAttribute("purpose") dc.appendAttribute("source") dc.appendAttribute("contents") dc.appendAttribute("container") dc.appendAttribute("dimension") dc.append([purpose, source, contents, tcontainer, dimension]) iDef.append(dc) dc = DataCategory("method") dc.appendAttribute("purpose") dc.appendAttribute("expression") tmpl = ''' With row as %s %s = [%s] ''' mText = tmpl % (catName, itemName, ','.join(keyItemList)) dc.append(['Evaluation', mText]) iDef.append(dc)
def __makeKeyItem(self, catName, attName, keyItemList, iDef): itemName = CifName.itemName(catName, attName) # dc = DataCategory("definition") dc.appendAttribute("id") dc.appendAttribute("scope") dc.appendAttribute("class") dc.appendAttribute("update") dc.append([itemName, "Item", "Single", "2013-08-22"]) iDef.append(dc) # dc = DataCategory("description") dc.appendAttribute("text") dc.append(["synthentic componsite key"]) iDef.append(dc) # dc = DataCategory("name") dc.appendAttribute("category_id") dc.appendAttribute("object_id") dc.append([catName, attName]) iDef.append(dc) tcontainer = "Set" purpose = "Composite" source = "Derived" contents = "Name" dimension = "[%d]" % len(keyItemList) # dc = DataCategory("type") dc.appendAttribute("purpose") dc.appendAttribute("source") dc.appendAttribute("contents") dc.appendAttribute("container") dc.appendAttribute("dimension") dc.append([purpose, source, contents, tcontainer, dimension]) iDef.append(dc) dc = DataCategory("method") dc.appendAttribute("purpose") dc.appendAttribute("expression") tmpl = """ With row as %s %s = [%s] """ mText = tmpl % (catName, itemName, ",".join(keyItemList)) dc.append(["Evaluation", mText]) iDef.append(dc)
def testBasicUnicode(self): """Test case - __eq__ and __ne__ methods w/ unicode""" try: dcA = DataCategory("A", self.__attributeList, self.__rowListUnicode) dcB = DataCategory("A", self.__attributeList, self.__rowListUnicode) self.assertEqual(dcA, dcA) self.assertEqual(dcB, dcB) self.assertEqual(dcA, dcB) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testBasicAsciiDiff(self): """Test case - __eq__ and __ne__ methods""" try: dcA = DataCategory("A", self.__attributeList, self.__rowListAsciiA) dcB = DataCategory("A", self.__attributeList, self.__rowListAsciiB) self.assertEqual(dcA, dcA) self.assertIs(dcA, dcA) self.assertEqual(dcB, dcB) self.assertNotEqual(dcA, dcB) self.assertIsNot(dcA, dcB) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_compare_attributes(self, category_data): dcU = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode']) dcM = DataCategory('A', category_data['attributeListMiss'], category_data['rowListUnicodeMiss']) na = len(dcU.getAttributeList()) t1, t2, t3 = dcU.cmpAttributeNames(dcU) assert len(t1) == 0 assert len(t3) == 0 assert len(t2) == na t1, t2, t3 = dcU.cmpAttributeNames(dcM) assert len(t1) == 0 assert len(t3) == 3 assert len(t2) == na
def __buildCategoryDefinition(self, name, description, keyAttributeNames, examples, contexts): """Construct an attribute definition from input attribute dictionary containing metadata extracted from the XML schema, and from the input schema name mapping dictionary. Args: name (str): category name description (str): category description keyAttributeNames (list): key attribute names examples (list): category examples contexts (list): category contexts Returns: Definition container (object): """ defC = DefinitionContainer(name) # dc = DataCategory( "category", attributeNameList=["id", "description", "mandatory_code"]) dc.append([name, description, "no"]) defC.append(dc) # dc = DataCategory("category_key", attributeNameList=["name"]) for keyAttributeName in keyAttributeNames: keyItemName = CifName.itemName(name, keyAttributeName) dc.append([keyItemName]) defC.append(dc) dc = DataCategory("category_group", attributeNameList=["id"]) dc.append(["inclusive_group"]) dc.append(["validation_report_group"]) defC.append(dc) # pdbx_category_context dc = DataCategory("pdbx_category_context", attributeNameList=["category_id", "type"]) for cType in contexts: dc.append([name, cType]) defC.append(dc) # dc = DataCategory("category_examples", attributeNameList=["detail", "case"]) for example in examples: dc.append([".", example]) defC.append(dc) return defC
def _createfile1(pathout): my_data_list = [] cur_container = DataContainer("myblock") acat = DataCategory("pdbx_item_enumeration") acat.appendAttribute("name") acat.appendAttribute("value") acat.appendAttribute("detail") acat.append(("1", "2", "3")) cur_container.append(acat) acat = DataCategory("exptl") acat.appendAttribute("absorpt_coefficient_mu") acat.appendAttribute("entry_id") acat.appendAttribute("method") acat.appendAttribute("details") acat.append(("?", "D_12345", "X-RAY DIFFRACTION", "some details")) cur_container.append(acat) acat = DataCategory("struct") acat.appendAttribute("title") acat.appendAttribute("pdbx_descriptor") acat.append(("Start title", "Start Descriptor")) cur_container.append(acat) my_data_list.append(cur_container) # Second block cur_container = DataContainer("secondblock") acat = DataCategory("pdbx_item_enumeration") acat.appendAttribute("name") acat.appendAttribute("value") acat.appendAttribute("detail") acat.append(("3", "2", "1")) cur_container.append(acat) my_data_list.append(cur_container) with open(pathout, "w") as ofh: pdbxw = PdbxWriter(ofh) pdbxw.setAlignmentFlag(flag=True) pdbxw.write(my_data_list)
def _createfile1(pathout): my_data_list = [] cur_container = DataContainer("myblock") acat = DataCategory("pdbx_item_enumeration") acat.appendAttribute("name") acat.appendAttribute("value") acat.appendAttribute("detail") acat.append(('1', '2', '3')) cur_container.append(acat) acat = DataCategory("exptl") acat.appendAttribute('absorpt_coefficient_mu') acat.appendAttribute('entry_id') acat.appendAttribute('method') acat.appendAttribute('details') acat.append(('?', 'D_12345', 'X-RAY DIFFRACTION', 'some details')) cur_container.append(acat) acat = DataCategory("struct") acat.appendAttribute('title') acat.appendAttribute('pdbx_descriptor') acat.append(('Start title', 'Start Descriptor')) cur_container.append(acat) my_data_list.append(cur_container) # Second block cur_container = DataContainer("secondblock") acat = DataCategory("pdbx_item_enumeration") acat.appendAttribute("name") acat.appendAttribute("value") acat.appendAttribute("detail") acat.append(('3', '2', '1')) cur_container.append(acat) my_data_list.append(cur_container) with open(pathout, "w") as ofh: pdbxw = PdbxWriter(ofh) pdbxw.setAlignmentFlag(flag=True) pdbxw.write(my_data_list)
def testReplaceValues(self): """Test case - replace values""" try: dcU = DataCategory("A", self.__attributeListMiss, self.__rowListUnicodeMiss) at = self.__attributeListMiss[3] curV = self.__testRowUnicodeMiss[2] self.assertEqual(dcU.replaceValue(curV, "newVal", at), dcU.getRowCount()) at = self.__attributeListMiss[4] curV = self.__testRowUnicodeMiss[3] self.assertEqual(dcU.replaceValue(curV, "newVal", at), dcU.getRowCount()) at = self.__attributeListMiss[5] curV = self.__testRowUnicodeMiss[4] self.assertEqual(dcU.replaceValue(curV, "newVal", at), dcU.getRowCount()) at = self.__attributeListMiss[6] curV = self.__testRowUnicodeMiss[5] self.assertEqual(dcU.replaceValue(curV, "newVal", at), dcU.getRowCount()) for ii in range(3, 7): at = self.__attributeListMiss[ii] self.assertEqual(dcU.replaceSubstring("newVal", "nextVal", at), dcU.getRowCount()) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_cond_select_values(self, category_data): dcM = DataCategory('A', category_data['attributeListMiss'], category_data['rowListUnicodeMiss']) atL = dcM.getAttributeList() for ii, at in enumerate(atL[1:]): assert len( dcM.selectIndices(category_data['testRowUnicodeMiss'][ii], at)) == dcM.getRowCount() # print("Window %r" % [tt for tt in window(atL)]) for atW in window(atL, size=1): assert len( dcM.selectValueListWhere( atW, category_data['testRowUnicodeMiss'][-1], category_data['attributeListMiss'] [-1])) == dcM.getRowCount() for atW in window(atL, size=2): assert len( dcM.selectValueListWhere( atW, category_data['testRowUnicodeMiss'][-1], category_data['attributeListMiss'] [-1])) == dcM.getRowCount() for atW in window(atL, size=3): assert len( dcM.selectValueListWhere( atW, category_data['testRowUnicodeMiss'][-1], category_data['attributeListMiss'] [-1])) == dcM.getRowCount() for atW in window(atL, size=4): assert len( dcM.selectValueListWhere( atW, category_data['testRowUnicodeMiss'][-1], category_data['attributeListMiss'] [-1])) == dcM.getRowCount()
def AddCategory(self, categoryID, items): """Add Category""" category = DataCategory(categoryID) for item in items: category.appendAttribute(item) # self.__container.append(category)
def testEditAttributes(self): """Test case - get and extend atttribute names""" try: # dcA = DataCategory("A", self.__attributeList, self.__rowListAsciiA) self.assertEqual(0, dcA.getRowIndex()) self.assertEqual(None, dcA.getCurrentAttribute()) # na = len(dcA.getAttributeList()) tL = dcA.getAttributeListWithOrder() self.assertEqual(len(tL), na) na = len(dcA.getAttributeList()) self.assertEqual(dcA.appendAttribute("ColNew"), na + 1) row = dcA.getFullRow(0) self.assertEqual(row[na], "?") # row = dcA.getFullRow(dcA.getRowCount() + 1) for cV in row: self.assertEqual(cV, "?") # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testGetValues(self): """Test case - value getters""" try: dcU = DataCategory("A", self.__attributeList, self.__rowListUnicode) aL = dcU.getAttributeList() logger.debug("Row length %r", dcU.getRowCount()) for ii, v in enumerate(self.__testRowUnicode): at = aL[ii + 1] for j in range(0, dcU.getRowCount()): logger.debug("ii %d j %d at %s val %r ", ii, j, at, v) self.assertEqual(dcU.getValue(at, j), v) self.assertEqual(dcU.getValueOrDefault(at, j, "mydefault"), v) # # negative indices are interpreted in the python manner self.assertEqual(dcU.getValueOrDefault("colOrd", -1, "default"), 9) self.assertRaises(IndexError, dcU.getValue, "colOrd", dcU.getRowCount() + 1) self.assertRaises(ValueError, dcU.getValue, "badAtt", 0) # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_single_row(self, rw_data): myDataList = [] curContainer = DataContainer("myblock") aCat = DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.appendAttribute("details") aCat.append([1, 2, 3, 4, 5, 6, 7, 'data_my_big_data_file']) aCat.append([1, 2, 3, 4, 5, 6, 7, 'loop_my_big_data_loop']) aCat.append([1, 2, 3, 4, 5, 6, 7, 'save_my_big_data_saveframe']) aCat.append([1, 2, 3, 4, 5, 6, 7, '_category.item']) curContainer.append(aCat) bCat = curContainer.getObj("pdbx_seqtool_mapping_ref") print("----attribute list %r\n" % bCat.getAttributeList()) row = bCat.getRow(0) print("----ROW %r\n" % row) with open(str(rw_data['pathOutputFile2']), "w") as ofh: myDataList.append(curContainer) pdbxW = PdbxWriter(ofh) pdbxW.write(myDataList) assert len(myDataList) == 1
def test_edit_extend(self, category_data): dcA = DataCategory('A', category_data['attributeList'], category_data['rowListAsciiA']) na = len(dcA.getAttributeList()) assert dcA.appendAttributeExtendRows('colNew') == na + 1 row = dcA.getRow(dcA.getRowCount() - 1) assert row[na] == "?"
def _createfile2(pathout): my_data_list = [] cur_container = DataContainer("test") acat = DataCategory("new") acat.appendAttribute("item") acat.append(('1',)) cur_container.append(acat) acat = DataCategory("second_category") acat.appendAttribute('row') acat.appendAttribute('rowb') acat.append(('1', '2')) cur_container.append(acat) acat = DataCategory("third") acat.appendAttribute('id') acat.appendAttribute('val') acat.append(('1', 'a')) acat.append(('2', 'b')) acat.append(('3', 'c')) cur_container.append(acat) acat = DataCategory("exptl") acat.appendAttribute('method') acat.appendAttribute('entry_id') acat.append(('NEW', 'something')) cur_container.append(acat) acat = DataCategory("struct") acat.appendAttribute('new') acat.appendAttribute('pdbx_descriptor') acat.append(('Something to add', 'Override descriptor')) cur_container.append(acat) my_data_list.append(cur_container) with open(pathout, "w") as ofh: pdbxw = PdbxWriter(ofh) pdbxw.setAlignmentFlag(flag=True) pdbxw.write(my_data_list)
def _createfile2(pathout): my_data_list = [] cur_container = DataContainer("test") acat = DataCategory("new") acat.appendAttribute("item") acat.append(("1",)) cur_container.append(acat) acat = DataCategory("second_category") acat.appendAttribute("row") acat.appendAttribute("rowb") acat.append(("1", "2")) cur_container.append(acat) acat = DataCategory("third") acat.appendAttribute("id") acat.appendAttribute("val") acat.append(("1", "a")) acat.append(("2", "b")) acat.append(("3", "c")) cur_container.append(acat) acat = DataCategory("exptl") acat.appendAttribute("method") acat.appendAttribute("entry_id") acat.append(("NEW", "something")) cur_container.append(acat) acat = DataCategory("struct") acat.appendAttribute("new") acat.appendAttribute("pdbx_descriptor") acat.append(("Something to add", "Override descriptor")) cur_container.append(acat) my_data_list.append(cur_container) with open(pathout, "w") as ofh: pdbxw = PdbxWriter(ofh) pdbxw.setAlignmentFlag(flag=True) pdbxw.write(my_data_list)
def test_edit_row_accessors(self, category_data): dcA = DataCategory('A', category_data['attributeList'], category_data['rowListAsciiA']) with pytest.raises(IndexError): dcA.getRow(dcA.getRowCount() + 1) with pytest.raises(IndexError): dcA.getRowAttributeDict(dcA.getRowCount() + 1) with pytest.raises(IndexError): dcA.getRowItemDict(dcA.getRowCount() + 1)
def testCompareAttributes(self): """Test case - compare object attributes -""" try: dcU = DataCategory("A", self.__attributeList, self.__rowListUnicode) dcM = DataCategory("A", self.__attributeListMiss, self.__rowListUnicodeMiss) na = len(dcU.getAttributeList()) t1, t2, t3 = dcU.cmpAttributeNames(dcU) self.assertEqual(len(t1), 0) self.assertEqual(len(t3), 0) self.assertEqual(len(t2), na) t1, t2, t3 = dcU.cmpAttributeNames(dcM) self.assertEqual(len(t1), 0) self.assertEqual(len(t3), 3) self.assertEqual(len(t2), na) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_edit_remove_row(self, category_data): dcA = DataCategory('A', category_data['attributeList'], category_data['rowListUnicode'], raiseExceptions=True) for jj in range(0, dcA.getRowCount()): ii = dcA.getRowCount() dcA.removeRow(0) assert ii - 1 == dcA.getRowCount() # assert 0 == dcA.getRowCount()
def add_category(self, category_id, items): """ This method creates a data category object, adds all items to it and appends it to the container :param category_id: a string; an mmcif category e.g. 'emd_admin' :param items: a list of strings; each element in the list is an item of mmcif category as defined by category_id :return: a list of strings; each element represents a value for the corresponding element in data_items """ category = DataCategory(category_id) for item in items: category.appendAttribute(item) self.__container.append(category)
def testUpdateDataFile(self): """Test case - update data file """ try: # Create a initial data file -- # myDataList = [] curContainer = DataContainer("myblock") aCat = DataCategory("pdbx_seqtool_mapping_ref") aCat.appendAttribute("ordinal") aCat.appendAttribute("entity_id") aCat.appendAttribute("auth_mon_id") aCat.appendAttribute("auth_mon_num") aCat.appendAttribute("pdb_chain_id") aCat.appendAttribute("ref_mon_id") aCat.appendAttribute("ref_mon_num") aCat.append([9, 2, 3, 4, 5, 6, 7]) aCat.append([10, 2, 3, 4, 5, 6, 7]) aCat.append([11, 2, 3, 4, 5, 6, 7]) aCat.append([12, 2, 3, 4, 5, 6, 7]) curContainer.append(aCat) myDataList.append(curContainer) ofh = open(self.__pathOutputFile1, "w") pdbxW = PdbxWriter(ofh) pdbxW.write(myDataList) ofh.close() # # # Read and update the data - # myDataList = [] ifh = open(self.__pathOutputFile1, "r") pRd = PdbxReader(ifh) pRd.read(myDataList) ifh.close() # myBlock = myDataList[0] # myBlock.printIt() myCat = myBlock.getObj("pdbx_seqtool_mapping_ref") # myCat.printIt() for iRow in range(0, myCat.getRowCount()): myCat.setValue("some value", "ref_mon_id", iRow) myCat.setValue(100, "ref_mon_num", iRow) with open(self.__pathOutputFile2, "w") as ofh: pdbxW = PdbxWriter(ofh) pdbxW.write(myDataList) # self.assertEqual(len(myDataList), 1) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_set_values(self, category_data): dcU = DataCategory('A', category_data['attributeListMiss'], category_data['rowListUnicodeMiss']) for i in range(0, dcU.getRowCount()): dcU.setValue('newValue', attributeName='colM1', rowIndex=i) assert dcU.setValue('newValue', attributeName='colM1', rowIndex=dcU.getRowCount() + 5) with pytest.raises(ValueError): dcU.setValue('newValue', 'colX', 0)