Skip to content

Commit 51bfda6

Browse files
Dominic Pricedominicprice
authored andcommitted
Improve help output of @algo algorithms
1 parent 093b191 commit 51bfda6

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

core/packages/cdb/utils/develop.cnb

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"cell_id": 13334862458884777602,
4848
"cell_origin": "client",
4949
"cell_type": "input",
50-
"source": "from cdb.utils._algorithm import apply_algo_base as _apply_algo_base\n\ndef _cast_algo(apply, can_apply = lambda node: True):\n\tif len(inspect.getfullargspec(apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: function must only take one 'node' parameter\")\n\tif len(inspect.getfullargspec(can_apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: predicate must only take one 'node' parameter\")\n\treturn type(apply.__name__, (Algorithm, ), { \n\t\t\"can_apply\": lambda self, node: can_apply(node), \n\t\t\"apply\": lambda self, node: apply(node)\n\t})\n\ndef _create_algo(cls, pre_order):\n\tif Algorithm not in cls.__bases__:\n\t\traise TypeError(\"Algorithm must derive from Algorithm type\")\n\tdef wrapper(ex, *args, deep=True, repeat=False, depth=0, **kwargs):\n\t\tx = cls(ex, *args, **kwargs)\n\t\treturn _apply_algo_base(x, ex, deep, repeat, depth, pre_order)\n\treturn wrapper"
50+
"source": "from cdb.utils._algorithm import apply_algo_base as _apply_algo_base\n\ndef _cast_algo(apply, can_apply = lambda node: True):\n\tif len(inspect.getfullargspec(apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: function must only take one 'node' parameter\")\n\tif len(inspect.getfullargspec(can_apply).args) != 1:\n\t\traise TypeError(\"Failed to convert function to algorithm: predicate must only take one 'node' parameter\")\n\treturn type(apply.__name__, (Algorithm, ), { \n\t\t\"can_apply\": lambda self, node: can_apply(node), \n\t\t\"apply\": lambda self, node: apply(node),\n\t\t\"__doc__\": apply.__doc__\n\t})\n\ndef _create_algo(cls, pre_order):\n\tif Algorithm not in cls.__bases__:\n\t\traise TypeError(\"Algorithm must derive from Algorithm type\")\n\tdef wrapper(ex, *args, deep=True, repeat=False, depth=0, **kwargs):\n\t\tx = cls(ex, *args, **kwargs)\n\t\treturn _apply_algo_base(x, ex, deep, repeat, depth, pre_order)\n\n\t# Generate correct meta-information\n\twrapper.__name__ = cls.__name__\n\twrapper.__doc__ = cls.__doc__\n\t# Get argument list of Algorithm constructor\n\ttry:\n\t\tparameters = list(inspect.signature(cls\t.__init__).parameters.values())[1:]\n\texcept:\n\t\tparameters = [ inspect.Parameter(\"ex\", inspect.Parameter.POSITIONAL_OR_KEYWORD) ]\n\t# Parameters of apply_algo_base\n\talgo_parameters = [\n\t\tinspect.Parameter(\"deep\", inspect.Parameter.KEYWORD_ONLY, default=True),\n\t\tinspect.Parameter(\"repeat\", inspect.Parameter.KEYWORD_ONLY, default=False),\n\t\tinspect.Parameter(\"depth\", inspect.Parameter.KEYWORD_ONLY, default=0) ]\n\t# If parameters has a **kwargs at the end, algo_parameters must be inserted before this\n\tif parameters[-1].kind == inspect.Parameter.VAR_KEYWORD:\n\t\twrapper.__signature__ = inspect.Signature(parameters[:-1] + algo_parameters + parameters[-1:])\n\telse:\n\t\twrapper.__signature__ = inspect.Signature(parameters + algo_parameters)\n\treturn wrapper"
5151
},
5252
{
5353
"cell_id": 2421359616022599147,
@@ -76,68 +76,86 @@
7676
"cell_type": "input",
7777
"cells": [
7878
{
79-
"cell_id": 4043273741898506206,
79+
"cell_id": 15761994739137138802,
8080
"cell_origin": "server",
8181
"cell_type": "latex_view",
8282
"cells": [
8383
{
84-
"cell_id": 11767503884852531506,
84+
"cell_id": 17415156100168742565,
8585
"cell_origin": "server",
8686
"cell_type": "input_form",
8787
"source": "A^{\\mu} + B^{\\mu}"
8888
}
8989
],
9090
"source": "\\begin{dmath*}{}A^{\\mu}+B^{\\mu}\\end{dmath*}"
91+
},
92+
{
93+
"cell_id": 15144194947899395553,
94+
"cell_origin": "server",
95+
"cell_type": "output",
96+
"source": "\\begin{verbatim}Help on function switch_indices in module __main__:\n\nswitch_indices(ex, *, deep=True, repeat=False, depth=0)\n Switch positions on all indices\n\n\\end{verbatim}"
9197
}
9298
],
9399
"ignore_on_import": true,
94-
"source": "@algo\ndef switch_indices(node):\n\tif node.parent_rel == parent_rel_t.sub:\n\t\tnode.parent_rel = parent_rel_t.super\n\t\treturn result_t.changed\n\tif node.parent_rel == parent_rel_t.super:\n\t\tnode.parent_rel = parent_rel_t.sub\n\t\treturn result_t.changed\n\treturn result_t.unchanged\n\n# also takes optional 'deep', 'repeat' and 'depth' arguments\nswitch_indices($A_{\\mu} + B_{\\mu}$);"
100+
"source": "@algo\ndef switch_indices(node):\n\t\"Switch positions on all indices\"\n\tif node.parent_rel == parent_rel_t.sub:\n\t\tnode.parent_rel = parent_rel_t.super\n\t\treturn result_t.changed\n\tif node.parent_rel == parent_rel_t.super:\n\t\tnode.parent_rel = parent_rel_t.sub\n\t\treturn result_t.changed\n\treturn result_t.unchanged\n\n# also takes optional 'deep', 'repeat' and 'depth' arguments\nswitch_indices($A_{\\mu} + B_{\\mu}$);\nhelp(switch_indices)"
95101
},
96102
{
97103
"cell_id": 9340011096170931285,
98104
"cell_origin": "client",
99105
"cell_type": "input",
100106
"cells": [
101107
{
102-
"cell_id": 14835945528453774882,
108+
"cell_id": 5850074756591759657,
103109
"cell_origin": "server",
104110
"cell_type": "latex_view",
105111
"cells": [
106112
{
107-
"cell_id": 15750930497414250141,
113+
"cell_id": 9620504524171758578,
108114
"cell_origin": "server",
109115
"cell_type": "input_form",
110116
"source": "2y"
111117
}
112118
],
113119
"source": "\\begin{dmath*}{}2y\\end{dmath*}"
120+
},
121+
{
122+
"cell_id": 17850582158135921593,
123+
"cell_origin": "server",
124+
"cell_type": "output",
125+
"source": "\\begin{verbatim}Help on function x_to_y in module __main__:\n\nx_to_y(ex, *, deep=True, repeat=False, depth=0)\n\n\\end{verbatim}"
114126
}
115127
],
116128
"ignore_on_import": true,
117-
"source": "@algo(pre_order=True, pred=lambda node: node.name == 'x')\ndef x_to_y(node):\n\tnode.name = 'y'\n\treturn result_t.changed\n\nx_to_y($x + y$);"
129+
"source": "@algo(pre_order=True, pred=lambda node: node.name == 'x')\ndef x_to_y(node):\n\tnode.name = 'y'\n\treturn result_t.changed\n\nx_to_y($x + y$);\nhelp(x_to_y)"
118130
},
119131
{
120132
"cell_id": 15634397355979861807,
121133
"cell_origin": "client",
122134
"cell_type": "input",
123135
"cells": [
124136
{
125-
"cell_id": 960346398456001684,
137+
"cell_id": 14537821031780487327,
126138
"cell_origin": "server",
127139
"cell_type": "latex_view",
128140
"cells": [
129141
{
130-
"cell_id": 1313190532695810937,
142+
"cell_id": 3417601803473011010,
131143
"cell_origin": "server",
132144
"cell_type": "input_form",
133145
"source": "2t_{\\mu}"
134146
}
135147
],
136148
"source": "\\begin{dmath*}{}2t_{\\mu}\\end{dmath*}"
149+
},
150+
{
151+
"cell_id": 17754394564063656935,
152+
"cell_origin": "server",
153+
"cell_type": "output",
154+
"source": "\\begin{verbatim}Help on function a_to_b in module __main__:\n\na_to_b(ex, a, b, *args, deep=True, repeat=False, depth=0, **kwargs)\n\n\\end{verbatim}"
137155
}
138156
],
139157
"ignore_on_import": true,
140-
"source": "@algo\nclass a_to_b(Algorithm):\n\tdef __init__(self, ex, a, b):\n\t\tAlgorithm.__init__(self, ex)\n\t\tself.a, self.b = a, b\n\t\n\tdef can_apply(self, node):\n\t\treturn node.name == self.a.head()\n\n\tdef apply(self, node):\n\t\tnode.name = self.b.head()\n\t\treturn result_t.changed\n\na_to_b($s_{\\mu} + t_{\\mu}$, $s_{\\mu}$, $t_{\\mu}$);"
158+
"source": "@algo\nclass a_to_b(Algorithm):\n\tdef __init__(self, ex, a, b, *args, **kwargs):\n\t\tAlgorithm.__init__(self, ex)\n\t\tself.a, self.b = a, b\n\t\n\tdef can_apply(self, node):\n\t\treturn node.name == self.a.head()\n\n\tdef apply(self, node):\n\t\tnode.name = self.b.head()\n\t\treturn result_t.changed\n\na_to_b($s_{\\mu} + t_{\\mu}$, $s_{\\mu}$, $t_{\\mu}$);\nhelp(a_to_b)"
141159
},
142160
{
143161
"cell_id": 10135200674124616797,
@@ -173,10 +191,10 @@
173191
"cell_type": "input",
174192
"cells": [
175193
{
176-
"cell_id": 16504503887878721184,
194+
"cell_id": 6295148871247203489,
177195
"cell_origin": "server",
178196
"cell_type": "verbatim",
179-
"source": "\\begin{verbatim}0:00:00.008331\\end{verbatim}"
197+
"source": "\\begin{verbatim}0:00:00.000875\\end{verbatim}"
180198
}
181199
],
182200
"ignore_on_import": true,
@@ -188,10 +206,10 @@
188206
"cell_type": "input",
189207
"cells": [
190208
{
191-
"cell_id": 17622425082194156367,
209+
"cell_id": 13532413926824958590,
192210
"cell_origin": "server",
193211
"cell_type": "verbatim",
194-
"source": "\\begin{verbatim}0:00:00.003194\\end{verbatim}"
212+
"source": "\\begin{verbatim}0:00:00.000493\\end{verbatim}"
195213
}
196214
],
197215
"ignore_on_import": true,
@@ -245,14 +263,14 @@
245263
"cell_type": "input",
246264
"cells": [
247265
{
248-
"cell_id": 17109594158398788775,
266+
"cell_id": 4855635716783607432,
249267
"cell_origin": "server",
250268
"cell_type": "output",
251-
"source": "\\begin{verbatim}sort_sum_test01 passed\nsort_sum_test02 FAILED\n Expected: a + b\n Produced: a + c\nsort_sum_test03 FAILED\n Expected: a + b\n Produced: a + c\n\\end{verbatim}"
269+
"source": "\\begin{verbatim}sort_sum_test01 passed\nsort_sum_test02 FAILED\n Expected: a + b\n Produced: a + c\nsort_sum_test03 FAILED\n Expected: a + b\n Produced: a + c\nRaised CadabraTestError\n\\end{verbatim}"
252270
}
253271
],
254272
"ignore_on_import": true,
255-
"source": "@test_algo($a + b + c$)\ndef sort_sum_test01():\n\tex := b + a + c.\n\treturn sort_sum(ex)\nsort_sum_test01()\n\n@test_algo($a + b$)\ndef sort_sum_test02():\n\tex := c + a.\n\treturn sort_sum(ex)\nsort_sum_test02()\n\n@test_algo($a + b$, throw_on_fail=True)\ndef sort_sum_test03():\n\tex := c + a.\n\treturn sort_sum(ex)\n\ntry:\n\tsort_sum_test03()\n\traise AssertionError(\"CadabraTestError not raised!\")\nexcept CadabraTestError:\n\tpass"
273+
"source": "@test_algo($a + b + c$)\ndef sort_sum_test01():\n\tex := b + a + c.\n\treturn sort_sum(ex)\nsort_sum_test01()\n\n@test_algo($a + b$)\ndef sort_sum_test02():\n\tex := c + a.\n\treturn sort_sum(ex)\nsort_sum_test02()\n\n@test_algo($a + b$, throw_on_fail=True)\ndef sort_sum_test03():\n\tex := c + a.\n\treturn sort_sum(ex)\n\ntry:\n\tsort_sum_test03()\n\traise AssertionError(\"CadabraTestError not raised!\")\nexcept CadabraTestError:\n\tprint(\"Raised CadabraTestError\")"
256274
},
257275
{
258276
"cell_id": 17571440201905887785,

0 commit comments

Comments
 (0)