<commit_before>from zen.graph cimport Graph
from zen.digraph cimport DiGraph
from zen import ZenException

cimport numpy as np

from cpython cimport bool

cdef class Community:
	"""
	This class represents a community that was discovered in a :py:class:`Graph` 
	using one of the community detection algorithms. This class should not be
	instantiated directly but rather obtained from a :py:class:`CommunitySet`.

	Public properties:
	
		* ``community_idx``: the index of this community in the 
		:py:class:`CommunitySet` it originates from.
	"""

	def __init__(Community self, int idx, G, set node_set):

		if type(G) != Graph and type(G) != DiGraph:
			raise ZenException, 'Unknown graph type: %s' % type(G)

		self._graph = G
		self._nodes = node_set
		self.community_idx = idx

	def __len__(Community self):
		return len(self._nodes)

	def __contains__(Community self, nobj):
		"""
		Return ``True`` if this community contains the node with node object
		``nobj``. If ``nobj`` does not belong to the graph associated to this
		community, return ``False``.
		"""
		cdef int nidx
		try:
			nidx = self._graph.node_idx(nobj)
		except KeyError:
			return False
		return self.has_node_index(nidx)


	cpdef bool has_node_index(Community self, int nidx):
		"""
		Return ``True`` if this community contains the node with node index
		``nidx`` 
		"""
		return nidx in self._nodes

cdef class CommunitySet:
	"""
	A set of non-overlapping communities detected in a :py:class:`Graph`. This 
	class should not be instantiated directly but rather obtained from the
	algorithms in the `zen.algorithms.community` package.
	"""
	def __init__(CommunitySet self, G, np.ndarray communities, 
					int num_communities):

		if type(G) != Graph and type(G) != DiGraph:
			raise ZenException, 'Unknown graph type: %s' % type(G)

		self._graph = G
		self._num_communities = num_communities

		# Array of communities, indexed by node indices (i.e. A[i] is the
		# community index of node with index i in graph G).
		self._communities = communities

	cdef void __raise_if_invalid_nidx(CommunitySet self, int nidx) except *:<commit_msg>Added iteration over Community objects.<commit_after>from zen.graph cimport Graph
from zen.digraph cimport DiGraph
from zen import ZenException

cimport numpy as np

from cpython cimport bool

cdef class Community:
	"""
	This class represents a community that was discovered in a :py:class:`Graph` 
	using one of the community detection algorithms. This class should not be
	instantiated directly but rather obtained from a :py:class:`CommunitySet`.

	Public properties:
	
		* ``community_idx``: the index of this community in the 
		:py:class:`CommunitySet` it originates from.
	"""

	def __init__(Community self, int idx, G, set node_set):

		if type(G) != Graph and type(G) != DiGraph:
			raise ZenException, 'Unknown graph type: %s' % type(G)

		self._graph = G
		self._nodes = node_set
		self.community_idx = idx

	def __len__(Community self):
		return len(self._nodes)

	def __contains__(Community self, nobj):
		"""
		Return ``True`` if this community contains the node with node object
		``nobj``. If ``nobj`` does not belong to the graph associated to this
		community, return ``False``.
		"""
		cdef int nidx
		try:
			nidx = self._graph.node_idx(nobj)
		except KeyError:
			return False
		return self.has_node_index(nidx)

	def __iter__(Community self):
		return iter(self._nodes)


	cpdef bool has_node_index(Community self, int nidx):
		"""
		Return ``True`` if this community contains the node with node index
		``nidx`` 
		"""
		return nidx in self._nodes

cdef class CommunitySet:
	"""
	A set of non-overlapping communities detected in a :py:class:`Graph`. This 
	class should not be instantiated directly but rather obtained from the
	algorithms in the `zen.algorithms.community` package.
	"""
	def __init__(CommunitySet self, G, np.ndarray communities, 
					int num_communities):

		if type(G) != Graph and type(G) != DiGraph:
			raise ZenException, 'Unknown graph type: %s' % type(G)

		self._graph = G
		self._num_communities = num_communities

		# Array of communities, indexed by node indices (i.e. A[i] is the
		# community index of node with index i in graph G).
		self._communities = communities

	cdef void __raise_if_invalid_nidx(CommunitySet self, int nidx) except *:
{'fraction_non_alphanumeric': 0.09171116786626422, 'fraction_numerical': 0.0, 'mean_word_length': 4.15311004784689, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1451400', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1296, 'n_words': 505}