Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:55:06+00:00 2026-06-17T10:55:06+00:00

This question is following up on link #!/usr/bin/env python import argparse import sys import

  • 0

This question is following up on link

#!/usr/bin/env python

import argparse
import sys
import itertools as IT
from snemail import *

parser = argparse.ArgumentParser(
        prog='snemail',
        usage='%(prog)s [-h] [--usage] action target [input]',
        epilog="refer to '%(prog)s usage' for examples of how to run snemail"
        )
parser.add_argument('action', metavar='action', nargs='?', type=str, choices=('list', 'add', 'remove'), help="list | remove | add")
parser.add_argument('target', metavar='target', nargs='?', type=str, choices=('domain', 'forwarding', 'transport', 'user', 'all'), help="domain | forwarding | transport | user")
parser.add_argument('input', nargs='?', type=str, default=None, help="required input to go with the 'remove' and 'add' flags. 'list' requires no input field")
parser.add_argument('--usage', action='store_true', help="show usage examples for snemail")
args = parser.parse_args()

if args.usage:
        usage()
        sys.exit(0)
if args.action is None or args.target is None:
        parser.print_help()
        sys.exit(0)
if args.target == 'all':
        all_list()
        sys.exit(0)
if len(sys.argv) >= 3:
        input = args.input
        input = input.split(',')
else:
        input = None

def usage():
        print 'usage info here'

targets = 'domain forwarding transport user'.split()
actions = 'list add remove'.split()

dispatch = {key:globals()['%s_%s' % key] for key in IT.product(targets, actions)}

if input is not None:
        dispatch.get((args.target, args.action), usage)(input)
elif args.action is not None and args.target is not None:
        parser.print_help()
        sys.exit(0)

This code will probably make the skin crawl of a professional python-coder, but it works for me, on Python 2.7 (which I use for my development-machine), but when I tried to execute this code on the machine that is going to run this code in production, it didn’t execute the code because

dispatch = {key:globals()['%s_%s' % key] for key in IT.product(targets, actions)}

doesn’t work in 2.7.

Dear gurus of the internet, how to make this work on Python 2.6? (because upgrading to 2.7 is not an option, but more importantly I’d like to write proper Python)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:55:07+00:00Added an answer on June 17, 2026 at 10:55 am

    Use a dict() with a generator expression:

    dispatch = dict((key, globals()['%s_%s' % key]) for key in IT.product(targets, actions))
    

    The generator expression creates (key, value) 2-value tuples; this works in both 2.6 and 2.7 (or python 3, for that matter).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is also started from following link: shared memory optimization confusion In above
Following this question, it seems that it is possible to open a file from
Following on from this question , I am interested in finding out how you
following on from this question (Developing to an interface with TDD), I'm still having
Looking at the answers to this question (link: How do I convert from int
I am using the following code that I originally found from this question: Prevent
I have place this question before on following link with code & screenshot. TableView
Following this question: Weird MIPS assembler behavior with jump (and link) instruction I have
Following on from this question: How should I go about animating over 200 images
Following on from this question , I am still struggling with the Twitter Bootstrap.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.