-2

In the code below I am getting an error.

An error occurred: WelcomeKitAssistant.find_otherbacode_onsamehost() missing 1 required positional argument: 'self'
import pandas as pd
import networkx as nx
import hashlib

class WelcomeKitAssistant:

    def create_graph(self, input_df):
        G = nx.DiGraph()  # recreating the graph afresh
        df = input_df.dropna(subset=['Hostnames'], how='all')
        for _, row in df.iterrows():
            barcodes = [barcode.strip() for barcode in row['Bar_Code'].split('|')]
            hosts = [host.strip() for host in row['Hostnames'].split('|')]
            for barcode in barcodes:
                G.add_node(barcode, type='barcode')
                for host in hosts:
                    G.add_node(host, type='host')
                    G.add_edge(barcode, host)
        return G



    def get_hostnames_for_barcode(self, graph, barcode):
        if barcode not in graph.nodes or graph.nodes[barcode]['type'] != 'barcode':
            return []

        neighbors = list(graph.successors(barcode))
        hostnames = [node for node in neighbors if graph.nodes[node]['type'] == 'host']
        return hostnames

    @staticmethod
    def find_otherbacode_onsamehost(self, graph, barcode):
        if barcode not in graph.nodes or graph.nodes[barcode]['type'] != 'barcode':
            return []

        barcode_hosts = set(self.get_hostnames_for_barcode(graph, barcode))
        similar_barcodes = []

        for other_barcode in graph.nodes:
            if other_barcode == barcode or graph.nodes[other_barcode]['type'] != 'barcode':
                continue

            other_barcode_hosts = set(self.get_hostnames_for_barcode(graph, other_barcode))

            if other_barcode_hosts.issubset(barcode_hosts):#or server_hosts.issubset(other_barcode_hosts)
                similar_barcodes.append(other_barcode)

        return similar_barcodes




    def main(self):
        print("What would you like to do today?")
        print("1. Get associated CAL bar codes for a given CAL code")
        print("2. Generate an updated file with CAL associations and Hostnames")

        choice = int(input("Enter your choice: "))

        if choice == 1:
            input_barcode = str(input("Enter a CAL BAR Code: "))
            input_csv_path = str(input("Enter the full path where the CAL extract CSV is located: "))
            try:
                CAL = pd.read_csv(input_csv_path)
                CAL.dropna(subset=['Bar_Code'], how='all', inplace=True)
                G = self.create_graph(CAL)
                similar_barcodes = self.find_otherbacode_onsamehost(graph = G, barcode=input_barcode)
                print(f"Hostnames for {input_barcode}: {', '.join(self.get_hostnames_for_barcode(G, input_barcode))}")
                print(f"CAL barcodes using same servers: {', '.join(similar_barcodes)}")
            except Exception as e:
                print("An error occurred:", e)

        elif choice == 2:
            print("Work in progress")

        else:
            print("Invalid choice.")

if __name__ == "__main__":
    assistant = WelcomeKitAssistant()
    assistant.main()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Akshay Kadidal
  • 515
  • 1
  • 7
  • 15
  • 3
    Why do you have `@staticmethod` on that one method? A staticmethod is not associated with any particular instance of the class, so doesn't receive a `self` parameter. But this method actually *uses* `self`, so it simply cannot be a staticmethod. – jasonharper Aug 31 '23 at 02:23
  • 1
    Bang on my friend! you caught it. Thank you :) I had been editing this code for some time now ....I missed this piece. Please submit this as an answer. – Akshay Kadidal Aug 31 '23 at 02:26
  • Stack Overflow is a Q&A site, but there's no question here. Please read [ask], which includes tips like how to write a good title and to make a [mre]. – wjandrea Sep 01 '23 at 16:30

1 Answers1

0

I updated the similar_barcodes assignment with assistant, which is my class instance within the find_otherbacode_onsamehost function which needed the third argument.

similar_barcodes = self.find_otherbacode_onsamehost(assistant, graph = G, barcode=input_barcode)

Which was originally:

similar_barcodes = self.find_otherbacode_onsamehost(graph = G, barcode=input_barcode)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Akshay Kadidal
  • 515
  • 1
  • 7
  • 15
  • The parameter shouldn't be called `self` if it's a different instance. `self` is only used for the instance the method is bound to. (TBF that's only a convention, but it's a very deep-rooted one.) – wjandrea Sep 01 '23 at 16:32