この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
どうも!大阪オフィスの西村祐二です。
Lambda関数を作成するとき基本的にPythonで書いているためBoto3をよく利用しています。
そんなとき、どんなメソッドがあったかなと、メソッドを探すときはドキュメントをみにいくことが多いのですが、
手元で簡単に確認できないかなと思うことがよくあります。
いろいろ調べているとpythonのdir
を使えば確認できそうだったので、試してみました。
メソッド一覧を表示してみる!
テストとして、メソッド数が少なそうなcloud9
のclientのメソッドを取得してみたいと思います。
ドキュメントでは下記のようなメソッドが利用できると記載があります。
http://boto3.readthedocs.io/en/latest/reference/services/cloud9.html
環境
- Mac
- macOS High Sierra 10.13.3
- Python 3.6.0
- boto3-1.6.16
- botocore-1.9.16
最新版にアップデートしておきます。
$ sudo pip install -U boto3
確認コード
どうやらメソッド以外も一緒に出力されてしまうようなので、type
もいっしょに出力するようにしてみました。
import boto3
client = boto3.client('cloud9')
for x in dir(client):
type_data = type(eval("client." + x))
print(f'{x:<40} : {type_data}')
出力結果
メソッド以外も表示されていますが、想定どおりタイプを含めた一覧が出力されました。
メソッドの使い方はpythonのhelpメソッドを使えば、ある程度わかるので、
ドキュメントを見に行く回数が減りそうです。
_PY_TO_OP_NAME : <class 'dict'>
__class__ : <class 'type'>
__delattr__ : <class 'method-wrapper'>
__dict__ : <class 'dict'>
__dir__ : <class 'builtin_function_or_method'>
__doc__ : <class 'NoneType'>
__eq__ : <class 'method-wrapper'>
__format__ : <class 'builtin_function_or_method'>
__ge__ : <class 'method-wrapper'>
__getattr__ : <class 'method'>
__getattribute__ : <class 'method-wrapper'>
__gt__ : <class 'method-wrapper'>
__hash__ : <class 'method-wrapper'>
__init__ : <class 'method'>
__init_subclass__ : <class 'builtin_function_or_method'>
__le__ : <class 'method-wrapper'>
__lt__ : <class 'method-wrapper'>
__module__ : <class 'str'>
__ne__ : <class 'method-wrapper'>
__new__ : <class 'builtin_function_or_method'>
__reduce__ : <class 'builtin_function_or_method'>
__reduce_ex__ : <class 'builtin_function_or_method'>
__repr__ : <class 'method-wrapper'>
__setattr__ : <class 'method-wrapper'>
__sizeof__ : <class 'builtin_function_or_method'>
__str__ : <class 'method-wrapper'>
__subclasshook__ : <class 'builtin_function_or_method'>
__weakref__ : <class 'NoneType'>
_cache : <class 'dict'>
_client_config : <class 'botocore.config.Config'>
_convert_to_request_dict : <class 'method'>
_emit_api_params : <class 'method'>
_endpoint : <class 'botocore.endpoint.Endpoint'>
_exceptions : <class 'NoneType'>
_exceptions_factory : <class 'botocore.errorfactory.ClientExceptionsFactory'>
_get_waiter_config : <class 'method'>
_load_exceptions : <class 'method'>
_loader : <class 'botocore.loaders.Loader'>
_make_api_call : <class 'method'>
_register_handlers : <class 'method'>
_request_signer : <class 'botocore.signers.RequestSigner'>
_response_parser : <class 'botocore.parsers.JSONParser'>
_serializer : <class 'botocore.validate.ParamValidationDecorator'>
_service_model : <class 'botocore.model.ServiceModel'>
can_paginate : <class 'method'>
create_environment_ec2 : <class 'method'>
create_environment_membership : <class 'method'>
delete_environment : <class 'method'>
delete_environment_membership : <class 'method'>
describe_environment_memberships : <class 'method'>
describe_environment_status : <class 'method'>
describe_environments : <class 'method'>
exceptions : <class 'botocore.errorfactory.Cloud9Exceptions'>
generate_presigned_url : <class 'method'>
get_paginator : <class 'method'>
get_waiter : <class 'method'>
list_environments : <class 'method'>
meta : <class 'botocore.client.ClientMeta'>
update_environment : <class 'method'>
update_environment_membership : <class 'method'>
waiter_names : <class 'list'>
さいごに
いかがだったでしょうか。
Pythonのdir
を使ってAWS SDK for Python (Boto3) でのオブジェクトのメソッド一覧を取得してみました。
今回の方法だとメソッド以外も出力されてしまいますが、ドキュメントに載っていない新たな情報が発見できるかもしれませんね。
メソッドだけ表示したい場合はif文で不要なものを除くでOKです。
誰かの参考になれば幸いです。