Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is a follow up to Log probe requests of WiFi devices focussing on a specific element of the code.

Can I simplify this code and make it more readable?

if not os.path.isfile(args.tshark_path):
    print "tshark not found at path {0}".format(args.tshark_path)
    sys.exit(1)
if not os.path.isfile(args.ifconfig_path):
    print "ifconfig not found at path {0}".format(args.ifconfig_path)
    sys.exit(1)
if not osx:
    if not os.path.isfile(args.iwconfig_path):
        print "iwconfig not found at path {0}".format(args.iwconfig_path)
        sys.exit(1)
share|improve this question
6  
Please put only the code's purpose in the title. –  Jamal 2 days ago

1 Answer 1

up vote 1 down vote accepted

Yes, just factor out the repetition:

for item, os_check in (('tshark', False), ('ifconfig', False), ('iwconfig', True)):
    if not os_check or osx:
        path = getattr(args, '{}_path'.format(item))
        if not os.path.isfile(path):
            print '{} not found at path {}'.format(item, path)
            sys.exit(1)
share|improve this answer
    
Sorry, you don't understood the code. In the last condition the program only exits if the OS is not OSX. –  Helio 2 days ago
    
@Helio updated, the principle is still the same. –  jonrsharpe 2 days ago
    
Thank you very much! Can I avoid surpassing the 79 characters on the line? –  Helio 2 days ago
    
@Helio yes, you can add line breaks in the tuple of tuples just as you can in a function call. –  jonrsharpe 2 days ago
    
Ok, thank you. I'll do with line breaks. –  Helio 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.