Inheritance diagram for IPython.core.magic:
Magic functions for InteractiveShell.
Bases: object
Base class for implementing magic functions.
Shell functions which can be reached as %function_name. All magic functions should accept a string, which they can parse for their own needs. This can make some functions easier to type, eg %cd ../ vs. %cd(”../”)
Classes providing magic functions need to subclass this class, and they MUST:
individual methods as magic functions, AND
methods are properly registered at the instance level upon instance initialization.
See magic_functions for examples of actual implementation classes.
Print docstring if incorrect arguments were passed
Make an entry in the options_table for fn, with value optstr
Format a string for latex inclusion.
Parse options passed to an argument string.
The interface is similar to that of getopt(), but it returns back a Struct with the options as keys and the stripped argument string still as a string.
arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc.
-mode: default ‘string’. If given as ‘list’, the argument string is returned as a list (split on whitespace) instead of a string.
-list_all: put all option values in lists. Normally only options appearing more than once are put in a list.
-posix (True): whether to split the input line in POSIX mode or not, as per the conventions outlined in the shlex module from the standard library.
Bases: IPython.config.configurable.Configurable
Object that handles all magic-related functionality for IPython.
A boolean (True, False) trait.
Return descriptive string with automagic status.
Get the config class config section
Get the help string for this class in ReST format.
If inst is given, it’s current trait values will be used in place of class defaults.
Get the help string for a single trait.
If inst is given, it’s current trait values will be used in place of the class default.
Get the help string for a single trait and print it.
Get a list of all the names of this classes traits.
This method is just like the trait_names() method, but is unbound.
Get a list of all the traits of this class.
This method is just like the traits() method, but is unbound.
The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
This follows the same algorithm as traits does and does not allow for any simple way of specifying merely that a metadata name exists, but has any value. This is because get_metadata returns None if a metadata key doesn’t exist.
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
[Deprecated] Expose own function as magic function for IPython.
Example:
def foo_impl(self, parameter_s=''):
'My very own magic!. (Use docstrings, IPython reads them).'
print 'Magic function. Passed parameter is between < >:'
print '<%s>' % parameter_s
print 'The self object is:', self
ip.define_magic('foo',foo_impl)
Return a dict of currently available magic functions.
The return dict has the keys ‘line’ and ‘cell’, corresponding to the two types of magics we support. Each value is a list of names.
Return dict of documentation of magic functions.
The return dict has the keys ‘line’ and ‘cell’, corresponding to the two types of magics we support. Each value is a dict keyed by magic name whose value is the function docstring. If a docstring is unavailable, the value of missing is used instead.
If brief is True, only the first line of each docstring will be returned.
An instance of a Python dict.
Setup a handler to be called when a trait changes.
This is used to setup dynamic notifications of trait changes.
Static handlers can be created by creating methods on a HasTraits subclass with the naming convention ‘_[traitname]_changed’. Thus, to create static handler for the trait ‘a’, create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below).
Parameters : | handler : callable
name : list, str, None
remove : bool
|
---|
Register one or more instances of Magics.
Take one or more classes or instances of classes that subclass the main core.Magic class, and register them with IPython to use the magic functions they provide. The registration process will then ensure that any methods that have decorated to provide line and/or cell magics will be recognized with the %x/%%x syntax as a line/cell magic respectively.
If classes are given, they will be instantiated with the default constructor. If your classes need a custom constructor, you should instanitate them first and pass the instance.
The provided arguments can be an arbitrary mix of classes and instances.
Parameters : | magic_objects : one or more classes or instances |
---|
Expose a standalone function as magic function for IPython.
This will create an IPython magic (line, cell or both) from a standalone function. The functions should have the following signatures:
In the latter case, the function will be called with cell==None when invoked as %f, and with cell as a string when invoked as %%f.
Parameters : | func : callable
magic_kind : str
magic_name : optional str
|
---|
An instance of a Python dict.
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Get metadata values for trait by key.
Get a list of all the names of this classes traits.
Get a list of all the traits of this class.
The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
This follows the same algorithm as traits does and does not allow for any simple way of specifying merely that a metadata name exists, but has any value. This is because get_metadata returns None if a metadata key doesn’t exist.
Fire the traits events when the config is updated.
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Compress a directory history into a new one with at most 20 entries.
Return a new list made from the first and last 10 elements of dhist after removal of duplicates.
Class decorator for all subclasses of the main Magics class.
Any class that subclasses Magics must also apply this decorator, to ensure that all the methods that have been decorated as line/cell magics get correctly registered in the class instance. This is necessary because when method decorators run, the class does not exist yet, so they temporarily store their information into a module global. Application of this class decorator copies that global data to the class instance and clears the global.
Obviously, this mechanism is not thread-safe, which means that the creation of subclasses of Magic should only be done in a single-thread context. Instantiation of the classes has no restrictions. Given that these classes are typically created at IPython startup time and before user application code becomes active, in practice this should not pose any problems.
Decorator to mark magic functions which need to local scope to run.
Return an ON/OFF string for a 1/0 input. Simple utility function.
Utility function to store a function as a magic of a specific kind.
Parameters : | dct : dict
magic_kind : str
magic_name : str
func : function
|
---|
Ensure that the given magic_kind is valid.
Check that the given magic_kind is one of the accepted spec types (stored in the global magic_spec), raise ValueError otherwise.