|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": 1, |
| 5 | + "execution_count": null, |
6 | 6 | "id": "064f4445-752d-48e7-bbac-72fe198e28a2", |
7 | 7 | "metadata": {}, |
8 | 8 | "outputs": [], |
|
12 | 12 | }, |
13 | 13 | { |
14 | 14 | "cell_type": "code", |
15 | | - "execution_count": 2, |
| 15 | + "execution_count": null, |
16 | 16 | "id": "6d2dda83-00d2-407a-87ff-b602b415cb5c", |
17 | 17 | "metadata": {}, |
18 | 18 | "outputs": [], |
19 | 19 | "source": [ |
20 | | - "root = ast.parse(\n", |
21 | | - "\"\"\"\n", |
22 | | - "x=1\n", |
| 20 | + "my_py_file = \"\"\"\n", |
23 | 21 | "import pandas as pd\n", |
24 | 22 | "from numpy import random\n", |
25 | | - "if x>0:\n", |
| 23 | + "try:\n", |
26 | 24 | " import click\n", |
27 | | - "elif x<0:\n", |
28 | | - " from typing import List\n", |
29 | | - "else:\n", |
| 25 | + "except:\n", |
30 | 26 | " import logging\n", |
31 | | - "\"\"\")" |
| 27 | + "\"\"\"\n", |
| 28 | + "root = ast.parse(my_py_file)" |
32 | 29 | ] |
33 | 30 | }, |
34 | 31 | { |
35 | 32 | "cell_type": "code", |
36 | | - "execution_count": 3, |
| 33 | + "execution_count": null, |
37 | 34 | "id": "00957720-c90a-47be-bb95-4e59dcc026d3", |
38 | 35 | "metadata": {}, |
39 | | - "outputs": [ |
40 | | - { |
41 | | - "data": { |
42 | | - "text/plain": [ |
43 | | - "<ast.Module at 0x110524fd0>" |
44 | | - ] |
45 | | - }, |
46 | | - "execution_count": 3, |
47 | | - "metadata": {}, |
48 | | - "output_type": "execute_result" |
49 | | - } |
50 | | - ], |
| 36 | + "outputs": [], |
51 | 37 | "source": [ |
52 | 38 | "root" |
53 | 39 | ] |
54 | 40 | }, |
55 | 41 | { |
56 | 42 | "cell_type": "code", |
57 | | - "execution_count": 8, |
| 43 | + "execution_count": null, |
58 | 44 | "id": "729215e5-abc3-4dae-98f7-96ab7c41727a", |
59 | 45 | "metadata": {}, |
60 | | - "outputs": [ |
61 | | - { |
62 | | - "name": "stdout", |
63 | | - "output_type": "stream", |
64 | | - "text": [ |
65 | | - "<ast.Assign object at 0x110524fa0>\n", |
66 | | - "<ast.Import object at 0x110524f10>\n", |
67 | | - "<ast.ImportFrom object at 0x110524e50>\n", |
68 | | - "<ast.If object at 0x110524e20>\n", |
69 | | - "<ast.Compare object at 0x110524be0>\n", |
70 | | - "<ast.ImportFrom object at 0x110524b20>\n", |
71 | | - "<ast.Import object at 0x1105249a0>\n" |
72 | | - ] |
73 | | - } |
74 | | - ], |
| 46 | + "outputs": [], |
75 | 47 | "source": [ |
76 | 48 | "for node in ast.iter_child_nodes(root):\n", |
77 | 49 | " print(node)\n", |
78 | | - " if isinstance(node, ast.If):\n", |
| 50 | + " if isinstance(node, ast.Try):\n", |
79 | 51 | " for node in ast.iter_child_nodes(node):\n", |
80 | | - " if isinstance(node, ast.If):\n", |
| 52 | + " print(node)\n", |
| 53 | + " if isinstance(node, ast.Try):\n", |
81 | 54 | " for node in ast.iter_child_nodes(node):\n", |
82 | | - " print(node)" |
| 55 | + " pass" |
83 | 56 | ] |
84 | 57 | }, |
85 | 58 | { |
86 | 59 | "cell_type": "code", |
87 | | - "execution_count": 6, |
88 | | - "id": "8f70351b-1852-4628-b683-db42be55e943", |
| 60 | + "execution_count": null, |
| 61 | + "id": "d4978dbe-2173-404f-a46a-8839e09a4e52", |
89 | 62 | "metadata": {}, |
90 | | - "outputs": [ |
91 | | - { |
92 | | - "ename": "TypeError", |
93 | | - "evalue": "'Module' object is not subscriptable", |
94 | | - "output_type": "error", |
95 | | - "traceback": [ |
96 | | - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
97 | | - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
98 | | - "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", |
99 | | - "\u001b[0;31mTypeError\u001b[0m: 'Module' object is not subscriptable" |
100 | | - ] |
101 | | - } |
102 | | - ], |
| 63 | + "outputs": [], |
103 | 64 | "source": [ |
104 | | - "for node in ast.iter_child_nodes(root):\n", |
105 | | - " print(node)" |
| 65 | + "from deptry.import_parser import ImportParser\n", |
| 66 | + "imported_modules = ImportParser().get_imported_modules_from_str(my_py_file)" |
106 | 67 | ] |
107 | 68 | }, |
108 | 69 | { |
109 | 70 | "cell_type": "code", |
110 | 71 | "execution_count": null, |
111 | | - "id": "d4978dbe-2173-404f-a46a-8839e09a4e52", |
| 72 | + "id": "874769dc-5275-469d-9af8-3bf6d6ff0b1e", |
112 | 73 | "metadata": {}, |
113 | 74 | "outputs": [], |
114 | | - "source": [] |
| 75 | + "source": [ |
| 76 | + "imported_modules" |
| 77 | + ] |
115 | 78 | } |
116 | 79 | ], |
117 | 80 | "metadata": { |
|
0 commit comments