неделя, 23 февруари 2020 г.

[SnakeData]create_db()

    This functon was built to create new database node and has just one argument - the name of the database we aim to create /create_db(db_name)/. To call the function just type the desired db name in quotations in place of db_name argument:
python create_db() function
I am using the setup of this usecase to illustrate functionallity of create_db( ). Full code can be seen on the printscreen below.

python 3 create_db() code function

With dir=os.getcwd( ) we are collecting the name of current working directory and store it as string in a variable called dir. On the next line /436/ we load the content of meta.xml, each distribution of SnakeData comes with default meta.xml file that holds the structure of all databases available in the "main" folder. meta.xml /... or at least pretends to.../ looks like below by default:


    And after creation of "case1" db meta.xml receives the look below:


We see one more tag named "case1" at the end of meta.xml, right before closing the root tag /which is "main" by default/.
    So, let's go back to our Python code. Line 437 collects the root tag /"main" as mentioned above/. On the next 438 line we're seeking for the name of the db we want to create. root.find(db_name) returns the position of where the string begins if found, otherwise returns None... which is quite enough to check whether the db already exists or not. In line 439 we initiate an if-elif-else condition that asks if current dir is called "main" AND root tag in meta.xml is also called "main" AND if the name of the new db is NOT already represented in meta.xml /and therefore doesn't exist as folder/. If the condition is sattisfied then main meta.xml receives one more tag with the name of the new db /lines 440-442/. Lines 444 to 449 create default folders for each database via os.mkdir( ) method /there folders are described on SnakeData main page/. Lines 451 to 456 create separate xml structure for the new db and on line 457 we enter the newly created database via os.chdir(db_name). There we bring the new xml structure to reallity /... okay, it's virtuallity in fact/ with writing it as new meta.xml file:


The last four lines /460 to 463/ are checking the cases where database already exists and current working directory is not called "main".

Няма коментари:

Публикуване на коментар