Created
December 18, 2017 04:35
-
-
Save tomplex/8eeccb2214c9c92c8cfb8fbce16adb8d to your computer and use it in GitHub Desktop.
Argparse & Pika example for /r/learnpython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Help for /r/learnpython: https://www.reddit.com/r/learnpython/comments/7kilzt/cant_get_my_head_around_using_argparse_in_my/ | |
""" | |
import argparse | |
import json | |
import pika | |
import sys | |
CONFIG_FILE = 'some_filename.json' | |
def get_argument_parser(): | |
""" | |
Get an argument parser with the arguments we care about. | |
Params: | |
None | |
Returns: | |
argparse.ArgumentParser | |
""" | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--date', '-d', required=True) | |
return parser | |
def parse_arguments(args): | |
""" | |
Parse the arguments to the script into a python Namespace object | |
Params: | |
args: list of arguments to the Python script. | |
Returns: | |
Parsed arguments. | |
""" | |
parser = get_argument_parser() | |
return parser.parse_args(args) | |
def get_channel(config): | |
""" | |
Get a publish-ready pika channel from a dict of configuration options. | |
Params: | |
config: dict of configuration | |
Returns: | |
pika.BlockingConnection: connection to specified RabbitMQ host | |
""" | |
credentials = pika.PlainCredentials(config['user'], config['password']) | |
params = pika.ConnectionParams(config['host'], config['port'], | |
config['vhost'], credentials) | |
connection = pika.BlockingConnection(params) | |
channel = connection.channel() | |
channel.queue_declare(queue=config['queue']) | |
return channel | |
def get_config(config_file): | |
""" | |
Load configuration info from a JSON config file. | |
Params: | |
config_file: a str file path | |
Returns: | |
dict: JSON loaded from file | |
""" | |
with open(config_file) as fobj: | |
return json.load(fobj) | |
def main(): | |
# First, we'll get our parsed arguments. | |
# Args will be a Python class with attributes for each argument we added in | |
# the get_argument_parser function above, such that args.date will be the | |
# date passed in using the --date argument. | |
args = parse_arguments(sys.argv[1:]) | |
# Get our config information | |
config = get_config(CONFIG_FILE) | |
# Get a channel | |
channel = get_channel(config) | |
# Create the message body. I'm doing JSON because it's easier. | |
body = json.dumps({'date': args.date}) | |
try: | |
# Try to publish the message. If succeeds, exit with a success code. | |
channel.basic_publish(exchange='', routing_key='message.test', | |
body=body) | |
exit(0) | |
except: | |
# please don't use a catch-all except in production code. | |
exit(1) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment