I used CodeClimate to evaluate my code and marked that the following piece of code appeared twice in my source:
except IOError as e:
log.error("I/O error({0}): {1}".format(e.errno, e.strerror))
app_exit(1)
The justification is that the amount of duplication mass is 33, which is above default limit of 32.
How would you solve this problem? Would you disable the check? Would you wrap the 2 lines in a new method? Something else?
The duplicate part in question is in main
:
#!/usr/bin/env python
import logging as log
def get_cfg(xmlfile):
# gets configuration from file which
# has the same name as XML
# but with different extension.
pass
def get_root(xmlfile):
pass
def app_exit(status):
log.info('Exiting...')
exit(status)
def setup_logging(args):
if args.verbose == 2:
log.basicConfig(level=log.DEBUG)
elif args.verbose == 1:
log.basicConfig(level=log.INFO)
else:
log.basicConfig(level=log.WARN)
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', help='XML file to be parsed')
parser.add_argument("-v", "--verbose", action="count",
help="increase output verbosity")
args = parser.parse_args()
setup_logging(args)
xmlfile = args.file
log.info('Getting root element from XML {}.'.format(xmlfile))
try:
root = get_root(xmlfile)
except ET.ParseError as e:
log.error("Error while parsing {0}: {1}".format(xmlfile, e))
app_exit(1)
except IOError as e:
log.error("I/O error({0}): {1}".format(e.errno, e.strerror))
app_exit(1)
log.info('Getting configuration of XML {}.'.format(xmlfile))
try:
cfg = get_cfg(xmlfile)
except IOError as e:
log.error("I/O error({0}): {1}".format(e.errno, e.strerror))
app_exit(1)
# processing of XML using configuration
if __name__ == '__main__':
main()