-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Florian Maas
authored
Sep 4, 2022
1 parent
b3bed87
commit 8c0a2b2
Showing
3 changed files
with
139 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "064f4445-752d-48e7-bbac-72fe198e28a2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import ast" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "6d2dda83-00d2-407a-87ff-b602b415cb5c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"root = ast.parse(\n", | ||
"\"\"\"\n", | ||
"x=1\n", | ||
"import pandas as pd\n", | ||
"from numpy import random\n", | ||
"if x>0:\n", | ||
" import click\n", | ||
"elif x<0:\n", | ||
" from typing import List\n", | ||
"else:\n", | ||
" import logging\n", | ||
"\"\"\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "00957720-c90a-47be-bb95-4e59dcc026d3", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<ast.Module at 0x110524fd0>" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"root" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "729215e5-abc3-4dae-98f7-96ab7c41727a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"<ast.Assign object at 0x110524fa0>\n", | ||
"<ast.Import object at 0x110524f10>\n", | ||
"<ast.ImportFrom object at 0x110524e50>\n", | ||
"<ast.If object at 0x110524e20>\n", | ||
"<ast.Compare object at 0x110524be0>\n", | ||
"<ast.ImportFrom object at 0x110524b20>\n", | ||
"<ast.Import object at 0x1105249a0>\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for node in ast.iter_child_nodes(root):\n", | ||
" print(node)\n", | ||
" if isinstance(node, ast.If):\n", | ||
" for node in ast.iter_child_nodes(node):\n", | ||
" if isinstance(node, ast.If):\n", | ||
" for node in ast.iter_child_nodes(node):\n", | ||
" print(node)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "8f70351b-1852-4628-b683-db42be55e943", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "TypeError", | ||
"evalue": "'Module' object is not subscriptable", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | ||
"Input \u001b[0;32mIn [6]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m node \u001b[38;5;129;01min\u001b[39;00m ast\u001b[38;5;241m.\u001b[39miter_child_nodes(\u001b[43mroot\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(node)\n", | ||
"\u001b[0;31mTypeError\u001b[0m: 'Module' object is not subscriptable" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for node in ast.iter_child_nodes(root):\n", | ||
" print(node)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d4978dbe-2173-404f-a46a-8839e09a4e52", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.11" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters