def append(self, field: Field): """ insert (at the end, so append) a field object into a record :param Field field: field object to append :: rec.append(Field("FIELD1", "Field description #1", FieldType("A","string"), 10)) """ # set field index at the same time field.index = len(self._field_list) # offset at this moment is merely the length of record (starts at 0) field.offset = self.length # add a field to record self._field_list.append(field) # as a field name can be appear more than once, we keep # track of all fields using this dict if field.name in self._field_dict: self._field_dict[field.name].append(field) else: self._field_dict[field.name] = [field] # record is becoming longer self.length += field.length # and adjust field bounds field.lower_bound = field.offset field.upper_bound = field.offset + field.length
def setUp(self): self.ft = FieldType("A/N", "string") self.rec = Record("RECORD1", "Description of record 1") self.rec.append(Field("FIELD1", "Description of field 1", self.ft, 10)) self.rec.append(Field("FIELD2", "Description of field 2", self.ft, 5)) self.rec.append(Field("FIELD2", "Description of field 2", self.ft, 5)) self.rec.append(Field("FIELD3", "Description of field 3", self.ft, 10)) self.line = "A" * 10 + "B" * 5 + "C" * 5 + "D" * 10
def test_11(self): self.rec = Record("RECORD1", "Description of record 1") # read data from external file for line in open("./record.data"): a = [s.strip() for s in line.split(';')] self.rec.append(Field(a[0], a[1], self.ft, int(a[2]))) self.assertEqual(len(self.rec), 400) self.assertEqual(self.rec.count(), 94) self.assertEqual(len(self.rec["COUN"]), 4) self.assertEqual(self.rec[28].name, "COUN") self.assertEqual("COUN" in self.rec, True) self.assertEqual("XXXX" not in self.rec, True) self.assertEqual("COUN" in self.rec.array_of('name'), True) l = self.rec.array_of('length') self.assertEqual(sum(l), 400) self.rec.delete(["COUN", "RCID"]) self.assertEqual("COUN" not in self.rec, True) with self.assertRaises(ValueError): f = self.rec["COUN"] self.rec.keep(["CABI", "STPO", "ORAC"]) self.assertEqual("CABI" in self.rec, True) self.assertEqual(len(self.rec.array_of('name')), 4 * 3)
def test(self): self.writer = writer("", WriterStyle.text) self.ft = FieldType("A/N", "string") self.rec = Record("RECORD1", "Description of record 1") self.rec.append( Field("LONG_FIELD1", "Description of field 1", self.ft, 10)) self.rec.append( Field("LONG_FIELD2", "Description of field 2", self.ft, 5)) self.rec.append( Field("LONG_FIELD2", "Description of field 2", self.ft, 5)) self.rec.append( Field("LONG_FIELD3", "Description of field 3", self.ft, 10)) self.line = "A" * 10 + "B" * 5 + "C" * 5 + "D" * 10 self.rec.value = self.line self.writer.to_tag(self.rec) self.assertEqual( 'RECORD1:LONG_FIELD1="AAAAAAAAAA" LONG_FIELD2="BBBBB" LONG_FIELD2="CCCCC" LONG_FIELD3="DDDDDDDDDD"\n', str(self.writer))
def setUp(self): self.ft1 = FieldType("AN", "string") self.f1 = Field("FIELD1", "Alpha field", self.ft1, 10) self.ft2 = FieldType("I", "integer") self.f2 = Field("FIELD2", "Integer field", self.ft2, 10) self.ft3 = FieldType("N", "decimal") self.f3 = Field("FIELD3", "Decimal field", self.ft3, 10) self.ft4 = FieldType("D", "date") self.f4 = Field("FIELD4", "Date field", self.ft4, 8) self.ft5 = FieldType("T", "time") self.f5 = Field("FIELD5", "Time field", self.ft5, 4)
def __init__(self, xml_file: str): # check file if accessible if not os.path.isfile(xml_file): raise ValueError( "XML description file {0} not found!!".format(xml_file)) # call parent ctor super(self.__class__, self).__init__(xml_file, "", 0) # init record dict self._record = {} # init field type dict ftypes = {} # parse document doc = minidom.parse(xml_file) # get <meta> attributes meta = doc.getElementsByTagName("meta")[0] # automatically add meta tags self._add_meta_tags(meta) # build field types dict for node in doc.getElementsByTagName("fieldtype"): # create Field object and save it into dict ft = FieldType.from_xml_node(node) ftypes[ft.name] = ft # loop on all records for rec in doc.getElementsByTagName("record"): # create first rec object recname = rec.attributes['name'].value recdesc = rec.attributes['description'].value self._record[recname] = Record(recname, recdesc) # now loop on fields and append field to record for node in rec.childNodes: if node.nodeType == node.ELEMENT_NODE and node.nodeName == "field": fname = node.attributes['name'].value fdesc = node.attributes['description'].value ftype = ftypes[node.attributes['type'].value] flength = int(node.attributes['length'].value) # add field to record self._record[recname].append( Field(fname, fdesc, ftype, flength))
class TestField(unittest.TestCase): def setUp(self): self.ft1 = FieldType("AN", "string") self.f1 = Field("FIELD1", "Alpha field", self.ft1, 10) self.ft2 = FieldType("I", "integer") self.f2 = Field("FIELD2", "Integer field", self.ft2, 10) self.ft3 = FieldType("N", "decimal") self.f3 = Field("FIELD3", "Decimal field", self.ft3, 10) self.ft4 = FieldType("D", "date") self.f4 = Field("FIELD4", "Date field", self.ft4, 8) self.ft5 = FieldType("T", "time") self.f5 = Field("FIELD5", "Time field", self.ft5, 4) def test_bad_cons(self): self.assertRaises(ValueError, Field, "", "Alpha field 1", self.ft1, 10) self.assertRaises(ValueError, Field, "FIELD1", "Alpha field", self.ft1, -1) """ def test_other_cons(self): self.doc = minidom.parseString('<field name="FIELD" description="Field desc" length="10" type="AN"/>') self.xml_node = self.doc.childNodes[0] self.other_f = Field.from_xml_node(self.xml_node) self.assertEqual(self.other_f.name, "FIELD") self.assertEqual(self.other_f.description, "Field desc") self.assertEqual(self.other_f.ftype.name, "AN") self.assertEqual(self.other_f.length, 10) """ def test_equality(self): self.assertEqual(self.f1, Field("FIELD1", "Alpha field", self.ft1, 10)) def test_properties(self): self.assertEqual(self.f1.name, "FIELD1") self.assertEqual(self.f1.description, "Alpha field") self.assertEqual(self.f1.ftype.name, "AN") self.assertEqual(self.f1.length, 10) def test_set_value(self): self.f1.value = " XXX" self.assertEqual(self.f1.value, "XXX") self.assertEqual(self.f1.raw_value, " XXX") def test_init(self): self.f1.initialize() self.assertEqual(self.f1.raw_value, " " * self.f1.length) self.f2.initialize() self.assertEqual(self.f2.raw_value, "0" * self.f2.length) self.f3.initialize() self.assertEqual(self.f3.raw_value, "0" * self.f3.length) def test_reset(self): setattr(self.ft1, "format", "%*.*s") self.f1.reset("AAA") self.assertEqual(self.f1.raw_value, " " * 7 + "AAA") setattr(self.ft1, "format", "%-*.*s") self.f1.reset("AAA") self.assertEqual(self.f1.raw_value, "AAA" + " " * 7) setattr(self.ft2, "format", "%0*d") self.f2.reset(314) self.assertEqual(self.f2.raw_value, "0000000314") setattr(self.ft2, "format", "%*d") self.f2.reset(314) self.assertEqual(self.f2.raw_value, " 314") setattr(self.ft3, "format", "%0*.2f") self.f3.reset(3.14) self.assertEqual(self.f3.raw_value, "0000003.14") setattr(self.ft3, "format", "%*.2f") self.f3.reset(3.14) self.assertEqual(self.f3.raw_value, " 3.14") def test_convert(self): self.f4.value = "20000101"
def test_equality(self): self.assertEqual(self.f1, Field("FIELD1", "Alpha field", self.ft1, 10))