Пример #1
0
    def test_numpy(self):
        np1 = np.zeros(10)
        np2 = np.zeros(11)
        np3 = np.zeros(10)

        self.assertEqual(get_hash(np1), get_hash(np3))
        self.assertNotEqual(get_hash(np1), get_hash(np2))
Пример #2
0
    def test_pandas(self):
        df1 = pd.DataFrame({'foo': [12]})
        df2 = pd.DataFrame({'foo': [42]})
        df3 = pd.DataFrame({'foo': [12]})

        self.assertEqual(get_hash(df1), get_hash(df3))
        self.assertNotEqual(get_hash(df1), get_hash(df2))
Пример #3
0
    def test_partial(self):
        p1 = functools.partial(int, base=2)
        p2 = functools.partial(int, base=3)
        p3 = functools.partial(int, base=2)

        self.assertEqual(get_hash(p1), get_hash(p3))
        self.assertNotEqual(get_hash(p1), get_hash(p2))
Пример #4
0
 def test_file_position(self):
     with open(__file__, 'r') as f:
         h1 = get_hash(f)
         self.assertEqual(h1, get_hash(f))
         f.readline()
         self.assertNotEqual(h1, get_hash(f))
         f.seek(0)
         self.assertEqual(h1, get_hash(f))
Пример #5
0
    def test_builtins(self):
        """Tes code with builtins."""

        def code_with_print():
            print(12)

        def code_with_type():
            type(12)

        self.assertNotEqual(get_hash(code_with_print), get_hash(code_with_type))
Пример #6
0
    def test_external_module(self):
        """Test code that references an external module."""

        def call_altair_concat():
            return alt.vegalite.v3.api.concat()

        def call_altair_layer():
            return alt.vegalite.v3.api.layer()

        self.assertNotEqual(get_hash(call_altair_concat), get_hash(call_altair_layer))
Пример #7
0
    def test_lambdas(self):
        """Test code with different lambdas produces different hashes."""

        v42 = 42
        v123 = 123

        def f1():
            lambda x: v42

        def f2():
            lambda x: v123

        self.assertNotEqual(get_hash(f1), get_hash(f2))
Пример #8
0
    def test_streamlit(self):
        """Test hashing streamlit functions."""
        def f():
            st.write("Hello")

        def g():
            st.write("World")

        def h():
            st.write("Hello")

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #9
0
    def test_defaults(self):
        """Test the hash of functions with defaults."""
        def f(x=42):
            return x

        def g(x=12):
            return x

        def h(x=42):
            return x

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #10
0
    def test_rename(self):
        """Test the hash of function with renamed variables."""
        def f(x, y):
            return x + y

        def g(x, y):
            return y + x

        def h(y, x):
            return y + x

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #11
0
    def test_simple(self):
        """Test the hash of simple functions."""
        def f(x):
            return x * x

        def g(x):
            return x + x

        def h(x):
            return x*x

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #12
0
    def test_lambdas_calls(self):
        """Test code with lambdas that call functions."""

        def f_lower():
            lambda x: x.lower()

        def f_upper():
            lambda x: x.upper()

        def f_lower2():
            lambda x: x.lower()

        self.assertNotEqual(get_hash(f_lower), get_hash(f_upper))
        self.assertEqual(get_hash(f_lower), get_hash(f_lower2))
Пример #13
0
    def test_coref(self):
        """Test code that references itself."""

        def f(x):
            return f(x)

        def g(x):
            return g(x) + 1

        def h(x):
            return h(x)

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #14
0
    def test_value(self):
        """Test the hash of functions with values."""
        def f():
            x = 42
            return x

        def g():
            x = 12
            return x

        def h():
            y = 42
            return y

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #15
0
def _write_to_mem_cache(key, value, allow_output_mutation, hash_funcs):
    if allow_output_mutation:
        hash = None
    else:
        hash = get_hash(value, hash_funcs=hash_funcs)

    _mem_cache[key] = CacheEntry(value=value, hash=hash)
Пример #16
0
        def hash_prog_3():
            o = Foo()

            def f():
                return o.get_x()

            return get_hash(f)
Пример #17
0
    def test_cached(self):
        """Test decorated functions."""
        @st.cache
        def f():
            return 42

        @st.cache
        def g():
            return 12

        @st.cache
        def h():
            return 42

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #18
0
        def hash_prog_2():
            o = Foo()

            def f():
                return o.get_y()

            return get_hash(f)
Пример #19
0
    def test_referenced(self):
        """Test the hash of functions that reference values."""

        x = 42
        y = 123

        def f():
            return x

        def g():
            return y

        def h():
            return x

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #20
0
    def test_import(self):
        """Test code that imports module."""

        def f():
            import numpy
            return numpy

        def g():
            import pandas
            return pandas

        def n():
            import foobar
            return foobar

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertNotEqual(get_hash(f), get_hash(n))
Пример #21
0
    def test_multiple(self):
        """Test code that references multiple objects."""

        x = 12
        y = 13
        z = 14

        def f():
            return x + z

        def g():
            return y + z

        def h():
            return x + z

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #22
0
    def test_files(self):
        temp1 = tempfile.NamedTemporaryFile()
        temp2 = tempfile.NamedTemporaryFile()

        with open(__file__, 'r') as f:
            with open(__file__, 'r') as g:
                self.assertEqual(get_hash(f), get_hash(g))

            self.assertNotEqual(get_hash(f), get_hash(temp1))

        self.assertEqual(get_hash(temp1), get_hash(temp1))
        self.assertNotEqual(get_hash(temp1), get_hash(temp2))
Пример #23
0
    def test_higher_order(self):
        """Test hashing higher order functions."""

        def f(x):
            def func(v):
                return v**x
            return func

        def g(x):
            def func(v):
                return v*x
            return func

        def h(x):
            def func(v):
                return v**x
            return func

        self.assertNotEqual(get_hash(f), get_hash(g))
Пример #24
0
        def hash_prog_1():
            x = 12

            def g():
                return x

            def f():
                return g()

            return get_hash(f)
Пример #25
0
        def hash_prog_2():
            x = 42

            def g():
                return x

            def f():
                return g()

            return get_hash(f)
Пример #26
0
    def test_dict_reference(self):
        """Test code with lambdas that call a dictionary."""

        a = {
            'foo': 42,
            'bar': {
                'baz': 12
            }
        }

        def f():
            return a['bar']['baz']

        def g():
            return a['foo']

        def h():
            return a['bar']['baz']

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))
Пример #27
0
def _read_from_mem_cache(key, ignore_hash):
    if key in _mem_cache:
        entry = _mem_cache[key]

        if ignore_hash or get_hash(entry.value) == entry.hash:
            LOGGER.debug('Memory cache HIT: %s', type(entry.value))
            return entry.value, entry.args_mutated
        else:
            LOGGER.debug('Cache object was mutated: %s', key)
            raise CachedObjectWasMutatedError()
    else:
        LOGGER.debug('Memory cache MISS: %s', key)
        raise CacheKeyNotFoundError('Key not found in mem cache')
Пример #28
0
def _read_from_mem_cache(key, allow_output_mutation):
    if key in _mem_cache:
        entry = _mem_cache[key]

        if allow_output_mutation or get_hash(entry.value) == entry.hash:
            LOGGER.debug("Memory cache HIT: %s", type(entry.value))
            return entry.value, entry.args_mutated
        else:
            LOGGER.debug("Cache object was mutated: %s", key)
            raise CachedObjectWasMutatedError()
    else:
        LOGGER.debug("Memory cache MISS: %s", key)
        raise CacheKeyNotFoundError("Key not found in mem cache")
Пример #29
0
        def hash_prog_2():
            class Foo():
                x = 42

                def get_x(self):
                    return self.x

            o = Foo()

            def f():
                return o.get_x()

            return get_hash(f)
Пример #30
0
    def test_decorated(self):
        """Test decorated functions."""

        def do(func):
            @functools.wraps(func)
            def wrapper_do(*args, **kwargs):
                return func(*args, **kwargs)
            return wrapper_do

        @do
        def f():
            return 42

        @do
        def g():
            return 12

        @do
        def h():
            return 42

        self.assertNotEqual(get_hash(f), get_hash(g))
        self.assertEqual(get_hash(f), get_hash(h))