Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. import http.client
  2. import json
  3. import time
  4. import timeit
  5. import pickle
  6.  
  7. class Graph:
  8.  
  9. def __init__(self, with_file=None):
  10.  
  11. if with_file is None:
  12. self.adjacency_list = {}
  13. else:
  14. with open(with_file, 'rb') as handle:
  15. self.adjacency_list = pickle.load(handle)
  16.  
  17. self.api_key = None
  18.  
  19. @staticmethod
  20. def user():
  21. """
  22. :return: string
  23. your GTUsername, NOT your 9-Digit GTId
  24. """
  25. return 'gburdell3'
  26.  
  27. def show_graph_info(self):
  28. """
  29. :return: none
  30. prints out the contents of the adjacency list
  31. """
  32. num_nodes = len(self.adjacency_list)
  33. print("nodes: " + str(num_nodes))
  34.  
  35. num_edges = 0
  36. for n in self.adjacency_list.keys():
  37. num_edges += len(self.adjacency_list[n])
  38. print("edges: " + str(num_edges))
  39.  
  40. print("------------------ADJACENCY--LIST---------------------")
  41. for n in self.adjacency_list.keys():
  42. print(str(n) + ": " + str(self.adjacency_list[n]))
  43.  
  44.  
  45. def get_sets(self, quantity=5, order_by=None):
  46.  
  47. """
  48. :param quantity: integer
  49. :param order_by: string
  50. :return: list of strings, each string is a set_num representing a set.
  51. """
  52.  
  53. if not isinstance(order_by, str):
  54. raise ValueError("order_by argument must be a string")
  55.  
  56. lego_sets = []
  57.  
  58. ################################################################
  59. # code to download and process lego sets data #
  60. # #
  61. ################################################################
  62.  
  63. return lego_sets
  64.  
  65.  
  66. def get_top_parts(self, set_num, max_top_parts=5):
  67.  
  68. """
  69. :param set_num: string, a valid set_num
  70. :param max_top_parts: integer, the number of parts to retrieve for the set_num
  71. :return: list of strings, each string is a part_num used by the set.
  72. """
  73.  
  74. if not isinstance(set_num, str):
  75. raise ValueError("set_num must be a string")
  76.  
  77. top_parts = []
  78.  
  79. #################################################################
  80. # code to download and process lego parts data #
  81. # #
  82. #################################################################
  83.  
  84. return top_parts
  85.  
  86.  
  87. def build_graph(self, sets_quantity=5, top_parts_quantity=5, set_ordering=None):
  88.  
  89. """
  90. Downloads data and builds an adjacency list representation of a graph.
  91. :param sets_quantity: integer, passed to the get_sets() `quantity` argument
  92. :param top_parts_quantity: integer, passed to the get_top_parts() 'max_top_parts' argument.
  93. :param set_ordering: string, passed to the get_sets() 'order_by' argument.
  94. :return: None
  95. """
  96.  
  97. set_nums = self.get_sets(quantity=sets_quantity, order_by=set_ordering)
  98.  
  99. for set_num in set_nums:
  100.  
  101. #################################################################
  102. # code to add Lego Set as a node to the adjacency list #
  103. # #
  104. #################################################################
  105.  
  106. top_parts = self.get_top_parts(set_num, max_top_parts=top_parts_quantity)
  107.  
  108. for part_num in top_parts:
  109.  
  110. #################################################################
  111. # code to add Lego as a node to the adjacency list #
  112. # then, add edges from lego sets to parts #
  113. #################################################################
  114. pass
  115.  
  116.  
  117. def out_degree_for_node(self, node_id):
  118. """
  119. Calculates the out-degree for a valid node id present in the adjacency list (graph)
  120. :param node_id: string
  121. :return: integer, the calculated out-degree
  122. """
  123. if not isinstance(node_id, str):
  124. raise ValueError("node_id must be a string")
  125. if node_id not in self.adjacency_list.keys():
  126. raise KeyError("node_id not found in adjacency list")
  127.  
  128. #################################################################
  129. # code to calculate and return out-degree. #
  130. # #
  131. #################################################################
  132. return NotImplemented
  133.  
  134.  
  135. def in_degree_for_node(self, node_id):
  136. """
  137. Calculates the in-degree for a valid node id present in the adjacency list (graph)
  138. :param node_id: string
  139. :return: integer, the calculated in-degree
  140. """
  141. if not isinstance(node_id, str):
  142. raise ValueError("node_id must be a string")
  143. if node_id not in self.adjacency_list.keys():
  144. raise KeyError("node_id not found in adjacency list")
  145.  
  146. #################################################################
  147. # code to calculate and return in-degree. #
  148. # #
  149. #################################################################
  150. return NotImplemented
  151.  
  152.  
  153. def max_in_degree(self):
  154.  
  155. """
  156. Find the node id and in-degree for the node having the highest in degree
  157. :return: tuple, (string, integer). The string is the node id, the integer is the in-degree
  158.  
  159. """
  160.  
  161. #################################################################
  162. # code to calculate and return max in-degree. #
  163. # #
  164. #################################################################
  165.  
  166. return NotImplemented
  167.  
  168. def average_in_degree(self):
  169. """
  170. Calculate the average in-degree of all nodes having an in_degree > 0.
  171. :return: decimal
  172. """
  173. #################################################################
  174. # code to calculate and return avg in-degree. #
  175. # #
  176. #################################################################
  177. return NotImplemented
  178.  
  179.  
  180. def filter_graph_min_in_degree(self, in_degree=0):
  181. """
  182. Removes nodes in the adjacency list having an in-degree > 0 AND having an in-degree < in_degree param.
  183. Does not remove nodes with an in-degree = 0
  184. :param in_degree: integer
  185. :return: None
  186. """
  187.  
  188. #################################################################
  189. # code to filter graph #
  190. # #
  191. #################################################################
  192. pass
  193.  
  194. def write_edges_file(self, path="graph.csv"):
  195. """
  196. write all edges out as .csv
  197. :param path: string
  198. :return: None
  199. """
  200. edges_path = path
  201. edges_file = open(edges_path, 'w')
  202.  
  203. edges_file.write("source"+","+"target"+"\n")
  204.  
  205. for n in self.adjacency_list.keys():
  206. adjacent_nodes = self.adjacency_list[n]
  207. for an in adjacent_nodes:
  208. edges_file.write(str(n) + "," + str(an) + "\n")
  209.  
  210. edges_file.close()
  211. print("finished writing edges as "+ path)
  212.  
  213.  
  214. def write_adjacency_list(self, path="graph.pickle"):
  215. """
  216. Serialize and write out adjacency list object as a .pickle
  217. :param path: string
  218. :return: None
  219. """
  220. with open(path, 'wb') as handle:
  221. pickle.dump(self.adjacency_list, handle)
  222. print("finished writing adjacency list as "+ path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement