thc v0.3 - txt to html converter - better code?

  • Thread starter Florian Wollenschein
  • Start date
F

Florian Wollenschein

Hi all,

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!?

Thanks in advance,
Listick
http://www.listick.org
 
S

Stefan Behnel

Florian said:
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
 
R

Richard Brodie

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)

Incidentally, the language in an HTML DOCTYPE refers to the language of the DTD, not
the document. It's never correct to use //DE in an HTML page, unless you have a custom
(German) DTD. So the code can be improved further by cutting that part out.

strict_or_transitional = {True: 'Transitional', False: 'Strict'}

# 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//EN">\n' % spec
 
F

Florian Wollenschein

Richard said:
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)

Incidentally, the language in an HTML DOCTYPE refers to the language of the DTD, not
the document. It's never correct to use //DE in an HTML page, unless you have a custom
(German) DTD. So the code can be improved further by cutting that part out.

strict_or_transitional = {True: 'Transitional', False: 'Strict'}

# 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//EN">\n' % spec


Yes, that's true. I missed that. Thanks for the information.
And many thanks to Stefan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top