<jupyter_start><jupyter_text>TinyDBTinyDB is a small and lightweight [NoSQL](http://en.wikipedia.org/wiki/NoSQL) database framework based on simple JSON files. SourceOfficial Website:- [getting started](http://tinydb.readthedocs.org/en/latest/getting-started.html)- [advanced usage](http://tinydb.readthedocs.org/en/latest/usage.html) CodeSome examples to create a database and insert, delete and seach for elements. BasicsGenerate new database. If `path` leads to an existing file, the data is read. Otherwise a new database is created.<jupyter_code>path = './testData.json'
from tinydb import TinyDB, where
db = TinyDB(path)<jupyter_output><empty_output><jupyter_text>Insert some data into `db`.<jupyter_code>print(db.insert({'a':1, 'b':3})) # return the elements id [1,2,..]
db.all() # returns list of dicts<jupyter_output>159<jupyter_text>Fill with some data ([iris](http://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html)).<jupyter_code>db.purge()

from sklearn import datasets
iris = datasets.load_iris()
data = iris.data
keys = iris.feature_names
print('== data ==\n', data[:5], '\n ...')
print('== keys ==\n', keys)
if(0 == len(db)):
    for d in data:
        db.insert({
            keys[0]: d[0],
            keys[1]: d[1],
            keys[2]: d[2],
            keys[3]: d[3]
        })
# or
# db.insert_multiple([ {}, {}, ...]) # returns list of ids
db.search(where(keys[0]) >= 7.6 and where(keys[1]) >= 4.2)<jupyter_output><empty_output><jupyter_text>Error Handling<jupyter_code>try:
    r = db.search(where(keys[0]) > 1000)[0]
    print(r)
except:
    print('Not in db.')
r = db.get(where(keys[0]) > 1000)
if not r:
    print('Not in db.') # r is None<jupyter_output>Not in db.<jupyter_text>If the actual data is irrelevant, to check for the existance of an element, use `contains` or `count`.<jupyter_code>db.contains(where(keys[2]) == 1)
db.count(where(keys[2]) == 1)<jupyter_output><empty_output><jupyter_text>IDs<jupyter_code>elem = db.get(where(keys[2]) == 1)
elem.eid
if db.contains(eids=[11, 12]):
    e1 = db.get(eid=11)
    e2 = db.get(eid=12)
    db.remove(eids=[11, 12])
    db.insert_multiple([e1, e2])

print(len(db))    
# db.remove(eids=list(arange(70, 75)))
# print(len(db))<jupyter_output>150<jupyter_text>Regex and nested queries<jupyter_code># db.remove(where('field').has('name').has('last_name') == 'Doe')
db.insert({'field': {'name': {'first_name': 'John', 'last_name': 'Doe'}}})
# print(db.search(where('field.name.last_name') == 'Doe'))
# print(db.search(where('field.name.last_name').matches('[0-9]*')),'\n')

db.remove(where('field').any(where('val') == 1))
db.insert({'field': [{'val': 1}, {'val': 2}, {'val': 3}]})
print(db.search(where('field').any(where('val') == 1)))<jupyter_output>[{'field': [{'val': 1}, {'val': 2}, {'val': 3}]}]
{'fraction_non_alphanumeric': 0.2017857142857143, 'fraction_numerical': 0.026071428571428572, 'mean_word_length': 5.07592190889371, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '520528', 'license': 'CC0-1.0', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1053, 'n_words': 217, 'path': 'ipynb/[tinydb] First steps.ipynb', 'repo_name': 'mamrehn/machine-learning-tutorials', 'size': '2800'}