Python program error 'list' object is not callable

Matthew Swart

I have followed this tutorial http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot, The aim is I have a whole lot of URL's in a list and I need to return one for my next Python Class so that I can scrape the stats from that page. I setup my code last night and made sure that it worked without any hassles but when I came back to it this morning I am getting the following:

/usr/bin/python2.7 /home/student/Project/country_selection.py
Traceback (most recent call last):
  File "/home/student/Project/country_selection.py", line 39, in <lambda>
    self.ui.irelandButton.clicked.connect(lambda: self.country_url(11))
TypeError: 'list' object is not callable

Process finished with exit code 0

This is the code I am using for this project I would greatly appreciate an input available.

import sys
from PyQt4 import QtCore, QtGui
from country_selection_ui import Ui_CricketStats

class CountryPlayer(QtGui.QMainWindow):
    """
    This program is used to return the url required for the player selection program
    each button on the GUI represents a different country and is defined so that when
    it is clicked it will pass a variable to the country_url method which in turn
    will then use the variable to return a url.
    """
    def __init__(self, parent=None):
        """
        In this method I have used the lambda function so that I can push the variable
        required in the country_url. Lambda is a built in tool in Python and is there
        for building functions. I once asked Darren Dowdall to explain what lambda was
        and his explanation seems to fit best with me "It is a throw away function". We
        use lambda function once then it is gone unlike def function which can be used
        over and over again.
        :param parent:
        """

        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_CricketStats()
        self.ui.setupUi(self)

        # what the button does when pushed
        self.ui.australiaButton.clicked.connect(lambda: self.country_url(0))
        self.ui.indiaButton.clicked.connect(lambda: self.country_url(1))
        self.ui.englandButton.clicked.connect(lambda: self.country_url(2))
        self.ui.pakistanButton.clicked.connect(lambda: self.country_url(3))
        self.ui.southafricaButton.clicked.connect(lambda: self.country_url(4))
        self.ui.newzealandButton.clicked.connect(lambda: self.country_url(5))
        self.ui.srilankaButton.clicked.connect(lambda: self.country_url(6))
        self.ui.westindiesButton.clicked.connect(lambda: self.country_url(7))
        self.ui.zimbabweButton.clicked.connect(lambda: self.country_url(8))
        self.ui.bangladeshButton.clicked.connect(lambda: self.country_url(9))
        self.ui.kenyaButton.clicked.connect(lambda: self.country_url(10))
        self.ui.irelandButton.clicked.connect(lambda: self.country_url(11))
        self.ui.canadaButton.clicked.connect(lambda: self.country_url(12))
        self.ui.netherlandsButton.clicked.connect(lambda: self.country_url(13))
        self.ui.scotlandButton.clicked.connect(lambda: self.country_url(14))
        self.ui.afghanistanButton.clicked.connect(lambda: self.country_url(15))
        self.ui.usaButton.clicked.connect(lambda: self.country_url(16))

    def country_url(self, n):
        """
        This method is used to return the url required for the player selection program.

        :param n: is the number which is directly related to the country url position in the
                  self.country_url list.
        :return:
        """
        self.country_url = ["http://www.espncricinfo.com/australia/content/player/country.html?country=2",
                            "http://www.espncricinfo.com/india/content/player/country.html?country=6",
                            "http://www.espncricinfo.com/england/content/player/country.html?country=1",
                            "http://www.espncricinfo.com/pakistan/content/player/country.html?country=7",
                            "http://www.espncricinfo.com/southafrica/content/player/country.html?country=3",
                            "http://www.espncricinfo.com/newzealand/content/player/country.html?country=5",
                            "http://www.espncricinfo.com/srilanka/content/player/country.html?country=8",
                            "http://www.espncricinfo.com/westindies/content/player/country.html?country=4",
                            "http://www.espncricinfo.com/zimbabwe/content/player/country.html?country=9",
                            "http://www.espncricinfo.com/bangladesh/content/player/country.html?country=25",
                            "http://www.espncricinfo.com/kenya/content/player/country.html?country=26",
                            "http://www.espncricinfo.com/ireland/content/player/country.html?country=29",
                            "http://www.espncricinfo.com/canada/content/player/country.html?country=17",
                            "http://www.espncricinfo.com/netherlands/content/player/country.html?country=15",
                            "http://www.espncricinfo.com/scotland/content/player/country.html?country=30",
                            "http://www.espncricinfo.com/afghanistan/content/player/country.html?country=40",
                            "http://www.espncricinfo.com/usa/content/player/country.html?country=11"]
        return self.country_url[n]

if __name__ == '__main__':


    app = QtGui.QApplication(sys.argv)
    myapp = CountryPlayer()
    myapp.show()
    sys.exit(app.exec_())

In case I have also put up my GUI code which is made with PyQt4 at the following link: https://paste.ee/p/OwAcw as I am completely lost at this point. I have no clue why it is throwing the error 'list' object is not callable. I also changed the brackets to [ ] but land up with the following error:

/usr/bin/python2.7 /home/student/Project/country_selection.py
Traceback (most recent call last):
  File "/home/student/Project/country_selection.py", line 39, in <lambda>
    self.ui.irelandButton.clicked.connect(lambda: self.country_url[11])
TypeError: 'instancemethod' object has no attribute '__getitem__'

Thanks for any input again.

asongtoruin

I can't fully test this, so apologies if there are any mistakes. This hopefully will be functional, but might not be best practice.

Your issue comes from using country_url for both a method and a property - this causes conflicts and is something you want to avoid.

We can make the code more readable and hopefully avoid such conflicts by changing your country_url method. Let's change this and rename it as get_url (as this is a more logical name for the method) as follows:

def get_url(self, country_name):
    """
    This method is used to return the url required for the player selection program.

    :param country_name: is the name of the country.
    :return:
    """

    country_dict = {'australia': '2',
                    'india': '6',
                    'england': '1',
                    'pakistan': '7',
                    'southafrica': '3',
                    'newzealand': '5',
                    'srilanka': '8',
                    'westindies': '4',
                    'zimbabwe': '9',
                    'bangladesh': '25',
                    'kenya': '26',
                    'ireland': '29',
                    'canada': '17',
                    'netherlands': '15',
                    'scotland': '30',
                    'afghanistan': '40',
                    'usa': '11'}

    if country_name in country_dict.keys():
        return "http://www.espncricinfo.com/{0}/content/player/country.html?country={1}"\
            .format(country_name, country_dict[country_name])
    else:
        raise ValueError

As the URL for each country is basically the same, we can construct it from a dictionary, which makes it nicer to find and easy to add to or alter.

Now, the buttons can be defined in the following manner:

self.ui.australiaButton.clicked.connect(lambda: self.get_url('australia'))

Which is, hopefully, a more readable way of setting the buttons up.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

list() but 'str' object is not callable error in python

Python Error: TypeError: 'list' object is not callable

python for loop giving error 'list' object is not callable

Python: 'list' object is not callable

Python 'list' object not callable

Python error: 'list' object is not callable after converting the map function to list

TypeError: 'list' object is not callable in python

Python - TypeError: 'list' object is not callable

'List' Object is not callable in Python Pandas

'int' object is not callable error in python

Map to List error: Series object not callable

List Object Not Callable, Can't Find Error

Python class return TypeError: 'list' object is not callable

Python/Pandas TypeError: 'list' object is not callable

Write to File Issues - 'list' object is not callable (Python)

TypeError: 'list' object is not callable for Dikjstra python implementation

python fbprophet error, TypeError: 'module' object is not callable

How to fix 'AffinityPropagation object is not callable' error in Python

error TypeError: 'str' object is not callable python

ggplot Python : Error : TypeError: 'module' object is not callable

How to fix python error (object is not callable)

Error plot str' object is not callable python

SQLite/python: 'str' object is not callable error

Getting error `SyntaxWarning: 'str' object is not callable` in Python

Python Error Message: TypeError: 'str' object is not callable

Python mypy type error with Union of callable and list of callable converted to list of callable

'list' object is not callable: TypeError

TypeError : 'list' object is not callable

TypeError: 'list' object is not callable Python Object Oriented Programming