X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/ddaa9596ebf94aa7b6ee720e06ceccd2d7dc43fc..a31420c0b77005cbe44f90d312c9091ca7f07fdf:/doc/sdk/python/cookbook.html.textile.liquid diff --git a/doc/sdk/python/cookbook.html.textile.liquid b/doc/sdk/python/cookbook.html.textile.liquid index ff8b8052e5..eda6563d7a 100644 --- a/doc/sdk/python/cookbook.html.textile.liquid +++ b/doc/sdk/python/cookbook.html.textile.liquid @@ -81,7 +81,7 @@ def get_cr_state(cr_uuid): return 'On hold' else: return 'Queued' - elif c['state'] == 'Complete' and c['exit_code'] != 0 + elif c['state'] == 'Complete' and c['exit_code'] != 0: return 'Failed' elif c['state'] == 'Running': if c['runtime_status'].get('error', None): @@ -144,7 +144,7 @@ child_requests = api.container_requests().list(filters=[ child_containers = {c["container_uuid"]: c for c in child_requests["items"]} cancelled_child_containers = api.containers().list(filters=[ ["exit_code", "!=", "0"], - ["uuid", "in", child_containers.keys()]], limit=1000).execute() + ["uuid", "in", list(child_containers.keys())]], limit=1000).execute() for c in cancelled_child_containers["items"]: print("%s (%s)" % (child_containers[c["uuid"]]["name"], child_containers[c["uuid"]]["uuid"])) {% endcodeblock %} @@ -159,7 +159,8 @@ container_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz" container_request = api.container_requests().get(uuid=container_request_uuid).execute() collection = arvados.collection.CollectionReader(container_request["log_uuid"]) for c in collection: - print(collection.open(c).read()) + if isinstance(collection.find(c), arvados.arvfile.ArvadosFile): + print(collection.open(c).read()) {% endcodeblock %} h2(#sharing_link). Create a collection sharing link @@ -236,13 +237,13 @@ with c.open(filename, "rb") as reader: print("Finished downloading %s" % filename) {% endcodeblock %} -h2. Copy files from a collection a new collection +h2. Copy files from a collection to a new collection {% codeblock as python %} import arvados.collection -source_collection = "x1u39-4zz18-krzg64ufvehgitl" -target_project = "x1u39-j7d0g-67q94einb8ptznm" +source_collection = "zzzzz-4zz18-zzzzzzzzzzzzzzz" +target_project = "zzzzz-j7d0g-zzzzzzzzzzzzzzz" target_name = "Files copied from source_collection" files_to_copy = ["folder1/sample1/sample1_R1.fastq", "folder1/sample2/sample2_R1.fastq"] @@ -256,3 +257,44 @@ for f in files_to_copy: target.save_new(name=target_name, owner_uuid=target_project) print("Created collection %s" % target.manifest_locator()) {% endcodeblock %} + +h2. Copy files from a collection to another collection + +{% codeblock as python %} +import arvados.collection + +source_collection = "zzzzz-4zz18-zzzzzzzzzzzzzzz" +target_collection = "zzzzz-4zz18-aaaaaaaaaaaaaaa" +files_to_copy = ["folder1/sample1/sample1_R1.fastq", + "folder1/sample2/sample2_R1.fastq"] + +source = arvados.collection.CollectionReader(source_collection) +target = arvados.collection.Collection(target_collection) + +for f in files_to_copy: + target.copy(f, "", source_collection=source) + +target.save() +{% endcodeblock %} + +h2. Delete a file from an existing collection + +{% codeblock as python %} +import arvados + +c = arvados.collection.Collection("zzzzz-4zz18-zzzzzzzzzzzzzzz") +c.remove("file2.txt") +c.save() +{% endcodeblock %} + +h2. Listing records with paging + +Use the @arvados.util.keyset_list_all@ helper method to iterate over all the records matching an optional filter. This method handles paging internally and returns results incrementally using a Python iterator. The first parameter of the method takes a @list@ method of an Arvados resource (@collections@, @container_requests@, etc). + +{% codeblock as python %} +import arvados.util + +api = arvados.api() +for c in arvados.util.keyset_list_all(api.collections().list, filters=[["name", "like", "%sample123%"]]): + print("got collection " + c["uuid"]) +{% endcodeblock %}