#!/usr/bin/env python
# -*- coding: utf8 -*-

# My imports
from __future__ import division, print_function
import argparse
import os


def VALDmail(wavelength=1000, step=1, wavelengths=None):
    """
    Create the mail to be send to VALD3 to extract all. This script make use of
    Thunderbird. Please have Thunderbird set up if you want to use this script
    with your email registered at VALD3.

    :wavelength: The center wavelength in Ångstrøm. (Default=1000Å).
    :step: The step (to each side) from the wavelength. Basically this make the
    window of the lines to extract. (Default=1Å).
    :wavelengths: If a list is give with multiple wavelengths multiple mails
    are begin prepared.
    """

    request_header = 'begin request\nextract all\nvia ftp\n'
    request_header += 'default configuration\nshort format\n'
    request_bottom = 'end request\n'

    dw = step
    if wavelengths:
        assert hasattr(wavelengths,
                       '__iter__'), '%s is not iterable' % wavelengths

        for wavelength in wavelengths:
            line_interval = '%s, %s\n' % (wavelength - step, wavelength + step)
            request = request_header
            request += line_interval
            request += request_bottom

            cmd = 'thunderbird -compose "to=vald3@vald.astro.univie.ac.at,'
            cmd += 'subject=VALD-EMS request: ' + str(line) + ','
            cmd += 'preselectid=id2,'
            cmd += 'body=\'$(cat tmp.mail)\'"'

            with open('tmp.mail', 'wb') as f:
                f.write(request)

            os.system(cmd)
            os.system('clear')
            raw_input('\nPress RETURN to continue: ')
            os.system('rm -f tmp.mail')
    else:
        line_interval = '%s, %s\n' % (wavelength - step, wavelength + step)
        request = request_header
        request += line_interval
        request += request_bottom

        cmd = 'thunderbird -compose "to=vald3@vald.astro.univie.ac.at,'
        cmd += 'subject=VALD-EMS request: ' + str(wavelength) + ','
        cmd += 'preselectid=id2,'
        cmd += 'body=\'$(cat tmp.mail)\'"'

        with open('tmp.mail', 'wb') as f:
            f.write(request)

        os.system(cmd)
        os.system('clear')
        os.system('rm -f tmp.mail')


def _parser():
    parser = argparse.ArgumentParser(description='Prepare emails with'
                                     'Thunderbird for VALD.')
    parser.add_argument('-w', '--wavelength',
                        help='The central wavelength',
                        type=float)
    parser.add_argument('-l', '--list',
                        required=False,
                        nargs='+',
                        type=float,
                        help='A list of wavelengths to be itereated over')
    parser.add_argument('-s', '--step',
                        default=1,
                        type=float,
                        help='The wavelength window, twice the size of the\
                              step.')
    return parser.parse_args()


def main():
    args = _parser()

    if args.wavelength:
        VALDmail(wavelength=args.wavelength, step=args.step)
    elif args.list:
        VALDmail(step=args.step, wavelengths=args.list)


if __name__ == '__main__':
    main()

@@ -128,16 +128,623 @@
 rt os%0A%0A%0A
+def send_email(wavelength, step):%0A    %22%22%22%0A    Prepare and send the email%0A    %22%22%22%0A    line_interval = '%25s, %25s%5Cn' %25 (wavelength - step, wavelength + step)%0A    request = request_header%0A    request += line_interval%0A    request += request_bottom%0A%0A    cmd = 'thunderbird -compose %22to=vald3@vald.astro.univie.ac.at,'%0A    cmd += 'subject=VALD-EMS request: ' + str(wavelength) + ','%0A    cmd += 'preselectid=id2,'%0A    cmd += 'body=%5C'$(cat tmp.mail)%5C'%22'%0A%0A    with open('tmp.mail', 'wb') as f:%0A        f.write(request)%0A%0A    os.system(cmd)%0A    raw_input('%5CnPress RETURN to continue: ')%0A    os.remove('rm -f tmp.mail')%0A%0A%0A
 def VALD
@@ -1475,22 +1475,8 @@
 n'%0A%0A
-    dw = step%0A
     
@@ -1654,728 +1654,30 @@
     
-line_interval = '%25s, %25s%5Cn' %25 (wavelength - step, wavelength + step)%0A            request = request_header%0A            request += line_interval%0A            request += request_bottom%0A%0A            cmd = 'thunderbird -compose %22to=vald3@vald.astro.univie.ac.at,'%0A            cmd += 'subject=VALD-EMS request: ' + str(line) + ','%0A            cmd += 'preselectid=id2,'%0A            cmd += 'body=%5C'$(cat tmp.mail)%5C'%22'%0A%0A            with open('tmp.mail', 'wb') as f:%0A                f.write(request)%0A%0A            os.system(cmd)%0A            os.system('clear')%0A            raw_input('%5CnPress RETURN to continue: ')%0A            os.system('rm -f tmp.mail')%0A    else:%0A        line_interval = '%25s, %25s%5Cn' %25 (wavelength - step, wavelength +
+send_email(wavelength,
  ste
@@ -1687,992 +1687,374 @@
     
-  
-  request = request_header%0A        request += line_interval%0A        request += request_bottom%0A%0A        cmd = 'thunderbird -compose %22to=vald3@vald.astro.univie.ac.at,'%0A        cmd += 'subject=VALD-EMS request: ' + str(wavelength) + ','%0A        cmd += 'preselectid=id2,'%0A        cmd += 'body=%5C'$(cat tmp.mail)%5C'%22'%0A%0A        with open('tmp.mail', 'wb') as f:%0A            f.write(request)%0A%0A        os.system(cmd)%0A        os.system('clear')%0A        os.system('rm -f tmp.mail')%0A%0A%0Adef _parser():%0A    parser = argparse.ArgumentParser(description='Prepare emails with'%0A                                     'Thunderbird for VALD.')%0A    parser.add_argument('-w', '--wavelength',%0A                        help='The central wavelength',%0A                        type=float)%0A    parser.add_argument('-l', '--list',%0A                        required=False,%0A                        nargs='+',%0A                        type=float,%0A                        help='A list of wavelengths to be itereated over'
+else:%0A        send_email(wavelength, step)%0A%0A%0Adef _parser():%0A    parser = argparse.ArgumentParser(description='Prepare emails with'%0A                                     'Thunderbird for VALD.')%0A    parser.add_argument('-w', '--wavelength',%0A                        help='The central wavelength(s)',%0A                        nargs='+',%0A                        type=float
 )%0A  
@@ -2346,37 +2346,8 @@
 r()%0A
-%0A    if args.wavelength:%0A    
     
@@ -2403,84 +2403,8 @@
 ep)%0A
-    elif args.list:%0A        VALDmail(step=args.step, wavelengths=args.list)%0A
 %0A%0Aif

Make simpler code