From: funilrys Date: Fri, 2 Mar 2018 21:07:11 +0000 (+0100) Subject: Review of domain_to_idna() X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=780e47ffe5e7a2e978b7a6a42fd63b44a7e05ece;p=stevenblack-hosts.git Review of domain_to_idna() This patch review the way we get the comment at the end of a line. I also did an application of DRY (Do not Repeat Yourself) and/or KISS (Keep It Simple, Stupid) by refactoring the 2 `else` statements into one line. --- diff --git a/updateHostsFile.py b/updateHostsFile.py index 0c78c3e2f..29fa4e5b1 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -1161,27 +1161,25 @@ def domain_to_idna(line): """ if not line.startswith('#'): - for separator in [' ', '\t']: - comment_to_append = '' + for separator in ['\t', ' ']: + comment = '' if separator in line: splited_line = line.split(separator) if '#' in splited_line[1]: - comment_to_append = splited_line[1].split('#')[1] + index_comment = splited_line[1].find('#') + + if index_comment > -1: + comment = splited_line[1][index_comment:] - if comment_to_append: splited_line[1] = splited_line[1] \ - .split(comment_to_append)[0] \ + .split(comment)[0] \ .encode("IDNA").decode("UTF-8") + \ - '#' + comment_to_append[1] - else: - splited_line[1] = splited_line[1] \ - .encode("IDNA") \ - .decode("UTF-8") - else: - splited_line[1] = splited_line[1] \ - .encode("IDNA") \ - .decode("UTF-8") + comment + + splited_line[1] = splited_line[1] \ + .encode("IDNA") \ + .decode("UTF-8") return separator.join(splited_line) return line.encode("IDNA").decode("UTF-8") return line.encode("UTF-8").decode("UTF-8")