Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import http.client
- import json
- import time
- import timeit
- import pickle
- class Graph:
- def __init__(self, with_file=None):
- if with_file is None:
- self.adjacency_list = {}
- else:
- with open(with_file, 'rb') as handle:
- self.adjacency_list = pickle.load(handle)
- self.api_key = None
- @staticmethod
- def user():
- """
- :return: string
- your GTUsername, NOT your 9-Digit GTId
- """
- return 'gburdell3'
- def show_graph_info(self):
- """
- :return: none
- prints out the contents of the adjacency list
- """
- num_nodes = len(self.adjacency_list)
- print("nodes: " + str(num_nodes))
- num_edges = 0
- for n in self.adjacency_list.keys():
- num_edges += len(self.adjacency_list[n])
- print("edges: " + str(num_edges))
- print("------------------ADJACENCY--LIST---------------------")
- for n in self.adjacency_list.keys():
- print(str(n) + ": " + str(self.adjacency_list[n]))
- def get_sets(self, quantity=5, order_by=None):
- """
- :param quantity: integer
- :param order_by: string
- :return: list of strings, each string is a set_num representing a set.
- """
- if not isinstance(order_by, str):
- raise ValueError("order_by argument must be a string")
- lego_sets = []
- ################################################################
- # code to download and process lego sets data #
- # #
- ################################################################
- return lego_sets
- def get_top_parts(self, set_num, max_top_parts=5):
- """
- :param set_num: string, a valid set_num
- :param max_top_parts: integer, the number of parts to retrieve for the set_num
- :return: list of strings, each string is a part_num used by the set.
- """
- if not isinstance(set_num, str):
- raise ValueError("set_num must be a string")
- top_parts = []
- #################################################################
- # code to download and process lego parts data #
- # #
- #################################################################
- return top_parts
- def build_graph(self, sets_quantity=5, top_parts_quantity=5, set_ordering=None):
- """
- Downloads data and builds an adjacency list representation of a graph.
- :param sets_quantity: integer, passed to the get_sets() `quantity` argument
- :param top_parts_quantity: integer, passed to the get_top_parts() 'max_top_parts' argument.
- :param set_ordering: string, passed to the get_sets() 'order_by' argument.
- :return: None
- """
- set_nums = self.get_sets(quantity=sets_quantity, order_by=set_ordering)
- for set_num in set_nums:
- #################################################################
- # code to add Lego Set as a node to the adjacency list #
- # #
- #################################################################
- top_parts = self.get_top_parts(set_num, max_top_parts=top_parts_quantity)
- for part_num in top_parts:
- #################################################################
- # code to add Lego as a node to the adjacency list #
- # then, add edges from lego sets to parts #
- #################################################################
- pass
- def out_degree_for_node(self, node_id):
- """
- Calculates the out-degree for a valid node id present in the adjacency list (graph)
- :param node_id: string
- :return: integer, the calculated out-degree
- """
- if not isinstance(node_id, str):
- raise ValueError("node_id must be a string")
- if node_id not in self.adjacency_list.keys():
- raise KeyError("node_id not found in adjacency list")
- #################################################################
- # code to calculate and return out-degree. #
- # #
- #################################################################
- return NotImplemented
- def in_degree_for_node(self, node_id):
- """
- Calculates the in-degree for a valid node id present in the adjacency list (graph)
- :param node_id: string
- :return: integer, the calculated in-degree
- """
- if not isinstance(node_id, str):
- raise ValueError("node_id must be a string")
- if node_id not in self.adjacency_list.keys():
- raise KeyError("node_id not found in adjacency list")
- #################################################################
- # code to calculate and return in-degree. #
- # #
- #################################################################
- return NotImplemented
- def max_in_degree(self):
- """
- Find the node id and in-degree for the node having the highest in degree
- :return: tuple, (string, integer). The string is the node id, the integer is the in-degree
- """
- #################################################################
- # code to calculate and return max in-degree. #
- # #
- #################################################################
- return NotImplemented
- def average_in_degree(self):
- """
- Calculate the average in-degree of all nodes having an in_degree > 0.
- :return: decimal
- """
- #################################################################
- # code to calculate and return avg in-degree. #
- # #
- #################################################################
- return NotImplemented
- def filter_graph_min_in_degree(self, in_degree=0):
- """
- Removes nodes in the adjacency list having an in-degree > 0 AND having an in-degree < in_degree param.
- Does not remove nodes with an in-degree = 0
- :param in_degree: integer
- :return: None
- """
- #################################################################
- # code to filter graph #
- # #
- #################################################################
- pass
- def write_edges_file(self, path="graph.csv"):
- """
- write all edges out as .csv
- :param path: string
- :return: None
- """
- edges_path = path
- edges_file = open(edges_path, 'w')
- edges_file.write("source"+","+"target"+"\n")
- for n in self.adjacency_list.keys():
- adjacent_nodes = self.adjacency_list[n]
- for an in adjacent_nodes:
- edges_file.write(str(n) + "," + str(an) + "\n")
- edges_file.close()
- print("finished writing edges as "+ path)
- def write_adjacency_list(self, path="graph.pickle"):
- """
- Serialize and write out adjacency list object as a .pickle
- :param path: string
- :return: None
- """
- with open(path, 'wb') as handle:
- pickle.dump(self.adjacency_list, handle)
- print("finished writing adjacency list as "+ path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement