def test_read_pajek(): from linkpred.linkpred import _read_pajek with temp_file(suffix=".net") as fname: with open(fname, "w") as fh: fh.write("""*vertices 2 1 "A" 2 "B" *arcs 2 1 2 1 2""") expected = nx.DiGraph() expected.add_edges_from([("A", "B")]) G = _read_pajek(fname) assert_is_instance(G, nx.DiGraph) assert_equal(sorted(G.edges()), sorted(expected.edges())) with open(fname, "w") as fh: fh.write("""*vertices 2 1 "A" 2 "B" *edges 2 1 2 1 2""") expected = nx.Graph() expected.add_edges_from([("A", "B")]) G = _read_pajek(fname) assert_is_instance(G, nx.Graph) assert_equal(sorted(G.edges()), sorted(expected.edges()))
def test_from_file(self): with temp_file() as fname: with open(fname, "wb") as fh: fh.write(self.expected) sheet = Scoresheet.from_file(fname) assert_dict_equal(sheet, self.sheet)
def test_to_file_from_file(self): data = np.array([[1, 0, 0, 1], [1, 1, 0, 0]]) sheet = EvaluationSheet(data) with temp_file() as fname: sheet.to_file(fname) newsheet = EvaluationSheet.from_file(fname) assert_array_equal(sheet.data, newsheet.data)
def _playbook(self): template_file = os.path.join(config.template_path, self._template_name()) with open(template_file) as f: template = f.read() data = str(Template(template, searchList=[self.params])) self.log.debug("Playbook content:\n%s", data) with utils.temp_file() as path: with open(path, 'w') as f: f.write(data) yield path
def test_read_network(): with temp_file(suffix=".net") as fname: with open(fname, "w") as fh: fh.write("""*vertices 2 1 "A" 2 "B" *arcs 2 1 2 2 1""") expected = nx.DiGraph() expected.add_edges_from([("A", "B"), ("B", "A")]) G = linkpred.read_network(fname) assert_equal(set(G.edges()), set(expected.edges())) with open(fname) as fh: G = linkpred.read_network(fname) assert_equal(set(G.edges()), set(expected.edges()))
def test_read_unknown_network_type(): with temp_file(suffix=".foo") as fname: linkpred.read_network(fname)
def test_to_file(self): with temp_file() as fname: self.sheet.to_file(fname) with open(fname, "rb") as fh: assert_equal(fh.read(), self.expected)
def test_to_file_from_file(self): with temp_file() as fname: self.scoresheet.to_file(fname) newsheet = BaseScoresheet.from_file(fname) assert_dict_equal(self.scoresheet, newsheet)