11import numpy as np
22
33from numpy .typing import DTypeLike
4- from typing import Any , Iterable
4+ from typing import Any , Iterable , Type
55
66from .enums import AttributeType
77from ..types import (
1010)
1111
1212__all__ = (
13- "get_igraph_attribute_type_from_iterable" ,
14- "get_numpy_attribute_type_from_iterable" ,
1513 "igraph_to_numpy_attribute_type" ,
14+ "iterable_to_igraph_attribute_type" ,
15+ "iterable_to_numpy_attribute_type" ,
16+ "python_type_to_igraph_attribute_type" ,
1617)
1718
1819
19- def get_igraph_attribute_type_from_iterable ( # noqa: C901
20+ def igraph_to_numpy_attribute_type (type : AttributeType ) -> DTypeLike :
21+ """Converts an igraph attribute type to an equivalent NumPy data type."""
22+ if type is AttributeType .BOOLEAN :
23+ return np_type_of_igraph_bool_t
24+ elif type is AttributeType .NUMERIC :
25+ return np_type_of_igraph_real_t
26+ else :
27+ return np .object_
28+
29+
30+ def iterable_to_igraph_attribute_type (
2031 it : Iterable [Any ],
2132) -> AttributeType :
2233 """Determines the appropriate igraph attribute type to store all the items
@@ -31,26 +42,18 @@ def get_igraph_attribute_type_from_iterable( # noqa: C901
3142 # Iterable empty
3243 return AttributeType .UNSPECIFIED
3344
34- best_fit : AttributeType
35- if isinstance (item , bool ):
36- best_fit = AttributeType .BOOLEAN
37- elif isinstance (item , (int , float , np .number )):
38- best_fit = AttributeType .NUMERIC
39- elif isinstance (item , str ):
40- best_fit = AttributeType .STRING
41- else :
42- return AttributeType .OBJECT
43-
45+ best_fit = python_type_to_igraph_attribute_type (type (item ))
4446 for item in it :
45- if isinstance (item , bool ):
47+ next_type = python_type_to_igraph_attribute_type (type (item ))
48+ if next_type is AttributeType .BOOLEAN :
4649 if best_fit == AttributeType .STRING :
4750 return AttributeType .OBJECT
48- elif isinstance ( item , ( int , float , np . number )) :
51+ elif next_type is AttributeType . NUMERIC :
4952 if best_fit == AttributeType .STRING :
5053 return AttributeType .OBJECT
5154 else :
5255 best_fit = AttributeType .NUMERIC
53- elif isinstance ( item , str ) :
56+ elif next_type is AttributeType . STRING :
5457 if best_fit != AttributeType .STRING :
5558 return AttributeType .OBJECT
5659 else :
@@ -59,9 +62,7 @@ def get_igraph_attribute_type_from_iterable( # noqa: C901
5962 return best_fit
6063
6164
62- def get_numpy_attribute_type_from_iterable (
63- it : Iterable [Any ],
64- ) -> DTypeLike :
65+ def iterable_to_numpy_attribute_type (it : Iterable [Any ]) -> DTypeLike :
6566 """Determines the appropriate NumPy attribute type to store all the items
6667 found in the given iterable as an attribute.
6768
@@ -70,17 +71,33 @@ def get_numpy_attribute_type_from_iterable(
7071
7172 When the iterable is empty, a numeric attribute will be assumed.
7273 """
73- attr_type = get_igraph_attribute_type_from_iterable (it )
74+ attr_type = iterable_to_igraph_attribute_type (it )
7475 if attr_type is AttributeType .UNSPECIFIED :
7576 attr_type = AttributeType .NUMERIC
7677 return igraph_to_numpy_attribute_type (attr_type )
7778
7879
79- def igraph_to_numpy_attribute_type (type : AttributeType ) -> DTypeLike :
80- """Converts an igraph attribute type to an equivalent NumPy data type."""
81- if type is AttributeType .BOOLEAN :
82- return np_type_of_igraph_bool_t
83- elif type is AttributeType .NUMERIC :
84- return np_type_of_igraph_real_t
85- else :
86- return np .object_
80+ def python_object_to_igraph_attribute_type (obj : Any ) -> AttributeType :
81+ """Converts the given Python object into the most fitting igraph attribute
82+ type.
83+ """
84+ if isinstance (obj , (bool , np .bool_ )):
85+ return AttributeType .BOOLEAN
86+ if isinstance (obj , (int , float , np .number )):
87+ return AttributeType .NUMERIC
88+ if isinstance (obj , str ):
89+ return AttributeType .STRING
90+ return AttributeType .OBJECT
91+
92+
93+ def python_type_to_igraph_attribute_type (obj : Type [Any ]) -> AttributeType :
94+ """Converts the given Python type into the most fitting igraph attribute
95+ type.
96+ """
97+ if issubclass (obj , (bool , np .bool_ )):
98+ return AttributeType .BOOLEAN
99+ if issubclass (obj , (int , float , np .number )):
100+ return AttributeType .NUMERIC
101+ if issubclass (obj , str ):
102+ return AttributeType .STRING
103+ return AttributeType .OBJECT
0 commit comments