@@ -21,8 +21,8 @@ class DataHandler(GenericHandler):
2121 re .IGNORECASE | re .DOTALL | re .MULTILINE
2222 )
2323
24- def __init__ (self , data = None ):
25- super (DataHandler , self ).__init__ (data )
24+ def __init__ (self , ** kw ):
25+ super (DataHandler , self ).__init__ (** kw )
2626 self .charlabels = {}
2727 self .attributes = []
2828 self .format = {}
@@ -118,7 +118,7 @@ def characters(self):
118118 return self ._characters
119119
120120 def is_missing_or_gap (self , state ):
121- return True if state in ('-' , '?' ) else False
121+ return state in ('-' , '?' )
122122
123123 def parse_format_line (self , data ):
124124 """
@@ -136,11 +136,10 @@ def parse_format_line(self, data):
136136 except IndexError :
137137 return None
138138
139- line = line .lower ()
140- line = line .replace (" =" , "=" ).replace ("= " , "=" ) # standardise
139+ line = line .lower ().replace (" =" , "=" ).replace ("= " , "=" ) # standardise
141140 for chunk in WHITESPACE_PATTERN .split (line ):
142141 try :
143- key , value = chunk .split ("=" )
142+ key , value = chunk .split ("=" , maxsplit = 1 )
144143 value = QUOTED_PATTERN .sub ('\\ 1' , value )
145144 except ValueError :
146145 key , value = chunk , True
@@ -240,7 +239,7 @@ def _parse_charstate_block(self, data):
240239 char_index += 1
241240 return new_data .split ("\n " )
242241
243- def write (self ):
242+ def iter_lines (self ):
244243 """
245244 Generates a string containing a nexus data block.
246245
@@ -269,34 +268,23 @@ def _make_format_line(self):
269268 fstring .append ("%s=%s" % (key , value ))
270269 return " " .join (fstring ) + ";"
271270
272- out = []
273- out .append ('begin data;' )
274271 for att in self .attributes :
275- out . append ( "\t %s" % att )
276- out . append ( '\t dimensions ntax=%d nchar=%d;' % (self .ntaxa , self .nchar ) )
277- out . append ( _make_format_line (self ) )
272+ yield "\t %s" % att
273+ yield '\t dimensions ntax=%d nchar=%d;' % (self .ntaxa , self .nchar )
274+ yield _make_format_line (self )
278275 # handle char block
279276 if self .charlabels :
280- out . append ( '\t charstatelabels' )
277+ yield '\t charstatelabels'
281278 max_id = max (self .charlabels )
282279 for char_id in sorted (self .charlabels ):
283- out .append ('\t \t %d %s%s' % (
284- char_id + 1 , # zero-indexing
285- self .charlabels [char_id ],
286- ',' if char_id < max_id else ''
287- ))
288- out .append ('\t ;' )
289- out .append ("matrix" )
280+ yield '\t \t %d %s%s' % ( # zero-indexing
281+ char_id + 1 , self .charlabels [char_id ], ',' if char_id < max_id else '' )
282+ yield '\t ;'
283+ yield "matrix"
290284 max_taxon_len = max ([len (_ ) for _ in self .matrix ])
291285 for taxon in sorted (self .matrix ):
292- out .append ("%s %s" % (taxon .ljust (max_taxon_len ), '' .join (self .matrix [taxon ])))
293- out .append (" ;" )
294- out .append ("end;" )
295- return "\n " .join (out )
296-
297- def __repr__ (self ):
298- return "<NexusDataBlock: %d characters from %d taxa>" % \
299- (self .nchar , self .ntaxa )
286+ yield "%s %s" % (taxon .ljust (max_taxon_len ), '' .join (self .matrix [taxon ]))
287+ yield " ;"
300288
301289
302290class CharacterHandler (DataHandler ):
0 commit comments