snapper
snapper

Reputation: 1187

Track/Record of Instances' Method Usage in Python

I am developing a sub-program which can help of my English learning.

I had developed some methods in class System which are crawler-based example/semantic of the target_word finder.

Then I generated a user through class User which generates a instance and inherits every method in class System.

Then what I want to do is, whenever a user use method inherited to class User, it tracks for which word the method has been used and scribe that info in the list = [] as a sheudo database

class System:
    def wik_sem(self, target_word):
        print("start searching for the target_word {}\n".format(target_word))
        driver.get("https://en.wiktionary.org/wiki/" + target_word) #open the page of chronological page
        elem_info =[]
        for i in range(0, 20): #20 is the assumed maximum number of li[]
            try:
                element = '//*[@id="mw-content-text"]/div/ol[1]/li['+str(i)+"]"
                elem_info.append(driver.find_elements_by_xpath(element))
            except:
                break     
        reference_wik = []
        for i in range(0, len(elem_info)):
            if elem_info[i] == []:
                pass
            else:
                reference_wik.append((elem_info[i][0].text).split("\n")[0].split())
        #simple_parser


        # remove 'qutations','▼'
        for i in range(0,len(reference_wik)):    
            if '▼' in reference_wik[i]:
                del reference_wik[i][(len(reference_wik[i])-1)] #use del twice to delete both of '▼' and 'quotations'
                del reference_wik[i][(len(reference_wik[i])-1)] #use del to remove an element in list with index
        #remove the first parenthesized element, e.g. (countable) 'the thing which can be counted' ...
        for i in range(0,len(reference_wik)):    
            if '(' in list(reference_wik[i][0]):
                del reference_wik[i][0] #use del to remove an element in list with index
        #remove '.' at the end of each definition
        for i in range(0,len(reference_wik)): 
            if '.' in list(reference_wik[i][-1]):
                reference_wik[i][-1] = reference_wik[i][-1].replace(".", "")
        print("Definition of {} from wiktionary.org".format(target_word))
        for i in range(len(elem_info)):
            try:
                print(elem_info[i][0].text)
            except:
                pass

        return reference_wik

    def wis(self, target_word):
        driver.get("https://wordsinasentence.com/" + target_word+ "-in-a-sentence/") #open the page of chronological page
        sentence_numb = 20
        for i in range(1, sentence_numb): 
            try:
                elem_info = driver.find_element_by_xpath('//*[@id="content"]/p[{}]'.format(i))
                if 'WATCH' in elem_info.text.split():
                    break
                print(elem_info.text+"\n")
            except:
                pass

    def finder(self, target_word):
        reference_wik = wik_sem(target_word)
        print('\n\n')
        wis(target_word)
        return reference_wik

    def ety(self, target_word):
        print("start searching for the etymology of the target_word {}\n".format(target_word))
        driver.get("https://en.wiktionary.org/wiki/" + target_word)
        elem_info = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/p[3]')
        return elem_info.text

class User(System):
    def __init__(self, id_ = None):
        super().__init__()
        id_ = uuid4()
        self.id = id_
        self.hist = []

yoon = User() #class instantiation
yoon.finder('eschew')

then the 'eschew' appended in to the yoon.hist_list -> how can I do this?

Upvotes: 1

Views: 156

Answers (1)

alpacahaircut
alpacahaircut

Reputation: 337

it looks like what you are after is to add the attribute hist to the parent class System rather than to the child User and then to append reference_wik to the list self.hist in the method finder rather than returning it:

class System():

    def __init__(self):
        self.hist = []

    #def wik_sem, wis ...

    def finder(self,target_word):
        reference_wik = wik_sem(target_word)
        print('\n\n')
        wis(target_word)
        self.hist.append(reference_wik)


    #rest of System def...

Then User could be defined as

class User(System):
   def __init__(self):
   super().__init__()
   id_ = uuid4()
   self.id = id_

Then

yoon = User()
yoon.finder('eschew')
print(yoon.hist)

yeilds:

#...output from print calls within finder...
['eschew']

Upvotes: 1

Related Questions