Reusable code uploads
Upload a code blob once and reference it from many submissions.
Some workflows reuse the same fitness function or callback across many runs. Instead of pasting the source into every submission body, upload it once as a code blob and reference it by id.
The client.code_uploads resource exposes this.
Upload a code blob¶
upload = client.code_uploads.create(
content=(
"def fitness_func(ga, sol, idx):\n"
" return -sum(s*s for s in sol)\n"
),
)
print(upload.code_id, upload.content_size)
An upload just stores the source and hands back a code_id. It is role-agnostic: you decide which role it fills when you reference it from a submission.
REST equivalent: POST /api/v1/code-uploads/.
Use a code blob in a submission¶
Reference an upload with the matching <role>_id parameter, passing the code_id you got back. Because the source lives on the server, the SDK can't read it to fill in the entry symbol for you, so pass <role>_entry yourself (the top-level name to call):
client.submissions.create(
name="reused fitness",
fitness_func_id=upload.code_id,
fitness_func_entry="fitness_func",
num_genes=5,
num_generations=50,
sol_per_pop=30,
)
The server resolves the blob, applies it as the fitness function, and runs the job. For the libraries available to imported code, see Libraries you can import.
Fetch an upload¶
blob = client.code_uploads.get(upload.code_id)
print(blob.code_id, blob.content_size, blob.is_expired)
REST equivalent: GET /api/v1/code-uploads/<id>.
What a CodeUpload contains¶
| Attribute | Type | What it is |
|---|---|---|
code_id |
str |
Blob identifier. Pass it as a <role>_id parameter (for example fitness_func_id) when creating submissions. |
content |
str \| None |
The stored source. Returned when you fetch a single upload. |
content_size |
int \| None |
Size of the source in bytes. |
content_sha256 |
str \| None |
SHA-256 of the source. |
created_at |
datetime \| None |
When the upload was saved. |
expires_at |
datetime \| None |
When the upload is scheduled to expire. |
is_expired |
bool \| None |
Whether the upload has passed its expiry. |
raw |
dict |
The full server response, preserved for forward compatibility. |