<jupyter_start><jupyter_text>utils<jupyter_code>import vectorbt as vbt

from vectorbt.utils import checks, config, decorators, attr
import numpy as np
import pandas as pd
from numba import njit
v1 = 0
a1 = np.array([1])
a2 = np.array([1, 2, 3])
a3 = np.array([[1, 2, 3]])
a4 = np.array([[1], [2], [3]])
a5 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sr1 = pd.Series([1], index=pd.Index(['x1'], name='i1'))
print(sr1)
sr2 = pd.Series([1, 2, 3], index=pd.Index(['x2', 'y2', 'z2'], name='i2'))
print(sr2)
df1 = pd.DataFrame(
    [[1]], 
    index=pd.Index(['x3'], name='i3'), 
    columns=pd.Index(['a3'], name='c3'))
print(df1)
df2 = pd.DataFrame(
    [[1], [2], [3]], 
    index=pd.Index(['x4', 'y4', 'z4'], name='i4'), 
    columns=pd.Index(['a4'], name='c4'))
print(df2)
df3 = pd.DataFrame(
    [[1, 2, 3]], 
    index=pd.Index(['x5'], name='i5'), 
    columns=pd.Index(['a5', 'b5', 'c5'], name='c5'))
print(df3)
df4 = pd.DataFrame(
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]], 
    index=pd.Index(['x6', 'y6', 'z6'], name='i6'), 
    columns=pd.Index(['a6', 'b6', 'c6'], name='c6'))
print(df4)

multi_i = pd.MultiIndex.from_arrays([['x7', 'y7', 'z7'], ['x8', 'y8', 'z8']], names=['i7', 'i8']) 
multi_c = pd.MultiIndex.from_arrays([['a7', 'b7', 'c7'], ['a8', 'b8', 'c8']], names=['c7', 'c8'])
df5 = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=multi_i, columns=multi_c)
print(df5)<jupyter_output>i1
x1    1
dtype: int64
i2
x2    1
y2    2
z2    3
dtype: int64
c3  a3
i3    
x3   1
c4  a4
i4    
x4   1
y4   2
z4   3
c5  a5  b5  c5
i5            
x5   1   2   3
c6  a6  b6  c6
i6            
x6   1   2   3
y6   4   5   6
z6   7   8   9
c7    a7 b7 c7
c8    a8 b8 c8
i7 i8         
x7 x8  1  2  3
y7 y8  4  5  6
z7 z8  7  8  9<jupyter_text>config<jupyter_code>conf = config.Config({'a': 0, 'b': {'c': 1}}, frozen_keys=False)
conf['b']['d'] = 2

conf = config.Config({'a': 0, 'b': {'c': 1}}, frozen_keys=True)
conf['a'] = 2

try:
    conf['d'] = 2
except Exception as e:
    print(e)

try:
    conf.update(d=2)
except Exception as e:
    print(e)
    
conf.update(d=2, force=True)
conf = config.Config({'a': 0, 'b': {'c': 1}}, readonly=True)

try:
    conf['a'] = 2
except Exception as e:
    print(e)
    
try:
    del conf['a']
except Exception as e:
    print(e)
    
try:
    conf.pop('a')
except Exception as e:
    print(e)
    
try:
    conf.popitem()
except Exception as e:
    print(e)
    
try:
    conf.clear()
except Exception as e:
    print(e)
    
try:
    conf.update(a=2)
except Exception as e:
    print(e)
    
print(conf.merge_with(dict(b=dict(d=2))))
print(config.merge_dicts({'a': 1}, {'b': 2}))
print(config.merge_dicts({'a': 1}, {'a': 2}))
print(config.merge_dicts({'a': {'b': 2}}, {'a': {'c': 3}}))
print(config.merge_dicts({'a': {'b': 2}}, {'a': {'b': 3}}))
class H(config.Configured):
    def __init__(self, a, b=2, **kwargs):
        super().__init__(a=a, b=b, **kwargs)
        
print(H(1).config)
print(H(1).copy(b=3).config)
print(H(1).copy(c=4).config)<jupyter_output>Config({
    "a": 1,
    "b": 2
})
Config({
    "a": 1,
    "b": 3
})
Config({
    "a": 1,
    "b": 2,
    "c": 4
})<jupyter_text>decorators<jupyter_code>class G():
    @decorators.class_or_instancemethod
    def g(self_or_cls):
        if isinstance(self_or_cls, type):
            print("class")
        else:
            print("instance")
            
G.g()
G().g()
class G():
    @decorators.cached_property(hello="world", hello2="world2")
    def cache_me(self): return np.random.uniform(size=(10000, 10000))
    
G.cache_me.flags
g = G()
%time _ = g.cache_me
%time _ = g.cache_me
dir(g)
G.cache_me.clear_cache(g)
%time _ = g.cache_me
%time _ = g.cache_me
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(instance=g, func='cache_me'))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(func='cache_me'))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(instance=g))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(cls=G))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(cls='G'))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(cls='g'))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(flags={'hello': 'world'}))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(flags={'hello': 'world', 'hello2': 'world2'}))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(flags={'hello': 'world', 'hello2': 'world2', 'hello3': 'world3'}))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(instance=g, func='cache_me'))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(func='cache_me'))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(instance=g))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(cls=G))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(cls='G'))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(flags={'hello': 'world'}))
%time _ = g.cache_me
%time _ = g.cache_me
vbt.settings.caching.reset()
class G():
    @decorators.cached_method(hello="world", hello2="world2")
    def cache_me(self, a): return np.random.uniform(size=(10000, 10000)) * a

G.cache_me.flags
g = G()
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
dir(g)
G.cache_me.clear_cache(g)
%time _ = g.cache_me(2)
G.cache_me.clear_cache(g)
G.cache_me.disabled = True
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
G.cache_me.disabled = False
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(func=g.cache_me))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(instance=g, func='cache_me'))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(func='cache_me'))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(instance=g))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(cls=G))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(cls='G'))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(cls='g'))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(flags={'hello': 'world'}))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(flags={'hello': 'world', 'hello2': 'world2'}))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['blacklist'].append(vbt.CacheCondition(flags={'hello': 'world', 'hello2': 'world2', 'hello3': 'world3'}))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(instance=g, func='cache_me'))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(func='cache_me'))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(instance=g))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(cls=G))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(cls='G'))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
G.cache_me.clear_cache(g)
vbt.settings.caching['enabled'] = False
vbt.settings.caching['whitelist'].append(vbt.CacheCondition(flags={'hello': 'world'}))
%time _ = g.cache_me(2)
%time _ = g.cache_me(2)
vbt.settings.caching.reset()
# Non-hashable arguments won't cache
%time _ = g.cache_me(np.asarray(2))
%time _ = g.cache_me(np.asarray(2))<jupyter_output>CPU times: user 1.23 s, sys: 161 ms, total: 1.39 s
Wall time: 1.39 s
CPU times: user 1.27 s, sys: 302 ms, total: 1.57 s
Wall time: 1.78 s<jupyter_text>checks<jupyter_code>print(checks.is_series(v1))
print(checks.is_series(a1))
print(checks.is_series(sr1))
print(checks.is_series(df1))
print(checks.is_frame(v1))
print(checks.is_frame(a1))
print(checks.is_frame(sr1))
print(checks.is_frame(df1))
print(checks.is_pandas(v1))
print(checks.is_pandas(a1))
print(checks.is_pandas(sr1))
print(checks.is_pandas(df1))
print(checks.is_any_array(v1))
print(checks.is_any_array(a1))
print(checks.is_any_array(sr1))
print(checks.is_any_array(df1))
print(checks.is_numba_func(lambda x: x))
print(checks.is_numba_func(njit(lambda x: x)))
print(checks.is_hashable(2))
print(checks.is_hashable(np.asarray(2)))
checks.assert_in(0, (0, 1))
checks.assert_numba_func(njit(lambda x: x))
checks.assert_not_none(v1)
checks.assert_type(v1, int)
checks.assert_type(a1, np.ndarray)
checks.assert_type(sr1, (np.ndarray, pd.Series))
checks.assert_type_equal(v1, v1)
checks.assert_type_equal(a1, a2)
checks.assert_type_equal(sr1, sr1)
checks.assert_type_equal(df1, df2)
checks.assert_dtype(a1, np.integer)
checks.assert_dtype_equal(v1, a1)
checks.assert_dtype_equal(a1, df1)
checks.assert_dtype_equal(df1, df2)
checks.assert_dtype_equal(df2, df3)
checks.assert_ndim(v1, 0)
checks.assert_ndim(a1, 1)
checks.assert_ndim(df1, 2)
checks.assert_len_equal([[1]], [[2]])
checks.assert_shape_equal(a1, sr1)
checks.assert_shape_equal(df2, df4, axis=0)
checks.assert_shape_equal(df3, df4, axis=1)
checks.assert_shape_equal(df2, df3, axis=(0, 1))
checks.assert_index_equal(df3.index, df3.index)
checks.assert_meta_equal(df3, df3)
checks.assert_array_equal(df3, df3)
checks.assert_level_not_exists(df3.columns, 'a')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1919452275473218, 'fraction_numerical': 0.032138542086186064, 'mean_word_length': 5.347648261758692, 'pattern_counts': {'":': 7, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '205276', 'license': 'Apache-2.0', 'n_tokens_mistral': 5428, 'n_tokens_neox': 5287, 'n_words': 826, 'path': 'tests/notebooks/utils.ipynb', 'repo_name': 'lmservas/vectorbt', 'size': '12415'}