Florian Wollenschein wrote: > here's some code of thc, my txt to html converter programmed with Python > and pyQT4: > ------------------------------------------------------------------------------- > > if self.rdioBtnTransitional.isChecked(): > if self.cmboBoxLang.currentText() == "English": > file_open.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML > 4.0 Transitional//EN">' + '\n') > elif self.cmboBoxLang.currentText() == "German": > file_open.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML > 4.0 Transitional//DE">' + '\n') > else: > file_open.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 > Strict//EN">' + '\n') > if self.cmboBoxLang.currentText() == "English": > file_open.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML > 4.0 Strict//EN">' + '\n') > elif self.cmboBoxLang.currentText() == "German": > file_open.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML > 4.0 Strict/DE">' + '\n') > -------------------------------------------------------------------------------- > > > Do you have any ideas for a better code than that? Could this be done > smarter, shorter, whatever!?
I assume you are referring to the code duplication above. You can build the string incrementally instead, e.g. language_map = {'English': 'EN', 'Deutsch': 'DE'} strict_or_transitional = {True: 'Transitional', False: 'Strict'} # this will raise a KeyError for unknown languages language = language_map[ self.cmboBoxLang.currentText() ] # this assumes that isChecked() returns True or False spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()] doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 %s/%s">\n' % ( spec, language) Stefan -- http://mail.python.org/mailman/listinfo/python-list