Skip to content

Commit f81513f

Browse files
committed
add: local seed with example in initial workflow
1 parent 09e78ac commit f81513f

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

doc/source/community_detection_guide/notebooks/functions.ipynb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,30 @@
1616
"outputs": [],
1717
"source": [
1818
"import igraph as ig\n",
19+
"import random\n",
20+
"from contextlib import contextmanager\n",
1921
"ig.config[\"plotting.backend\"] = \"matplotlib\""
2022
]
2123
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"id": "d2d768d4-a5d4-4c72-b044-25ea095a7c0d",
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"@contextmanager\n",
32+
"def local_random(seed=None):\n",
33+
" \"\"\"Temporarily set the random state, restoring it afterwards.\"\"\"\n",
34+
" state = random.getstate()\n",
35+
" if seed is not None:\n",
36+
" random.seed(seed)\n",
37+
" try:\n",
38+
" yield\n",
39+
" finally:\n",
40+
" random.setstate(state)"
41+
]
42+
},
2243
{
2344
"cell_type": "code",
2445
"execution_count": 2,

doc/source/community_detection_guide/notebooks/initial_workflow.ipynb

Lines changed: 51 additions & 32 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)