-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.py
150 lines (127 loc) · 4.28 KB
/
registry.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from typing import Callable
import inspect
# Initialize a registry dictionary
method_registry = {}
parser_registry = {}
# Define a decorator for registering functions
def register_method(name: str) -> Callable:
"""
Decorator for registering evaluation methods.
Args:
name (str): The name of the method to register.
Returns:
Callable: The decorator function.
"""
def decorator(func: Callable):
if name in method_registry:
raise ValueError(f"A method with the name '{name}' is already registered.")
# Capture function metadata
func_metadata = {
"name": func.__name__,
"doc": func.__doc__,
"signature": inspect.signature(func),
"module": func.__module__,
}
method_registry[name] = {"func": func, "metadata": func_metadata}
return func
return decorator
# Define a decorator for registering parsers
def register_parser(name: str) -> Callable:
"""
Decorator for registering parsers.
Args:
name (str): The name of the parser to register.
Returns:
Callable: The decorator function.
"""
def decorator(func: Callable):
if name in parser_registry:
raise ValueError(f"A parser with the name '{name}' is already registered.")
func_metadata = {
"name": func.__name__,
"doc": func.__doc__,
"signature": inspect.signature(func),
"module": func.__module__,
}
parser_registry[name] = {"func": func, "metadata": func_metadata}
return func
return decorator
# Define a function to get a method from the registry
def get_method(name: str) -> Callable:
"""
Get an evaluation method from the registry by name.
Args:
name (str): The name of the method to get.
Returns:
Callable: The evaluation method.
"""
if name not in method_registry:
raise ValueError(f"No method with the name '{name}' is registered.")
return method_registry[name]["func"]
# Define a function to get a parser from the registry
def get_parser(name: str) -> Callable:
"""
Get a parser from the registry by name.
Args:
name (str): The name of the parser to get.
Returns:
Callable: The parser function.
"""
if name not in parser_registry:
raise ValueError(f"No parser with the name '{name}' is registered.")
return parser_registry[name]["func"]
# Get method info
def method_info(name: str) -> None:
"""
Print information about an evaluation method.
Args:
name (str): The name of the method to get information about.
Returns:
None
"""
if name not in method_registry:
raise ValueError(f"No method with the name '{name}' is registered.")
print(f"Method: {name}")
print(f"Module: {method_registry[name]['metadata']['module']}")
print(f"Signature: {method_registry[name]['metadata']['signature']}")
print(f"Docstring: {method_registry[name]['metadata']['doc']}")
print(f"Function: {method_registry[name]['metadata']['name']}")
# Get parser info
def parser_info(name: str) -> None:
"""
Print information about a parser.
Args:
name (str): The name of the parser to get information about.
Returns:
None
"""
if name not in parser_registry:
raise ValueError(f"No parser with the name '{name}' is registered.")
print(f"Parser: {name}")
print(f"Module: {parser_registry[name]['metadata']['module']}")
print(f"Signature: {parser_registry[name]['metadata']['signature']}")
print(f"Docstring: {parser_registry[name]['metadata']['doc']}")
print(f"Function: {parser_registry[name]['metadata']['name']}")
# List methods
def list_methods(verbose: bool = True) -> None:
"""
List all registered evaluation methods.
"""
for method in method_registry:
if verbose:
if verbose:
method_info(method)
else:
print(f"Method: {method}")
print("-" * 50)
# List parsers
def list_parsers(verbose: bool = True) -> None:
"""
List all registered parsers.
"""
for parser in parser_registry:
if verbose:
parser_info(parser)
else:
print(f"Parser: {parser}")
print("-" * 50)