ocean-runner 0.2.23__py3-none-any.whl → 0.2.24__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ocean_runner/config.py CHANGED
@@ -1,12 +1,12 @@
1
1
  from enum import StrEnum, auto
2
2
  from logging import Logger
3
3
  from pathlib import Path
4
- from typing import Generic, Sequence, TypeVar
4
+ from typing import Generic, Sequence, Type, TypeVar
5
5
 
6
6
  from pydantic import BaseModel, ConfigDict, Field
7
7
  from pydantic_settings import BaseSettings
8
8
 
9
- InputT = TypeVar("InputT")
9
+ InputT = TypeVar("InputT", BaseModel, None)
10
10
 
11
11
  DEFAULT = "DEFAULT"
12
12
 
@@ -21,14 +21,14 @@ class Keys(StrEnum):
21
21
  class Environment(BaseSettings):
22
22
  """Environment configuration loaded from environment variables"""
23
23
 
24
- base_dir: str | Path | None = Field(
24
+ base_dir: str | Path = Field(
25
25
  default_factory=lambda: Path("/data"),
26
26
  validation_alias=Keys.BASE_DIR.value,
27
27
  description="Base data directory, defaults to '/data'",
28
28
  )
29
29
 
30
- dids: list[Path] | str | None = Field(
31
- default_factory=list,
30
+ dids: str | None = Field(
31
+ default=None,
32
32
  validation_alias=Keys.DIDS.value,
33
33
  description='Datasets DID\'s, format: ["XXXX"]',
34
34
  )
@@ -51,7 +51,7 @@ class Config(BaseModel, Generic[InputT]):
51
51
 
52
52
  model_config = ConfigDict(arbitrary_types_allowed=True)
53
53
 
54
- custom_input: InputT | None = Field(
54
+ custom_input: Type[InputT] | None = Field(
55
55
  default=None,
56
56
  description="Algorithm's custom input types, must be a dataclass_json",
57
57
  )
ocean_runner/runner.py CHANGED
@@ -5,13 +5,14 @@ import inspect
5
5
  from dataclasses import InitVar, dataclass, field
6
6
  from logging import Logger
7
7
  from pathlib import Path
8
- from typing import Awaitable, Callable, Generic, TypeAlias, TypeVar
8
+ from typing import Awaitable, Callable, Dict, Generic, TypeAlias, TypeVar
9
9
 
10
- from oceanprotocol_job_details import load_job_details, JobDetails # type: ignore
10
+ from oceanprotocol_job_details import JobDetails, load_job_details
11
+ from pydantic import BaseModel, JsonValue
11
12
 
12
13
  from ocean_runner.config import Config
13
14
 
14
- InputT = TypeVar("InputT")
15
+ InputT = TypeVar("InputT", BaseModel, None)
15
16
  ResultT = TypeVar("ResultT")
16
17
  T = TypeVar("T")
17
18
 
@@ -110,7 +111,7 @@ class Algorithm(Generic[InputT, ResultT]):
110
111
  f"Added [{len(configuration.source_paths)}] entries to PATH"
111
112
  )
112
113
 
113
- self.configuration = configuration
114
+ self.configuration: Config[InputT] = configuration
114
115
 
115
116
  class Error(RuntimeError): ...
116
117
 
@@ -151,15 +152,15 @@ class Algorithm(Generic[InputT, ResultT]):
151
152
  # ---------------------------
152
153
 
153
154
  async def execute(self) -> ResultT | None:
154
- self._job_details = load_job_details(
155
- {
156
- "base_dir": self.configuration.environment.base_dir,
157
- "dids": self.configuration.environment.dids or [],
158
- "secret": self.configuration.environment.secret,
159
- "transformation_did": self.configuration.environment.transformation_did,
160
- },
161
- self.configuration.custom_input,
162
- )
155
+ env = self.configuration.environment
156
+ config: Dict[str, JsonValue] = {
157
+ "base_dir": str(env.base_dir),
158
+ "dids": env.dids,
159
+ "secret": env.secret,
160
+ "transformation_did": env.transformation_did,
161
+ }
162
+
163
+ self._job_details = load_job_details(config, self.configuration.custom_input)
163
164
 
164
165
  self.logger.info("Loaded JobDetails")
165
166
  self.logger.debug(self.job_details.model_dump())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ocean-runner
3
- Version: 0.2.23
3
+ Version: 0.2.24
4
4
  Summary: A fluent API for OceanProtocol algorithms
5
5
  Project-URL: Homepage, https://github.com/AgrospAI/ocean-runner
6
6
  Project-URL: Issues, https://github.com/AgrospAI/ocean-runner/issues
@@ -18,7 +18,7 @@ Classifier: Operating System :: OS Independent
18
18
  Classifier: Programming Language :: Python :: 3
19
19
  Requires-Python: >=3.10
20
20
  Requires-Dist: aiofiles>=25.1.0
21
- Requires-Dist: oceanprotocol-job-details>=0.3.4
21
+ Requires-Dist: oceanprotocol-job-details>=0.3.9
22
22
  Requires-Dist: pydantic-settings>=2.12.0
23
23
  Requires-Dist: pydantic>=2.12.5
24
24
  Requires-Dist: pytest>=8.4.2
@@ -28,7 +28,6 @@ Description-Content-Type: text/markdown
28
28
 
29
29
  Ocean Runner is a package that eases algorithm creation in the scope of OceanProtocol.
30
30
 
31
-
32
31
  ## Installation
33
32
 
34
33
  ```bash
@@ -49,7 +48,7 @@ algorithm = Algorithm()
49
48
 
50
49
 
51
50
  @algorithm.run
52
- def run():
51
+ def run(_: Algorithm):
53
52
  return random.randint()
54
53
 
55
54
 
@@ -76,14 +75,14 @@ algorithm = Algorithm(
76
75
  Config(
77
76
  custom_input: ... # dataclass
78
77
  # Custom algorithm parameters dataclass.
79
-
78
+
80
79
  logger: ... # type: logging.Logger
81
80
  # Custom logger to use.
82
81
 
83
82
  source_paths: ... # type: Iterable[Path]
84
83
  # Source paths to include in the PATH
85
-
86
- environment: ...
84
+
85
+ environment: ...
87
86
  # type: ocean_runner.Environment. Mock of environment variables.
88
87
  )
89
88
  )
@@ -92,12 +91,12 @@ algorithm = Algorithm(
92
91
  ```python
93
92
  import logging
94
93
 
94
+ from pydantic import BaseModel
95
95
  from ocean_runner import Algorithm, Config
96
96
 
97
97
 
98
- @dataclass
99
- class CustomInput:
100
- foobar: string
98
+ class CustomInput(BaseModel):
99
+ foobar: string
101
100
 
102
101
 
103
102
  logger = logging.getLogger(__name__)
@@ -107,7 +106,7 @@ algorithm = Algorithm(
107
106
  Config(
108
107
  custom_input: CustomInput,
109
108
  """
110
- Load the Algorithm's Custom Input into a CustomInput dataclass instance.
109
+ Load the Algorithm's Custom Input into a CustomInput instance.
111
110
  """
112
111
 
113
112
  source_paths: [Path("/algorithm/src")],
@@ -163,34 +162,32 @@ algorithm = Algorithm()
163
162
 
164
163
 
165
164
  @algorithm.on_error
166
- def error_callback(ex: Exception):
165
+ def error_callback(algorithm: Algorithm, ex: Exception):
167
166
  algorithm.logger.exception(ex)
168
167
  raise algorithm.Error() from ex
169
168
 
170
169
 
171
170
  @algorithm.validate
172
- def val():
171
+ def val(algorithm: Algorithm):
173
172
  assert algorithm.job_details.files, "Empty input dir"
174
173
 
175
174
 
176
175
  @algorithm.run
177
- def run() -> pd.DataFrame:
178
- _, filename = next(algorithm.job_details.next_path())
176
+ def run(algorithm: Algorithm) -> pd.DataFrame:
177
+ _, filename = next(algorithm.job_details.inputs())
179
178
  return pd.read_csv(filename).describe(include="all")
180
179
 
181
180
 
182
181
  @algorithm.save_results
183
- def save(results: pd.DataFrame, path: Path):
184
- algorithm.logger.info(f"Descriptive statistics: {results}")
185
- results.to_csv(path / "results.csv")
182
+ def save(algorithm: Algorithm, result: pd.DataFrame, base: Path):
183
+ algorithm.logger.info(f"Descriptive statistics: {result}")
184
+ result.to_csv(base / "result.csv")
186
185
 
187
186
 
188
187
  if __name__ == "__main__":
189
188
  algorithm()
190
189
  ```
191
190
 
192
-
193
-
194
191
  ### Default implementations
195
192
 
196
193
  As seen in the minimal example, all methods implemented in `Algorithm` have a default implementation which will be commented here.
@@ -206,7 +203,7 @@ As seen in the minimal example, all methods implemented in `Algorithm` have a de
206
203
 
207
204
  .run()
208
205
 
209
- """
206
+ """
210
207
  Has NO default implementation, must pass a callback that returns a result of any type.
211
208
  """
212
209
 
@@ -222,7 +219,8 @@ As seen in the minimal example, all methods implemented in `Algorithm` have a de
222
219
  To load the OceanProtocol JobDetails instance, the program will read some environment variables, they can be mocked passing an instance of `Environment` through the configuration of the algorithm.
223
220
 
224
221
  Environment variables:
222
+
225
223
  - `DIDS` (optional) Input dataset(s) DID's, must have format: `["abc..90"]`. Defaults to reading them automatically from the `DDO` data directory.
226
224
  - `TRANSFORMATION_DID` (optional, default="DEFAULT"): Algorithm DID, must have format: `abc..90`.
227
- - `SECRET` (optional, default="DEFAULT"): Algorithm secret.
225
+ - `SECRET` (optional, default="DEFAULT"): Algorithm secret.
228
226
  - `BASE_DIR` (optional, default="/data"): Base path to the OceanProtocol data directories.
@@ -0,0 +1,8 @@
1
+ ocean_runner/__init__.py,sha256=hRyMrE7K0DEbNRa-iNwA2xFOM85JJvjeKIdiWvcOm4Y,154
2
+ ocean_runner/config.py,sha256=VfDNyYjO6PpM2tmtiCdCiY4oGT1JMB_zl5hZjLeT0Vg,1928
3
+ ocean_runner/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ ocean_runner/runner.py,sha256=bmWzQqZci3NSUclCwGuzfYfWOuXgKCUT0u3D3HljBMo,6235
5
+ ocean_runner-0.2.24.dist-info/METADATA,sha256=axJvnOryvmL_Y6UuN1aEOpGSJ0pR_DOOMGRfcScPRec,6755
6
+ ocean_runner-0.2.24.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
7
+ ocean_runner-0.2.24.dist-info/licenses/LICENSE,sha256=_B25KqK4amoADWkMN150tnZFm_Fy7VvZpvIC8ZydWdI,1053
8
+ ocean_runner-0.2.24.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- ocean_runner/__init__.py,sha256=hRyMrE7K0DEbNRa-iNwA2xFOM85JJvjeKIdiWvcOm4Y,154
2
- ocean_runner/config.py,sha256=_P8MbKGzL36p8LO_m6nj3cPB0h6L2YL8satxUNXXnUI,1927
3
- ocean_runner/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- ocean_runner/runner.py,sha256=0-McElMM_dzubLUbRdeDaE0WY_dNj0IN_5cGen4-4bM,6242
5
- ocean_runner-0.2.23.dist-info/METADATA,sha256=HkaOEgMvm98GQT9VsGMSN6FwTNzzMnocGpZyKu4HgSs,6667
6
- ocean_runner-0.2.23.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
7
- ocean_runner-0.2.23.dist-info/licenses/LICENSE,sha256=_B25KqK4amoADWkMN150tnZFm_Fy7VvZpvIC8ZydWdI,1053
8
- ocean_runner-0.2.23.dist-info/RECORD,,