codeoceansdk.Computation
1import logging 2from dataclasses import dataclass, field 3from enum import Enum 4from typing import Optional 5 6from codeoceansdk.CodeOcean import CodeOcean 7 8logger = logging.getLogger(__name__) 9 10 11@dataclass(frozen=True, slots=True) 12class Parameter: 13 """ 14 Parameters used during computation 15 """ 16 name: str = None 17 """Parameter name""" 18 value: str = None 19 """Parameter value""" 20 21 22@dataclass(frozen=True, slots=True) 23class File: 24 """ 25 File metadata 26 """ 27 28 name: str 29 """File name""" 30 path: str 31 """File full path""" 32 type: Enum("type", ["file", "folder"]) 33 """File type (file or folder)""" 34 size: int = None 35 """Item size in bytes (only available for files)""" 36 37 38@dataclass(kw_only=True) 39class Computation(CodeOcean): 40 """Computation from a capsule""" 41 id: str 42 """Computation internal id""" 43 parameters: Optional[list[Parameter]] = field(default_factory=list) 44 """List of run parameters""" 45 created: int = 0 46 """Computation creation time""" 47 name: str = None 48 """Display name of the computation""" 49 run_time: int = 0 50 """Total run time in seconds""" 51 has_results: bool = False 52 """Indicates whether the computation has results.""" 53 state: Enum( 54 "state", ["initializing", "running", "finalizing", "completed"] 55 ) = "completed" 56 """Current status""" 57 end_status: Enum("end_status", ["stopped", "failed", "succeeded"]) = None 58 """Status after completion.""" 59 cloud_workstation: bool = None 60 """Generated from cloud workstation""" 61 62 def __post_init__(self): 63 super().__post_init__() 64 self.computation_url = f"{self.api_url}/computations/{self.id}" 65 66 @staticmethod 67 def from_dict(computation_dict: dict, domain: str, api_key): 68 """ 69 Parse dictionary to Computation object 70 71 :param api_key: Capsule API 72 :param domain: Code Ocean domain 73 :param computation_dict: Input dictionary of computation parameters. 74 :return: Computation object. 75 """ 76 if "parameters" in computation_dict: 77 computation_dict["parameters"] = [ 78 Parameter(**x) for x in computation_dict["parameters"] 79 ] 80 computation_dict["domain"] = domain 81 computation_dict["api_key"] = api_key 82 return Computation(**computation_dict) 83 84 def list_computation_result_files(self): 85 """ 86 List results for a particular computation. 87 88 :return: List of files. 89 """ 90 logger.debug(f"Retrieving all files from computation ${self.id}") 91 input_url = f"{self.computation_url}/results" 92 logger.debug(f"Input url: {input_url}") 93 req = self.post(input_url).json() 94 if "items" in req: 95 return [File(**x) for x in req["items"]] 96 else: 97 return None 98 99 def get_download_url(self, file_name: str): 100 """Get download url for curr_id run. 101 102 :param file_name: File name 103 :return: Pre-signed url to data file. 104 """ 105 logger.debug(f"Retrieving {file_name} from computation ${self.id}") 106 input_url = f"{self.computation_url}/results/download_url?path={file_name}" 107 logger.debug(f"Input url: {input_url}") 108 109 req = self.get(input_url).json() 110 111 if "url" in req.keys(): 112 return req["url"] 113 else: 114 logging.debug(f"Unable to retrieve {file_name}") 115 return None 116 117 def get_computation(self): 118 """ 119 Get computation metadata. 120 """ 121 logger.debug(f"Retrieving computation from {self.computation_url}") 122 req = self.get(self.computation_url) 123 new_comp = self.from_dict(req.json(), self.domain, self.api_key) 124 self.__dict__.update(new_comp.__dict__)
logger =
<Logger codeoceansdk.Computation (WARNING)>
@dataclass(frozen=True, slots=True)
class
Parameter:
12@dataclass(frozen=True, slots=True) 13class Parameter: 14 """ 15 Parameters used during computation 16 """ 17 name: str = None 18 """Parameter name""" 19 value: str = None 20 """Parameter value"""
Parameters used during computation
@dataclass(frozen=True, slots=True)
class
File:
23@dataclass(frozen=True, slots=True) 24class File: 25 """ 26 File metadata 27 """ 28 29 name: str 30 """File name""" 31 path: str 32 """File full path""" 33 type: Enum("type", ["file", "folder"]) 34 """File type (file or folder)""" 35 size: int = None 36 """Item size in bytes (only available for files)"""
File metadata
39@dataclass(kw_only=True) 40class Computation(CodeOcean): 41 """Computation from a capsule""" 42 id: str 43 """Computation internal id""" 44 parameters: Optional[list[Parameter]] = field(default_factory=list) 45 """List of run parameters""" 46 created: int = 0 47 """Computation creation time""" 48 name: str = None 49 """Display name of the computation""" 50 run_time: int = 0 51 """Total run time in seconds""" 52 has_results: bool = False 53 """Indicates whether the computation has results.""" 54 state: Enum( 55 "state", ["initializing", "running", "finalizing", "completed"] 56 ) = "completed" 57 """Current status""" 58 end_status: Enum("end_status", ["stopped", "failed", "succeeded"]) = None 59 """Status after completion.""" 60 cloud_workstation: bool = None 61 """Generated from cloud workstation""" 62 63 def __post_init__(self): 64 super().__post_init__() 65 self.computation_url = f"{self.api_url}/computations/{self.id}" 66 67 @staticmethod 68 def from_dict(computation_dict: dict, domain: str, api_key): 69 """ 70 Parse dictionary to Computation object 71 72 :param api_key: Capsule API 73 :param domain: Code Ocean domain 74 :param computation_dict: Input dictionary of computation parameters. 75 :return: Computation object. 76 """ 77 if "parameters" in computation_dict: 78 computation_dict["parameters"] = [ 79 Parameter(**x) for x in computation_dict["parameters"] 80 ] 81 computation_dict["domain"] = domain 82 computation_dict["api_key"] = api_key 83 return Computation(**computation_dict) 84 85 def list_computation_result_files(self): 86 """ 87 List results for a particular computation. 88 89 :return: List of files. 90 """ 91 logger.debug(f"Retrieving all files from computation ${self.id}") 92 input_url = f"{self.computation_url}/results" 93 logger.debug(f"Input url: {input_url}") 94 req = self.post(input_url).json() 95 if "items" in req: 96 return [File(**x) for x in req["items"]] 97 else: 98 return None 99 100 def get_download_url(self, file_name: str): 101 """Get download url for curr_id run. 102 103 :param file_name: File name 104 :return: Pre-signed url to data file. 105 """ 106 logger.debug(f"Retrieving {file_name} from computation ${self.id}") 107 input_url = f"{self.computation_url}/results/download_url?path={file_name}" 108 logger.debug(f"Input url: {input_url}") 109 110 req = self.get(input_url).json() 111 112 if "url" in req.keys(): 113 return req["url"] 114 else: 115 logging.debug(f"Unable to retrieve {file_name}") 116 return None 117 118 def get_computation(self): 119 """ 120 Get computation metadata. 121 """ 122 logger.debug(f"Retrieving computation from {self.computation_url}") 123 req = self.get(self.computation_url) 124 new_comp = self.from_dict(req.json(), self.domain, self.api_key) 125 self.__dict__.update(new_comp.__dict__)
Computation from a capsule
Computation( *, domain: str, api_key: str, id: str, parameters: Optional[list[Parameter]] = <factory>, created: int = 0, name: str = None, run_time: int = 0, has_results: bool = False, state: codeoceansdk.Computation.state = 'completed', end_status: codeoceansdk.Computation.end_status = None, cloud_workstation: bool = None)
@staticmethod
def
from_dict(computation_dict: dict, domain: str, api_key):
67 @staticmethod 68 def from_dict(computation_dict: dict, domain: str, api_key): 69 """ 70 Parse dictionary to Computation object 71 72 :param api_key: Capsule API 73 :param domain: Code Ocean domain 74 :param computation_dict: Input dictionary of computation parameters. 75 :return: Computation object. 76 """ 77 if "parameters" in computation_dict: 78 computation_dict["parameters"] = [ 79 Parameter(**x) for x in computation_dict["parameters"] 80 ] 81 computation_dict["domain"] = domain 82 computation_dict["api_key"] = api_key 83 return Computation(**computation_dict)
Parse dictionary to Computation object
Parameters
- api_key: Capsule API
- domain: Code Ocean domain
- computation_dict: Input dictionary of computation parameters.
Returns
Computation object.
def
list_computation_result_files(self):
85 def list_computation_result_files(self): 86 """ 87 List results for a particular computation. 88 89 :return: List of files. 90 """ 91 logger.debug(f"Retrieving all files from computation ${self.id}") 92 input_url = f"{self.computation_url}/results" 93 logger.debug(f"Input url: {input_url}") 94 req = self.post(input_url).json() 95 if "items" in req: 96 return [File(**x) for x in req["items"]] 97 else: 98 return None
List results for a particular computation.
Returns
List of files.
def
get_download_url(self, file_name: str):
100 def get_download_url(self, file_name: str): 101 """Get download url for curr_id run. 102 103 :param file_name: File name 104 :return: Pre-signed url to data file. 105 """ 106 logger.debug(f"Retrieving {file_name} from computation ${self.id}") 107 input_url = f"{self.computation_url}/results/download_url?path={file_name}" 108 logger.debug(f"Input url: {input_url}") 109 110 req = self.get(input_url).json() 111 112 if "url" in req.keys(): 113 return req["url"] 114 else: 115 logging.debug(f"Unable to retrieve {file_name}") 116 return None
Get download url for curr_id run.
Parameters
- file_name: File name
Returns
Pre-signed url to data file.
def
get_computation(self):
118 def get_computation(self): 119 """ 120 Get computation metadata. 121 """ 122 logger.debug(f"Retrieving computation from {self.computation_url}") 123 req = self.get(self.computation_url) 124 new_comp = self.from_dict(req.json(), self.domain, self.api_key) 125 self.__dict__.update(new_comp.__dict__)
Get computation metadata.