Coverage for src/chrisjen/workshop.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-14 09:29 +0000

1"""Functions for building a project's workflow from its settings. 

2 

3The workshop decides what each name in the settings is (a worker, a step, or a 

4technique) and which class to build it with. Each class builds itself from the 

5settings with its `build` method. 

6 

7Contents: 

8 build_criteria: builds the criteria named in settings. 

9 build_node: builds a worker or a technique from its name. 

10 build_step: builds the alternatives for a step. 

11 build_worker: builds a worker and all of the nodes in it. 

12 build_workflow: builds the workflow of a project. 

13 class_names: returns the names of the classes that a node can be 

14 built with. 

15 

16""" 

17 

18from __future__ import annotations 

19 

20import re 

21from typing import TYPE_CHECKING, cast 

22 

23from . import base, nodes, options 

24 

25if TYPE_CHECKING: 

26 from . import interface 

27 

28 

29def build_criteria(name: str, project: interface.Project) -> base.Criteria: 

30 """Builds the criteria named `name`. 

31 

32 Args: 

33 name: name of a `Criteria` subclass in the library, or the import path 

34 of a scoring function. 

35 project: the project the criteria belongs to. 

36 

37 Returns: 

38 A `Criteria` instance. 

39 

40 """ 

41 parameters = project.idea.parameters.get(name, {}) 

42 criteria = cast( 

43 'type[base.Criteria] | None', project.library.all.get(name)) 

44 if criteria is None: 

45 return base.Criteria(contents = name, parameters = parameters) 

46 return criteria(parameters = parameters) 

47 

48 

49def build_node(name: str, project: interface.Project) -> base.Vertex: 

50 """Builds the node `name`: a worker or a technique. 

51 

52 A name is a worker if it has a section of its own or a list of steps (see 

53 `build_worker`). Otherwise, it is built with the class in the library that 

54 has its name (usually a `Technique` subclass). If there is none and the 

55 name ends in a number (such as "logit2"), the class named without the 

56 number ("logit") is used, so that one technique can be used more than once 

57 with different settings (such as "logit2_parameters"). 

58 

59 Args: 

60 name: name of the node. 

61 project: the project the node belongs to. 

62 

63 Raises: 

64 KeyError: if `name` is not a worker and no class in the library has 

65 its name (or, if it ends in a number, its name without it). 

66 

67 Returns: 

68 The built node. 

69 

70 """ 

71 idea = project.idea 

72 if name in idea.workers or name in idea.steps: 

73 return build_worker(name, project) 

74 kind = cast('type[base.Vertex]', project.library.borrow( 

75 class_names(name))) 

76 return kind.build(name, project) 

77 

78 

79def build_step(name: str, project: interface.Project) -> list[base.Vertex]: 

80 """Builds the alternatives for the step `name`. 

81 

82 A step with techniques has one node for each of them: a `Step` that wraps 

83 the technique and is named "{technique}_{step}". The class of the step is 

84 the `Step` subclass named `name`, if there is one. As with techniques (see 

85 `build_node`), a step such as "scale2" can use the `Step` subclass named 

86 "scale". Its parameters come from the "{step}_parameters" section of the 

87 settings and, for one technique in that step, the 

88 "{technique}_{step}_parameters" section. A step without 

89 techniques (or that is a worker) is a node itself (see `build_node`). 

90 

91 Args: 

92 name: name of the step. 

93 project: the project the step belongs to. 

94 

95 Returns: 

96 The alternatives for the step. 

97 

98 """ 

99 idea = project.idea 

100 if name in idea.workers or name not in idea.techniques: 

101 return [build_node(name, project)] 

102 try: 

103 kind = cast('type[nodes.Step]', project.library.borrow( 

104 class_names(name), genre = 'step')) 

105 except KeyError: 

106 kind = nodes.Step 

107 alternatives: list[base.Vertex] = [] 

108 for technique in idea.techniques[name]: 

109 label = f'{technique}_{name}' 

110 step = kind.build( 

111 name, 

112 project, 

113 parameters = idea.parameters.get(label, {}), 

114 contents = build_node(technique, project)) 

115 step.name = label 

116 alternatives.append(step) 

117 return alternatives 

118 

119 

120def build_worker(name: str, project: interface.Project) -> nodes.Worker: 

121 """Builds the worker `name` and all of the nodes in it. 

122 

123 The class of the worker is the worker class named `name`, if there is one, 

124 and otherwise the class of its design (from the settings or 

125 `options._DEFAULT_DESIGN`). Its nodes are its steps, each a list of 

126 alternatives (see `build_step`), or, if it has no steps, its techniques, 

127 which are alternatives to each other. The `populate` method of the worker 

128 connects them: one after another for a `Flow`, or as every combination of 

129 alternatives for a `Contest` or `Survey`. 

130 

131 Args: 

132 name: name of the worker. 

133 project: the project the worker belongs to. 

134 

135 Returns: 

136 The built worker. 

137 

138 """ 

139 idea = project.idea 

140 design = idea.designs.get(name, options._DEFAULT_DESIGN) 

141 kind = cast('type[nodes.Worker]', project.library.borrow( 

142 [name, design], genre = 'worker')) 

143 worker = kind.build(name, project) 

144 if name in idea.criteria: 

145 # Only some designs (such as `Benchmark` and `Contest`) declare 

146 # `criteria`, but it is set on any worker whose settings name one. 

147 worker.criteria = build_criteria( # type: ignore[attr-defined] 

148 idea.criteria[name], project) 

149 if name in idea.steps: 

150 parts = [build_step(step, project) for step in idea.steps[name]] 

151 else: 

152 techniques = idea.techniques.get(name, []) 

153 parts = [[build_node(technique, project) for technique in techniques]] 

154 worker.populate(parts) 

155 return worker 

156 

157 

158def build_workflow(project: interface.Project) -> nodes.Worker: 

159 """Builds the workflow of `project`. 

160 

161 The workflow is the worker for the project section of the settings. 

162 

163 Args: 

164 project: the project to build the workflow for. 

165 

166 Returns: 

167 The workflow of the project. 

168 

169 """ 

170 return build_worker(project.name, project) 

171 

172 

173def class_names(name: str) -> str | list[str]: 

174 """Returns the names of the classes that the node `name` can be built with. 

175 

176 A name that ends in a number, such as "logit2", can be built with the class 

177 named without the number ("logit"). The number can follow an underscore 

178 ("logit_2"), which numbers a name that ends in a digit itself ("sha256_2" 

179 is "sha256", but "sha2562" would be "sha"). 

180 

181 Args: 

182 name: name of a node in the settings. 

183 

184 Returns: 

185 `name`, if it does not end in a number. Otherwise, a `list` of `name` 

186 and `name` without the number (and an underscore before it), in 

187 the order to look for them with `Library.borrow`. 

188 

189 """ 

190 numbered = re.fullmatch(r'(?P<base>.+?)_?\d+', name) 

191 if numbered is None: 

192 return name 

193 return [name, numbered['base']] 

194 

195 

196# The private name of `class_names`, which packages built on `chrisjen` 

197# used before it was public. 

198_class_names = class_names