<jupyter_start><jupyter_text>Download if file is not in folder.<jupyter_code>from litedict import SQLDict
TEST_1 = "key_test_1"
TEST_2 = "key_test_2"<jupyter_output><empty_output><jupyter_text>Basic functionality<jupyter_code>d = SQLDict(":memory:")

d[TEST_1] = "asdfoobar"

assert d[TEST_1] == "asdfoobar"

del d[TEST_1]

assert d.get(TEST_1, None) is None<jupyter_output><empty_output><jupyter_text>Glob matching<jupyter_code>d[TEST_1] = "asdfoobar"

d[TEST_2] = "foobarasd"

d["key_testx_3"] = "barasdfoo"

assert d.glob("key_test*") == ["asdfoobar", "foobarasd", "barasdfoo"]

assert d.glob("key_test_?") == ["asdfoobar", "foobarasd"]

assert d.glob("key_tes[tx]*") == ["asdfoobar", "foobarasd", "barasdfoo"]<jupyter_output><empty_output><jupyter_text>Numbers<jupyter_code>d[TEST_1] = 1

d[TEST_2] = 2

assert d[TEST_1] + d[TEST_2] == 3
with d.transaction():
    d["asd"] = "efg"
    d["foo"] = "bar"
    assert d.conn.in_transaction
try:
    with d.transaction():
        d["failed"] = "no"

        assert d.conn.in_transaction

        raise Exception
except:
    # check the transaction succesfully rolled back
    assert d.get("failed") is None<jupyter_output><empty_output><jupyter_text>Benchmarks<jupyter_code>from string import ascii_lowercase, printable
from random import choice
import random


def random_string(string_length=10, fuzz=False, space=False):
    """Generate a random string of fixed length """
    letters = ascii_lowercase
    letters = letters + " " if space else letters
    if fuzz:
        letters = printable
    return "".join(choice(letters) for i in range(string_length))
import gc

import pickle

import json<jupyter_output><empty_output><jupyter_text>**Pickle**<jupyter_code>d = SQLDict(
    ":memory:",
    encoder=lambda x: pickle.dumps(x).hex(),
    decoder=lambda x: pickle.loads(bytes.fromhex(x)),
)
gc.collect()
%%timeit -n20000 -r10

d[random_string(8)] = random_string(50)

d.get(random_string(8), None)<jupyter_output>82.2 µs ± 6.66 µs per loop (mean ± std. dev. of 10 runs, 20000 loops each)<jupyter_text>**Noop**<jupyter_code>d = SQLDict(
    ":memory:",
    encoder=lambda x: x,
    decoder=lambda x: x,
)
gc.collect()
%%timeit -n20000 -r10

d[random_string(8)] = random_string(50)

d.get(random_string(8), None)<jupyter_output>82.4 µs ± 4.83 µs per loop (mean ± std. dev. of 10 runs, 20000 loops each)<jupyter_text>**JSON**<jupyter_code>d = SQLDict(
    ":memory:",
    encoder=lambda x: json.dumps(x),
    decoder=lambda x: json.loads(x),
)
gc.collect()
%%timeit -n20000 -r10

d[random_string(8)] = random_string(50)

d.get(random_string(8), None)<jupyter_output>95.4 µs ± 24.5 µs per loop (mean ± std. dev. of 10 runs, 20000 loops each)<jupyter_text>**Pickle Python obj**<jupyter_code>d = SQLDict(
    ":memory:",
    encoder=lambda x: pickle.dumps(x).hex(),
    decoder=lambda x: pickle.loads(bytes.fromhex(x)),
)
gc.collect()
class C:
    def __init__(self, x):
        self.x = x

    def pp(self):
        return x

    def f(self):
        def _f(y):
            return y * self.x ** 2

        return _f
%%timeit -n20000 -r10

d[random_string(8)] = C(random.randint(1, 200))

d.get(random_string(8), None)<jupyter_output>58.6 µs ± 21.2 µs per loop (mean ± std. dev. of 10 runs, 20000 loops each)<jupyter_text>**Dictionary**<jupyter_code>d = {}
gc.collect()
%%timeit -n20000 -r10

d[random_string(8)] = random_string(50)

d.get(random_string(8), None)<jupyter_output>59.4 µs ± 2.59 µs per loop (mean ± std. dev. of 10 runs, 20000 loops each)
{'fraction_non_alphanumeric': 0.15305539691604797, 'fraction_numerical': 0.04083380925185608, 'mean_word_length': 4.613782051282051, 'pattern_counts': {'":': 5, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '348105', 'license': 'MIT', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1388, 'n_words': 304, 'path': 'tests.ipynb', 'repo_name': 'litements/litedict', 'size': '3502'}