def test_splitter(self): """FieldWrapper with splitter should use that splitter""" fields = ["label", "count"] splitter = DelimitedSplitter(":", -1) f = FieldWrapper(fields, splitter) self.assertEqual(f(""), {}) self.assertEqual(f("nknasd:"), {"label": "nknasd", "count": ""}) self.assertEqual(f("n:k:n:a:sd "), {"label": "n:k:n:a", "count": "sd"})
def test_default(self): """Default FieldWrapper should wrap fields and labels""" fields = list("abcde") f = FieldWrapper(fields) self.assertEqual(f(""), {}) self.assertEqual(f("xy za "), {"a": "xy", "b": "za"}) self.assertEqual( f("1 2\t\t 3 \n4 5 6"), {"a": "1", "b": "2", "c": "3", "d": "4", "e": "5"}, )
maketrans = str.maketrans strip = str.strip rstrip = str.rstrip all_chars = maketrans("", "") dna_lc = "utacgrywsmkbdhvn" dna_lc_cmp = "aatgcyrwskmvhdbn" dna_trans = maketrans(dna_lc + dna_lc.upper(), dna_lc_cmp + dna_lc_cmp.upper()) rna_lc = "utacgrywsmkbdhvn" rna_lc_cmp = "aaugcyrwskmvhdbn" rna_trans = maketrans(rna_lc + rna_lc.upper(), rna_lc_cmp + rna_lc_cmp.upper()) locus_fields = [ None, "locus", "length", None, "mol_type", "topology", "db", "date" ] _locus_parser = FieldWrapper(locus_fields) # need to turn off line stripping, because whitespace is significant GbFinder = DelimitedRecordFinder("//", constructor=rstrip) class PartialRecordError(Exception): pass def parse_locus(line): """Parses a locus line, including conversion of Length to an int. WARNING: Gives incorrect results on legacy records that omit the topology. All records spot-checked on 8/30/05 had been updated to include the topology even when prior versions omitted it.
rstrip = str.rstrip def ll_start(line): """Returns True if line looks like the start of a LocusLink record.""" return line.startswith(">>") LLFinder = LabeledRecordFinder(ll_start) pipes = DelimitedSplitter("|", None) first_pipe = DelimitedSplitter("|") commas = DelimitedSplitter(",", None) first_colon = DelimitedSplitter(":", 1) accession_wrapper = FieldWrapper(["Accession", "Gi", "Strain"], pipes) def _read_accession(line): """Reads accession lines: format is Accession | Gi | Strain.""" return MappedRecord(accession_wrapper(line)) rell_wrapper = FieldWrapper(["Description", "Id", "IdType", "Printable"], pipes) def _read_rell(line): """Reads RELL lines: format is Description|Id|IdType|Printable""" return MappedRecord(rell_wrapper(line))
def test_constructor(self): """FieldWrapper with constructor should use that constructor""" fields = list("abc") f = FieldWrapper(fields, constructor=fake_dict) self.assertEqual(f("x y"), {"a": "x", "b": "y"}) assert isinstance(f("x y"), fake_dict)