跳转到主要内容

boto3与自动补全和数据类不是字典

项目描述

安装

pip install autoboto

简介

  • 项目状态:Alpha。您应该只在探索AWS时使用它进行ad-hoc查询。这是为那些大致知道他们想要从AWS获得什么,但不想在漫长的(且良好的)boto3文档页面上上下滚动以找到正确的方法和参数名称的人准备的。我们处于21世纪,对于面向用户的代码,自动补全应该工作。

  • 所有响应对象都是数据类。

  • 所有响应对象都有一个response_metadata,这是一个未修改的字典,通常在响应字典的ResponseMetadata键下返回。

  • 传递嵌套对象不像传递嵌套字典那样容易,但它有好处。

  • 方法名称与boto3中的一致,但参数名称已从CamelCase更改为snake_case

  • boto3未从botocore生成的自定义方法(例如,s3.upload_file)简单地委托给boto3客户端,并且目前没有可用的文档。参数名称与原始方法相同。

from autoboto.services import s3

s3_client = s3.Client()

for bucket in s3_client.list_buckets().buckets:
    print(bucket.name)
    for obj in s3_client.list_objects_v2(bucket_name=bucket.name).contents:
        print(f" - {obj.key}")

您还可以分页

for page in s3_client.list_objects_v2(bucket_name=bucket.name).paginate():
    for obj in page.contents:
        print(f" - {obj.key}")

代码生成

您不需要阅读本节。它关于如何生成autoboto代码。

当您从pypi.org安装autoboto时,该包已包含boto3支持的所有服务的生成代码。

如果您想重新生成代码,可以使用包含的botogen

代码生成包括导入生成的模块和实例化生成的客户端类。为了使此功能正常工作,您需要有一个指向已设置region的AWS配置文件的AWS_PROFILE环境变量。

python -m botogen --services s3,cloudformation,lambda

组件

  • autoboto - 在发布前放置生成代码的包。请勿手动添加任何内容。此目录中的所有文件都可能被覆盖。

  • botogen - 负责生成autoboto的代码

  • botogen.autoboto_template - 此包的内容最终会放在生成的autoboto包中。

    • 位于botogen/autoboto_template下的.gitignore文件指示git忽略目录中的所有文件。这是为了确保autoboto包中的生成代码永远不会添加到版本控制中。因此,在autoboto开发期间,当您向botogen/autoboto_template添加新文件时,需要添加-f标志以强制将它们添加到git。

  • botogen.indentist - 通用Python代码生成器

目录结构

build/                                  All build artifacts are put here

    release/                            Release builds happen here

        20180909_135602/                individual release build directory; Added to sys.path
            autoboto/                   generated autoboto package; an augmented copy of botogen/autoboto_complete
                core/
                examples/
                services/
                __init__.py

    test/                               Test builds happen here

        20180909_135330/                individual test build directory; Added to sys.path
            autoboto_20180909_135330/   generated autoboto package; an augmented copy of botogen/autoboto_complete
                core/
                examples/
                services/
                __init__.py

    test-packages/                      Target directory for test builds -- where the generated
                                        packages are put after successful completion of a build
                                        and tests passing on the generated code.
  • build_dir – 生成所有构建工件的一个临时目录。在上面的示例中,build/test/20180909_135330build/release/20180909_135602是构建目录。

  • target_package – 生成的目标包的名称;用于生成的代码中的导入语句。发布时为autoboto;测试时为autoboto_{timestamp}

  • target_dir – 放置目标包的目录。默认情况下是当前目录,但在测试中是build/test-packages

注意

不要在生成的代码的测试中使用来自botogen.autoboto_template的任何导入,因为生成的代码不从此处导入。相反,请使用专门的固定装置(对于这些装置,自动完成功能不会工作)。

测试

tests目录包含单元测试和集成测试。对于单元测试,您不需要AWS账户。

pytest tests/unit

要跨多个Python版本运行测试,请使用tox。要运行tox,您首先需要生成autoboto包,否则它将失败。

如果您使用pyenv和虚拟环境,请不要在虚拟环境中运行tox

项目详情


下载文件

下载您平台上的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源代码分发

autoboto-0.4.3.tar.gz (3.1 MB 查看散列)

源代码

构建分发

autoboto-0.4.3-py3-none-any.whl (3.3 MB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面