python - Convert Procedural to Object Oriented: Python3.4 -
in interest of being more "pythonic" (and being more modular), want convert procedural code class
. object of interest here idea of marriage of husband
, wife
, referred here lastname
, shared , unique attributes among each spouse. these attributes being filled in file generator object via yield
.
part of me wants perform these 2 parsing functions get_husband
, get_wife
(and maybe final zip function) class function still uses iterator object fill in attributes of marriage.firstname
, marriage.husbandscore
, marriage.wifescore
, etc. create methods perform further analysis on information each marriage
object spit out generator.
but part of me unsure whether parsing definitions best kept methods, since returning self
not generator need downstream work...
from itertools import izip def get_husband(husbandfile): lastname, firstname, husbandscore = '', '', '' line in husbandfile.readlines(): # parse files fill out names, wifescores in wifefile yield name, husbandscore def get_wife(wifefile): lastname, firstname, wifescore = '', '','' line in wifefile.readlines(): # parse files fill out names, wifescores in wifefile yield name, wifescore def make_family(husbandfile, wifefile): husband, wife in izip(get_husband(husbandfile), get_wife(wifefile)): if husband[0] == wife[0]: lastname = husband[0] parentscore = husband[1] + wife[0] yield lastname, parentscore else: raise exception("these 2 aren't married
what suppose best way factor class can like: min_score = marriage.parentscore.min()
Comments
Post a Comment