Beispiel #1
0
 def __init__(self, graph=None, file=None, logger=None):
     '''
     init
         Parameters:
             graph: the existing networkx graph, default to None (Graph)
             file: the text file representing the graph
     '''
     if graph is None and file is None:
         self._g = nx.Graph()
     elif graph is not None:
         self._g = graph
     else:
         read = False
         with open(file) as f:
             content = f.read()
             lines = content.replace("\r", "")
             lines = lines.split("\n")
             self._g = text_to_networkx(lines)
             read = True
         if not read:
             raise Exception("Not a valid file")
     if logger is not None:
         self.logger = logger
     else:
         logging.basicConfig(level=logging.DEBUG,
                         format='%(asctime)s %(message)s')
         self.logger = logging.getLogger(__name__)
Beispiel #2
0
 def load(self, file):
     """
     a method to help load a graph from a file
     Parameters:
         file: the graph file (filepath)
     Returns:
         G: the graph G (networkx)
     """
     fp = os.path.join(self.directory, file)
     with open(fp) as file:
         text = file.read()
         lines = text.split("\n")
         G = text_to_networkx(lines)
     return G
Beispiel #3
0
 def testSaveBasic(self):
     g = make_claw()
     f = File(self.directory, G=g, logger=self.logger)
     expect = f.file_path(f.get_name())
     self.assertEqual(os.path.exists(expect), False)
     f.save()
     self.assertEqual(os.path.exists(expect), True)
     # make sure the graph was valid
     with open(expect) as file:
         text = file.read()
         lines = text.split("\n")
         claw = text_to_networkx(lines)
         self.assertEqual(g.nodes(), claw.nodes())
         self.assertEqual(g.edges(), claw.edges())