Skip to content

Commit 4e176d3

Browse files
committed
feat: added create_famous_graph() and eperimented with string handling
1 parent cf09bb4 commit 4e176d3

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

src/codegen/types.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ DOUBLE:
2929

3030
CSTRING:
3131
PY_TYPE: str
32+
INCONV:
33+
IN: '%C% = %I%.encode("utf-8")'
34+
OUT: "%C% = c_char_p()"
35+
OUTCONV: '%I% = %C%.value.decode("utf-8", "replace")'
3236

3337
VECTOR:
3438
PY_TYPE: Iterable[float]

src/igraph_ctypes/_internal/functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,11 +655,11 @@ def kautz(m: int, n: int) -> _Graph:
655655
return graph
656656

657657

658-
def famous(name: str = "") -> _Graph:
658+
def famous(name: str) -> _Graph:
659659
"""Type-annotated wrapper for ``igraph_famous``."""
660660
# Prepare input arguments
661661
c_graph = _Graph()
662-
c_name = name
662+
c_name = name.encode("utf-8")
663663

664664
# Call wrapped function
665665
igraph_famous(c_graph, c_name)
@@ -5844,7 +5844,7 @@ def vertex_path_from_edge_path(graph: _Graph, start: VertexLike, edge_path: Iter
58445844
def version() -> Tuple[str, int, int, int]:
58455845
"""Type-annotated wrapper for ``igraph_version``."""
58465846
# Prepare input arguments
5847-
c_version_string = None
5847+
c_version_string = c_char_p()
58485848
c_major = c_int()
58495849
c_minor = c_int()
58505850
c_subminor = c_int()
@@ -5853,7 +5853,7 @@ def version() -> Tuple[str, int, int, int]:
58535853
igraph_version(c_version_string, c_major, c_minor, c_subminor)
58545854

58555855
# Prepare output arguments
5856-
version_string = c_version_string.value
5856+
version_string = c_version_string.value.decode("utf-8", "replace")
58575857
major = c_major.value
58585858
minor = c_minor.value
58595859
subminor = c_subminor.value

src/igraph_ctypes/constructors.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Iterable
22

33
from .graph import Graph
4-
from ._internal.functions import create
4+
from ._internal.functions import create, famous
55

66
__all__ = ("create_empty_graph", "create_graph_from_edge_list")
77

@@ -16,6 +16,18 @@ def create_empty_graph(n: int, directed: bool = False) -> Graph:
1616
return Graph(n, directed)
1717

1818

19+
def create_famous_graph(name: str) -> Graph:
20+
"""Creates one of the "famous" graphs embedded into igraph by name.
21+
22+
See the documentation of the ``igraph_famous()`` function in igraph's C core
23+
for a list of names accepted by this function.
24+
25+
Parameters:
26+
name: the name of the graph to construct
27+
"""
28+
return Graph(_wrap=famous(name))
29+
30+
1931
def create_graph_from_edge_list(
2032
edges: Iterable[int], n: int = 0, directed: bool = False
2133
) -> Graph:

test/test_constructors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
from numpy import array, ndarray
44

5-
from igraph_ctypes.constructors import create_graph_from_edge_list
5+
from igraph_ctypes.constructors import create_famous_graph, create_graph_from_edge_list
6+
7+
8+
def test_create_famous_graph():
9+
g = create_famous_graph("zachary")
10+
assert not g.is_directed() and g.vcount() == 34 and g.ecount() == 78
11+
12+
g = create_famous_graph("petersen")
13+
assert not g.is_directed() and g.vcount() == 10 and g.ecount() == 15
614

715

816
@pytest.mark.parametrize(

test/test_misc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from igraph_ctypes._internal.functions import version
2+
3+
4+
def test_version():
5+
version_str, major, minor, patch = version()
6+
assert major >= 0 and major <= 255
7+
assert minor >= (10 if major == 0 else 0) and minor <= 255
8+
assert patch >= 0 and patch <= 255
9+
assert version_str.startswith(f"{major}.{minor}.{patch}")

0 commit comments

Comments
 (0)