петък, 24 януари 2020 г.

[SnakeData]The terminal

   Hey there, the first post related to SnakeData will be dedicated to the console.py file and some basix functionalities of the standalone terminal /console.exe/. The entire code is on the printscreen below:


Python 3, .py

    Let's focus on first three rows of code where the modules needed are imported:

console.py import modules snakedata python
Modules in Python are, roughly explained, .py files with useful code that can be imported in our program when needed. Therefore we can import SnakeData as module /should be in the same folder as the programming file/ and it's functions become available in the terminal. This could be also written as "import SnakeData", it will have the same functionallity as it's current version. In case we need only specific function to be imported to a given file, then we replace "import *" with "import [function_name]", e.g. "from SnakeData import select_db". Lines 2 and 3 are not necessary in fact - csv and os modules are already imported in SnakeData.py, and console.py inherits them when SnakeData is imported /I will fix this in following versions/.
    The core functionality of the terminal is based on a while loop which permanently requests user input that gets executed. This means that the user input should be either SnakeData function or valid Python code in order to work properly. While loop is one of the two available loops in Python and is running until its condition becomes false. The loop in the example is the so called "eternal loop" and  could be broken only if the user types "exit" in the input field in this example. A programmer should be very careful when using it - in more complex programs it could cause system overloads/failures, etc. That's why it is always wise to keep a way to exit the loop - the exit keyword in this case.
    The loop is simply initiated by writing "while True:" and is followed by indented "dir=os.getcwd()"/indented spaces are very important in Python - this is how hierarchy is applied to code/. getcwd() stands for "Get Current Working Directory" and is a method that returns the directory where Python code is executed as string, i.e. dir is a user-defined variable that contains a string/text/ with the path to the working directory. Next we're checking whether we're in the root folder called "main". As stated here this is the main/root/ folder and the system will not work if the console is not started from here.
console.py while loop snakedata python
If the condition is satisfied, i.e. if the path ends with "main" and we're in main folder, then dir variable receives another content as described on the printscreen and another variable is described: command. This is the variable that collects user input that could potentially be executed. Afterwards we set  the escape pod from the eternal loop - if the user input is equal to "exit" the loop breaks and the program closes. Here is how the terminal looks:
console.exe first look snakedata python







If you simply type exit after the two dots the program will close.
console.py exception hangling snakedata python
With the lines on the left we set a condition in case the user input is different than exit. In this case we're in fact handling with exception - the possibility of having incorrect code/command in the input field is quite high and if syntax error occurs the program gets closed. To avoid that and continue receiving requests for user input we need to handle with possible errors. The code above says "try to execute the command except it's an error - display a message in this case". Here is how it works practically:

console.exe error message python snakedata









At the moment it is not providing much info about what's wrong with the command so I consider improvement in that direction for future versions.

With the code below we're finalizing the console.py:

console.py else statement python snakedata
On line 20 starts the else statement which defines the actions in the case when current directory is not main /the assumption is when you select a database or different folder you will dive deeper in the root structure and will leave the main folder/. On line 21 dir string variable is processed as a list /popular data structure in Python/ and is sliced from where "main" is located till the end of the string/path. See below how it works:

console.exe example2 python snakedata
I realize there's quite a lot to be upgraded and added as functionallity, so I'm open to suggestions :)







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

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