Scan
ScanPyImports.scan
¶
This module provides a set of classes and functions to extract import statements from Python files (.py
) and Jupyter notebooks (.ipynb
) of a given directory.
Classes:
-
Line
–Parses a single import statement.
-
File
–Manages the parsing of Python files and Jupyter notebooks.
-
Directory
–Manages a collection of files in a directory, extracting import statements from all Python and notebook files found.
Classes¶
Line(text)
¶
A single import statement.
Parameters:
-
text
(str
) –A string with an import statement.
Source code in ScanPyImports/scan.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
Attributes¶
original: str
instance-attribute
¶
The original line of code with the import statement.
lfrom: str
instance-attribute
¶
The from
part of the import statement, if any.
limports: str
instance-attribute
¶
The import
part of the import statement.
imports: List[List[str]]
instance-attribute
¶
A list of lists, where each inner list represents an imported submodule, containing the package name, modules, and the submodule itself.
alias: List[str]
instance-attribute
¶
The aliases used in the import statement, if any.
Functions¶
compile_alias()
¶
Match alias in import statements.
Returns:
-
Pattern
–A compiled regular expression pattern.
Source code in ScanPyImports/scan.py
42 43 44 45 46 47 48 49 |
|
compile_from_import()
¶
Match and group from
package and import
modules
from statements.
Returns:
-
Pattern
–A compiled regular expression pattern.
Source code in ScanPyImports/scan.py
52 53 54 55 56 57 58 59 60 61 |
|
get_from_import(statement)
¶
Extract from-import components from the line of code.
Parameters:
-
statement
(str
) –Line of code.
Returns:
Source code in ScanPyImports/scan.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
get_imports(lfrom, limports)
¶
Extract the imported submodules from the import statement.
Each imported submodule is represented as an ordered list containing:
- The package of the submodule
- The module
- The submodule
- ...
- The submodule itself with alias, if any.
Returns:
Source code in ScanPyImports/scan.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
get_alias_list(imports)
¶
Get the alias list and subtract aliases from the import list.
Parameters:
Returns:
-
Tuple[List[List[str]], List[str]]
–List of imports without aliases, and list of aliases. Both lists have the same length. Empty strings indicate that the submodule does not have an alias.
Source code in ScanPyImports/scan.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
get_alias(statement)
¶
Extract alias from the line of code.
Parameters:
-
statement
(str
) –Line of code.
Returns:
Source code in ScanPyImports/scan.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
File(filepath)
¶
A Python file or a Jypiter notebook.
A File class bundles a set of Line objects.
Parameters:
-
filepath
(str
) –Path to the Python file or notebook file.
Source code in ScanPyImports/scan.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
Attributes¶
file: str
instance-attribute
¶
Path to the Python or notebook file.
extension: str
instance-attribute
¶
The file extension (i.e. py or ipynb)
code: str
instance-attribute
¶
Text from py file or notebook.
code_lines: List[str]
instance-attribute
¶
All lines of code in the file.
statements: List[str]
instance-attribute
¶
List of import statements in the file.
has_imports: bool
instance-attribute
¶
Indicates if the file has any import statements.
Functions¶
remove_all_comments(code)
¶
Remove comments from code.
Parameters:
-
code
(str
) –Line of code
Returns:
-
str
–Line of code with comments removed.
Source code in ScanPyImports/scan.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
get_code()
¶
Get code text from py file or notebook.
For Jupyter notebooks, only the content of the code cells is extracted and returned as a single string.
Returns:
-
str
–The content of the Python file or the concatenated code cells from the Jupyter notebook.
Source code in ScanPyImports/scan.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
get_code_lines()
¶
Extracts all lines of code from the file.
Returns:
Source code in ScanPyImports/scan.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
compile_import()
¶
Match text starting with either import
or from
.
Text can be precedeed by blanks.
Returns:
-
Pattern
–Compiled pattern
Source code in ScanPyImports/scan.py
247 248 249 250 251 252 253 254 255 256 257 |
|
get_import_lines(lines)
¶
Extract import statements of the file.
Parameters:
Returns:
Source code in ScanPyImports/scan.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
get_lines_obj()
¶
Line objects of the file.
Returns:
Source code in ScanPyImports/scan.py
275 276 277 278 279 280 281 282 283 284 285 286 |
|
Directory(path)
¶
The root directory that bundles a set of File objects.
Files in the root directory are scanned in search for import statements.
Parameters:
-
path
(str
) –Path to the directory. Path to python or notebook files are also accepted.
Source code in ScanPyImports/scan.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
Attributes¶
path: str
instance-attribute
¶
Path to the directory.
exists: bool
instance-attribute
¶
Indicates if the directory exists.
isfile: bool
instance-attribute
¶
Indicates if the path is a file.
isdir: bool
instance-attribute
¶
Indicates if the path is a directory.
filepaths: List[str]
instance-attribute
¶
Paths to .py or .ipynb files in the directory.
Functions¶
get_filepaths()
¶
Get paths to .py and .ipynb files within a directory and its subdirectories.
Returns:
Source code in ScanPyImports/scan.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
get_files_obj()
¶
Get File objects for each file path.
Returns:
Source code in ScanPyImports/scan.py
352 353 354 355 356 357 358 359 360 361 362 |
|