Credits

Saturday, March 23, 2019

Serial, Sequential and Random Access File Organization Using Python

Records in a file can be stored in different ways:

  • Serial - records are kept without defined /specific order.They are stored as they happened like how we store deposit/withdrawal transactions. We append them after each other.
  • Sequential - Unlike serial, sequential requires the use of key field (like an ID) and organize them using this ID. 
  • Random - Serial and sequential records may be searched from beginning until the record is found. This cant be possible if we need fast access (like ATM bank query or withdrawal). So, random file requires that we store and search a record in any address (usually defined by a hash function).
See the program code below in Python how records are stored and searched using serial organization in a text file:


Now, using random file organization, we can use binary file to implement random access where seek ( ) is used to locate an address and dump( )  is used to write into the binary file and load( ) to read from the binary file. Know that we can also use text files to implement random and sequential organization. We only use binary file as these are the common file formats used be executable files and other software.

Unlike text files where we use the file mode: w (write), r (read),a (append), binary files use the following file mode: wb (write binary), rb (read binary), rb+ (read and append), you may use other file modes  too.

Writing to binary file:

Reading from binary file:

Please note that unlike text files, binary file keep records in its internal representation, meaning, you may special characters in the file as you open it in a text editor like Notepad.

Now try these and explore.




Friday, March 15, 2019

Palindrome using Recursion in Python

Recursion is one that calls itself.  Any loop can be represented in recursion.

Take for instance the example I wrote to check whether a word is a palindrome or not.

Palindrome is  a word, number, phrase or other sequence of characters which reads the same backward as forward such as madam, racecar or the number 10801.

See how the two functions are implemented, one using ordinary loop and one using recursion.


You can try the code and see whether the words are palindrome or not.

Monday, February 18, 2019

Integrating all Database Modules Using Python

We have done all CRUD operations of inserting records, reading records from the tables, updating records and finally deleting the records if necessary.  Click on the links if you need to review these blog articles.

The modules are all useless if they are called separately and you check your XAMPP SQL database back and forth  to see if changes have indeed happened.

This is now where we create our user interface by having a menu.  Now you may want to create hierarchical menus like the example below.

You can modify this, so, you may just have one main menu only.


 The import commands above are to link your menu to the CRUD modules you have done previously. So, modify the names if your files are named  differently.

Once the menu is shown, the if . . else  condition executed. So, if I choose 1, the insertSalesPerson module found in the file named connectsqlinsertdynamic will be called. 

Remember, however, that once we are done with one module (example Insert), we make sure that our main menu will appear again until we choose to exit.

Now, it is your turn to create the menu interface to call all CRUD modules of insert, read, update and delete database records.

Updating and Deleting Records From the Database Using Python

I have previously created blog articles how to insert a record into our database and  how to read database records using Python. Now, it is time to update and / or delete records from our database.

When we update, we can ask our user for a record to update, search it in our database and once this record is found, we retrieve it for updating.

The sample Python program code below shows how we can update the age of a certain person using the fname(first name) as our search condition (see highlighted text below).

You can change the age as a user input to make your code more dynamic.


Now, if you need to delete a record, we follow the same process we did with updating but we change our SQL command to enforce record deletion.

In the Python program code below, we ask the user for a first name to locate and delete, once we found the record, the SQL Delete command shall be executed.

You may need to refresh your database table to see effect.


Now, it is your turn to practice these modules on your own database!

AddThis