How to fix: TypeError: filter expected 2 arguments, got 1

Question:

I am making a browser using python and PyQt5/PySide6. I implemented an adblock which uses a list to block ads from opening as tabs.

`

class WebPage(QWebEnginePage):

    adblocker = filter(open(os.path.join("build files", "adlist.txt"), encoding="utf8"))

    def __init__(self, parent=None):
        super().__init__(parent)

    def acceptNavigationRequest(self, url,  _type, isMainFrame):
        urlString = url.toString()
        resp = False
        resp = WebPage.adblocker.match(url.toString())

        if resp:
            print("Blocking url --- "+url.toString())
            return False
        else: 
            print("TYPE", _type)
            return True
        return QWebEnginePage.acceptNavigationRequest(self, url,  _type, isMainFrame)

I tried to fix this, but to no avail.

I am expecting the code to raise no errors. The code is supposed to prevent the user from opening ads.

Asked By: Michael

||

Answers:

you need to add the filtering function to filter()

adblocker = filter(filter_by, open(os.path.join("build files", "adlist.txt"), encoding="utf8"))

where filter_by is a function that takes an element of the giving array and returns a bool
example:

array = [1, 2, 3, 4, 54, -5, -1]
def positive(x):
    return x>=0

filter_ed = filter(positive, array) # return [1, 2, 3, 4, 54]