doejs
doejs

Reputation: 96

class method telling me that I used 1 more pos. argument than needed, but i didnt

I get the following error from my code: TypeError: get_manifest() takes 0 positional arguments but 1 was given

I have a class scan, and get_manifest is a function in scan:

import amfy
import requests
import json
from datetime import timedelta, datetime 
class Scan:
        def __init__(self, realm = 416, cluster = 9):

            self.events = list()
            self.url = "http://realm{}.c{}0.castle.rykaiju.com/api/locales/en/get_serialized_new".format(realm, cluster)


        def get_manifest():                 
            self.request = requests.get(self.url)

            if self.request.status_code == 200:
                self.manifest = self.request.json() if "json" in self.request.headers["content-type"] else amfy.loads(self.request.content)

            else:
                self.manifest = {"failed": req.reason}



        def get_time(day):

            self.today = datetime.now()

            self.plusdays = timedelta(days = day)

            self.new_date = sel.ftoday + self.plusdays

            self.year, self.month, self.day = self.new_date.year, self.new_date.month, self.new_date.day

            if len(str(self.day)) == 1:
                day = "0{}".format(self.day)

            if len(str(self.month)) == 1:
                month = "0{}".format(self.month)

            self.date = str(self.year) + str(self.month) + str(self.day)

            return self.date


        def search_events():

            for day in range(0, 11):
                date = self.get_time(day)

                for section in doaManifest:                
                    for key, value in doaManifest[section].items():                    
                        if date in key:
                            event_title = key 
                            try:
                                event_desc = value['text']
                            except KeyError:
                                event_desc = """" /!\ No Event Description /!\""""
                            my_dict = {'title' : event_title, 'contents' : event_desc}
                            self.events.append(my_dict)

Then, in another class, which is my app GUI (written with tkinter), I have a button that calls on this class. The button's command is this:

def scan(self):

    if self.scan['text'] == 'Scan':
        self.scan['text'] = 'Re-Scan'
        self.scanned = True

    self.scan = Scan()
    self.scan.get_manifest()
    self.scan.search_events()

I don't feed get_manifest any arguments, so why is it saying I am?

Upvotes: 0

Views: 35

Answers (2)

A Magoon
A Magoon

Reputation: 1210

First you need to add the self parameter to all class methods unless you use @staticmethod. So your class should look like this:

class Scan:
    def __init__(self, realm = 416, cluster = 9):

        # code...

    def get_manifest(self):                 

        # code...

    def get_time(self, day):

        # code...

    def search_events(self):

        # code...

Furthermore, to use this class within your scan() method you need to initialize and save the class instance to a variable, then call on the get_manifest() method from the saved instance variable.

def scan(self):
    # I assume this scan() method is actually a part of a class since 'self' is used

    if self.scan['text'] == 'Scan':
        self.scan['text'] = 'Re-Scan'
        self.scanned = True

    #self.scan = Scan()  # You are overwriting the definition of 'scan' here...

    # So instead do the below

    # Initialize and save your Scan() instance to a variable
    scan_instance = Scan()

    # Now you may use the object's methods 
    scan_instance.get_manifest()
    scan_instance.search_events()

    # Here you can now access the object's attributes that were created in get_manifest()
    print(scan_instance.manifest)

Upvotes: 0

Bharel
Bharel

Reputation: 26901

Seems like you forgot adding self to get_manifest(). Redefine get_manifest() like so:

def get_manifest(self):
    code...

Instance methods should always accept self as the first argument as it's automatically inserted. You may use @staticmethod if you don't wish to receive it but as you're using self I guess that it's not something you want.

Upvotes: 2

Related Questions