### Basic YDBPython Usage Example Source: https://gitlab.com/yottadb/lang/ydbpython/-/blob/master/README.md Demonstrates basic operations like creating keys, setting and getting values, and deleting nodes using YDBPython. ```python import yottadb # Create Key objects for conveniently accessing and manipulating database nodes key1 = yottadb.Key('^hello') # Create a key referencing the global variable '^hello' print(f"{key1}: {key1.value}") # Display current value of '^hello' key1.value = b'Hello world!' # Set '^hello' to 'Hello world!' print(f"{key1}: {key1.value}") key2 = yottadb.Key('^hello')['cowboy'] # Add a 'cowboy' subscript to the global variable '^hello', creating a new key key2.value = 'Howdy partner!' # Set '^hello('cowboy') to 'Howdy partner!' print(f"{key2}: {key2.value}") key3 = yottadb.Key('^hello')['chinese'] # Add a second subscript to '^hello', creating a third key key3.value = bytes('你好世界!', encoding="utf-8") # The value can be set to anything that can be encoded to `bytes` print(key3, str(key3.value, encoding="utf-8")) # Returned values are `bytes` objects, and so may need to be encoded for subscript in key1.subscripts: # Loop through all the subscripts of a key sub_key = key1[subscript] print(f"{sub_key}: {sub_key.value}") key1.delete_node() # Delete the value of '^hello', but not any of its child nodes print(f"{key1}: {key1.value}") # No value is printed for subscript in key1.subscripts: # The values of the child nodes are still in the database sub_key = key1[subscript] print(f"{sub_key}: {sub_key.value}") key1.value = 'Hello world!' # Reset the value of '^hello' print(f"{key1}: {key1.value}") # Prints the value key1.delete_tree() # Delete both the value at the '^hello' node and all of it's children print(f"{key1}: {key1.value}") # Prints no value for subscript in key1.subscripts: # Loop terminates immediately and displays no subscripts sub_key = key1[subscript] print(sub_key, sub_key.value) ``` -------------------------------- ### Install Pre-commit Hook Source: https://gitlab.com/yottadb/lang/ydbpython/-/blob/master/README.md Installs the pre-commit hook for contributing to YDBPython. Requires black to be installed. ```bash ln -s ../../tools/git_hooks/pre-commit.sh .git/hooks/pre-commit ``` -------------------------------- ### Run YDBPython Test Suite Source: https://gitlab.com/yottadb/lang/ydbpython/-/blob/master/README.md Executes the YDBPython test suite after installation from source. May require cleanup between test runs. ```bash python -m pytest ``` -------------------------------- ### Cleanup Test Artifacts Source: https://gitlab.com/yottadb/lang/ydbpython/-/blob/master/README.md Removes files listed in .gitignore to clean up between test runs. Use with caution as it deletes core files. ```bash for artifact in $(cat .gitignore); do rm -rf $artifact; done ``` -------------------------------- ### Implement Simple Database Transaction in YDBPython Source: https://gitlab.com/yottadb/lang/ydbpython/-/blob/master/README.md Use the @yottadb.transaction decorator to define a function that performs database operations. This snippet demonstrates setting local variables and handling potential rollback or restart exceptions. ```python import yottadb @yottadb.transaction def simple_transaction(value): # Set values directly with the set() function yottadb.set('test1', value=value) # Set the local variable 'test1' to the given value yottadb.set('test2', value=value) # Set the local variable 'test2' to the given value condition_a = False condition_b = False if condition_a: # When a yottadb.YDBTPRollback exception is raised YottaDB will rollback the transaction # and then propagate the exception to the calling code. raise yottadb.YDBTPRollback("reason for the rollback") elif condition_b: # When a yottadb.YDBTPRestart exception is raised YottaDB will call the transaction again. # Warning: This code is intentionally simplistic. An infinite loop will occur # if yottadb.YDBTPRestart is continually raised raise yottadb.YDBTPRestart() else: return yottadb.YDB_OK # Success, transaction will be committed simple_transaction(b'test', db) print(f"{db[b'test1']}: {db[b'test1'].value}") print(f"{db[b'test2']}: {db[b'test2'].value}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.