Eche provides a ClusterHelper
class to conveniently handle entity clusters.
from eche import ClusterHelper
ch = ClusterHelper([{"a1", "b1"}, {"a2", "b2"}])
print(ch.clusters)
{0: {'a1', 'b1'}, 1: {'a2', 'b2'}}
Add an element to a cluster
ch.add_to_cluster(0, "c1")
print(ch.clusters)
{0: {'a1', 'b1', 'c1'}, 1: {'a2', 'b2'}}
Add a new cluster
ch.add({"e2", "f1", "c3"})
print(ch.clusters)
{0: {'a1', 'b1', 'c1'}, 1: {'a2', 'b2'}, 2: {'f1', 'e2', 'c3'}}
Remove an element from a cluster
ch.remove("b1")
print(ch.clusters)
{0: {'a1', 'c1'}, 1: {'a2', 'b2'}, 2: {'f1', 'e2', 'c3'}}
The __contains__
function is smartly overloaded. You can check if an entity is in the ClusterHelper
:
"a1" in ch
# True
If a cluster is present
{"c1","a1"} in ch
# True
And even if a link exists or not
("f1","e2") in ch
# True
("a1","e2") in ch
# False
To know the cluster id of an entity you can look it up with
print(ch.elements["a1"])
0
To get members of a cluster either use
print(ch.members(0))
{'a1', 'b1', 'c1'}
or simply
print(ch[0])
{'a1', 'b1', 'c1'}
More functions can be found in the Documentation.
Simply use pip
for installation:
pip install eche