File: autoregistry.rst

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (101 lines) | stat: -rw-r--r-- 4,056 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
============
AutoRegistry
============

AutoRegistry_ is a python library that automatically creates string-to-functionality mappings, making it trivial to instantiate classes or invoke functions from CLI parameters.

Lets consider the following program that can download a file from either a GCP, AWS, or Azure bucket (without worrying about the implementation):

.. code-block:: python

   import cyclopts
   from pathlib import Path
   from typing import Literal

   def _download_gcp(bucket: str, key: str, dst: Path):
       print("Downloading data from Google.")

   def _download_s3(bucket: str, key: str, dst: Path):
       print("Downloading data from Amazon.")

   def _download_azure(bucket: str, key: str, dst: Path):
       print("Downloading data from Azure.")

   _downloaders = {
       "gcp": _download_gcp,
       "s3": _download_s3,
       "azure": _download_azure,
   }

   app = cyclopts.App()

   @app.command
   def download(bucket: str, key: str, dst: Path, provider: Literal[tuple(_downloaders)] = "gcp"):
       downloader = _downloaders[provider]
       downloader(bucket, key, dst)

   app()

.. code-block:: console

   $ my-script download --help
   ╭─ Parameters ────────────────────────────────────────────────────────────╮
   │ *  BUCKET,--bucket      [required]                                      │
   │ *  KEY,--key            [required]                                      │
   │ *  DST,--dst            [required]                                      │
   │    PROVIDER,--provider  [choices: gcp,s3,azure] [default: gcp]          │
   ╰─────────────────────────────────────────────────────────────────────────╯

   $ my-script my-bucket my-key local.bin --provider=s3
   Downloading data from Amazon.


Not bad, but let's see how this would look with AutoRegistry.

.. code-block:: python

   import cyclopts
   from autoregistry import Registry
   from pathlib import Path
   from typing import Literal

   _downloaders = Registry(prefix="_download_")

   @_downloaders
   def _download_gcp(bucket: str, key: str, dst: Path):
       print("Downloading data from Google.")

   @_downloaders
   def _download_s3(bucket: str, key: str, dst: Path):
       print("Downloading data from Amazon.")

   @_downloaders
   def _download_azure(bucket: str, key: str, dst: Path):
       print("Downloading data from Azure.")

   app = cyclopts.App()

   @app.command
   def download(bucket: str, key: str, dst: Path, provider: Literal[tuple(_downloaders)] = "gcp"):
       downloader = _downloaders[provider]
       downloader(bucket, key, dst)

   app()

.. code-block:: console

   $ my-script download --help
   ╭─ Parameters ────────────────────────────────────────────────────────────╮
   │ *  BUCKET,--bucket      [required]                                      │
   │ *  KEY,--key            [required]                                      │
   │ *  DST,--dst            [required]                                      │
   │    PROVIDER,--provider  [choices: gcp,s3,azure] [default: gcp]          │
   ╰─────────────────────────────────────────────────────────────────────────╯

   $ my-script my-bucket my-key local.bin --provider=s3
   Downloading data from Amazon.

Exactly the same functionality, but more terse and organized.
With Autoregistry, the download providers are much more self-contained, do not require changes in other code locations, and reduce duplication.

.. _AutoRegistry: https://github.com/BrianPugh/autoregistry