<jupyter_start><jupyter_text>Create the client and create the database<jupyter_code>client = WOQLClient(server_url)
client.connect(user=user,account=account,key=key,db=dbid)

try:
    client.create_database(dbid,user,label=label, description=description)
except Exception as E:
    error_obj = E.errorObj
    if "api:DatabaseAlreadyExists" == error_obj.get("api:error",{}).get("@type",None):
        print(f'Warning: Database "{dbid}" already exists!\n')
    else:
        raise(E)<jupyter_output><empty_output><jupyter_text>Add the schema (there is no harm in adding repeatedly as it is idempotent)<jupyter_code>WQ().woql_and(
    WQ().doctype("scm:Widget")
        .label("Widget")
        .description("A widget")
        .property("sku", "xsd:string")
            .label("sku")
            .cardinality(1)
        .property("date_added","xsd:dateTime")
            .label("date added")
        .property("category","xsd:string")
            .label("category")
).execute(client, "Adding schema")<jupyter_output><empty_output><jupyter_text>Make the production branch<jupyter_code>production = "production"
try:
    client.branch(production)
except Exception as E:
    error_obj = E.errorObj
    if "api:BranchExistsError" == error_obj.get("api:error",{}).get("@type",None):
        print(f'Warning: Branch "{production}" already exists!\n')
    else:
        raise(E)<jupyter_output><empty_output><jupyter_text>Return to the 'main' branch<jupyter_code>client.checkout('main')<jupyter_output><empty_output><jupyter_text>Add the data from csv to the main branch (again idempotent as widget ids are chosen from sku)<jupyter_code>sku, date, category, widget_ID = WQ().vars('v:Sku','Date','Category', 'Widget_ID')
WQ().woql_and(
    WQ().get(
        WQ().woql_as('sku', sku)
            .woql_as('date_added', date, "xsd:dateTime")
            .woql_as('category', category),
        WQ().remote(widgets_url)),
    WQ().idgen("doc:Widget",[sku],widget_ID),
    WQ().insert(widget_ID, "scm:Widget")
    .property("sku", sku)
    .property("date_added", date)
    .property("category", category)
).execute(client, "Insert from CSV")<jupyter_output><empty_output><jupyter_text>Move widgets data from main to production<jupyter_code>WQ().woql_and(
    WQ().node(widget_ID,"Widget")
    .property("sku", sku)
    .property("date_added", date)
    .property("category", category),
    WQ().eq(date, WQ().literal('1970-01-01T00:00:00','xsd:dateTime')),
    WQ().eq(category, 'widgets'),
    WQ().using(
        f'{account}/{dbid}/{repository}/branch/{production}',
        WQ().woql_and(
            WQ().insert(widget_ID, "scm:Widget")
            .property("sku",sku)
            .property("date_added", date)
            .property("category", category)
        )
    )
).execute(client, "Add to production branch")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16596194503171247, 'fraction_numerical': 0.005285412262156448, 'mean_word_length': 4.143115942028985, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '568849', 'license': 'Apache-2.0', 'n_tokens_mistral': 985, 'n_tokens_neox': 959, 'n_words': 160, 'path': 'episode-2/episode2.ipynb', 'repo_name': 'csaranbalaji/terminusdb-tutorials', 'size': '2838'}