<commit_before># coding: utf-8

from ljson.base.generic import Header
from ljson.base.mem import Table
from ljson.base import disk
from ljson.convert.csv import table2csv, csv2table
from io import StringIO
import json

header = Header({"id": {"type": "int", "modifiers":["unique"]}, 
		"name": {"type": "str", "modifiers": []}, 
		"object":{"type": "json", "modifiers": []}})
table = Table(header, [{"id": 1, "name": "foo", "object": {"next": 2}}, 
			{"id": 2, "name": "bar", "object": {"next": 3}}, 
			{"id": 3, "name": "bar", "object": {"next": 1}}])


print("TESTING QUERIES")
print(table[{"id": 1}]["name"])
print({"id": 1} in table)

print(table[{"id": 3}]["name"])
table[{"id": 3}]["name"] = "baz"
print(list(table[{"id": 3}]))

print("TESTING built-in functions")
print(max(table, key = lambda r: r["id"]))

print("TESTING setter")
table[{"id": 3}]["name"] = "bar"
print(table[{"name": "bar"}]["id"])

print("TESTING READ/WRITE")
fout = open("test.ljson", "w")
table.save(fout)
fout.close()
#
print()
#
f = open("test.ljson", "r+")
#
disktable = disk.Table.from_file(f)
#
disktable[{"id": 3}]["name"] = "bongo"
#
for row in disktable:
	print(row)

print("TESTING CONVERSION")
fout = StringIO()

table2csv(table, fout)
fout.seek(0)
print(fout.read())
fout.seek(0)
print(list(csv2table(fout, types = {"id": "int", "name": "str", "object": "json"})))

print("DONE")<commit_msg>test for slapdash<commit_after># coding: utf-8
from ljson.slapdash.mem import *
table = Table(SlapdashHeader({}), [])
table.additem({"foo": "bar", "cnt": 12, "store": True})
table.additem({"foo": ["bar"], "cnt": 12, "store": True})
table.additem({"foo": 23, "cnt": 12, "store": True})
get_ipython().magic('save test.py 1-6')
table.header.descriptor["field_count"]["foo"] / table.header.descriptor["length"]
table.additem({"bar": "foo", "cnt": 12, "store": True})
table.header.descriptor["field_count"]["foo"] / table.header.descriptor["length"]
{'fraction_non_alphanumeric': 0.2568759730150493, 'fraction_numerical': 0.017125064867669952, 'mean_word_length': 6.4728682170542635, 'pattern_counts': {'":': 44, '<': 3, '<?xml version=': 0, '>': 3, '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}}
{'dir': 'git-commits-cleaned', 'id': '4747431', 'n_tokens_mistral': 772, 'n_tokens_neox': 737, 'n_words': 170}