Write a Python Program to Define a Subclass Using Threading And Instantiate The Subclass And Trigger The Thread that Implements The Task of Building Dictionary For Each Character From The The Given Input String

Hi in this article you will learn a Python program that takes an input as a string and then builds a dictionary containing the characters present in the string as the key and value will be the number of times the character has occurred in a string.

 

python threading

 

 


Write a Python Program to Define a Subclass Using Threading And Instantiate The Subclass And Trigger The Thread that Implements The Task of Building Dictionary For Each Character From The The Given Input String

Let's take a class and over the thread class,  define the unit function by initializing the thread,  text and an empty dictionary.

Override the run function by iterating all the items present in the given string and setting the dictionary values using the print function call to print the key value pair of the dictionary.

class SubClass(threading.Thread):
    def __init__(self, text):
        threading.Thread.__init__(self)
        self.text = text
        self.d = dict()

    def run(self):
        for letter in self.text:
            if letter in self.d:
                self.d[letter] += 1
            else:
                self.d[letter] = 1

        print("The dictionary that we have built is ...")
        for key, value in self.d.items():
            print(key, value)



In the main program, take the object of the subclass and  use the object to start the thread and use a join() function to wait and tell the thread is completed.

data = input("Enter a String : ")
t1 = SubClass(data)
t1.start()
t1.join()




Complete Python program
==============================

import threading

class SubClass(threading.Thread):
    def __init__(self, text):
        threading.Thread.__init__(self)
        self.text = text
        self.d = dict()

    def run(self):
        for letter in self.text:
            if letter in self.d:
                self.d[letter] += 1
            else:
                self.d[letter] = 1

        print("The dictionary that we have built is ...")
        for key, value in self.d.items():
            print(key, value)

data = input("Enter a String : ")
t1 = SubClass(data)
t1.start()
t1.join()





Output
================

Enter a String : hello world
The dictionary that we have built is ...
h 1
e 1
l 3
o 2
  1
w 1
r 1
d 1







The above Python program can also be implemented using multi class,  where the main class contains another nested subclass.

Just use two classes. First is the main class and next is a subclass within the main class.  use the init() function to get the instance of subclass object.

Use another function called call_subclass() to get the object and run the thread using start() and join() function call.




Complete Python program another version
======================================

import threading

class MainClass():
   class SubClass(threading.Thread):
       def __init__(self, text):
           threading.Thread.__init__(self)
           self.text = text
           self.d = dict()

       def run(self):
           for letter in self.text:
               if letter in self.d:
                   self.d[letter] += 1
               else:
                   self.d[letter] = 1

           print("The dictionary that we have built is ...")
           for key, value in self.d.items():
               print(key, value)

   def __init__(self):
       self.input_text = input("Enter a String : ")
       self.sub_object = None

   def call_subclass(self):
       self.sub_object = self.SubClass(self.input_text)
       self.sub_object.start()
       self.sub_object.join()


main_object = MainClass()
main_object.call_subclass()





Output
===================

Enter a String : code with tj
The dictionary that we have built is ...
c 1
o 1
d 1
e 1
  2
w 1
i 1
t 2
h 1
j 1



Code Explanation on YouTube, Subscribe to my channel
====================================================
 
 
Other Thread Programs
=======================
Write a Python Program to Print Alternate Numbers Using 2 Threads. Implement Using Wait And Notify Construct
 
 


Conclusion
===================

The above Python program uses a subclass to overwrite the thread class.  the subclass will also over right there run() function,  then call the run function by taking an object of the class.


The run function contains the Logic to build a dictionary from the string and also print the dictionary containing key value pairs.  The key is a character present in the string and the value is the count of the number of times the character occurs.


Execute the above Python program by providing a random string as input to it.  Comment  it down below if you have any queries regarding the above python program.



Post a Comment

0 Comments