New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
%load -s to load specific functions or classes #4311
Conversation
… function or classes from the python source given. forcing the patch ignoring whitespace changes
Nice! I think there are cases where the end-of-block finding could go wrong, but they probably won't be a big problem in practice. |
travis is angry though :
|
code = code.split('\n') | ||
for symbol in symbols.split(','): | ||
if symbol in symbols_lines: | ||
blocks.append('\n'.join(code[slice(*symbols_lines[symbol])])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be clearer broken up into a couple of lines. E.g.
start, end = symbols_lines[symbol]
blocks.append('\n'.join(code[start:end])
Also, a few lines of spacing and explanatory comments wouldn't go amiss throughout the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, simple is better than complex :)
Can some code be shared with |
That's finding the code for objects that are already loaded. We could run the module first, but that might have unwanted side effects. However, it does use |
Good idea! I am agree with the comments adressed by @takluyver regarding spacing and explanatory comments. |
On mgaitan/ipython@04432cc I've refactor the symbol parser function to get a better I've also updated the demo to show this http://nbviewer.ipython.org/6766501 |
That looks really awesome. |
@@ -239,6 +288,9 @@ def load(self, arg_s): | |||
|
|||
contents = self.shell.find_user_code(args) | |||
|
|||
if 's' in opts: | |||
contents = '\n'.join(extract_symbols(contents, opts['s'])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a message if any symbols are not found, maybe even an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in mgaitan/ipython@7391345
|
||
# construct a dictionary with elements | ||
# {'symbol_name': (start_lineno, end_lineno), ...} | ||
end = len(code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another quick comment on this line, please - something like "step backwards to a non-blank line". It just took me a minute to work it out.
This looks good. Apart from those couple of minor points, I'm in agreement with Matthias about merging this soon. |
I too am strongly +1 on this one. Is it ready to go? |
On Wed, Oct 9, 2013 at 5:05 PM, Brian E. Granger
I think so :) btw, thanks for all the feedback guys. |
Just, one question . On mgaitan/ipython@f7cd9ed I've catch a bug introduced in mgaitan/ipython@7391345 . But that involves a design decision: When in any case, if no symbol were found (because the input isn't python or the symbol/s is/are not present) should the empty cell be inserted anyway? |
I think it should say something like "Unable to parse the file as Python..." and not insert the empty cell of the input file is not valid Python. Good catch. |
I agree with @ellisonbg... |
Ok, done. |
Great, thanks. |
%load -s to load specific functions or classes
%load -s to load specific functions or classes
This PR adds the optional parameter
-s
to the magic%load
that loads specific function or classes from the python source given. It's a complementary option to #4295.A simple demo could be seen here http://nbviewer.ipython.org/6766501