Skip to content

Commit

Permalink
Modify API
Browse files Browse the repository at this point in the history
  • Loading branch information
weilycoder committed Oct 8, 2024
1 parent 3ca37c8 commit 316ebdc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
9 changes: 4 additions & 5 deletions cyaron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ def to_str(self, **kwargs):
**kwargs(Keyword args):
bool shuffle = False -> whether shuffle the output or not
str output(Edge) = str -> the convert function which converts object Edge to str. the default way is to use str()
list[int] node_shuffler(list[int])
= lambda table: random.sample(table, k=len(table))
list[int] node_shuffler(int)
= lambda n: random.sample(range(1, n + 1), k=n)
-> the random function which shuffles the vertex sequence.
Note that this function will actually be passed in a `range`!
list[Edge] edge_shuffler(list[Edge])
-> a random function. the default is to shuffle the edge sequence,
also, if the graph is undirected, it will swap `u` and `v` randomly.
Expand All @@ -96,10 +95,10 @@ def _edge_shuffler_default(table):

shuffle = kwargs.get("shuffle", False)
output = kwargs.get("output", str)
node_shuffler = kwargs.get("node_shuffler", lambda table: random.sample(table, k=len(table)))
node_shuffler = kwargs.get("node_shuffler", lambda n: random.sample(range(1, n + 1), k=n))
edge_shuffler = kwargs.get("edge_shuffler", _edge_shuffler_default)
if shuffle:
new_node_id = [0] + node_shuffler(range(1, len(self.edges)))
new_node_id = [0] + node_shuffler(self.vertex_count())
edge_buf = []
for edge in self.iterate_edges():
edge_buf.append(
Expand Down
3 changes: 1 addition & 2 deletions cyaron/tests/graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,8 @@ def unit_test(n, m, shuffle_kwargs = {}, check_kwargs = {}):
unit_test(8, 20)
unit_test(8, 20, {"shuffle": True})
mapping = [0] + random.sample(range(1, 8), k = 7)
shuffer = lambda seq: list(map(lambda i: mapping[i], seq))
shuffer = lambda n: list(map(lambda i: mapping[i], range(1, n + 1)))
unit_test(7, 10, {"shuffle": True, "node_shuffler": shuffer})
unit_test(7, 14, {"shuffle": True, "node_shuffler": shuffer}, {"mapping": mapping})
shuffer_without_swap = lambda table: random.sample(table, k=len(table))
unit_test(7, 12, {"shuffle": True, "edge_shuffler": shuffer_without_swap}, {"directed": True})

0 comments on commit 316ebdc

Please sign in to comment.