Load code into the current frontend. Usage:\\ %load [options] source where source can be a filename, URL, input history range, macro, or element in the user namespace If no arguments are given, loads the history of this session up to this poin
(self, arg_s)
| 316 | |
| 317 | @line_magic |
| 318 | def load(self, arg_s): |
| 319 | """Load code into the current frontend. |
| 320 | |
| 321 | Usage:\\ |
| 322 | %load [options] source |
| 323 | |
| 324 | where source can be a filename, URL, input history range, macro, or |
| 325 | element in the user namespace |
| 326 | |
| 327 | If no arguments are given, loads the history of this session up to this |
| 328 | point. |
| 329 | |
| 330 | Options: |
| 331 | |
| 332 | -r <lines>: Specify lines or ranges of lines to load from the source. |
| 333 | Ranges could be specified as x-y (x..y) or in python-style x:y |
| 334 | (x..(y-1)). Both limits x and y can be left blank (meaning the |
| 335 | beginning and end of the file, respectively). |
| 336 | |
| 337 | -s <symbols>: Specify function or classes to load from python source. |
| 338 | |
| 339 | -y : Don't ask confirmation for loading source above 200 000 characters. |
| 340 | |
| 341 | -n : Include the user's namespace when searching for source code. |
| 342 | |
| 343 | This magic command can either take a local filename, a URL, an history |
| 344 | range (see %history) or a macro as argument, it will prompt for |
| 345 | confirmation before loading source with more than 200 000 characters, unless |
| 346 | -y flag is passed or if the frontend does not support raw_input:: |
| 347 | |
| 348 | %load |
| 349 | %load myscript.py |
| 350 | %load 7-27 |
| 351 | %load myMacro |
| 352 | %load http://www.example.com/myscript.py |
| 353 | %load -r 5-10 myscript.py |
| 354 | %load -r 10-20,30,40: foo.py |
| 355 | %load -s MyClass,wonder_function myscript.py |
| 356 | %load -n MyClass |
| 357 | %load -n my_module.wonder_function |
| 358 | """ |
| 359 | opts,args = self.parse_options(arg_s,'yns:r:') |
| 360 | search_ns = 'n' in opts |
| 361 | contents = self.shell.find_user_code(args, search_ns=search_ns) |
| 362 | |
| 363 | if 's' in opts: |
| 364 | try: |
| 365 | blocks, not_found = extract_symbols(contents, opts['s']) |
| 366 | except SyntaxError: |
| 367 | # non python code |
| 368 | error("Unable to parse the input as valid Python code") |
| 369 | return |
| 370 | |
| 371 | if len(not_found) == 1: |
| 372 | warn('The symbol `%s` was not found' % not_found[0]) |
| 373 | elif len(not_found) > 1: |
| 374 | warn('The symbols %s were not found' % get_text_list(not_found, |
| 375 | wrap_item_with='`') |