|
24 | 24 | "from contextlib import contextmanager\n",
|
25 | 25 | "\n",
|
26 | 26 | "from varname import (\n",
|
27 |
| - " varname, nameof, will, argname,\n", |
| 27 | + " varname, will, argname,\n", |
28 | 28 | " config,\n",
|
29 | 29 | " ImproperUseError, VarnameRetrievingError, QualnameNonUniqueError\n",
|
30 | 30 | ")\n",
|
|
43 | 43 | " try:\n",
|
44 | 44 | " yield\n",
|
45 | 45 | " except error as exc:\n",
|
46 |
| - " print(f'{error.__name__} raised!')\n", |
| 46 | + " print(f'{type(exc).__name__} raised!')\n", |
47 | 47 | " else:\n",
|
48 | 48 | " raise Exception(f'{error.__name__!r} NOT raised!')"
|
49 | 49 | ]
|
|
382 | 382 | "name": "stderr",
|
383 | 383 | "output_type": "stream",
|
384 | 384 | "text": [
|
385 |
| - "/workspaces/python-varname/varname/ignore.py:175: MaybeDecoratedFunctionWarning: You asked varname to ignore function 'wrapper', which may be decorated. If it is not intended, you may need to ignore all intermediate frames with a tuple of the function and the number of its decorators.\n", |
| 385 | + "/home/pwwang/github/python-varname/varname/ignore.py:175: MaybeDecoratedFunctionWarning: You asked varname to ignore function 'wrapper', which may be decorated. If it is not intended, you may need to ignore all intermediate frames with a tuple of the function and the number of its decorators.\n", |
386 | 386 | " warnings.warn(\n"
|
387 | 387 | ]
|
388 | 388 | }
|
|
471 | 471 | "output_type": "stream",
|
472 | 472 | "text": [
|
473 | 473 | "[varname] DEBUG: >>> IgnoreList initiated <<<\n",
|
474 |
| - "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /workspaces/python-varname/varname/core.py:105]\n", |
475 |
| - "[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func' at /workspaces/python-varname/playground/module_all_calls.py:6]\n", |
476 |
| - "[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func2' at /workspaces/python-varname/playground/module_all_calls.py:9]\n", |
477 |
| - "[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func3' at /workspaces/python-varname/playground/module_all_calls.py:12]\n", |
478 |
| - "[varname] DEBUG: Skipping (0 more to skip) [In 'func' at /tmp/ipykernel_16149/3068660293.py:4]\n", |
479 |
| - "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_16149/3068660293.py:7]\n" |
| 474 | + "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:105]\n", |
| 475 | + "[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func' at /home/pwwang/github/python-varname/playground/module_all_calls.py:6]\n", |
| 476 | + "[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func2' at /home/pwwang/github/python-varname/playground/module_all_calls.py:9]\n", |
| 477 | + "[varname] DEBUG: Ignored by IgnoreModule('module_all_calls') [In 'func3' at /home/pwwang/github/python-varname/playground/module_all_calls.py:12]\n", |
| 478 | + "[varname] DEBUG: Skipping (0 more to skip) [In 'func' at /tmp/ipykernel_316759/3068660293.py:4]\n", |
| 479 | + "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_316759/3068660293.py:7]\n" |
480 | 480 | ]
|
481 | 481 | },
|
482 | 482 | {
|
|
527 | 527 | "output_type": "stream",
|
528 | 528 | "text": [
|
529 | 529 | "[varname] DEBUG: >>> IgnoreList initiated <<<\n",
|
530 |
| - "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /workspaces/python-varname/varname/core.py:105]\n", |
531 |
| - "[varname] DEBUG: Ignored by IgnoreModuleQualname('module_glob_qualname', '_func*') [In '_func' at /workspaces/python-varname/playground/module_glob_qualname.py:6]\n", |
532 |
| - "[varname] DEBUG: Ignored by IgnoreModuleQualname('module_glob_qualname', '_func*') [In '_func2' at /workspaces/python-varname/playground/module_glob_qualname.py:9]\n", |
533 |
| - "[varname] DEBUG: Skipping (0 more to skip) [In 'func3' at /workspaces/python-varname/playground/module_glob_qualname.py:12]\n", |
534 |
| - "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_16149/491507787.py:4]\n" |
| 530 | + "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:105]\n", |
| 531 | + "[varname] DEBUG: Ignored by IgnoreModuleQualname('module_glob_qualname', '_func*') [In '_func' at /home/pwwang/github/python-varname/playground/module_glob_qualname.py:6]\n", |
| 532 | + "[varname] DEBUG: Ignored by IgnoreModuleQualname('module_glob_qualname', '_func*') [In '_func2' at /home/pwwang/github/python-varname/playground/module_glob_qualname.py:9]\n", |
| 533 | + "[varname] DEBUG: Skipping (0 more to skip) [In 'func3' at /home/pwwang/github/python-varname/playground/module_glob_qualname.py:12]\n", |
| 534 | + "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_316759/491507787.py:4]\n" |
535 | 535 | ]
|
536 | 536 | },
|
537 | 537 | {
|
|
614 | 614 | "output_type": "stream",
|
615 | 615 | "text": [
|
616 | 616 | "[varname] DEBUG: >>> IgnoreList initiated <<<\n",
|
617 |
| - "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /workspaces/python-varname/varname/core.py:105]\n", |
618 |
| - "[varname] DEBUG: Skipping (0 more to skip) [In 'func' at /tmp/ipykernel_16149/2761136102.py:2]\n", |
619 |
| - "[varname] DEBUG: Ignored by IgnoreOnlyQualname(None, '*<lambda>') [In '<lambda>' at /tmp/ipykernel_16149/2761136102.py:4]\n", |
620 |
| - "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_16149/2761136102.py:7]\n" |
| 617 | + "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:105]\n", |
| 618 | + "[varname] DEBUG: Skipping (0 more to skip) [In 'func' at /tmp/ipykernel_316759/2761136102.py:2]\n", |
| 619 | + "[varname] DEBUG: Ignored by IgnoreOnlyQualname(None, '*<lambda>') [In '<lambda>' at /tmp/ipykernel_316759/2761136102.py:4]\n", |
| 620 | + "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_316759/2761136102.py:7]\n" |
621 | 621 | ]
|
622 | 622 | },
|
623 | 623 | {
|
|
667 | 667 | "output_type": "stream",
|
668 | 668 | "text": [
|
669 | 669 | "[varname] DEBUG: >>> IgnoreList initiated <<<\n",
|
670 |
| - "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /workspaces/python-varname/varname/core.py:105]\n", |
671 |
| - "[varname] DEBUG: Skipping (0 more to skip) [In '__init__' at /tmp/ipykernel_16149/641638691.py:8]\n", |
672 |
| - "[varname] DEBUG: Ignored by IgnoreStdlib('/usr/local/python/3.10.13/lib/python3.10/') [In '__call__' at /home/codespace/.python/current/lib/python3.10/typing.py:957]\n", |
673 |
| - "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_16149/641638691.py:11]\n" |
| 670 | + "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:105]\n", |
| 671 | + "[varname] DEBUG: Skipping (0 more to skip) [In '__init__' at /tmp/ipykernel_316759/641638691.py:8]\n", |
| 672 | + "[varname] DEBUG: Ignored by IgnoreStdlib('/home/pwwang/miniconda3/lib/python3.12/') [In '__call__' at /home/pwwang/miniconda3/lib/python3.12/typing.py:1140]\n", |
| 673 | + "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_316759/641638691.py:11]\n" |
674 | 674 | ]
|
675 | 675 | },
|
676 | 676 | {
|
|
812 | 812 | "output_type": "stream",
|
813 | 813 | "text": [
|
814 | 814 | "[varname] DEBUG: >>> IgnoreList initiated <<<\n",
|
815 |
| - "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /workspaces/python-varname/varname/core.py:105]\n", |
816 |
| - "[varname] DEBUG: Ignored by IgnoreDecorated('wrapper', 2) [In 'func' at /tmp/ipykernel_16149/652967550.py:2]\n", |
817 |
| - "[varname] DEBUG: Skipping (1 more to skip) [In 'wrapper' at /tmp/ipykernel_16149/652967550.py:9]\n", |
818 |
| - "[varname] DEBUG: Skipping (0 more to skip) [In 'func3' at /tmp/ipykernel_16149/652967550.py:18]\n", |
819 |
| - "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_16149/652967550.py:21]\n" |
| 815 | + "[varname] DEBUG: Ignored by IgnoreModule('varname') [In 'varname' at /home/pwwang/github/python-varname/varname/core.py:105]\n", |
| 816 | + "[varname] DEBUG: Ignored by IgnoreDecorated('wrapper', 2) [In 'func' at /tmp/ipykernel_316759/652967550.py:2]\n", |
| 817 | + "[varname] DEBUG: Skipping (1 more to skip) [In 'wrapper' at /tmp/ipykernel_316759/652967550.py:9]\n", |
| 818 | + "[varname] DEBUG: Skipping (0 more to skip) [In 'func3' at /tmp/ipykernel_316759/652967550.py:18]\n", |
| 819 | + "[varname] DEBUG: Gotcha! [In '<module>' at /tmp/ipykernel_316759/652967550.py:21]\n" |
820 | 820 | ]
|
821 | 821 | },
|
822 | 822 | {
|
|
883 | 883 | "2 (a, b) = ('a', 'b')\n",
|
884 | 884 | "3 a = ('a',)\n",
|
885 | 885 | "4 (a, b, c) = ('a', 'b', 'c')\n",
|
886 |
| - "5 (a, b, x.c) = ('a', 'b', 'c')\n", |
887 |
| - "ImproperUseError raised!\n" |
| 886 | + "5 (a, b, x.c) = ('a', 'b', 'x.c')\n", |
| 887 | + "TypeError raised!\n" |
888 | 888 | ]
|
889 | 889 | }
|
890 | 890 | ],
|
|
910 | 910 | "a, (b, x.c) = function()\n",
|
911 | 911 | "print(5, '(a, b, x.c) =', (a, b, x.c))\n",
|
912 | 912 | "\n",
|
913 |
| - "with expect_raising(ImproperUseError):\n", |
| 913 | + "with expect_raising((ImproperUseError, TypeError)):\n", |
914 | 914 | " a, b[0] = function()"
|
915 | 915 | ]
|
916 | 916 | },
|
|
998 | 998 | "name": "stderr",
|
999 | 999 | "output_type": "stream",
|
1000 | 1000 | "text": [
|
1001 |
| - "/workspaces/python-varname/varname/core.py:124: MultiTargetAssignmentWarning: Multiple targets in assignment, variable name on the very right is used. \n", |
| 1001 | + "/home/pwwang/github/python-varname/varname/core.py:124: MultiTargetAssignmentWarning: Multiple targets in assignment, variable name on the very right is used. \n", |
1002 | 1002 | " warnings.warn(\n"
|
1003 | 1003 | ]
|
1004 | 1004 | }
|
|
1012 | 1012 | "# f1 == f2 == 'f1' when varname < 0.8"
|
1013 | 1013 | ]
|
1014 | 1014 | },
|
1015 |
| - { |
1016 |
| - "attachments": {}, |
1017 |
| - "cell_type": "markdown", |
1018 |
| - "metadata": {}, |
1019 |
| - "source": [ |
1020 |
| - "## Use of `nameof`" |
1021 |
| - ] |
1022 |
| - }, |
1023 |
| - { |
1024 |
| - "cell_type": "code", |
1025 |
| - "execution_count": 22, |
1026 |
| - "metadata": { |
1027 |
| - "execution": { |
1028 |
| - "iopub.execute_input": "2021-08-13T18:11:04.767662Z", |
1029 |
| - "iopub.status.busy": "2021-08-13T18:11:04.767117Z", |
1030 |
| - "iopub.status.idle": "2021-08-13T18:11:04.772991Z", |
1031 |
| - "shell.execute_reply": "2021-08-13T18:11:04.773352Z" |
1032 |
| - } |
1033 |
| - }, |
1034 |
| - "outputs": [ |
1035 |
| - { |
1036 |
| - "name": "stdout", |
1037 |
| - "output_type": "stream", |
1038 |
| - "text": [ |
1039 |
| - "x\n" |
1040 |
| - ] |
1041 |
| - } |
1042 |
| - ], |
1043 |
| - "source": [ |
1044 |
| - "x = lambda: None\n", |
1045 |
| - "print(nameof(x))" |
1046 |
| - ] |
1047 |
| - }, |
1048 |
| - { |
1049 |
| - "attachments": {}, |
1050 |
| - "cell_type": "markdown", |
1051 |
| - "metadata": {}, |
1052 |
| - "source": [ |
1053 |
| - "Get full name of a chain of attributes" |
1054 |
| - ] |
1055 |
| - }, |
1056 |
| - { |
1057 |
| - "cell_type": "code", |
1058 |
| - "execution_count": 23, |
1059 |
| - "metadata": { |
1060 |
| - "execution": { |
1061 |
| - "iopub.execute_input": "2021-08-13T18:11:04.778858Z", |
1062 |
| - "iopub.status.busy": "2021-08-13T18:11:04.777851Z", |
1063 |
| - "iopub.status.idle": "2021-08-13T18:11:04.797968Z", |
1064 |
| - "shell.execute_reply": "2021-08-13T18:11:04.797563Z" |
1065 |
| - } |
1066 |
| - }, |
1067 |
| - "outputs": [ |
1068 |
| - { |
1069 |
| - "name": "stdout", |
1070 |
| - "output_type": "stream", |
1071 |
| - "text": [ |
1072 |
| - "x.a\n", |
1073 |
| - "x.a.b\n", |
1074 |
| - "('x.a', 'x.a.b')\n", |
1075 |
| - "x.a()\n" |
1076 |
| - ] |
1077 |
| - } |
1078 |
| - ], |
1079 |
| - "source": [ |
1080 |
| - "x.a = x\n", |
1081 |
| - "x.a.b = x\n", |
1082 |
| - "\n", |
1083 |
| - "print(nameof(x.a, vars_only=False))\n", |
1084 |
| - "print(nameof(x.a.b, vars_only=False))\n", |
1085 |
| - "print(nameof(x.a, x.a.b, vars_only=False))\n", |
1086 |
| - "print(nameof(x.a(), vars_only=False))" |
1087 |
| - ] |
1088 |
| - }, |
1089 |
| - { |
1090 |
| - "attachments": {}, |
1091 |
| - "cell_type": "markdown", |
1092 |
| - "metadata": {}, |
1093 |
| - "source": [ |
1094 |
| - "If you want to wrap `nameof`" |
1095 |
| - ] |
1096 |
| - }, |
1097 |
| - { |
1098 |
| - "cell_type": "code", |
1099 |
| - "execution_count": 24, |
1100 |
| - "metadata": { |
1101 |
| - "execution": { |
1102 |
| - "iopub.execute_input": "2021-08-13T18:11:04.803768Z", |
1103 |
| - "iopub.status.busy": "2021-08-13T18:11:04.803314Z", |
1104 |
| - "iopub.status.idle": "2021-08-13T18:11:04.811178Z", |
1105 |
| - "shell.execute_reply": "2021-08-13T18:11:04.810839Z" |
1106 |
| - } |
1107 |
| - }, |
1108 |
| - "outputs": [ |
1109 |
| - { |
1110 |
| - "data": { |
1111 |
| - "text/plain": [ |
1112 |
| - "'x'" |
1113 |
| - ] |
1114 |
| - }, |
1115 |
| - "execution_count": 24, |
1116 |
| - "metadata": {}, |
1117 |
| - "output_type": "execute_result" |
1118 |
| - } |
1119 |
| - ], |
1120 |
| - "source": [ |
1121 |
| - "def nameof2(var, *more_vars):\n", |
1122 |
| - " return nameof(var, *more_vars, frame=2)\n", |
1123 |
| - "\n", |
1124 |
| - "nameof2(x)" |
1125 |
| - ] |
1126 |
| - }, |
1127 | 1015 | {
|
1128 | 1016 | "attachments": {},
|
1129 | 1017 | "cell_type": "markdown",
|
|
1134 | 1022 | },
|
1135 | 1023 | {
|
1136 | 1024 | "cell_type": "code",
|
1137 |
| - "execution_count": 25, |
| 1025 | + "execution_count": 22, |
1138 | 1026 | "metadata": {
|
1139 | 1027 | "execution": {
|
1140 | 1028 | "iopub.execute_input": "2021-08-13T18:11:04.816768Z",
|
|
1180 | 1068 | },
|
1181 | 1069 | {
|
1182 | 1070 | "cell_type": "code",
|
1183 |
| - "execution_count": 26, |
| 1071 | + "execution_count": 23, |
1184 | 1072 | "metadata": {
|
1185 | 1073 | "execution": {
|
1186 | 1074 | "iopub.execute_input": "2021-08-13T18:11:04.833103Z",
|
|
1240 | 1128 | },
|
1241 | 1129 | {
|
1242 | 1130 | "cell_type": "code",
|
1243 |
| - "execution_count": 27, |
| 1131 | + "execution_count": 24, |
1244 | 1132 | "metadata": {
|
1245 | 1133 | "execution": {
|
1246 | 1134 | "iopub.execute_input": "2021-08-13T18:11:04.854380Z",
|
|
1288 | 1176 | },
|
1289 | 1177 | {
|
1290 | 1178 | "cell_type": "code",
|
1291 |
| - "execution_count": 28, |
| 1179 | + "execution_count": 25, |
1292 | 1180 | "metadata": {
|
1293 | 1181 | "execution": {
|
1294 | 1182 | "iopub.execute_input": "2021-08-13T18:11:04.865673Z",
|
|
1333 | 1221 | },
|
1334 | 1222 | {
|
1335 | 1223 | "cell_type": "code",
|
1336 |
| - "execution_count": 29, |
| 1224 | + "execution_count": 26, |
1337 | 1225 | "metadata": {
|
1338 | 1226 | "execution": {
|
1339 | 1227 | "iopub.execute_input": "2021-08-13T18:11:04.881609Z",
|
|
1386 | 1274 | },
|
1387 | 1275 | {
|
1388 | 1276 | "cell_type": "code",
|
1389 |
| - "execution_count": 30, |
| 1277 | + "execution_count": 27, |
1390 | 1278 | "metadata": {
|
1391 | 1279 | "execution": {
|
1392 | 1280 | "iopub.execute_input": "2021-08-13T18:11:04.895641Z",
|
|
1402 | 1290 | "'f'"
|
1403 | 1291 | ]
|
1404 | 1292 | },
|
1405 |
| - "execution_count": 30, |
| 1293 | + "execution_count": 27, |
1406 | 1294 | "metadata": {},
|
1407 | 1295 | "output_type": "execute_result"
|
1408 | 1296 | }
|
|
1426 | 1314 | },
|
1427 | 1315 | {
|
1428 | 1316 | "cell_type": "code",
|
1429 |
| - "execution_count": 31, |
| 1317 | + "execution_count": 28, |
1430 | 1318 | "metadata": {
|
1431 | 1319 | "execution": {
|
1432 | 1320 | "iopub.execute_input": "2021-08-13T18:11:04.907363Z",
|
|
1442 | 1330 | "'foo'"
|
1443 | 1331 | ]
|
1444 | 1332 | },
|
1445 |
| - "execution_count": 31, |
| 1333 | + "execution_count": 28, |
1446 | 1334 | "metadata": {},
|
1447 | 1335 | "output_type": "execute_result"
|
1448 | 1336 | }
|
|
1466 | 1354 | },
|
1467 | 1355 | {
|
1468 | 1356 | "cell_type": "code",
|
1469 |
| - "execution_count": 32, |
| 1357 | + "execution_count": 29, |
1470 | 1358 | "metadata": {},
|
1471 | 1359 | "outputs": [
|
1472 | 1360 | {
|
|
1495 | 1383 | },
|
1496 | 1384 | {
|
1497 | 1385 | "cell_type": "code",
|
1498 |
| - "execution_count": 33, |
| 1386 | + "execution_count": 30, |
1499 | 1387 | "metadata": {
|
1500 | 1388 | "execution": {
|
1501 | 1389 | "iopub.execute_input": "2021-08-13T18:11:04.919563Z",
|
|
1544 | 1432 | },
|
1545 | 1433 | {
|
1546 | 1434 | "cell_type": "code",
|
1547 |
| - "execution_count": 34, |
| 1435 | + "execution_count": 31, |
1548 | 1436 | "metadata": {},
|
1549 | 1437 | "outputs": [
|
1550 | 1438 | {
|
|
1553 | 1441 | "['1', '2']"
|
1554 | 1442 | ]
|
1555 | 1443 | },
|
1556 |
| - "execution_count": 34, |
| 1444 | + "execution_count": 31, |
1557 | 1445 | "metadata": {},
|
1558 | 1446 | "output_type": "execute_result"
|
1559 | 1447 | }
|
|
1576 | 1464 | ],
|
1577 | 1465 | "metadata": {
|
1578 | 1466 | "kernelspec": {
|
1579 |
| - "display_name": "Python 3.9.5 ('base')", |
| 1467 | + "display_name": "varname-wNAL0Xj7-py3.12", |
1580 | 1468 | "language": "python",
|
1581 | 1469 | "name": "python3"
|
1582 | 1470 | },
|
|
1590 | 1478 | "name": "python",
|
1591 | 1479 | "nbconvert_exporter": "python",
|
1592 | 1480 | "pygments_lexer": "ipython3",
|
1593 |
| - "version": "3.10.13" |
1594 |
| - }, |
1595 |
| - "vscode": { |
1596 |
| - "interpreter": { |
1597 |
| - "hash": "9ed5c94d10bf621c6841991b7e31ffd0f3c8de8ec4167710459737a50edc58e4" |
1598 |
| - } |
| 1481 | + "version": "3.12.2" |
1599 | 1482 | }
|
1600 | 1483 | },
|
1601 | 1484 | "nbformat": 4,
|
|
0 commit comments