Created
December 16, 2014 07:26
-
-
Save a-hisame/8ed080f39f5c103ec512 to your computer and use it in GitHub Desktop.
DynamoDBのテーブルに処理結果が格納されたかをチェックするプログラム
This file contains hidden or 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
#!/usr/bin/env python | |
# coding: utf-8 | |
# ----------------------- | |
# 設定によって変更するところ | |
# ----------------------- | |
REGION = 'ap-northeast-1' | |
AWS_ACCESS_KEY = '' | |
AWS_SECRET_ACCESS_KEY = '' | |
TABLE_NAME = '' | |
# ----------------------- | |
def get_result(id, else_value=None): | |
''' | |
idに対応する処理結果をDynamoDBから取得する。 | |
dataがない場合はelse_valueを返す。 | |
''' | |
import boto.dynamodb2 | |
from boto.dynamodb2.table import Table | |
conn = boto.dynamodb2.connect_to_region(REGION, | |
aws_access_key_id=AWS_ACCESS_KEY, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY) | |
table = Table(TABLE_NAME, connection=conn) | |
if not table.has_item(id=id): | |
return else_value | |
# Itemにはgetメソッド、あるいは'key名'の形でアクセスする | |
item = table.get_item(id=id) | |
return item.get('value', default=else_value) | |
def main(id): | |
result = get_result(id) | |
if result is not None: | |
print 'Id: {} is Executed, Result={}'.format(id, result) | |
else: | |
print 'Id: {} is Not Executed'.format(id) | |
def _args_parser(): | |
''' コマンドラインからの入力解析を行うパーサを作成して返します。 ''' | |
import argparse | |
parser = argparse.ArgumentParser(description=u'Example Sender') | |
parser.add_argument('--id', type=str, help=u'Message ID', required=True) | |
return parser | |
if __name__ == '__main__': | |
import sys | |
args = _args_parser().parse_args(sys.argv[1:]) | |
main(args.id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment