Skip to content

Commit

Permalink
ruff fixes + 1 mypy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Oct 2, 2024
1 parent 02d7339 commit b162636
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 65 deletions.
2 changes: 2 additions & 0 deletions docs/tutorials/4-creating-flows.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@
"source": [
"from jobflow import job\n",
"\n",
"\n",
"@job\n",
"def add(a, b, c=2):\n",
" return a + b + c\n",
"\n",
"\n",
"@job\n",
"def mult(a, b):\n",
" return a * b"
Expand Down
79 changes: 48 additions & 31 deletions docs/tutorials/5-dynamic-flows.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
"execution_count": 1,
"id": "78348798",
"metadata": {
"nbsphinx": "hidden",
"tags": [],
"ExecuteTime": {
"end_time": "2023-11-23T22:55:01.113171569Z",
"start_time": "2023-11-23T22:55:01.112486877Z"
}
},
"nbsphinx": "hidden",
"tags": []
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -98,26 +98,28 @@
],
"source": [
"from random import randint\n",
"from jobflow import job, Flow, Response\n",
"\n",
"from jobflow import Flow, Response, job\n",
"from jobflow.managers.local import run_locally\n",
"\n",
"\n",
"@job\n",
"def make_list(a):\n",
" return [a] * randint(2, 5)\n",
"\n",
"\n",
"@job\n",
"def add(a, b):\n",
" return a + b\n",
"\n",
"\n",
"@job\n",
"def add_distributed(list_a):\n",
" jobs = []\n",
" for val in list_a:\n",
" jobs.append(add(val, 1))\n",
" \n",
" jobs = [add(val, 1) for val in list_a]\n",
" flow = Flow(jobs)\n",
" return Response(replace=flow)\n",
"\n",
"\n",
"job1 = make_list(2)\n",
"job2 = add_distributed(job1.output)\n",
"flow = Flow([job1, job2])\n",
Expand Down Expand Up @@ -215,14 +217,17 @@
],
"source": [
"@job\n",
"def add(a, b):\n",
"def add(a, b): # noqa: F811\n",
" return a + b\n",
"\n",
"\n",
"@job\n",
"def add_with_logic(a, b):\n",
" if a < 10:\n",
" return Response(addition=add(a, b))\n",
" \n",
" return None\n",
"\n",
"\n",
"job1 = add(1, 2)\n",
"job2 = add_with_logic(job1.output, 2)\n",
"flow = Flow([job1, job2])\n",
Expand Down Expand Up @@ -294,11 +299,14 @@
"def add(a, b):\n",
" return a + b\n",
"\n",
"\n",
"@job\n",
"def add_with_logic(a, b):\n",
" if a < 10:\n",
" return Response(addition=add(a, b))\n",
" \n",
" return None\n",
"\n",
"\n",
"job1 = add(1, 20)\n",
"job2 = add_with_logic(job1.output, 20)\n",
"flow = Flow([job1, job2])\n",
Expand Down Expand Up @@ -341,16 +349,25 @@
},
{
"cell_type": "markdown",
"source": [
"In this way, one can also compute the Fibonacci numbers:"
],
"id": "7fb27b941602401d91542211134fc71a",
"metadata": {
"collapsed": false
}
},
"source": [
"In this way, one can also compute the Fibonacci numbers:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "acae54e37e7d407bbb7b55eff062a284",
"metadata": {
"ExecuteTime": {
"end_time": "2023-11-23T22:55:13.426518952Z",
"start_time": "2023-11-23T22:55:13.322421257Z"
},
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -392,13 +409,12 @@
}
],
"source": [
"\"\"\"A dynamic workflow that calculates the Fibonacci sequence.\"\"\"\n",
"from jobflow import Response, job, run_locally\n",
"from jobflow import job, run_locally\n",
"\n",
"\n",
"@job\n",
"def fibonacci(smaller, larger, stop_point=1000):\n",
" \"\"\"Calculate the next number in the Fibonacci sequence.\n",
" \"\"\"A dynamic workflow that calculates the Fibonacci sequence.\n",
"\n",
" If the number is larger than stop_point, the job will stop the workflow\n",
" execution, otherwise, a new job will be submitted to calculate the next number.\n",
Expand All @@ -415,15 +431,8 @@
"fibonacci_job = fibonacci(1, 1)\n",
"\n",
"# run the job; responses will contain the output from all jobs\n",
"responses = run_locally(fibonacci_job)\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-23T22:55:13.426518952Z",
"start_time": "2023-11-23T22:55:13.322421257Z"
}
}
"responses = run_locally(fibonacci_job)"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -472,11 +481,14 @@
"def add(a, b):\n",
" return a + b\n",
"\n",
"\n",
"@job\n",
"def add_with_logic(a, b):\n",
" if a < 10:\n",
" return Response(detour=add(a, b))\n",
" \n",
" return None\n",
"\n",
"\n",
"job1 = add(1, 2)\n",
"job2 = add_with_logic(job1.output, 2)\n",
"flow = Flow([job1, job2])\n",
Expand All @@ -497,7 +509,12 @@
"outputs": [
{
"data": {
"text/plain": "{'301d75f0-7042-494a-9f24-cab0428c2fd1': {1: Response(output=3, detour=None, addition=None, replace=None, stored_data=None, stop_children=False, stop_jobflow=False)},\n '97be61a8-eec4-4e64-bf53-ba37621575e7': {1: Response(output=None, detour=Flow(name='Flow', uuid='0de995a5-1110-4200-b010-276cb2017474')\n 1. Job(name='add', uuid='d4c31f68-09ad-418a-ac52-89b303fc2a00'), addition=None, replace=None, stored_data=None, stop_children=False, stop_jobflow=False)},\n 'd4c31f68-09ad-418a-ac52-89b303fc2a00': {1: Response(output=5, detour=None, addition=None, replace=None, stored_data=None, stop_children=False, stop_jobflow=False)}}"
"text/plain": [
"{'301d75f0-7042-494a-9f24-cab0428c2fd1': {1: Response(output=3, detour=None, addition=None, replace=None, stored_data=None, stop_children=False, stop_jobflow=False)},\n",
" '97be61a8-eec4-4e64-bf53-ba37621575e7': {1: Response(output=None, detour=Flow(name='Flow', uuid='0de995a5-1110-4200-b010-276cb2017474')\n",
" 1. Job(name='add', uuid='d4c31f68-09ad-418a-ac52-89b303fc2a00'), addition=None, replace=None, stored_data=None, stop_children=False, stop_jobflow=False)},\n",
" 'd4c31f68-09ad-418a-ac52-89b303fc2a00': {1: Response(output=5, detour=None, addition=None, replace=None, stored_data=None, stop_children=False, stop_jobflow=False)}}"
]
},
"execution_count": 10,
"metadata": {},
Expand All @@ -519,9 +536,9 @@
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"display_name": "Python 3 (ipykernel)"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down
20 changes: 12 additions & 8 deletions docs/tutorials/6-makers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@
],
"source": [
"from dataclasses import dataclass\n",
"from jobflow import job, Flow, Maker\n",
"\n",
"from jobflow import Flow, Maker, job\n",
"from jobflow.managers.local import run_locally\n",
"\n",
"\n",
"@dataclass\n",
"class AddMaker(Maker):\n",
" name: str = \"Add Maker\"\n",
Expand All @@ -73,10 +75,9 @@
" def make(self, a, b):\n",
" if self.operation == \"add\":\n",
" return a + b\n",
" elif self.operation == \"mult\":\n",
" if self.operation == \"mult\":\n",
" return a * b\n",
" else:\n",
" raise ValueError(f\"Unknown operation: {self.operation}\")\n",
" raise ValueError(f\"Unknown operation: {self.operation}\")\n",
"\n",
"\n",
"job1 = AddMaker().make(a=2, b=3)\n",
Expand Down Expand Up @@ -122,9 +123,11 @@
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from jobflow import job, Flow, Maker\n",
"\n",
"from jobflow import Flow, Maker, job\n",
"from jobflow.managers.local import run_locally\n",
"\n",
"\n",
"@dataclass\n",
"class AddMaker(Maker):\n",
" name: str = \"Add Maker\"\n",
Expand All @@ -134,10 +137,10 @@
" def make(self, a, b):\n",
" if self.operation == \"add\":\n",
" return a + b\n",
" elif self.operation == \"mult\":\n",
" if self.operation == \"mult\":\n",
" return a * b\n",
" else:\n",
" raise ValueError(f\"Unknown operation: {self.operation}\")\n",
" raise ValueError(f\"Unknown operation: {self.operation}\")\n",
"\n",
"\n",
"@dataclass\n",
"class SubtractMaker(Maker):\n",
Expand All @@ -147,6 +150,7 @@
" def make(self, a, b):\n",
" return b - a\n",
"\n",
"\n",
"job1 = AddMaker().make(a=2, b=3)\n",
"job2 = SubtractMaker().make(a=job1.output, b=4)\n",
"flow = Flow([job1, job2])"
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/7-generalized-makers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"metadata": {},
"outputs": [],
"source": [
"from jobflow import Maker, job, Flow\n",
"from jobflow import Flow, Maker, job\n",
"from jobflow.managers.local import run_locally"
]
},
Expand All @@ -59,7 +59,7 @@
"source": [
"class BaseMaker(Maker):\n",
" def code_specific_func(self, arg1):\n",
" raise NotImplementedError()\n",
" raise NotImplementedError\n",
"\n",
" def make(self):\n",
" return Flow([job1(self.code_specific_func, \"ARG1\")])\n",
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ fireworks = ["FireWorks"]
strict = [
"FireWorks==2.0.3",
"PyYAML==6.0.2",
"maggma==0.69.4",
"maggma==0.69.3",
"matplotlib==3.9.2",
"monty==2024.7.30",
"moto==4.2.13",
"networkx==3.2.1",
"pydantic-settings==2.5.2",
"pydantic==2.9.2",
"pydantic==2.9.1",
"pydash==8.0.3",
"pydot==2.0.0",
"python-ulid==2.7.0",
Expand Down Expand Up @@ -121,6 +121,7 @@ exclude_lines = [

[tool.ruff]
target-version = "py39"
output-format = "concise"

[tool.ruff.lint]
select = [
Expand Down Expand Up @@ -171,7 +172,6 @@ ignore = [
]
pydocstyle.convention = "numpy"
isort.known-first-party = ["jobflow"]
ignore-init-module-imports = true

[tool.ruff.lint.per-file-ignores]
# F401: unused import
Expand All @@ -180,3 +180,4 @@ ignore-init-module-imports = true
# PLR2004: magic-value-comparison
# PT004: pytest-missing-fixture-name-underscore
"**/tests/*" = ["ANN", "ARG001", "D", "PLR2004", "PT004", "S101"]
"docs/tutorials/*" = ["D", "PLR2004"]
2 changes: 1 addition & 1 deletion src/jobflow/core/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def __sub__(self, other: Flow | Job) -> Flow:
if other not in self:
raise ValueError(f"{other!r} not found in flow")
new_flow = deepcopy(self)
new_flow.jobs = tuple([job for job in new_flow if job != other])
new_flow.jobs = tuple(job for job in new_flow if job != other)
return new_flow

def __repr__(self, level: int = 0, prefix: str = "") -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/jobflow/core/maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def recursive_call(

if isinstance(class_filter, Maker):
# Maker instance supplied rather than a Maker class
class_filter = class_filter.__class__
class_filter = class_filter.__class__ # type: ignore[assignment]

def _filter(nested_obj: Maker):
# Filter the Maker object
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def mongo_jobstore(database):
return store


@pytest.fixture()
@pytest.fixture
def memory_jobstore():
from maggma.stores import MemoryStore

Expand All @@ -38,7 +38,7 @@ def memory_jobstore():
return store


@pytest.fixture()
@pytest.fixture
def memory_data_jobstore():
from maggma.stores import MemoryStore

Expand All @@ -50,7 +50,7 @@ def memory_data_jobstore():
return store


@pytest.fixture()
@pytest.fixture
def clean_dir():
import os
import shutil
Expand Down Expand Up @@ -85,7 +85,7 @@ def lpad(database, debug_mode):
lpad.db[coll].drop()


@pytest.fixture()
@pytest.fixture
def no_pydot(monkeypatch):
import builtins

Expand All @@ -99,7 +99,7 @@ def mocked_import(name, *args, **kwargs):
monkeypatch.setattr(builtins, "__import__", mocked_import)


@pytest.fixture()
@pytest.fixture
def no_matplotlib(monkeypatch):
import builtins

Expand Down
Loading

0 comments on commit b162636

Please sign in to comment.