Mastering File Handling in Python and Qt Designer Part 1.

in Steem Cameroon13 hours ago

fombae-learning-cover.png

Greetings Steemit friends

The program calculates Mersenne numbers within a specified range using PyQt5. A Mersenne number is defined as ( M = 2^N - 1 ), where ( N ) is a prime number.

It is another session working with PyQt5, and this time creating and reading data. It might look challenging, but very exciting when you get the concept. Our first program will be calculating the Mersenne number within the range. That is the first input (A) is greater than 2 and the second input (B) is greater than 2 and A, but all input less than 50.000. Using the built-in function in Python, we create the file mersenne.txt to save the Mersenne numbers and results.

I started by designing interfaces on DT design, which I think we should have some level of understanding of the different widget boxes.

Screenshot 2024-11-28 090452.jpg

For our PyQt5 program to work properly and use the inbuilt function we have to import the required modules. I will add math, which will aid us in working with mathematical functions. The rest are the default for the widget and ui, which we have used in previous weeks.

To avoid unnecessary interaction, I created a function to determine if a number is a prime number. The number needs to be greater than 2 and go ahead to get the square root of the number.

Now let's calculate our Mersenne of each of the prime numbers. Note our inputs, both A and B need to be validated within our range 2 < A <aB < 50,000. Calculating Mersenne, we are making use of our formal M= 2^N -1. At this point, we can filter the primes to generate the Mersenne numbers.

Now we have our Mersenne numbers, let's go ahead and look at how to create and write the data on the file. We have the inbuilt function to help create our file and next a loop to write data in the file. Which in our case will be the result of the calculation of the Mersenne numbers.

I try to have error messages at each validation and confirmation, which is a good practice when developing a program.

Next will be looking at the display result, by reading the data from the file we created mersenne.txt. We will first populate the table by getting each line from the file split and filling the rows.

The rest function RESET, is a familiar function from previous weeks.

This program calculates the prime factors of a given number and saves the results in a text file named factors.txt.

Note this task is not that different from the previous task. We will be going ahead to get the prime factors of a number input and save the result on the text file.

Screenshot 2024-11-28 093204.jpg

I just copied the code of the previous task and will be making some changes. The first will be the function to calculate and give us the list of prime factors. Computing will require the number to be a divisor of 2 which is the smallest prime number. I used the while loop to check every possible divisor from the number input.

Now we can get out input and process using the function above to get out a list of prime factors if validated.

To convert the list to a string, it will have been easy using the join() function. Let's try longer methods, which will help us gain a better understanding. Here we are going to use a loop to iterate over the list and concatenate them to strings. The " x " is to separate the factors. I have done a check, not to apply the x if we have just one prime factor.

The next line is the in-build function, which creates a file and writes the data.

The rest functions are pretty much the same as in the previous task. Except I have to make changes to widget names.

This program is designed to validate 13-digit codes based on specific criteria and manage them using a file named Codes.txt.

Screenshot 2024-11-28 102433.jpg

Here we have to break the program into small sections, to be able to validate, save, and read code from the file. Things to consider:

  • Users need to input exactly 13 digits
  • The first three digits represent a prime number
  • The middle five digits, when converted to binary, contain more than eight zeros
  • The last five digits are divisible by the first three digits

We need a function to validate the input, starting by checking if the input length is exactly 13 digits. We had our function in the first task to check if a number is a prime. I will just copy and paste it here. If the input is checked for exactly a 13-digit return true, we go ahead to check the first three digits from a prime number using the function is_prime.

Next, we convert the middle five digits to binary using the in-built function bin() and check if it contains more than 8 zeros. Finally, check if the last five digits are divisible by the first three digits. Here is our validation function and the return out is either true or false. If the input passes all checks, return true and the code is valid and vice versa.

The next function is to create and write data on our file. The data to write is passed as a parameter for the save_code function. The read_code functions to read all the data on the file.

Now we have the VEREFYCODE function, which is triggered by the verify button. Here the input is checked by making use of the validate_code function. If the input is valid, we go ahead to iterate to check if the input has been written and saved on the file.

If the input is not in the already saved codes, it goes ahead to override and add the new code. If not, it gives us the error from the verification function (validate_code).

The next function is triggered by the display button, which reads the save code from the file. Now we can sort the code when the radiobutton is checked, and finally populate the result_sheet with the sorted codes.

The rest functions are familiar, and we have come across their functionalities.

This program identifies Smith numbers within a user-defined range through a graphical user interface. A Smith number is a composite number where the sum of its digits equals the sum of the digits of its prime factors.</center?

Screenshot 2024-11-28 104150.jpg

Just like the previous projects, we import the necessary modules. For this task, we will be using images for the QT design of our interface. This is not new, as we had the chance to use images in one of the sessions. The rest of the widget box is pretty much the same.

For us to be able to calculate the Smith numbers. We need to check if the number is a prime number, and then sum and get the prime factors. In the previous task, I have functions that I will make use of. Note we have to make sure that the first input (a) is at least 4 and that the first input is less than or equal to the second input (b). I will try to break the whole process into small functions for easy validation and code understanding

For a number to be a Smith number, we need to check if it is prime before getting its prime factors. Next, we compare the sum of the digits of the number with the sum of its prime factors. If both sums are the same, then it is a Smith number. At this point, we can go ahead to find the Smith numbers within the range of input (a) and input (b).

The Smith numbers will be obtained by looping through all the numbers within the range to get the numbers that return true to be Smith numbers.

The function PROCESS was just a breakdown of all the functions to get the Smith number within the range.

Note, that the RESET and QUIT function are similar and do not have a lot of significant changes. We need to just specify the fields we want to clear and the final function to quit the program.



Cheers
Thanks for dropping by
@fombae

Sort:  

Upvoted! Thank you for supporting witness @jswit.

Coin Marketplace

STEEM 0.26
TRX 0.20
JST 0.038
BTC 94819.33
ETH 3557.02
USDT 1.00
SBD 3.79