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()