Skip to content

Commit

Permalink
В наилучшем виде
Browse files Browse the repository at this point in the history
  • Loading branch information
TreshMom committed Feb 21, 2024
1 parent 7709094 commit f2f1f50
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
4 changes: 3 additions & 1 deletion project/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
print("import sources directory")
from .task_1 import graph_info, create_labeled_two_cycle_graph

__all__ = ["graph_info", "create_labeled_two_cycle_graph"]
23 changes: 13 additions & 10 deletions project/task_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import cfpq_data
from networkx.drawing import nx_pydot as pydot
from typing import Any, Tuple, Set
from typing import Tuple, Set


def get_graph_info(graph: Any) -> Tuple[int, int, Set[str]]:
countNode = graph.number_of_nodes()
countEdges = graph.number_of_edges()
countLabels = set()
def graph_info(name_graph: str) -> Tuple[int, int, Set[str]]:
labels = set()
for _, _, data in name_graph.edges(data=True):
if 'label' in data:
labels.add(data['label'])

for edge in graph.edges:
countLabels.add(edge.attr['label'])

return countNode, countEdges, countLabels
return (
name_graph.number_of_nodes(),
name_graph.number_of_edges(),
labels
)


def save_labeled_two_cycle_graph(
def create_labeled_two_cycle_graph(
countNode1: int, countNode2: int, nameLabels: Tuple[str, str],
path: str) -> None:
pydot.write_dot(
Expand All @@ -24,4 +27,4 @@ def save_labeled_two_cycle_graph(
labels=nameLabels,
),
path=path,
)
)
32 changes: 32 additions & 0 deletions tests/test_task_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import networkx as nx
import os
import tempfile
import pytest
from project import create_labeled_two_cycle_graph, graph_info

@pytest.fixture()
def start():
print("Start test")

def test_create_labeled_two_cycle_graph():
with tempfile.NamedTemporaryFile(suffix='.dot') as tmp_file:
create_labeled_two_cycle_graph(3, 4, ('a', 'b'), tmp_file.name)
assert os.path.exists(tmp_file.name)

G = nx.drawing.nx_pydot.read_dot(tmp_file.name)

assert G.number_of_nodes() == 8
assert G.number_of_edges() == 9
for _, _, data in G.edges(data=True):
assert data['label'] in ['a', 'b']

def test_graph_info():
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
G.add_edges_from([(1, 2, {'label': 'a'}), (2, 3, {'label': 'b'})])

node_count, edge_count, labels = graph_info(G)

assert node_count == 3
assert edge_count == 2
assert labels == {'a', 'b'}

0 comments on commit f2f1f50

Please sign in to comment.