#820 Add check_call_in_cache method to check cache without calling function (as per note in the code). Add some tests.

Open David Cottrell cottrell
Showing 2 of 4 files from the diff.

@@ -19,7 +19,6 @@
Loading
19 19
import traceback
20 20
import warnings
21 21
import inspect
22 -
import sys
23 22
import weakref
24 23
25 24
from tokenize import open as open_py_source
@@ -33,7 +32,6 @@
Loading
33 32
from ._store_backends import StoreBackendBase, FileSystemStoreBackend
34 33
35 34
36 -
37 35
FIRST_LINE_TEXT = "# first line:"
38 36
39 37
# TODO: The following object should have a data store object as a sub
@@ -136,7 +134,6 @@
Loading
136 134
            "supported by joblib. Returning None instead.".format(
137 135
                location.__class__.__name__), UserWarning)
138 136
139 -
140 137
    return None
141 138
142 139
@@ -361,6 +358,12 @@
Loading
361 358
        # Argument "warn" is for compatibility with MemorizedFunc.clear
362 359
        pass
363 360
361 +
    def call(self, *args, **kwargs):
362 +
        return self.func(*args, **kwargs)
363 +
364 +
    def check_call_in_cache(self, *args, **kwargs):
365 +
        return False
366 +
364 367
365 368
###############################################################################
366 369
# class `MemorizedFunc`
@@ -606,6 +609,21 @@
Loading
606 609
607 610
        return state
608 611
612 +
    def check_call_in_cache(self, *args, **kwargs):
613 +
        """Check if function call is in the memory cache.
614 +
615 +
        Does not call the function or do any work besides func inspection
616 +
        and arg hashing.
617 +
618 +
        Returns
619 +
        -------
620 +
        is_called_in_cache: bool
621 +
            whether or not the function has been cached for the input
622 +
            arguments that have been passed.
623 +
        """
624 +
        func_id, args_id = self._get_output_identifiers(*args, **kwargs)
625 +
        return self.store_backend.contains_item((func_id, args_id))
626 +
609 627
    # ------------------------------------------------------------------------
610 628
    # Private interface
611 629
    # ------------------------------------------------------------------------
@@ -683,8 +701,8 @@
Loading
683 701
                extract_first_line(
684 702
                    self.store_backend.get_cached_func_code([func_id]))
685 703
        except (IOError, OSError):  # some backend can also raise OSError
686 -
                self._write_func_code(func_code, first_line)
687 -
                return False
704 +
            self._write_func_code(func_code, first_line)
705 +
            return False
688 706
        if old_func_code == func_code:
689 707
            return True
690 708
@@ -821,8 +839,6 @@
Loading
821 839
                          % this_duration, stacklevel=5)
822 840
        return metadata
823 841
824 -
    # XXX: Need a method to check if results are available.
825 -
826 842
    # ------------------------------------------------------------------------
827 843
    # Private `object` interface
828 844
    # ------------------------------------------------------------------------

@@ -582,6 +582,19 @@
Loading
582 582
    gp(1)
583 583
584 584
585 +
def test_check_call_in_cache(tmpdir):
586 +
    for func in (MemorizedFunc(f, tmpdir.strpath),
587 +
                 Memory(location=tmpdir.strpath, verbose=0).cache(f)):
588 +
        result = func.check_call_in_cache(2)
589 +
        assert not result
590 +
        assert isinstance(result, bool)
591 +
        assert func(2) == 5
592 +
        result = func.check_call_in_cache(2)
593 +
        assert result
594 +
        assert isinstance(result, bool)
595 +
        func.clear()
596 +
597 +
585 598
def test_call_and_shelve(tmpdir):
586 599
    # Test MemorizedFunc outputting a reference to cache.
587 600

Learn more Showing 47 files with coverage changes found.

Changes in joblib/test/test_logger.py
+13
+1
Loading file...
Changes in joblib/_dask.py
+190
+11
Loading file...
Changes in joblib/test/test_numpy_pickle_utils.py
+7
Loading file...
Changes in joblib/test/test_testing.py
+29
Loading file...
Changes in joblib/__init__.py
+18
Loading file...
Changes in joblib/test/test_my_exceptions.py
+24
+3
Loading file...
Changes in joblib/_store_backends.py
+177
+18
Loading file...
Changes in joblib/_parallel_backends.py
+243
+13
Loading file...
Changes in joblib/format_stack.py
+12
Loading file...
Changes in joblib/test/test_module.py
+17
Loading file...
Changes in joblib/compressor.py
+271
+33
Loading file...
Changes in joblib/test/test_func_inspect.py
+116
+12
Loading file...
Changes in joblib/_deprecated_my_exceptions.py
+40
+7
Loading file...
Changes in joblib/test/testutils.py
+3
+3
Loading file...
Changes in joblib/test/test_init.py
+5
Loading file...
Changes in joblib/test/test_numpy_pickle.py
+563
+40
Loading file...
Changes in joblib/test/test_hashing.py
+189
+2
Loading file...
Changes in joblib/test/test_func_inspect_special_encoding.py
+1
+1
Loading file...
Changes in joblib/testing.py
+38
+2
Loading file...
Changes in joblib/numpy_pickle.py
+197
+4
Loading file...
Changes in joblib/test/test_backports.py
+26
Loading file...
Changes in joblib/test/test_format_stack.py
+68
+1
Loading file...
Changes in joblib/test/test_disk.py
+37
Loading file...
Changes in joblib/test/test_dask.py
+263
+3
Loading file...
Changes in joblib/parallel.py
+354
+13
Loading file...
Changes in joblib/test/__init__.py
+2
Loading file...
Changes in joblib/hashing.py
+103
+10
Loading file...
Changes in joblib/executor.py
+43
Loading file...
Changes in joblib/test/test_numpy_pickle_compat.py
+10
Loading file...
Changes in joblib/numpy_pickle_utils.py
+76
+6
Loading file...
Changes in joblib/_multiprocessing_helpers.py
+20
+5
Loading file...
Changes in joblib/numpy_pickle_compat.py
+90
+9
Loading file...
Changes in joblib/test/test_store_backends.py
+32
+3
Loading file...
Changes in joblib/my_exceptions.py
+14
Loading file...
Changes in joblib/test/common.py
+45
+13
Loading file...
Changes in joblib/test/test_deprecated_objects.py
+19
Loading file...
Changes in joblib/_deprecated_format_stack.py
+148
+57
Loading file...
Changes in joblib/test/test_memory.py
+683
+10
Loading file...
Changes in joblib/test/test_parallel.py
+822
+24
Loading file...
Changes in joblib/memory.py
+351
+15
Loading file...
Changes in joblib/pool.py
+108
+15
Loading file...
Changes in joblib/func_inspect.py
+155
+13
Loading file...
Changes in joblib/logger.py
+66
+10
Loading file...
Changes in joblib/disk.py
+58
+5
Loading file...
Changes in joblib/test/test_memmapping.py
+522
+4
Loading file...
Changes in joblib/_memmapping_reducer.py
+256
+9
Loading file...
Changes in joblib/backports.py
+32
+6
Loading file...
Files Coverage
joblib 0.56% 94.49%
Project Totals (47 files) 94.49%
Loading