[IPython-dev] ip.core.ipapi.get

Darren Dale dsdale24 at gmail.com
Wed Sep 30 17:07:56 EDT 2009


Thanks Brian!

On Wed, Sep 30, 2009 at 5:04 PM, Brian Granger <ellisonbg.net at gmail.com> wrote:
> Some of the APIs have changed a little bit, but here is an overview of how
> you want to
> accomplish this:
>
> In your package, create a module for an ipython extension:
>
> mypackage
>   __init__.py
>   # all your other modules and packages and then...
>   myextension.py
>
> In that file, create a load_in_ipython function that takes a single
> argument:
>
> def load_in_ipython(ip)
>   # ip is the same thing as get_ipython()
>   # do whatever you need to with it
>   # This is where you will do the types of things Robert was referring to
>
> Then to activate your extension, just put it in your new-style config file:
>
> # .ipythondir/ipython_config.py
> c = get_config()
> c.Global.extensions = ['mypackage.myextension']
>
> Upon starting up, IPython will import your extension and call the
> load_in_ipython passing
> itself as the argument.
>
> Here is our docs on the new config system:
>
> http://bazaar.launchpad.net/~ipython-dev/ipython/trunk/annotate/head%3A/docs/source/config/overview.txt
> http://bazaar.launchpad.net/~ipython-dev/ipython/trunk/annotate/head%3A/docs/source/config/ipython.txt
>
> (No HTML up yet, sorry)
>
> Cheers,
>
> Brian
>
> On Wed, Sep 30, 2009 at 10:43 AM, Darren Dale <dsdale24 at gmail.com> wrote:
>>
>> My use case is to check in a package's __init__.py to see if it is
>> being imported within an ipython session, and if so to load a custom
>> completer. I don't think get_ipython will work:
>>
>> import IPython as ip
>>
>> ip.InteractiveShell.get_ipython()
>> ------------------------------------------------------------
>> Traceback (most recent call last):
>>  File "<ipython console>", line 1, in <module>
>> TypeError: unbound method get_ipython() must be called with
>> InteractiveShell instance as first argument (got nothing instead)
>>
>> Am I using it incorrectly?
>>
>>
>> On Wed, Sep 30, 2009 at 1:21 PM, Brian Granger <ellisonbg.net at gmail.com>
>> wrote:
>> > This function is essentially deprecated.  This is a bug though that
>> > you are getting an exception.  But, in the mean time please call the
>> > following function:
>> >
>> > In [1]: ip = get_ipython()
>> >
>> > This function is always available inside IPython and returns basically
>> > the
>> > same
>> > thing as get used to.
>> >
>> > For the curious, the problem with the old ipapi.get is that it assumed
>> > that
>> > there
>> > was always only ONE ipython and it returned that one.  The new
>> > get_ipython
>> > function is smart: it doesn't assume there is only 1 ipython, and it
>> > always
>> > returns
>> > the right one.
>> >
>> > Cheers,
>> >
>> > Brian
>> >
>> > On Wed, Sep 30, 2009 at 9:35 AM, Darren Dale <dsdale24 at gmail.com> wrote:
>> >>
>> >> I think ip.core.ipapi.get() is behaving differently in the trunk than
>> >> it did before the refactor. Here is the new implementation:
>> >>
>> >> def get():
>> >>    """Get the most recently created InteractiveShell instance."""
>> >>    from IPython.core.iplib import InteractiveShell
>> >>    insts = InteractiveShell.get_instances()
>> >>    most_recent = insts[0]
>> >>    for inst in insts[1:]:
>> >>        if inst.created > most_recent.created:
>> >>            most_recent = inst
>> >>    return most_recent
>> >>
>> >> If I call get from the python prompt, instead of Ipython, I used to
>> >> get None, but now I get an error because insts is an empty list so
>> >> insts[0] raises an IndexError. Perhaps:
>> >>
>> >> def get():
>> >>    """Get the most recently created InteractiveShell instance."""
>> >>    from IPython.core.iplib import InteractiveShell
>> >>    insts = InteractiveShell.get_instances()
>> >>    if not insts:
>> >>        return None
>> >>    most_recent = insts[0]
>> >>    for inst in insts[1:]:
>> >>        if inst.created > most_recent.created:
>> >>            most_recent = inst
>> >>    return most_recent
>> >>
>> >>
>> >> Darren
>
>



-- 
"In our description of nature, the purpose is not to disclose the real
essence of the phenomena but only to track down, so far as it is
possible, relations between the manifold aspects of our experience" -
Niels Bohr

"It is a bad habit of physicists to take their most successful
abstractions to be real properties of our world." - N. David Mermin

"Once we have granted that any physical theory is essentially only a
model for the world of experience, we must renounce all hope of
finding anything like the correct theory ... simply because the
totality of experience is never accessible to us." - Hugh Everett III



More information about the IPython-dev mailing list