14
14
15
15
from attrs import define
16
16
from attrs import field
17
+ from typing_extensions import deprecated
17
18
from upath import UPath
18
19
from upath ._stat import UPathStatResult
19
20
28
29
from _pytask .typing import no_default
29
30
30
31
if TYPE_CHECKING :
32
+ from io import BufferedReader
33
+ from io import BufferedWriter
34
+
31
35
from _pytask .mark import Mark
32
36
from _pytask .models import NodeInfo
33
37
from _pytask .tree_util import PyTree
40
44
"PythonNode" ,
41
45
"Task" ,
42
46
"TaskWithoutPath" ,
47
+ "get_state_of_path" ,
43
48
]
44
49
45
50
@@ -145,7 +150,7 @@ def signature(self) -> str:
145
150
146
151
def state (self ) -> str | None :
147
152
"""Return the state of the node."""
148
- return _get_state (self .path )
153
+ return get_state_of_path (self .path )
149
154
150
155
def execute (self , ** kwargs : Any ) -> Any :
151
156
"""Execute the task."""
@@ -188,7 +193,7 @@ def state(self) -> str | None:
188
193
The state is given by the modification timestamp.
189
194
190
195
"""
191
- return _get_state (self .path )
196
+ return get_state_of_path (self .path )
192
197
193
198
def load (self , is_product : bool = False ) -> Path : # noqa: ARG002
194
199
"""Load the value."""
@@ -310,12 +315,18 @@ class PickleNode(PPathNode):
310
315
The path to the file.
311
316
attributes: dict[Any, Any]
312
317
A dictionary to store additional information of the task.
318
+ serializer
319
+ A function to serialize the object. Defaults to :func:`pickle.dump`.
320
+ deserializer
321
+ A function to deserialize the object. Defaults to :func:`pickle.load`.
313
322
314
323
"""
315
324
316
325
path : Path
317
326
name : str = ""
318
327
attributes : dict [Any , Any ] = field (factory = dict )
328
+ serializer : Callable [[Any , BufferedWriter ], None ] = field (default = pickle .dump )
329
+ deserializer : Callable [[BufferedReader ], Any ] = field (default = pickle .load )
319
330
320
331
@property
321
332
def signature (self ) -> str :
@@ -332,17 +343,17 @@ def from_path(cls, path: Path) -> PickleNode:
332
343
return cls (name = path .as_posix (), path = path )
333
344
334
345
def state (self ) -> str | None :
335
- return _get_state (self .path )
346
+ return get_state_of_path (self .path )
336
347
337
348
def load (self , is_product : bool = False ) -> Any :
338
349
if is_product :
339
350
return self
340
351
with self .path .open ("rb" ) as f :
341
- return pickle . load (f ) # noqa: S301
352
+ return self . deserializer (f )
342
353
343
354
def save (self , value : Any ) -> None :
344
355
with self .path .open ("wb" ) as f :
345
- pickle . dump (value , f )
356
+ self . serializer (value , f )
346
357
347
358
348
359
@define (kw_only = True )
@@ -387,7 +398,7 @@ def collect(self) -> list[Path]:
387
398
return list (self .root_dir .glob (self .pattern )) # type: ignore[union-attr]
388
399
389
400
390
- def _get_state (path : Path ) -> str | None :
401
+ def get_state_of_path (path : Path ) -> str | None :
391
402
"""Get state of a path.
392
403
393
404
A simple function to handle local and remote files.
@@ -411,3 +422,13 @@ def _get_state(path: Path) -> str | None:
411
422
return stat .as_info ().get ("ETag" , "0" )
412
423
msg = "Unknown stat object."
413
424
raise NotImplementedError (msg )
425
+
426
+
427
+ @deprecated ("Use 'pytask.get_state_of_path' instead." )
428
+ def _get_state (path : Path ) -> str | None :
429
+ """Get state of a path.
430
+
431
+ A simple function to handle local and remote files.
432
+
433
+ """
434
+ return get_state_of_path (path )
0 commit comments