Package: yottadb
YDBPythonYDBPython provides a Pythonic API for accessing YottaDB databases.RequirementsUbuntu Server 20.04 (or similar)Python > 3.6 (f-string and type annotation used), including thepython3-devpackage that containsPython.hlibffiYottaDB 1.34 or laterInstallationInstall Ubuntu Server 20.04Install YottaDB per theQuick Startguide instructions or fromsourceInstall prerequisites:Ubuntu/Debian:sudo apt install python3-dev python3-setuptools libffi-dev pkg-configRHEL/CentOS:yum install gcc python3 python3-setuptools python3-devel libffi-devel pkg-configArch Linux:sudo yay -Sy python-{setuptools,pip} libffi pkg-configSet environment variablesSet YottaDB environment variables:source $(pkg-config --variable=prefix yottadb)/ydb_env_setOptional: If YottaDB is built with Address Sanitization (ASAN) enabled,LD_PRELOADandASAN_OPTIONSmust be set:export ASAN_OPTIONS="detect_leaks=0:disable_coredump=0:unmap_shadow_on_exit=1:abort_on_error=1"export LD_PRELOAD=$(gcc -print-file-name=libasan.so)Install:Option 1: From PyPI:Option 1: Install invenv:Enter directory where install is desired, e.g.cd my-python-projectInstall thepython3-venvpackage:Ubuntu/Debian:sudo apt install python3-venvRHEL/CentOS:sudo yum install python3-virtualenvArch Linux:sudo yay -Sy install python3-virtualenvCreate venv:python3 -m venv .venvActivate venv:source .venv/bin/activateInstall into venv:pip install yottadbOption 2: Install to user:pip3 install yottadb --userOption 3: Install globally:sudo -E pip3 install yottadbOption 2: From source:Get the code:git clone https://gitlab.com/YottaDB/Lang/YDBPython.gitEnter code directorycd YDBPython/Runsetup.pyto install:Option 1: Install invenv:Install the python3-venv package:Ubuntu/Debian:sudo apt install python3-venvRHEL/CentOS:sudo yum install python3-virtualenvArch Linux:sudo yay -Sy install python3-virtualenvCreatevenv:python3 -m venv .venvActivatevenv:source .venv/bin/activateInstall intovenv:python setup.py installOption 2: Install to user:python3 setup.py install --userOption 3: Install globally (not suggested):sudo -E python3 setup.py installTestingTo run YDBPython's test suite with a YDBPython source installation:Enter YDBPython directory containing code repository, e.g.cd YDBPython/Installpytest,pytest-order, andpsutilIfpipforpython3is not installed do so:Ubuntu/Debian:sudo apt install python3-pipRHEL/CentOS:sudo yum install python3-pipArch Linux:sudo yay -Sy install python3-pipUsepipto installpytest,pytest-order,psutilOption 1: install intovenvActivatevenvif it is not already:source .venv/bin/activateInstall:pip install pytest pytest-order psutilOption 2: install for user:pip3 install --user pytest pytest-order psutilOption 3: install globally (not suggested):sudo pip3 install pytest pytest-order psutilRun the tests:Option 1: invenv:python -m pytestOption 2: with user or global installation:python3 -m pytestOptional: Cleanup between tests:When making changes to code between test runs, some cleanup may be needed to prevent new changes being ignored due to Python caching. To clean up these files:for artifact in $(cat .gitignore); do rm -rf $artifact; done. Note that this will delete all files listed in.gitignore, including core files. If these or any other such files need to be retained, move or rename them before running the aforementioned command.Basic Example Usageimportyottadb# Create Key objects for conveniently accessing and manipulating database nodeskey1=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 keykey2.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 keykey3.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 encodedforsubscriptinkey1.subscripts:# Loop through all the subscripts of a keysub_key=key1[subscript]print(f"{sub_key}:{sub_key.value}")key1.delete_node()# Delete the value of '^hello', but not any of its child nodesprint(f"{key1}:{key1.value}")# No value is printedforsubscriptinkey1.subscripts:# The values of the child nodes are still in the databasesub_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 valuekey1.delete_tree()# Delete both the value at the '^hello' node and all of it's childrenprint(f"{key1}:{key1.value}")# Prints no valueforsubscriptinkey1.subscripts:# Loop terminates immediately and displays no subscriptssub_key=key1[subscript]print(sub_key,sub_key.value)# Database transactions are also available@yottadb.transactiondefsimple_transaction(value):# Set values directly with the set() functionyottadb.set('test1',value=value)# Set the local variable 'test1' to the given valueyottadb.set('test2',value=value)# Set the local variable 'test2' to the given valuecondition_a=Falsecondition_b=Falseifcondition_a:# When a yottadb.YDBTPRollback exception is raised YottaDB will rollback the transaction# and then propagate the exception to the calling code.raiseyottadb.YDBTPRollback("reason for the rollback")elifcondition_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 raisedraiseyottadb.YDBTPRestart()else:returnyottadb.YDB_OK# Success, transaction will be committedsimple_transaction(b'test',db)print(f"{db[b'test1']}:{db[b'test1'].value}")print(f"{db[b'test2']}:{db[b'test2'].value}")Frequently Asked QuestionsDoes YDBPython support multi-threading?No, YDBPython does not support multithreading. This is due to the limitations of the Python Global Interpreter Lock for CPU-intensive multithreading. For background, see the following resources:Python documentation:Thread State and the Global Interpreter LockPython's GIL - A Hurdle to Multithreaded ProgramGrok the GIL: How to write fast and thread-safe PythonYDBPython GitLab discussion:Issue #7Accordingly, the Pythonthreadingandmultithreadingshould be avoided when developing applications with YDBPython. However, YDBPython does support multiprocessing and may be safely used with the Pythonmultiprocessinglibrary for parallelism. For an example ofmultiprocessingusage, seetests/test_threeenp1.py.