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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 | class PyroProjectResource:
"""
An interface for projects in the Pyro ecosystem.
Provides an organization mechanism for Pyro jobs and job groups.
"""
def __init__(self, client: PyroApiClient):
self.client = client
self._endpoint = "projects"
def create(self, name: Optional[str] = None):
"""
# Create a project
## Example
```python
pyro = PyroDash(...)
project = pyro.projects.create("my pyro project")
print(project.name)
print(project.id)
"""
data = {"name": name}
raw = self.client.request("POST", self._endpoint, data)
_dict = {**raw, "_resource": self}
return PyroProject.from_dict(_dict)
def get(self, id: str):
"""
# Retrieve a project by ID
## Example
```python
pyro = PyroDash(...)
id = "p_3QZ12NDKxyokJiwLbNBM7G"
project = pyro.pyrojects.get(id)
```
"""
url = f"{self._endpoint}/{id}"
raw = self.client.request("GET", url)
_dict = {**raw, "_resource": self}
return PyroProject.from_dict(_dict)
def filter(self, filters: List[ProjectFilter] = [], page=1, num_per_page=10):
"""
# Retrieve a list of projects
The projects returned are filtered and paginated in accordance
with the params you provide. If no filters are provided,
then all of the projects are retrieved.
## Example
```python
pyro = PyroDash(...)
# Get all of my projects (returns maximum of 20)
projects = pyro.projects.filter(num_per_page=20)
# Get all projects that have wildest in the name (not case sensitive)
filters = [ProjectFilter("name", "wildest")]
wildest_projects = pyro.projects.filter(filters)
```
"""
params = {
"page": page,
"limit": num_per_page,
"filters": json.dumps([filter.__dict__ for filter in filters]),
}
raw = self.client.request("GET", self._endpoint, params)
projects: List[PyroProject] = []
for data in raw["data"]:
_dict = {**data, "_resource": self}
project = PyroProject.from_dict(_dict)
projects.append(project)
return projects
def find_by_name(self, name: str):
"""
# Find a project by name
This function only expects exactly one match. If more or less
are found, a `ValueError` will be raised.
## Example
```python
pyro = PyroDash(...)
project = pyro.projects.find_by_name("my project")
print(project.name)
>>> "my_project"
project.list_jobs() # etc
```
"""
projects = self.filter([ProjectFilter("name", name)])
if len(projects) == 0:
raise ValueError(f"Cannot find project with name: {name}")
if len(projects) > 1:
raise ValueError(f"Name {name} is ambiguous, too many results returned")
return projects[0]
def create_job(self, id: str, job: Union[PyroJob, dict]) -> PyroJob:
"""
# Creates a new job in a project
Creates a job and handles automatically adding it to a project.
You can specify either a `PyroJob` or a `dict` as the `job` parameter.
The system will automatically use the fields in this object to create
an entirely new job in this project.
When you specify a `PyroJob` a new job is created that is simply "modelled"
after that job i.e. using a set of its properties.
## Args:
id (str): project id
job (Union[PyroJob | dict]): the properties to use when creating the job
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h"
job = {"type": "fsim", "name": "my fsim job"}
new_job = pyro.projects.create_job(project_id, job)
assert new_job.name == "my fsim job"
```
"""
if isinstance(job, PyroJob):
return self.create_job_from_pyrojob(id, job)
elif isinstance(job, dict):
return self.create_job_from_dict(id, job)
else:
raise TypeError(
"source job provided is of unexpected type. cannot create a new job from it."
)
def create_job_from_dict(self, id: str, job: dict) -> PyroJob:
"""
# Create a new job in a project (from a dictionary)
## Args:
id (str): project id
job (dict): the dictionary to use when creating the job
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h"
job = {"type": "fsim", "name": "my fsim job"}
new_job = pyro.projects.create_job_from_dict(project_id, job)
assert new_job.name == "my fsim job"
```
"""
if job["type"] is None:
raise ValueError("dict specified does not have a type!")
job_rsc = PyroJobResource(self.client)
new_job = job_rsc.create(job["type"])
new_job.update(
name=job.get("name", "Unlabeled"), description=job.get("description", "")
)
self.add_job(id, new_job.id)
return job_rsc.get(new_job.id)
def create_job_from_pyrojob(self, id: str, job: PyroJob) -> PyroJob:
"""
# Create a new job in a project (from a `PyroJob` object)
## Args:
id (str): project id
job (dict): the dictionary to use when creating the job
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h"
job = PyroJob("j_r62X", "fsim", ..)
new_job = pyro.projects.create_job_from_pyrojob(project_id, job)
```
"""
if job.type is None:
raise ValueError("job specified does not have a type!")
job_rsc = PyroJobResource(self.client)
new_job = job_rsc.create(job.type) # pyright: ignore
new_job.update(name=job.name, description=job.description)
self.add_job(id, new_job.id)
return job_rsc.get(new_job.id)
def add_job(self, id: str, job_id: str) -> PyroJob:
"""
# Add a job to a project
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h.."
job_id = "j_r62X.."
job = pyro.projects.add_job(project_id, job_id)
```
"""
url = f"{self._endpoint}/{id}/add_job"
raw = self.client.request(POST, url, {"job_id": job_id})
_dict = {**raw, "_resource": PyroJobResource(self.client)}
return PyroJob.from_dict(_dict)
def add_job_group(self, id: str, job_group_id: str) -> PyroJobGroup:
"""
# Add job group to a project
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h.."
job_group_id = "jg_la043"
job_group = pyro.projects.add_job_group(project_id, job_group_id)
```
"""
url = f"{self._endpoint}/{id}/add_job_group"
resp = self.client.request(POST, url, {"job_group_id": job_group_id})
_dict = {**resp, "_resource": PyroJobGroupResource(self.client)}
return PyroJobGroup.from_dict(_dict)
def duplicate_job(self, id: str, job_id: str) -> PyroJob:
"""
# Duplicate a job in a project
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h.."
job_id = "j_r62X.."
duplicate_job = pyro.projects.duplicate_job(project_id, job_id)
```
"""
url = f"{self._endpoint}/{id}/duplicate_job"
raw = self.client.request(POST, url, {"job_id": job_id})
_dict = {**raw, "_resource": PyroJobResource(self.client)}
return PyroJob.from_dict(_dict)
def delete(self, id: str):
"""
# Delete a project
Deleting a project will also delete any jobs and data
associated with it. Tread carefully.
## Example
```python
pyro = PyroDash(...)
project = pyro.projects.find_by_name("my cringe project")
pyro.projects.delete(project.id)
```
"""
url = f"{self._endpoint}/{id}"
raw = self.client.request(DEL, url)
_dict = {**raw, "_resource": self}
return PyroProject.from_dict(_dict)
def list_jobs(self, id: str) -> list[PyroJob]:
"""
# Retrieves jobs in a project
This function will return ALL jobs in a project,
those that are directly associated with a project
and those that are associated with a project
through a job group.
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h.."
jobs = pyro.projects.list_jobs(project_id)
```
"""
jobs = self.list_ungrouped_jobs(id)
# also fetch any jobs that may be in job groups
# for this project
job_groups = self.list_job_groups(id)
for group in job_groups:
grouped_jobs = group.list_jobs()
for grouped_job in grouped_jobs:
jobs.append(grouped_job)
return jobs
def list_job_groups(self, id: str) -> list[PyroJobGroup]:
"""
# Retrieves job groups in a project
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h.."
job_groups = pyro.projects.list_job_groups(project_id)
```
"""
job_groups = []
url = f"{self._endpoint}/{id}/job_groups"
resp = self.client.request(GET, url)
for job_group in resp["data"]:
_dict = {**job_group, "_resource": PyroJobGroupResource(self.client)}
job_groups.append(PyroJobGroup.from_dict(_dict))
return job_groups
def list_ungrouped_jobs(self, id: str) -> list[PyroJob]:
"""
# Retrieves ungrouped jobs in a project
## Example
```python
pyro = PyroDash(...)
project_id = "p_2VF7h.."
jobs = pyro.projects.list_ungrouped_jobs(project_id)
```
"""
url = f"{self._endpoint}/{id}/jobs"
resp = self.client.request(GET, url)
jobs = []
for lite_job_data in resp["data"]:
job_id = lite_job_data["id"]
url = f"jobs/{job_id}"
job_resp = self.client.request(GET, url)
_dict = {**job_resp, "_resource": PyroJobResource(self.client)}
jobs.append(PyroJob.from_dict(_dict))
return jobs
|