-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_methods.py
74 lines (54 loc) · 2.23 KB
/
c_methods.py
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
"""
Concepts: you can decorate methods. And classes!
Same thing but for classes. This introduces a new wrinkle when decorating methods.
The problem is that our decorator is invoked as each method is defined - we get
the function that results from the definition.
But to store that function function we're going to need to have access to the
containing class to get to the `ENDPOINTS` class variable... and the class
hasn't been defined when our decorator runs!
We need to defer actually collecting the methods until after the class has been
defined - when it can be done by a class decorator!
Update the `endpoint` decorator to mark decorated methods and then update the
`collect` decorator to loop through the attributes of the class finding the
methods we marked and storing them in the `RemoteAPI.ENDPOINTS` dict.
Hint: help(vars). Also - functions are objects like anything else, open by
default so its totally fine to say `function._my_mark = 1`.
"""
import argparse
def endpoint(f):
return f
def collect(klass):
return klass
@collect
class RemoteAPI:
ENDPOINTS = {}
def __init__(self, secrets=None):
# Presumably load some secrets from a file so we can authenticate our client
pass
@endpoint
def sessions(self, event_id=None):
"""Returns the session ids for the event."""
return [1, 2, 3]
@endpoint
def events(self):
"""Returns the events to which you have access"""
return [2717]
if __name__ == "__main__":
# Setup CLI options
parser = argparse.ArgumentParser()
# Add a subcommand for every endpoint we've implemented
subparsers = parser.add_subparsers(
title="Endpoints",
help="The following endpoints are supported:",
dest="subcommand",
)
client = RemoteAPI()
# Using inspect to add flags for endpoint arguments ommitted for brevity
for (name, func) in client.ENDPOINTS.items():
sub = subparsers.add_parser(name, help=func.__doc__)
# Pick a subcommand to run
args = parser.parse_args()
if not args.subcommand:
parser.exit("Please specify a subcommand")
function = client.ENDPOINTS[args.subcommand]
print(function(client)) # Passing instance of RemoteAPI to self. Do you know why?