Show More
@@ -1,61 +1,61 b'' | |||||
1 | .. _extensions_overview: |
|
1 | .. _extensions_overview: | |
2 |
|
2 | |||
3 | ================== |
|
3 | ================== | |
4 | IPython extensions |
|
4 | IPython extensions | |
5 | ================== |
|
5 | ================== | |
6 |
|
6 | |||
7 | Configuration files are just the first level of customization that IPython |
|
7 | Configuration files are just the first level of customization that IPython | |
8 | supports. The next level is that of extensions. An IPython extension is an |
|
8 | supports. The next level is that of extensions. An IPython extension is an | |
9 | importable Python module that has a a few special function. By defining these |
|
9 | importable Python module that has a a few special function. By defining these | |
10 | functions, users can customize IPython by accessing the actual runtime objects |
|
10 | functions, users can customize IPython by accessing the actual runtime objects | |
11 | of IPython. Here is a sample extension:: |
|
11 | of IPython. Here is a sample extension:: | |
12 |
|
12 | |||
13 | # myextension.py |
|
13 | # myextension.py | |
14 |
|
14 | |||
15 | def load_ipython_extension(ipython): |
|
15 | def load_ipython_extension(ipython): | |
16 | # The ``ipython`` argument is the currently active |
|
16 | # The ``ipython`` argument is the currently active | |
17 | # :class:`InteractiveShell` instance that can be used in any way. |
|
17 | # :class:`InteractiveShell` instance that can be used in any way. | |
18 | # This allows you do to things like register new magics, plugins or |
|
18 | # This allows you do to things like register new magics, plugins or | |
19 | # aliases. |
|
19 | # aliases. | |
20 |
|
20 | |||
21 | def unload_ipython_extension(ipython): |
|
21 | def unload_ipython_extension(ipython): | |
22 | # If you want your extension to be unloadable, put that logic here. |
|
22 | # If you want your extension to be unloadable, put that logic here. | |
23 |
|
23 | |||
24 | This :func:`load_ipython_extension` function is called after your extension is |
|
24 | This :func:`load_ipython_extension` function is called after your extension is | |
25 | imported and the currently active :class:`InteractiveShell` instance is passed |
|
25 | imported and the currently active :class:`InteractiveShell` instance is passed | |
26 | as the only argument. You can do anything you want with IPython at that point. |
|
26 | as the only argument. You can do anything you want with IPython at that point. | |
27 |
|
27 | |||
28 | The :func:`load_ipython_extension` will be called again is you load or reload |
|
28 | The :func:`load_ipython_extension` will be called again is you load or reload | |
29 | the extension again. It is up to the extension author to add code to manage |
|
29 | the extension again. It is up to the extension author to add code to manage | |
30 | that. |
|
30 | that. | |
31 |
|
31 | |||
32 | You can put your extension modules anywhere you want, as long as they can be |
|
32 | You can put your extension modules anywhere you want, as long as they can be | |
33 | imported by Python's standard import mechanism. However, to make it easy to |
|
33 | imported by Python's standard import mechanism. However, to make it easy to | |
34 | write extensions, you can also put your extensions in |
|
34 | write extensions, you can also put your extensions in | |
35 | ``os.path.join(self.ipython_dir, 'extensions')``. This directory is added to |
|
35 | ``os.path.join(self.ipython_dir, 'extensions')``. This directory is added to | |
36 | ``sys.path`` automatically. |
|
36 | ``sys.path`` automatically. | |
37 |
|
37 | |||
38 | Using extensions |
|
38 | Using extensions | |
39 | ================ |
|
39 | ================ | |
40 |
|
40 | |||
41 | There are two ways you can tell IPython to use your extension: |
|
41 | There are two ways you can tell IPython to use your extension: | |
42 |
|
42 | |||
43 | 1. Listing it in a configuration file. |
|
43 | 1. Listing it in a configuration file. | |
44 | 2. Using the ``%load_ext`` magic function. |
|
44 | 2. Using the ``%load_ext`` magic function. | |
45 |
|
45 | |||
46 | To load an extension called :file:`myextension.py` add the following logic |
|
46 | To load an extension called :file:`myextension.py` add the following logic | |
47 | to your configuration file:: |
|
47 | to your configuration file:: | |
48 |
|
48 | |||
49 | c.InteractiveShellApp.extensions = [ |
|
49 | c.InteractiveShellApp.extensions = [ | |
50 | 'myextension' |
|
50 | 'myextension' | |
51 | ] |
|
51 | ] | |
52 |
|
52 | |||
53 |
To load that same extension at runtime, use the ``%load_ext`` magic: |
|
53 | To load that same extension at runtime, use the ``%load_ext`` magic: | |
54 |
|
54 | |||
55 | .. sourcecode:: ipython |
|
55 | .. sourcecode:: ipython | |
56 |
|
56 | |||
57 | In [1]: %load_ext myextension |
|
57 | In [1]: %load_ext myextension | |
58 |
|
58 | |||
59 | To summarize, in conjunction with configuration files and profiles, IPython |
|
59 | To summarize, in conjunction with configuration files and profiles, IPython | |
60 | extensions give you complete and flexible control over your IPython |
|
60 | extensions give you complete and flexible control over your IPython | |
61 | setup. |
|
61 | setup. |
@@ -1,479 +1,479 b'' | |||||
1 | .. _config_overview: |
|
1 | .. _config_overview: | |
2 |
|
2 | |||
3 | ============================================ |
|
3 | ============================================ | |
4 | Overview of the IPython configuration system |
|
4 | Overview of the IPython configuration system | |
5 | ============================================ |
|
5 | ============================================ | |
6 |
|
6 | |||
7 | This section describes the IPython configuration system. Starting with version |
|
7 | This section describes the IPython configuration system. Starting with version | |
8 | 0.11, IPython has a completely new configuration system that is quite |
|
8 | 0.11, IPython has a completely new configuration system that is quite | |
9 | different from the older :file:`ipythonrc` or :file:`ipy_user_conf.py` |
|
9 | different from the older :file:`ipythonrc` or :file:`ipy_user_conf.py` | |
10 | approaches. The new configuration system was designed from scratch to address |
|
10 | approaches. The new configuration system was designed from scratch to address | |
11 | the particular configuration needs of IPython. While there are many |
|
11 | the particular configuration needs of IPython. While there are many | |
12 | other excellent configuration systems out there, we found that none of them |
|
12 | other excellent configuration systems out there, we found that none of them | |
13 | met our requirements. |
|
13 | met our requirements. | |
14 |
|
14 | |||
15 | .. warning:: |
|
15 | .. warning:: | |
16 |
|
16 | |||
17 | If you are upgrading to version 0.11 of IPython, you will need to migrate |
|
17 | If you are upgrading to version 0.11 of IPython, you will need to migrate | |
18 | your old :file:`ipythonrc` or :file:`ipy_user_conf.py` configuration files |
|
18 | your old :file:`ipythonrc` or :file:`ipy_user_conf.py` configuration files | |
19 | to the new system. Read on for information on how to do this. |
|
19 | to the new system. Read on for information on how to do this. | |
20 |
|
20 | |||
21 | The discussion that follows is focused on teaching users how to configure |
|
21 | The discussion that follows is focused on teaching users how to configure | |
22 | IPython to their liking. Developers who want to know more about how they |
|
22 | IPython to their liking. Developers who want to know more about how they | |
23 | can enable their objects to take advantage of the configuration system |
|
23 | can enable their objects to take advantage of the configuration system | |
24 | should consult our :ref:`developer guide <developer_guide>` |
|
24 | should consult our :ref:`developer guide <developer_guide>` | |
25 |
|
25 | |||
26 | The main concepts |
|
26 | The main concepts | |
27 | ================= |
|
27 | ================= | |
28 |
|
28 | |||
29 | There are a number of abstractions that the IPython configuration system uses. |
|
29 | There are a number of abstractions that the IPython configuration system uses. | |
30 | Each of these abstractions is represented by a Python class. |
|
30 | Each of these abstractions is represented by a Python class. | |
31 |
|
31 | |||
32 | Configuration object: :class:`~IPython.config.loader.Config` |
|
32 | Configuration object: :class:`~IPython.config.loader.Config` | |
33 | A configuration object is a simple dictionary-like class that holds |
|
33 | A configuration object is a simple dictionary-like class that holds | |
34 | configuration attributes and sub-configuration objects. These classes |
|
34 | configuration attributes and sub-configuration objects. These classes | |
35 | support dotted attribute style access (``Foo.bar``) in addition to the |
|
35 | support dotted attribute style access (``Foo.bar``) in addition to the | |
36 | regular dictionary style access (``Foo['bar']``). Configuration objects |
|
36 | regular dictionary style access (``Foo['bar']``). Configuration objects | |
37 | are smart. They know how to merge themselves with other configuration |
|
37 | are smart. They know how to merge themselves with other configuration | |
38 | objects and they automatically create sub-configuration objects. |
|
38 | objects and they automatically create sub-configuration objects. | |
39 |
|
39 | |||
40 | Application: :class:`~IPython.config.application.Application` |
|
40 | Application: :class:`~IPython.config.application.Application` | |
41 | An application is a process that does a specific job. The most obvious |
|
41 | An application is a process that does a specific job. The most obvious | |
42 | application is the :command:`ipython` command line program. Each |
|
42 | application is the :command:`ipython` command line program. Each | |
43 | application reads *one or more* configuration files and a single set of |
|
43 | application reads *one or more* configuration files and a single set of | |
44 | command line options |
|
44 | command line options | |
45 | and then produces a master configuration object for the application. This |
|
45 | and then produces a master configuration object for the application. This | |
46 | configuration object is then passed to the configurable objects that the |
|
46 | configuration object is then passed to the configurable objects that the | |
47 | application creates. These configurable objects implement the actual logic |
|
47 | application creates. These configurable objects implement the actual logic | |
48 | of the application and know how to configure themselves given the |
|
48 | of the application and know how to configure themselves given the | |
49 | configuration object. |
|
49 | configuration object. | |
50 |
|
50 | |||
51 | Applications always have a `log` attribute that is a configured Logger. |
|
51 | Applications always have a `log` attribute that is a configured Logger. | |
52 | This allows centralized logging configuration per-application. |
|
52 | This allows centralized logging configuration per-application. | |
53 |
|
53 | |||
54 | Configurable: :class:`~IPython.config.configurable.Configurable` |
|
54 | Configurable: :class:`~IPython.config.configurable.Configurable` | |
55 | A configurable is a regular Python class that serves as a base class for |
|
55 | A configurable is a regular Python class that serves as a base class for | |
56 | all main classes in an application. The |
|
56 | all main classes in an application. The | |
57 | :class:`~IPython.config.configurable.Configurable` base class is |
|
57 | :class:`~IPython.config.configurable.Configurable` base class is | |
58 | lightweight and only does one things. |
|
58 | lightweight and only does one things. | |
59 |
|
59 | |||
60 | This :class:`~IPython.config.configurable.Configurable` is a subclass |
|
60 | This :class:`~IPython.config.configurable.Configurable` is a subclass | |
61 | of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure |
|
61 | of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure | |
62 | itself. Class level traits with the metadata ``config=True`` become |
|
62 | itself. Class level traits with the metadata ``config=True`` become | |
63 | values that can be configured from the command line and configuration |
|
63 | values that can be configured from the command line and configuration | |
64 | files. |
|
64 | files. | |
65 |
|
65 | |||
66 | Developers create :class:`~IPython.config.configurable.Configurable` |
|
66 | Developers create :class:`~IPython.config.configurable.Configurable` | |
67 | subclasses that implement all of the logic in the application. Each of |
|
67 | subclasses that implement all of the logic in the application. Each of | |
68 | these subclasses has its own configuration information that controls how |
|
68 | these subclasses has its own configuration information that controls how | |
69 | instances are created. |
|
69 | instances are created. | |
70 |
|
70 | |||
71 | Singletons: :class:`~IPython.config.configurable.SingletonConfigurable` |
|
71 | Singletons: :class:`~IPython.config.configurable.SingletonConfigurable` | |
72 | Any object for which there is a single canonical instance. These are |
|
72 | Any object for which there is a single canonical instance. These are | |
73 | just like Configurables, except they have a class method |
|
73 | just like Configurables, except they have a class method | |
74 | :meth:`~IPython.config.configurable.SingletonConfigurable.instance`, |
|
74 | :meth:`~IPython.config.configurable.SingletonConfigurable.instance`, | |
75 | that returns the current active instance (or creates one if it |
|
75 | that returns the current active instance (or creates one if it | |
76 | does not exist). Examples of singletons include |
|
76 | does not exist). Examples of singletons include | |
77 | :class:`~IPython.config.application.Application`s and |
|
77 | :class:`~IPython.config.application.Application`s and | |
78 | :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets |
|
78 | :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets | |
79 | objects easily connect to the current running Application without passing |
|
79 | objects easily connect to the current running Application without passing | |
80 | objects around everywhere. For instance, to get the current running |
|
80 | objects around everywhere. For instance, to get the current running | |
81 | Application instance, simply do: ``app = Application.instance()``. |
|
81 | Application instance, simply do: ``app = Application.instance()``. | |
82 |
|
82 | |||
83 |
|
83 | |||
84 | .. note:: |
|
84 | .. note:: | |
85 |
|
85 | |||
86 | Singletons are not strictly enforced - you can have many instances |
|
86 | Singletons are not strictly enforced - you can have many instances | |
87 | of a given singleton class, but the :meth:`instance` method will always |
|
87 | of a given singleton class, but the :meth:`instance` method will always | |
88 | return the same one. |
|
88 | return the same one. | |
89 |
|
89 | |||
90 | Having described these main concepts, we can now state the main idea in our |
|
90 | Having described these main concepts, we can now state the main idea in our | |
91 | configuration system: *"configuration" allows the default values of class |
|
91 | configuration system: *"configuration" allows the default values of class | |
92 | attributes to be controlled on a class by class basis*. Thus all instances of |
|
92 | attributes to be controlled on a class by class basis*. Thus all instances of | |
93 | a given class are configured in the same way. Furthermore, if two instances |
|
93 | a given class are configured in the same way. Furthermore, if two instances | |
94 | need to be configured differently, they need to be instances of two different |
|
94 | need to be configured differently, they need to be instances of two different | |
95 | classes. While this model may seem a bit restrictive, we have found that it |
|
95 | classes. While this model may seem a bit restrictive, we have found that it | |
96 | expresses most things that need to be configured extremely well. However, it |
|
96 | expresses most things that need to be configured extremely well. However, it | |
97 | is possible to create two instances of the same class that have different |
|
97 | is possible to create two instances of the same class that have different | |
98 | trait values. This is done by overriding the configuration. |
|
98 | trait values. This is done by overriding the configuration. | |
99 |
|
99 | |||
100 | Now, we show what our configuration objects and files look like. |
|
100 | Now, we show what our configuration objects and files look like. | |
101 |
|
101 | |||
102 | Configuration objects and files |
|
102 | Configuration objects and files | |
103 | =============================== |
|
103 | =============================== | |
104 |
|
104 | |||
105 | A configuration file is simply a pure Python file that sets the attributes |
|
105 | A configuration file is simply a pure Python file that sets the attributes | |
106 | of a global, pre-created configuration object. This configuration object is a |
|
106 | of a global, pre-created configuration object. This configuration object is a | |
107 | :class:`~IPython.config.loader.Config` instance. While in a configuration |
|
107 | :class:`~IPython.config.loader.Config` instance. While in a configuration | |
108 | file, to get a reference to this object, simply call the :func:`get_config` |
|
108 | file, to get a reference to this object, simply call the :func:`get_config` | |
109 | function. We inject this function into the global namespace that the |
|
109 | function. We inject this function into the global namespace that the | |
110 | configuration file is executed in. |
|
110 | configuration file is executed in. | |
111 |
|
111 | |||
112 | Here is an example of a super simple configuration file that does nothing:: |
|
112 | Here is an example of a super simple configuration file that does nothing:: | |
113 |
|
113 | |||
114 | c = get_config() |
|
114 | c = get_config() | |
115 |
|
115 | |||
116 | Once you get a reference to the configuration object, you simply set |
|
116 | Once you get a reference to the configuration object, you simply set | |
117 | attributes on it. All you have to know is: |
|
117 | attributes on it. All you have to know is: | |
118 |
|
118 | |||
119 | * The name of each attribute. |
|
119 | * The name of each attribute. | |
120 | * The type of each attribute. |
|
120 | * The type of each attribute. | |
121 |
|
121 | |||
122 | The answers to these two questions are provided by the various |
|
122 | The answers to these two questions are provided by the various | |
123 | :class:`~IPython.config.configurable.Configurable` subclasses that an |
|
123 | :class:`~IPython.config.configurable.Configurable` subclasses that an | |
124 | application uses. Let's look at how this would work for a simple configurable |
|
124 | application uses. Let's look at how this would work for a simple configurable | |
125 | subclass:: |
|
125 | subclass:: | |
126 |
|
126 | |||
127 | # Sample configurable: |
|
127 | # Sample configurable: | |
128 | from IPython.config.configurable import Configurable |
|
128 | from IPython.config.configurable import Configurable | |
129 | from IPython.utils.traitlets import Int, Float, Unicode, Bool |
|
129 | from IPython.utils.traitlets import Int, Float, Unicode, Bool | |
130 |
|
130 | |||
131 | class MyClass(Configurable): |
|
131 | class MyClass(Configurable): | |
132 | name = Unicode(u'defaultname', config=True) |
|
132 | name = Unicode(u'defaultname', config=True) | |
133 | ranking = Int(0, config=True) |
|
133 | ranking = Int(0, config=True) | |
134 | value = Float(99.0) |
|
134 | value = Float(99.0) | |
135 | # The rest of the class implementation would go here.. |
|
135 | # The rest of the class implementation would go here.. | |
136 |
|
136 | |||
137 | In this example, we see that :class:`MyClass` has three attributes, two |
|
137 | In this example, we see that :class:`MyClass` has three attributes, two | |
138 | of whom (``name``, ``ranking``) can be configured. All of the attributes |
|
138 | of whom (``name``, ``ranking``) can be configured. All of the attributes | |
139 | are given types and default values. If a :class:`MyClass` is instantiated, |
|
139 | are given types and default values. If a :class:`MyClass` is instantiated, | |
140 | but not configured, these default values will be used. But let's see how |
|
140 | but not configured, these default values will be used. But let's see how | |
141 | to configure this class in a configuration file:: |
|
141 | to configure this class in a configuration file:: | |
142 |
|
142 | |||
143 | # Sample config file |
|
143 | # Sample config file | |
144 | c = get_config() |
|
144 | c = get_config() | |
145 |
|
145 | |||
146 | c.MyClass.name = 'coolname' |
|
146 | c.MyClass.name = 'coolname' | |
147 | c.MyClass.ranking = 10 |
|
147 | c.MyClass.ranking = 10 | |
148 |
|
148 | |||
149 | After this configuration file is loaded, the values set in it will override |
|
149 | After this configuration file is loaded, the values set in it will override | |
150 | the class defaults anytime a :class:`MyClass` is created. Furthermore, |
|
150 | the class defaults anytime a :class:`MyClass` is created. Furthermore, | |
151 | these attributes will be type checked and validated anytime they are set. |
|
151 | these attributes will be type checked and validated anytime they are set. | |
152 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, |
|
152 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, | |
153 | which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types. |
|
153 | which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types. | |
154 | In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides |
|
154 | In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides | |
155 | traitlets for a number of other types. |
|
155 | traitlets for a number of other types. | |
156 |
|
156 | |||
157 | .. note:: |
|
157 | .. note:: | |
158 |
|
158 | |||
159 | Underneath the hood, the :class:`Configurable` base class is a subclass of |
|
159 | Underneath the hood, the :class:`Configurable` base class is a subclass of | |
160 | :class:`IPython.utils.traitlets.HasTraits`. The |
|
160 | :class:`IPython.utils.traitlets.HasTraits`. The | |
161 | :mod:`IPython.utils.traitlets` module is a lightweight version of |
|
161 | :mod:`IPython.utils.traitlets` module is a lightweight version of | |
162 | :mod:`enthought.traits`. Our implementation is a pure Python subset |
|
162 | :mod:`enthought.traits`. Our implementation is a pure Python subset | |
163 | (mostly API compatible) of :mod:`enthought.traits` that does not have any |
|
163 | (mostly API compatible) of :mod:`enthought.traits` that does not have any | |
164 | of the automatic GUI generation capabilities. Our plan is to achieve 100% |
|
164 | of the automatic GUI generation capabilities. Our plan is to achieve 100% | |
165 | API compatibility to enable the actual :mod:`enthought.traits` to |
|
165 | API compatibility to enable the actual :mod:`enthought.traits` to | |
166 | eventually be used instead. Currently, we cannot use |
|
166 | eventually be used instead. Currently, we cannot use | |
167 | :mod:`enthought.traits` as we are committed to the core of IPython being |
|
167 | :mod:`enthought.traits` as we are committed to the core of IPython being | |
168 | pure Python. |
|
168 | pure Python. | |
169 |
|
169 | |||
170 | It should be very clear at this point what the naming convention is for |
|
170 | It should be very clear at this point what the naming convention is for | |
171 | configuration attributes:: |
|
171 | configuration attributes:: | |
172 |
|
172 | |||
173 | c.ClassName.attribute_name = attribute_value |
|
173 | c.ClassName.attribute_name = attribute_value | |
174 |
|
174 | |||
175 | Here, ``ClassName`` is the name of the class whose configuration attribute you |
|
175 | Here, ``ClassName`` is the name of the class whose configuration attribute you | |
176 | want to set, ``attribute_name`` is the name of the attribute you want to set |
|
176 | want to set, ``attribute_name`` is the name of the attribute you want to set | |
177 | and ``attribute_value`` the the value you want it to have. The ``ClassName`` |
|
177 | and ``attribute_value`` the the value you want it to have. The ``ClassName`` | |
178 | attribute of ``c`` is not the actual class, but instead is another |
|
178 | attribute of ``c`` is not the actual class, but instead is another | |
179 | :class:`~IPython.config.loader.Config` instance. |
|
179 | :class:`~IPython.config.loader.Config` instance. | |
180 |
|
180 | |||
181 | .. note:: |
|
181 | .. note:: | |
182 |
|
182 | |||
183 | The careful reader may wonder how the ``ClassName`` (``MyClass`` in |
|
183 | The careful reader may wonder how the ``ClassName`` (``MyClass`` in | |
184 | the above example) attribute of the configuration object ``c`` gets |
|
184 | the above example) attribute of the configuration object ``c`` gets | |
185 | created. These attributes are created on the fly by the |
|
185 | created. These attributes are created on the fly by the | |
186 | :class:`~IPython.config.loader.Config` instance, using a simple naming |
|
186 | :class:`~IPython.config.loader.Config` instance, using a simple naming | |
187 | convention. Any attribute of a :class:`~IPython.config.loader.Config` |
|
187 | convention. Any attribute of a :class:`~IPython.config.loader.Config` | |
188 | instance whose name begins with an uppercase character is assumed to be a |
|
188 | instance whose name begins with an uppercase character is assumed to be a | |
189 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` |
|
189 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` | |
190 | instance is dynamically created for that attribute. This allows deeply |
|
190 | instance is dynamically created for that attribute. This allows deeply | |
191 | hierarchical information created easily (``c.Foo.Bar.value``) on the fly. |
|
191 | hierarchical information created easily (``c.Foo.Bar.value``) on the fly. | |
192 |
|
192 | |||
193 | Configuration files inheritance |
|
193 | Configuration files inheritance | |
194 | =============================== |
|
194 | =============================== | |
195 |
|
195 | |||
196 | Let's say you want to have different configuration files for various purposes. |
|
196 | Let's say you want to have different configuration files for various purposes. | |
197 | Our configuration system makes it easy for one configuration file to inherit |
|
197 | Our configuration system makes it easy for one configuration file to inherit | |
198 | the information in another configuration file. The :func:`load_subconfig` |
|
198 | the information in another configuration file. The :func:`load_subconfig` | |
199 | command can be used in a configuration file for this purpose. Here is a simple |
|
199 | command can be used in a configuration file for this purpose. Here is a simple | |
200 | example that loads all of the values from the file :file:`base_config.py`:: |
|
200 | example that loads all of the values from the file :file:`base_config.py`:: | |
201 |
|
201 | |||
202 | # base_config.py |
|
202 | # base_config.py | |
203 | c = get_config() |
|
203 | c = get_config() | |
204 | c.MyClass.name = 'coolname' |
|
204 | c.MyClass.name = 'coolname' | |
205 | c.MyClass.ranking = 100 |
|
205 | c.MyClass.ranking = 100 | |
206 |
|
206 | |||
207 | into the configuration file :file:`main_config.py`:: |
|
207 | into the configuration file :file:`main_config.py`:: | |
208 |
|
208 | |||
209 | # main_config.py |
|
209 | # main_config.py | |
210 | c = get_config() |
|
210 | c = get_config() | |
211 |
|
211 | |||
212 | # Load everything from base_config.py |
|
212 | # Load everything from base_config.py | |
213 | load_subconfig('base_config.py') |
|
213 | load_subconfig('base_config.py') | |
214 |
|
214 | |||
215 | # Now override one of the values |
|
215 | # Now override one of the values | |
216 | c.MyClass.name = 'bettername' |
|
216 | c.MyClass.name = 'bettername' | |
217 |
|
217 | |||
218 | In a situation like this the :func:`load_subconfig` makes sure that the |
|
218 | In a situation like this the :func:`load_subconfig` makes sure that the | |
219 | search path for sub-configuration files is inherited from that of the parent. |
|
219 | search path for sub-configuration files is inherited from that of the parent. | |
220 | Thus, you can typically put the two in the same directory and everything will |
|
220 | Thus, you can typically put the two in the same directory and everything will | |
221 | just work. |
|
221 | just work. | |
222 |
|
222 | |||
223 | You can also load configuration files by profile, for instance: |
|
223 | You can also load configuration files by profile, for instance: | |
224 |
|
224 | |||
225 | .. sourcecode:: python |
|
225 | .. sourcecode:: python | |
226 |
|
226 | |||
227 | load_subconfig('ipython_config.py', profile='default') |
|
227 | load_subconfig('ipython_config.py', profile='default') | |
228 |
|
228 | |||
229 | to inherit your default configuration as a starting point. |
|
229 | to inherit your default configuration as a starting point. | |
230 |
|
230 | |||
231 |
|
231 | |||
232 | Class based configuration inheritance |
|
232 | Class based configuration inheritance | |
233 | ===================================== |
|
233 | ===================================== | |
234 |
|
234 | |||
235 | There is another aspect of configuration where inheritance comes into play. |
|
235 | There is another aspect of configuration where inheritance comes into play. | |
236 | Sometimes, your classes will have an inheritance hierarchy that you want |
|
236 | Sometimes, your classes will have an inheritance hierarchy that you want | |
237 | to be reflected in the configuration system. Here is a simple example:: |
|
237 | to be reflected in the configuration system. Here is a simple example:: | |
238 |
|
238 | |||
239 | from IPython.config.configurable import Configurable |
|
239 | from IPython.config.configurable import Configurable | |
240 | from IPython.utils.traitlets import Int, Float, Unicode, Bool |
|
240 | from IPython.utils.traitlets import Int, Float, Unicode, Bool | |
241 |
|
241 | |||
242 | class Foo(Configurable): |
|
242 | class Foo(Configurable): | |
243 | name = Unicode(u'fooname', config=True) |
|
243 | name = Unicode(u'fooname', config=True) | |
244 | value = Float(100.0, config=True) |
|
244 | value = Float(100.0, config=True) | |
245 |
|
245 | |||
246 | class Bar(Foo): |
|
246 | class Bar(Foo): | |
247 | name = Unicode(u'barname', config=True) |
|
247 | name = Unicode(u'barname', config=True) | |
248 | othervalue = Int(0, config=True) |
|
248 | othervalue = Int(0, config=True) | |
249 |
|
249 | |||
250 | Now, we can create a configuration file to configure instances of :class:`Foo` |
|
250 | Now, we can create a configuration file to configure instances of :class:`Foo` | |
251 | and :class:`Bar`:: |
|
251 | and :class:`Bar`:: | |
252 |
|
252 | |||
253 | # config file |
|
253 | # config file | |
254 | c = get_config() |
|
254 | c = get_config() | |
255 |
|
255 | |||
256 | c.Foo.name = u'bestname' |
|
256 | c.Foo.name = u'bestname' | |
257 | c.Bar.othervalue = 10 |
|
257 | c.Bar.othervalue = 10 | |
258 |
|
258 | |||
259 | This class hierarchy and configuration file accomplishes the following: |
|
259 | This class hierarchy and configuration file accomplishes the following: | |
260 |
|
260 | |||
261 | * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be |
|
261 | * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be | |
262 | 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also |
|
262 | 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also | |
263 | picks up the configuration information for :class:`Foo`. |
|
263 | picks up the configuration information for :class:`Foo`. | |
264 | * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be |
|
264 | * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be | |
265 | ``100.0``, which is the value specified as the class default. |
|
265 | ``100.0``, which is the value specified as the class default. | |
266 | * The default value for :attr:`Bar.othervalue` will be 10 as set in the |
|
266 | * The default value for :attr:`Bar.othervalue` will be 10 as set in the | |
267 | configuration file. Because :class:`Foo` is the parent of :class:`Bar` |
|
267 | configuration file. Because :class:`Foo` is the parent of :class:`Bar` | |
268 | it doesn't know anything about the :attr:`othervalue` attribute. |
|
268 | it doesn't know anything about the :attr:`othervalue` attribute. | |
269 |
|
269 | |||
270 |
|
270 | |||
271 | .. _ipython_dir: |
|
271 | .. _ipython_dir: | |
272 |
|
272 | |||
273 | Configuration file location |
|
273 | Configuration file location | |
274 | =========================== |
|
274 | =========================== | |
275 |
|
275 | |||
276 | So where should you put your configuration files? IPython uses "profiles" for |
|
276 | So where should you put your configuration files? IPython uses "profiles" for | |
277 | configuration, and by default, all profiles will be stored in the so called |
|
277 | configuration, and by default, all profiles will be stored in the so called | |
278 | "IPython directory". The location of this directory is determined by the |
|
278 | "IPython directory". The location of this directory is determined by the | |
279 | following algorithm: |
|
279 | following algorithm: | |
280 |
|
280 | |||
281 | * If the ``ipython_dir`` command line flag is given, its value is used. |
|
281 | * If the ``ipython_dir`` command line flag is given, its value is used. | |
282 |
|
282 | |||
283 | * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir` |
|
283 | * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir` | |
284 | is used. This function will first look at the :envvar:`IPYTHON_DIR` |
|
284 | is used. This function will first look at the :envvar:`IPYTHON_DIR` | |
285 | environment variable and then default to a platform-specific default. |
|
285 | environment variable and then default to a platform-specific default. | |
286 |
|
286 | |||
287 | On posix systems (Linux, Unix, etc.), IPython respects the ``$XDG_CONFIG_HOME`` |
|
287 | On posix systems (Linux, Unix, etc.), IPython respects the ``$XDG_CONFIG_HOME`` | |
288 | part of the `XDG Base Directory`_ specification. If ``$XDG_CONFIG_HOME`` is |
|
288 | part of the `XDG Base Directory`_ specification. If ``$XDG_CONFIG_HOME`` is | |
289 | defined and exists ( ``XDG_CONFIG_HOME`` has a default interpretation of |
|
289 | defined and exists ( ``XDG_CONFIG_HOME`` has a default interpretation of | |
290 | :file:`$HOME/.config`), then IPython's config directory will be located in |
|
290 | :file:`$HOME/.config`), then IPython's config directory will be located in | |
291 | :file:`$XDG_CONFIG_HOME/ipython`. If users still have an IPython directory |
|
291 | :file:`$XDG_CONFIG_HOME/ipython`. If users still have an IPython directory | |
292 | in :file:`$HOME/.ipython`, then that will be used. in preference to the |
|
292 | in :file:`$HOME/.ipython`, then that will be used. in preference to the | |
293 | system default. |
|
293 | system default. | |
294 |
|
294 | |||
295 | For most users, the default value will simply be something like |
|
295 | For most users, the default value will simply be something like | |
296 | :file:`$HOME/.config/ipython` on Linux, or :file:`$HOME/.ipython` |
|
296 | :file:`$HOME/.config/ipython` on Linux, or :file:`$HOME/.ipython` | |
297 | elsewhere. |
|
297 | elsewhere. | |
298 |
|
298 | |||
299 | Once the location of the IPython directory has been determined, you need to know |
|
299 | Once the location of the IPython directory has been determined, you need to know | |
300 | which profile you are using. For users with a single configuration, this will |
|
300 | which profile you are using. For users with a single configuration, this will | |
301 | simply be 'default', and will be located in |
|
301 | simply be 'default', and will be located in | |
302 | :file:`<IPYTHON_DIR>/profile_default`. |
|
302 | :file:`<IPYTHON_DIR>/profile_default`. | |
303 |
|
303 | |||
304 | The next thing you need to know is what to call your configuration file. The |
|
304 | The next thing you need to know is what to call your configuration file. The | |
305 | basic idea is that each application has its own default configuration filename. |
|
305 | basic idea is that each application has its own default configuration filename. | |
306 | The default named used by the :command:`ipython` command line program is |
|
306 | The default named used by the :command:`ipython` command line program is | |
307 | :file:`ipython_config.py`, and *all* IPython applications will use this file. |
|
307 | :file:`ipython_config.py`, and *all* IPython applications will use this file. | |
308 | Other applications, such as the parallel :command:`ipcluster` scripts or the |
|
308 | Other applications, such as the parallel :command:`ipcluster` scripts or the | |
309 | QtConsole will load their own config files *after* :file:`ipython_config.py`. To |
|
309 | QtConsole will load their own config files *after* :file:`ipython_config.py`. To | |
310 | load a particular configuration file instead of the default, the name can be |
|
310 | load a particular configuration file instead of the default, the name can be | |
311 | overridden by the ``config_file`` command line flag. |
|
311 | overridden by the ``config_file`` command line flag. | |
312 |
|
312 | |||
313 | To generate the default configuration files, do:: |
|
313 | To generate the default configuration files, do:: | |
314 |
|
314 | |||
315 | $> ipython profile create |
|
315 | $> ipython profile create | |
316 |
|
316 | |||
317 | and you will have a default :file:`ipython_config.py` in your IPython directory |
|
317 | and you will have a default :file:`ipython_config.py` in your IPython directory | |
318 | under :file:`profile_default`. If you want the default config files for the |
|
318 | under :file:`profile_default`. If you want the default config files for the | |
319 | :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the |
|
319 | :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the | |
320 | command-line args. |
|
320 | command-line args. | |
321 |
|
321 | |||
322 | .. _Profiles: |
|
322 | .. _Profiles: | |
323 |
|
323 | |||
324 | Profiles |
|
324 | Profiles | |
325 | ======== |
|
325 | ======== | |
326 |
|
326 | |||
327 | A profile is a directory containing configuration and runtime files, such as |
|
327 | A profile is a directory containing configuration and runtime files, such as | |
328 | logs, connection info for the parallel apps, and your IPython command history. |
|
328 | logs, connection info for the parallel apps, and your IPython command history. | |
329 |
|
329 | |||
330 | The idea is that users often want to maintain a set of configuration files for |
|
330 | The idea is that users often want to maintain a set of configuration files for | |
331 | different purposes: one for doing numerical computing with NumPy and SciPy and |
|
331 | different purposes: one for doing numerical computing with NumPy and SciPy and | |
332 | another for doing symbolic computing with SymPy. Profiles make it easy to keep a |
|
332 | another for doing symbolic computing with SymPy. Profiles make it easy to keep a | |
333 | separate configuration files, logs, and histories for each of these purposes. |
|
333 | separate configuration files, logs, and histories for each of these purposes. | |
334 |
|
334 | |||
335 | Let's start by showing how a profile is used: |
|
335 | Let's start by showing how a profile is used: | |
336 |
|
336 | |||
337 | .. code-block:: bash |
|
337 | .. code-block:: bash | |
338 |
|
338 | |||
339 | $ ipython profile=sympy |
|
339 | $ ipython --profile=sympy | |
340 |
|
340 | |||
341 | This tells the :command:`ipython` command line program to get its configuration |
|
341 | This tells the :command:`ipython` command line program to get its configuration | |
342 | from the "sympy" profile. The file names for various profiles do not change. The |
|
342 | from the "sympy" profile. The file names for various profiles do not change. The | |
343 | only difference is that profiles are named in a special way. In the case above, |
|
343 | only difference is that profiles are named in a special way. In the case above, | |
344 | the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHON_DIR>/profile_sympy`. |
|
344 | the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHON_DIR>/profile_sympy`. | |
345 |
|
345 | |||
346 | The general pattern is this: simply create a new profile with: |
|
346 | The general pattern is this: simply create a new profile with: | |
347 |
|
347 | |||
348 | .. code-block:: bash |
|
348 | .. code-block:: bash | |
349 |
|
349 | |||
350 | ipython profile create <name> |
|
350 | ipython profile create <name> | |
351 |
|
351 | |||
352 | which adds a directory called ``profile_<name>`` to your IPython directory. Then |
|
352 | which adds a directory called ``profile_<name>`` to your IPython directory. Then | |
353 | you can load this profile by adding ``profile=<name>`` to your command line |
|
353 | you can load this profile by adding ``--profile=<name>`` to your command line | |
354 | options. Profiles are supported by all IPython applications. |
|
354 | options. Profiles are supported by all IPython applications. | |
355 |
|
355 | |||
356 | IPython ships with some sample profiles in :file:`IPython/config/profile`. If |
|
356 | IPython ships with some sample profiles in :file:`IPython/config/profile`. If | |
357 | you create profiles with the name of one of our shipped profiles, these config |
|
357 | you create profiles with the name of one of our shipped profiles, these config | |
358 | files will be copied over instead of starting with the automatically generated |
|
358 | files will be copied over instead of starting with the automatically generated | |
359 | config files. |
|
359 | config files. | |
360 |
|
360 | |||
361 | .. _commandline: |
|
361 | .. _commandline: | |
362 |
|
362 | |||
363 | Command-line arguments |
|
363 | Command-line arguments | |
364 | ====================== |
|
364 | ====================== | |
365 |
|
365 | |||
366 | IPython exposes *all* configurable options on the command-line. The command-line |
|
366 | IPython exposes *all* configurable options on the command-line. The command-line | |
367 | arguments are generated from the Configurable traits of the classes associated |
|
367 | arguments are generated from the Configurable traits of the classes associated | |
368 | with a given Application. Configuring IPython from the command-line may look |
|
368 | with a given Application. Configuring IPython from the command-line may look | |
369 | very similar to an IPython config file |
|
369 | very similar to an IPython config file | |
370 |
|
370 | |||
371 | IPython applications use a parser called |
|
371 | IPython applications use a parser called | |
372 | :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config |
|
372 | :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config | |
373 | object. Values are assigned in much the same way as in a config file: |
|
373 | object. Values are assigned in much the same way as in a config file: | |
374 |
|
374 | |||
375 | .. code-block:: bash |
|
375 | .. code-block:: bash | |
376 |
|
376 | |||
377 | $> ipython InteractiveShell.use_readline=False BaseIPythonApplication.profile='myprofile' |
|
377 | $> ipython --InteractiveShell.use_readline=False --BaseIPythonApplication.profile='myprofile' | |
378 |
|
378 | |||
379 | Is the same as adding: |
|
379 | Is the same as adding: | |
380 |
|
380 | |||
381 | .. sourcecode:: python |
|
381 | .. sourcecode:: python | |
382 |
|
382 | |||
383 | c.InteractiveShell.use_readline=False |
|
383 | c.InteractiveShell.use_readline=False | |
384 | c.BaseIPythonApplication.profile='myprofile' |
|
384 | c.BaseIPythonApplication.profile='myprofile' | |
385 |
|
385 | |||
386 | to your config file. Key/Value arguments *always* take a value, separated by '=' |
|
386 | to your config file. Key/Value arguments *always* take a value, separated by '=' | |
387 | and no spaces. |
|
387 | and no spaces. | |
388 |
|
388 | |||
389 | Aliases |
|
389 | Aliases | |
390 | ------- |
|
390 | ------- | |
391 |
|
391 | |||
392 | For convenience, applications have a mapping of commonly |
|
392 | For convenience, applications have a mapping of commonly | |
393 | used traits, so you don't have to specify the whole class name. For these **aliases**, the class need not be specified: |
|
393 | used traits, so you don't have to specify the whole class name. For these **aliases**, the class need not be specified: | |
394 |
|
394 | |||
395 | .. code-block:: bash |
|
395 | .. code-block:: bash | |
396 |
|
396 | |||
397 | $> ipython profile='myprofile' |
|
397 | $> ipython --profile='myprofile' | |
398 | # is equivalent to |
|
398 | # is equivalent to | |
399 | $> ipython BaseIPythonApplication.profile='myprofile' |
|
399 | $> ipython --BaseIPythonApplication.profile='myprofile' | |
400 |
|
400 | |||
401 | Flags |
|
401 | Flags | |
402 | ----- |
|
402 | ----- | |
403 |
|
403 | |||
404 | Applications can also be passed **flags**. Flags are options that take no |
|
404 | Applications can also be passed **flags**. Flags are options that take no | |
405 | arguments, and are always prefixed with ``--``. They are simply wrappers for |
|
405 | arguments, and are always prefixed with ``--``. They are simply wrappers for | |
406 | setting one or more configurables with predefined values, often True/False. |
|
406 | setting one or more configurables with predefined values, often True/False. | |
407 |
|
407 | |||
408 | For instance: |
|
408 | For instance: | |
409 |
|
409 | |||
410 | .. code-block:: bash |
|
410 | .. code-block:: bash | |
411 |
|
411 | |||
412 | $> ipcontroller --debug |
|
412 | $> ipcontroller --debug | |
413 | # is equivalent to |
|
413 | # is equivalent to | |
414 | $> ipcontroller Application.log_level=DEBUG |
|
414 | $> ipcontroller --Application.log_level=DEBUG | |
415 | # and |
|
415 | # and | |
416 | $> ipython --pylab |
|
416 | $> ipython --pylab | |
417 | # is equivalent to |
|
417 | # is equivalent to | |
418 | $> ipython pylab=auto |
|
418 | $> ipython --pylab=auto | |
419 |
|
419 | |||
420 | Subcommands |
|
420 | Subcommands | |
421 | ----------- |
|
421 | ----------- | |
422 |
|
422 | |||
423 |
|
423 | |||
424 | Some IPython applications have **subcommands**. Subcommands are modeled after |
|
424 | Some IPython applications have **subcommands**. Subcommands are modeled after | |
425 | :command:`git`, and are called with the form :command:`command subcommand |
|
425 | :command:`git`, and are called with the form :command:`command subcommand | |
426 | [...args]`. Currently, the QtConsole is a subcommand of terminal IPython: |
|
426 | [...args]`. Currently, the QtConsole is a subcommand of terminal IPython: | |
427 |
|
427 | |||
428 | .. code-block:: bash |
|
428 | .. code-block:: bash | |
429 |
|
429 | |||
430 | $> ipython qtconsole profile=myprofile |
|
430 | $> ipython qtconsole --profile=myprofile | |
431 |
|
431 | |||
432 | and :command:`ipcluster` is simply a wrapper for its various subcommands (start, |
|
432 | and :command:`ipcluster` is simply a wrapper for its various subcommands (start, | |
433 | stop, engines). |
|
433 | stop, engines). | |
434 |
|
434 | |||
435 | .. code-block:: bash |
|
435 | .. code-block:: bash | |
436 |
|
436 | |||
437 | $> ipcluster start profile=myprofile n=4 |
|
437 | $> ipcluster start --profile=myprofile --n=4 | |
438 |
|
438 | |||
439 |
|
439 | |||
440 | To see a list of the available aliases, flags, and subcommands for an IPython application, simply pass ``-h`` or ``--help``. And to see the full list of configurable options (*very* long), pass ``--help-all``. |
|
440 | To see a list of the available aliases, flags, and subcommands for an IPython application, simply pass ``-h`` or ``--help``. And to see the full list of configurable options (*very* long), pass ``--help-all``. | |
441 |
|
441 | |||
442 |
|
442 | |||
443 | Design requirements |
|
443 | Design requirements | |
444 | =================== |
|
444 | =================== | |
445 |
|
445 | |||
446 | Here are the main requirements we wanted our configuration system to have: |
|
446 | Here are the main requirements we wanted our configuration system to have: | |
447 |
|
447 | |||
448 | * Support for hierarchical configuration information. |
|
448 | * Support for hierarchical configuration information. | |
449 |
|
449 | |||
450 | * Full integration with command line option parsers. Often, you want to read |
|
450 | * Full integration with command line option parsers. Often, you want to read | |
451 | a configuration file, but then override some of the values with command line |
|
451 | a configuration file, but then override some of the values with command line | |
452 | options. Our configuration system automates this process and allows each |
|
452 | options. Our configuration system automates this process and allows each | |
453 | command line option to be linked to a particular attribute in the |
|
453 | command line option to be linked to a particular attribute in the | |
454 | configuration hierarchy that it will override. |
|
454 | configuration hierarchy that it will override. | |
455 |
|
455 | |||
456 | * Configuration files that are themselves valid Python code. This accomplishes |
|
456 | * Configuration files that are themselves valid Python code. This accomplishes | |
457 | many things. First, it becomes possible to put logic in your configuration |
|
457 | many things. First, it becomes possible to put logic in your configuration | |
458 | files that sets attributes based on your operating system, network setup, |
|
458 | files that sets attributes based on your operating system, network setup, | |
459 | Python version, etc. Second, Python has a super simple syntax for accessing |
|
459 | Python version, etc. Second, Python has a super simple syntax for accessing | |
460 | hierarchical data structures, namely regular attribute access |
|
460 | hierarchical data structures, namely regular attribute access | |
461 | (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to |
|
461 | (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to | |
462 | import configuration attributes from one configuration file to another. |
|
462 | import configuration attributes from one configuration file to another. | |
463 | Fourth, even though Python is dynamically typed, it does have types that can |
|
463 | Fourth, even though Python is dynamically typed, it does have types that can | |
464 | be checked at runtime. Thus, a ``1`` in a config file is the integer '1', |
|
464 | be checked at runtime. Thus, a ``1`` in a config file is the integer '1', | |
465 | while a ``'1'`` is a string. |
|
465 | while a ``'1'`` is a string. | |
466 |
|
466 | |||
467 | * A fully automated method for getting the configuration information to the |
|
467 | * A fully automated method for getting the configuration information to the | |
468 | classes that need it at runtime. Writing code that walks a configuration |
|
468 | classes that need it at runtime. Writing code that walks a configuration | |
469 | hierarchy to extract a particular attribute is painful. When you have |
|
469 | hierarchy to extract a particular attribute is painful. When you have | |
470 | complex configuration information with hundreds of attributes, this makes |
|
470 | complex configuration information with hundreds of attributes, this makes | |
471 | you want to cry. |
|
471 | you want to cry. | |
472 |
|
472 | |||
473 | * Type checking and validation that doesn't require the entire configuration |
|
473 | * Type checking and validation that doesn't require the entire configuration | |
474 | hierarchy to be specified statically before runtime. Python is a very |
|
474 | hierarchy to be specified statically before runtime. Python is a very | |
475 | dynamic language and you don't always know everything that needs to be |
|
475 | dynamic language and you don't always know everything that needs to be | |
476 | configured when a program starts. |
|
476 | configured when a program starts. | |
477 |
|
477 | |||
478 |
|
478 | |||
479 | .. _`XDG Base Directory`: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
|
479 | .. _`XDG Base Directory`: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
@@ -1,391 +1,393 b'' | |||||
1 | .. _testing: |
|
1 | .. _testing: | |
2 |
|
2 | |||
3 | ========================================== |
|
3 | ========================================== | |
4 | Testing IPython for users and developers |
|
4 | Testing IPython for users and developers | |
5 | ========================================== |
|
5 | ========================================== | |
6 |
|
6 | |||
7 | Overview |
|
7 | Overview | |
8 | ======== |
|
8 | ======== | |
9 |
|
9 | |||
10 | It is extremely important that all code contributed to IPython has tests. |
|
10 | It is extremely important that all code contributed to IPython has tests. | |
11 | Tests should be written as unittests, doctests or other entities that the |
|
11 | Tests should be written as unittests, doctests or other entities that the | |
12 | IPython test system can detect. See below for more details on this. |
|
12 | IPython test system can detect. See below for more details on this. | |
13 |
|
13 | |||
14 | Each subpackage in IPython should have its own :file:`tests` directory that |
|
14 | Each subpackage in IPython should have its own :file:`tests` directory that | |
15 | contains all of the tests for that subpackage. All of the files in the |
|
15 | contains all of the tests for that subpackage. All of the files in the | |
16 | :file:`tests` directory should have the word "tests" in them to enable |
|
16 | :file:`tests` directory should have the word "tests" in them to enable | |
17 | the testing framework to find them. |
|
17 | the testing framework to find them. | |
18 |
|
18 | |||
19 | In docstrings, examples (either using IPython prompts like ``In [1]:`` or |
|
19 | In docstrings, examples (either using IPython prompts like ``In [1]:`` or | |
20 | 'classic' python ``>>>`` ones) can and should be included. The testing system |
|
20 | 'classic' python ``>>>`` ones) can and should be included. The testing system | |
21 | will detect them as doctests and will run them; it offers control to skip parts |
|
21 | will detect them as doctests and will run them; it offers control to skip parts | |
22 | or all of a specific doctest if the example is meant to be informative but |
|
22 | or all of a specific doctest if the example is meant to be informative but | |
23 | shows non-reproducible information (like filesystem data). |
|
23 | shows non-reproducible information (like filesystem data). | |
24 |
|
24 | |||
25 | If a subpackage has any dependencies beyond the Python standard library, the |
|
25 | If a subpackage has any dependencies beyond the Python standard library, the | |
26 | tests for that subpackage should be skipped if the dependencies are not found. |
|
26 | tests for that subpackage should be skipped if the dependencies are not found. | |
27 | This is very important so users don't get tests failing simply because they |
|
27 | This is very important so users don't get tests failing simply because they | |
28 | don't have dependencies. |
|
28 | don't have dependencies. | |
29 |
|
29 | |||
30 | The testing system we use is a hybrid of nose_ and Twisted's trial_ test runner. |
|
30 | The testing system we use is a hybrid of nose_ and Twisted's trial_ test runner. | |
31 | We use both because nose detects more things than Twisted and allows for more |
|
31 | We use both because nose detects more things than Twisted and allows for more | |
32 | flexible (and lighter-weight) ways of writing tests; in particular we've |
|
32 | flexible (and lighter-weight) ways of writing tests; in particular we've | |
33 | developed a nose plugin that allows us to paste verbatim IPython sessions and |
|
33 | developed a nose plugin that allows us to paste verbatim IPython sessions and | |
34 | test them as doctests, which is extremely important for us. But the parts of |
|
34 | test them as doctests, which is extremely important for us. But the parts of | |
35 | IPython that depend on Twisted must be tested using trial, because only trial |
|
35 | IPython that depend on Twisted must be tested using trial, because only trial | |
36 | manages the Twisted reactor correctly. |
|
36 | manages the Twisted reactor correctly. | |
37 |
|
37 | |||
38 | .. _nose: http://code.google.com/p/python-nose |
|
38 | .. _nose: http://code.google.com/p/python-nose | |
39 | .. _trial: http://twistedmatrix.com/trac/wiki/TwistedTrial |
|
39 | .. _trial: http://twistedmatrix.com/trac/wiki/TwistedTrial | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | For the impatient: running the tests |
|
42 | For the impatient: running the tests | |
43 | ==================================== |
|
43 | ==================================== | |
44 |
|
44 | |||
45 | You can run IPython from the source download directory without even installing |
|
45 | You can run IPython from the source download directory without even installing | |
46 | it system-wide or having configure anything, by typing at the terminal: |
|
46 | it system-wide or having configure anything, by typing at the terminal: | |
47 |
|
47 | |||
48 | .. code-block:: bash |
|
48 | .. code-block:: bash | |
49 |
|
49 | |||
50 | python ipython.py |
|
50 | python ipython.py | |
51 |
|
51 | |||
52 | In order to run the test suite, you must at least be able to import IPython, |
|
52 | In order to run the test suite, you must at least be able to import IPython, | |
53 | even if you haven't fully installed the user-facing scripts yet (common in a |
|
53 | even if you haven't fully installed the user-facing scripts yet (common in a | |
54 | development environment). You can then run the tests with: |
|
54 | development environment). You can then run the tests with: | |
55 |
|
55 | |||
56 | .. code-block:: bash |
|
56 | .. code-block:: bash | |
57 |
|
57 | |||
58 | python -c "import IPython; IPython.test()" |
|
58 | python -c "import IPython; IPython.test()" | |
59 |
|
59 | |||
60 | Once you have installed IPython either via a full install or using: |
|
60 | Once you have installed IPython either via a full install or using: | |
61 |
|
61 | |||
62 | .. code-block:: bash |
|
62 | .. code-block:: bash | |
63 |
|
63 | |||
64 | python setup.py develop |
|
64 | python setup.py develop | |
65 |
|
65 | |||
66 | you will have available a system-wide script called :file:`iptest` that runs |
|
66 | you will have available a system-wide script called :file:`iptest` that runs | |
67 | the full test suite. You can then run the suite with: |
|
67 | the full test suite. You can then run the suite with: | |
68 |
|
68 | |||
69 | .. code-block:: bash |
|
69 | .. code-block:: bash | |
70 |
|
70 | |||
71 | iptest [args] |
|
71 | iptest [args] | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | Regardless of how you run things, you should eventually see something like: |
|
74 | Regardless of how you run things, you should eventually see something like: | |
75 |
|
75 | |||
76 | .. code-block:: bash |
|
76 | .. code-block:: bash | |
77 |
|
77 | |||
78 | ********************************************************************** |
|
78 | ********************************************************************** | |
79 | Test suite completed for system with the following information: |
|
79 | Test suite completed for system with the following information: | |
80 | {'commit_hash': '144fdae', |
|
80 | {'commit_hash': '144fdae', | |
81 | 'commit_source': 'repository', |
|
81 | 'commit_source': 'repository', | |
82 | 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython', |
|
82 | 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython', | |
83 | 'ipython_version': '0.11.dev', |
|
83 | 'ipython_version': '0.11.dev', | |
84 | 'os_name': 'posix', |
|
84 | 'os_name': 'posix', | |
85 | 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick', |
|
85 | 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick', | |
86 | 'sys_executable': '/usr/bin/python', |
|
86 | 'sys_executable': '/usr/bin/python', | |
87 | 'sys_platform': 'linux2', |
|
87 | 'sys_platform': 'linux2', | |
88 | 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \n[GCC 4.4.5]'} |
|
88 | 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \n[GCC 4.4.5]'} | |
89 |
|
89 | |||
90 | Tools and libraries available at test time: |
|
90 | Tools and libraries available at test time: | |
91 | curses foolscap gobject gtk pexpect twisted wx wx.aui zope.interface |
|
91 | curses foolscap gobject gtk pexpect twisted wx wx.aui zope.interface | |
92 |
|
92 | |||
93 | Ran 9 test groups in 67.213s |
|
93 | Ran 9 test groups in 67.213s | |
94 |
|
94 | |||
95 | Status: |
|
95 | Status: | |
96 | OK |
|
96 | OK | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | If not, there will be a message indicating which test group failed and how to |
|
99 | If not, there will be a message indicating which test group failed and how to | |
100 | rerun that group individually. For example, this tests the |
|
100 | rerun that group individually. For example, this tests the | |
101 | :mod:`IPython.utils` subpackage, the :option:`-v` option shows progress |
|
101 | :mod:`IPython.utils` subpackage, the :option:`-v` option shows progress | |
102 | indicators: |
|
102 | indicators: | |
103 |
|
103 | |||
104 | .. code-block:: bash |
|
104 | .. code-block:: bash | |
105 |
|
105 | |||
106 | $ iptest -v IPython.utils |
|
106 | $ iptest -v IPython.utils | |
107 | ..........................SS..SSS............................S.S... |
|
107 | ..........................SS..SSS............................S.S... | |
108 | ......................................................... |
|
108 | ......................................................... | |
109 | ---------------------------------------------------------------------- |
|
109 | ---------------------------------------------------------------------- | |
110 | Ran 125 tests in 0.119s |
|
110 | Ran 125 tests in 0.119s | |
111 |
|
111 | |||
112 | OK (SKIP=7) |
|
112 | OK (SKIP=7) | |
113 |
|
113 | |||
114 |
|
114 | |||
115 | Because the IPython test machinery is based on nose, you can use all nose |
|
115 | Because the IPython test machinery is based on nose, you can use all nose | |
116 | options and syntax, typing ``iptest -h`` shows all available options. For |
|
116 | options and syntax, typing ``iptest -h`` shows all available options. For | |
117 | example, this lets you run the specific test :func:`test_rehashx` inside the |
|
117 | example, this lets you run the specific test :func:`test_rehashx` inside the | |
118 | :mod:`test_magic` module: |
|
118 | :mod:`test_magic` module: | |
119 |
|
119 | |||
120 | .. code-block:: bash |
|
120 | .. code-block:: bash | |
121 |
|
121 | |||
122 | $ iptest -vv IPython.core.tests.test_magic:test_rehashx |
|
122 | $ iptest -vv IPython.core.tests.test_magic:test_rehashx | |
123 | IPython.core.tests.test_magic.test_rehashx(True,) ... ok |
|
123 | IPython.core.tests.test_magic.test_rehashx(True,) ... ok | |
124 | IPython.core.tests.test_magic.test_rehashx(True,) ... ok |
|
124 | IPython.core.tests.test_magic.test_rehashx(True,) ... ok | |
125 |
|
125 | |||
126 | ---------------------------------------------------------------------- |
|
126 | ---------------------------------------------------------------------- | |
127 | Ran 2 tests in 0.100s |
|
127 | Ran 2 tests in 0.100s | |
128 |
|
128 | |||
129 | OK |
|
129 | OK | |
130 |
|
130 | |||
131 | When developing, the :option:`--pdb` and :option:`--pdb-failures` of nose are |
|
131 | When developing, the :option:`--pdb` and :option:`--pdb-failures` of nose are | |
132 | particularly useful, these drop you into an interactive pdb session at the |
|
132 | particularly useful, these drop you into an interactive pdb session at the | |
133 | point of the error or failure respectively. |
|
133 | point of the error or failure respectively. | |
134 |
|
134 | |||
135 | To run Twisted-using tests, use the :command:`trial` command on a per file or |
|
135 | To run Twisted-using tests, use the :command:`trial` command on a per file or | |
136 | package basis: |
|
136 | package basis: | |
137 |
|
137 | |||
138 | .. code-block:: bash |
|
138 | .. code-block:: bash | |
139 |
|
139 | |||
140 | trial IPython.kernel |
|
140 | trial IPython.kernel | |
141 |
|
141 | |||
142 | .. note:: |
|
142 | .. note:: | |
143 |
|
143 | |||
144 | The system information summary printed above is accessible from the top |
|
144 | The system information summary printed above is accessible from the top | |
145 | level package. If you encounter a problem with IPython, it's useful to |
|
145 | level package. If you encounter a problem with IPython, it's useful to | |
146 | include this information when reporting on the mailing list; use:: |
|
146 | include this information when reporting on the mailing list; use:: | |
147 |
|
147 | |||
148 | from IPython import sys_info |
|
148 | from IPython import sys_info | |
149 | print sys_info() |
|
149 | print sys_info() | |
150 |
|
150 | |||
151 | and include the resulting information in your query. |
|
151 | and include the resulting information in your query. | |
152 |
|
152 | |||
153 | For developers: writing tests |
|
153 | For developers: writing tests | |
154 | ============================= |
|
154 | ============================= | |
155 |
|
155 | |||
156 | By now IPython has a reasonable test suite, so the best way to see what's |
|
156 | By now IPython has a reasonable test suite, so the best way to see what's | |
157 | available is to look at the :file:`tests` directory in most subpackages. But |
|
157 | available is to look at the :file:`tests` directory in most subpackages. But | |
158 | here are a few pointers to make the process easier. |
|
158 | here are a few pointers to make the process easier. | |
159 |
|
159 | |||
160 |
|
160 | |||
161 | Main tools: :mod:`IPython.testing` |
|
161 | Main tools: :mod:`IPython.testing` | |
162 | ---------------------------------- |
|
162 | ---------------------------------- | |
163 |
|
163 | |||
164 | The :mod:`IPython.testing` package is where all of the machinery to test |
|
164 | The :mod:`IPython.testing` package is where all of the machinery to test | |
165 | IPython (rather than the tests for its various parts) lives. In particular, |
|
165 | IPython (rather than the tests for its various parts) lives. In particular, | |
166 | the :mod:`iptest` module in there has all the smarts to control the test |
|
166 | the :mod:`iptest` module in there has all the smarts to control the test | |
167 | process. In there, the :func:`make_exclude` function is used to build a |
|
167 | process. In there, the :func:`make_exclude` function is used to build a | |
168 | blacklist of exclusions, these are modules that do not get even imported for |
|
168 | blacklist of exclusions, these are modules that do not get even imported for | |
169 | tests. This is important so that things that would fail to even import because |
|
169 | tests. This is important so that things that would fail to even import because | |
170 | of missing dependencies don't give errors to end users, as we stated above. |
|
170 | of missing dependencies don't give errors to end users, as we stated above. | |
171 |
|
171 | |||
172 | The :mod:`decorators` module contains a lot of useful decorators, especially |
|
172 | The :mod:`decorators` module contains a lot of useful decorators, especially | |
173 | useful to mark individual tests that should be skipped under certain conditions |
|
173 | useful to mark individual tests that should be skipped under certain conditions | |
174 | (rather than blacklisting the package altogether because of a missing major |
|
174 | (rather than blacklisting the package altogether because of a missing major | |
175 | dependency). |
|
175 | dependency). | |
176 |
|
176 | |||
177 | Our nose plugin for doctests |
|
177 | Our nose plugin for doctests | |
178 | ---------------------------- |
|
178 | ---------------------------- | |
179 |
|
179 | |||
180 | The :mod:`plugin` subpackage in testing contains a nose plugin called |
|
180 | The :mod:`plugin` subpackage in testing contains a nose plugin called | |
181 | :mod:`ipdoctest` that teaches nose about IPython syntax, so you can write |
|
181 | :mod:`ipdoctest` that teaches nose about IPython syntax, so you can write | |
182 | doctests with IPython prompts. You can also mark doctest output with ``# |
|
182 | doctests with IPython prompts. You can also mark doctest output with ``# | |
183 | random`` for the output corresponding to a single input to be ignored (stronger |
|
183 | random`` for the output corresponding to a single input to be ignored (stronger | |
184 | than using ellipsis and useful to keep it as an example). If you want the |
|
184 | than using ellipsis and useful to keep it as an example). If you want the | |
185 | entire docstring to be executed but none of the output from any input to be |
|
185 | entire docstring to be executed but none of the output from any input to be | |
186 | checked, you can use the ``# all-random`` marker. The |
|
186 | checked, you can use the ``# all-random`` marker. The | |
187 | :mod:`IPython.testing.plugin.dtexample` module contains examples of how to use |
|
187 | :mod:`IPython.testing.plugin.dtexample` module contains examples of how to use | |
188 | these; for reference here is how to use ``# random``:: |
|
188 | these; for reference here is how to use ``# random``:: | |
189 |
|
189 | |||
190 | def ranfunc(): |
|
190 | def ranfunc(): | |
191 | """A function with some random output. |
|
191 | """A function with some random output. | |
192 |
|
192 | |||
193 | Normal examples are verified as usual: |
|
193 | Normal examples are verified as usual: | |
194 | >>> 1+3 |
|
194 | >>> 1+3 | |
195 | 4 |
|
195 | 4 | |
196 |
|
196 | |||
197 | But if you put '# random' in the output, it is ignored: |
|
197 | But if you put '# random' in the output, it is ignored: | |
198 | >>> 1+3 |
|
198 | >>> 1+3 | |
199 | junk goes here... # random |
|
199 | junk goes here... # random | |
200 |
|
200 | |||
201 | >>> 1+2 |
|
201 | >>> 1+2 | |
202 | again, anything goes #random |
|
202 | again, anything goes #random | |
203 | if multiline, the random mark is only needed once. |
|
203 | if multiline, the random mark is only needed once. | |
204 |
|
204 | |||
205 | >>> 1+2 |
|
205 | >>> 1+2 | |
206 | You can also put the random marker at the end: |
|
206 | You can also put the random marker at the end: | |
207 | # random |
|
207 | # random | |
208 |
|
208 | |||
209 | >>> 1+2 |
|
209 | >>> 1+2 | |
210 | # random |
|
210 | # random | |
211 | .. or at the beginning. |
|
211 | .. or at the beginning. | |
212 |
|
212 | |||
213 | More correct input is properly verified: |
|
213 | More correct input is properly verified: | |
214 | >>> ranfunc() |
|
214 | >>> ranfunc() | |
215 | 'ranfunc' |
|
215 | 'ranfunc' | |
216 | """ |
|
216 | """ | |
217 | return 'ranfunc' |
|
217 | return 'ranfunc' | |
218 |
|
218 | |||
219 | and an example of ``# all-random``:: |
|
219 | and an example of ``# all-random``:: | |
220 |
|
220 | |||
221 | def random_all(): |
|
221 | def random_all(): | |
222 | """A function where we ignore the output of ALL examples. |
|
222 | """A function where we ignore the output of ALL examples. | |
223 |
|
223 | |||
224 | Examples: |
|
224 | Examples: | |
225 |
|
225 | |||
226 | # all-random |
|
226 | # all-random | |
227 |
|
227 | |||
228 | This mark tells the testing machinery that all subsequent examples |
|
228 | This mark tells the testing machinery that all subsequent examples | |
229 | should be treated as random (ignoring their output). They are still |
|
229 | should be treated as random (ignoring their output). They are still | |
230 | executed, so if a they raise an error, it will be detected as such, |
|
230 | executed, so if a they raise an error, it will be detected as such, | |
231 | but their output is completely ignored. |
|
231 | but their output is completely ignored. | |
232 |
|
232 | |||
233 | >>> 1+3 |
|
233 | >>> 1+3 | |
234 | junk goes here... |
|
234 | junk goes here... | |
235 |
|
235 | |||
236 | >>> 1+3 |
|
236 | >>> 1+3 | |
237 | klasdfj; |
|
237 | klasdfj; | |
238 |
|
238 | |||
239 | In [8]: print 'hello' |
|
239 | In [8]: print 'hello' | |
240 | world # random |
|
240 | world # random | |
241 |
|
241 | |||
242 | In [9]: iprand() |
|
242 | In [9]: iprand() | |
243 | Out[9]: 'iprand' |
|
243 | Out[9]: 'iprand' | |
244 | """ |
|
244 | """ | |
245 | return 'iprand' |
|
245 | return 'iprand' | |
246 |
|
246 | |||
247 |
|
247 | |||
248 | When writing docstrings, you can use the ``@skip_doctest`` decorator to |
|
248 | When writing docstrings, you can use the ``@skip_doctest`` decorator to | |
249 | indicate that a docstring should *not* be treated as a doctest at all. The |
|
249 | indicate that a docstring should *not* be treated as a doctest at all. The | |
250 | difference between ``# all-random`` and ``@skip_doctest`` is that the former |
|
250 | difference between ``# all-random`` and ``@skip_doctest`` is that the former | |
251 | executes the example but ignores output, while the latter doesn't execute any |
|
251 | executes the example but ignores output, while the latter doesn't execute any | |
252 | code. ``@skip_doctest`` should be used for docstrings whose examples are |
|
252 | code. ``@skip_doctest`` should be used for docstrings whose examples are | |
253 | purely informational. |
|
253 | purely informational. | |
254 |
|
254 | |||
255 | If a given docstring fails under certain conditions but otherwise is a good |
|
255 | If a given docstring fails under certain conditions but otherwise is a good | |
256 | doctest, you can use code like the following, that relies on the 'null' |
|
256 | doctest, you can use code like the following, that relies on the 'null' | |
257 | decorator to leave the docstring intact where it works as a test:: |
|
257 | decorator to leave the docstring intact where it works as a test:: | |
258 |
|
258 | |||
259 | # The docstring for full_path doctests differently on win32 (different path |
|
259 | # The docstring for full_path doctests differently on win32 (different path | |
260 | # separator) so just skip the doctest there, and use a null decorator |
|
260 | # separator) so just skip the doctest there, and use a null decorator | |
261 | # elsewhere: |
|
261 | # elsewhere: | |
262 |
|
262 | |||
263 | doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco |
|
263 | doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco | |
264 |
|
264 | |||
265 | @doctest_deco |
|
265 | @doctest_deco | |
266 | def full_path(startPath,files): |
|
266 | def full_path(startPath,files): | |
267 | """Make full paths for all the listed files, based on startPath...""" |
|
267 | """Make full paths for all the listed files, based on startPath...""" | |
268 |
|
268 | |||
269 | # function body follows... |
|
269 | # function body follows... | |
270 |
|
270 | |||
271 | With our nose plugin that understands IPython syntax, an extremely effective |
|
271 | With our nose plugin that understands IPython syntax, an extremely effective | |
272 | way to write tests is to simply copy and paste an interactive session into a |
|
272 | way to write tests is to simply copy and paste an interactive session into a | |
273 | docstring. You can writing this type of test, where your docstring is meant |
|
273 | docstring. You can writing this type of test, where your docstring is meant | |
274 | *only* as a test, by prefixing the function name with ``doctest_`` and leaving |
|
274 | *only* as a test, by prefixing the function name with ``doctest_`` and leaving | |
275 | its body *absolutely empty* other than the docstring. In |
|
275 | its body *absolutely empty* other than the docstring. In | |
276 | :mod:`IPython.core.tests.test_magic` you can find several examples of this, but |
|
276 | :mod:`IPython.core.tests.test_magic` you can find several examples of this, but | |
277 | for completeness sake, your code should look like this (a simple case):: |
|
277 | for completeness sake, your code should look like this (a simple case):: | |
278 |
|
278 | |||
279 | def doctest_time(): |
|
279 | def doctest_time(): | |
280 | """ |
|
280 | """ | |
281 | In [10]: %time None |
|
281 | In [10]: %time None | |
282 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
282 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
283 | Wall time: 0.00 s |
|
283 | Wall time: 0.00 s | |
284 | """ |
|
284 | """ | |
285 |
|
285 | |||
286 | This function is only analyzed for its docstring but it is not considered a |
|
286 | This function is only analyzed for its docstring but it is not considered a | |
287 | separate test, which is why its body should be empty. |
|
287 | separate test, which is why its body should be empty. | |
288 |
|
288 | |||
289 |
|
289 | |||
290 | Parametric tests done right |
|
290 | Parametric tests done right | |
291 | --------------------------- |
|
291 | --------------------------- | |
292 |
|
292 | |||
293 | If you need to run multiple tests inside the same standalone function or method |
|
293 | If you need to run multiple tests inside the same standalone function or method | |
294 | of a :class:`unittest.TestCase` subclass, IPython provides the ``parametric`` |
|
294 | of a :class:`unittest.TestCase` subclass, IPython provides the ``parametric`` | |
295 | decorator for this purpose. This is superior to how test generators work in |
|
295 | decorator for this purpose. This is superior to how test generators work in | |
296 | nose, because IPython's keeps intact your stack, which makes debugging vastly |
|
296 | nose, because IPython's keeps intact your stack, which makes debugging vastly | |
297 | easier. For example, these are some parametric tests both in class form and as |
|
297 | easier. For example, these are some parametric tests both in class form and as | |
298 | a standalone function (choose in each situation the style that best fits the |
|
298 | a standalone function (choose in each situation the style that best fits the | |
299 | problem at hand, since both work):: |
|
299 | problem at hand, since both work):: | |
300 |
|
300 | |||
301 | from IPython.testing import decorators as dec |
|
301 | from IPython.testing import decorators as dec | |
302 |
|
302 | |||
303 | def is_smaller(i,j): |
|
303 | def is_smaller(i,j): | |
304 | assert i<j,"%s !< %s" % (i,j) |
|
304 | assert i<j,"%s !< %s" % (i,j) | |
305 |
|
305 | |||
306 | class Tester(ParametricTestCase): |
|
306 | class Tester(ParametricTestCase): | |
307 |
|
307 | |||
308 | def test_parametric(self): |
|
308 | def test_parametric(self): | |
309 | yield is_smaller(3, 4) |
|
309 | yield is_smaller(3, 4) | |
310 | x, y = 1, 2 |
|
310 | x, y = 1, 2 | |
311 | yield is_smaller(x, y) |
|
311 | yield is_smaller(x, y) | |
312 |
|
312 | |||
313 | @dec.parametric |
|
313 | @dec.parametric | |
314 | def test_par_standalone(): |
|
314 | def test_par_standalone(): | |
315 | yield is_smaller(3, 4) |
|
315 | yield is_smaller(3, 4) | |
316 | x, y = 1, 2 |
|
316 | x, y = 1, 2 | |
317 | yield is_smaller(x, y) |
|
317 | yield is_smaller(x, y) | |
318 |
|
318 | |||
319 |
|
319 | |||
320 | Writing tests for Twisted-using code |
|
320 | Writing tests for Twisted-using code | |
321 | ------------------------------------ |
|
321 | ------------------------------------ | |
322 |
|
322 | |||
323 | Tests of Twisted [Twisted]_ using code should be written by subclassing the |
|
323 | Tests of Twisted [Twisted]_ using code should be written by subclassing the | |
324 | ``TestCase`` class that comes with ``twisted.trial.unittest``. Furthermore, all |
|
324 | ``TestCase`` class that comes with ``twisted.trial.unittest``. Furthermore, all | |
325 | :class:`Deferred` instances that are created in the test must be properly |
|
325 | :class:`Deferred` instances that are created in the test must be properly | |
326 | chained and the final one *must* be the return value of the test method. |
|
326 | chained and the final one *must* be the return value of the test method. | |
327 |
|
327 | |||
328 | .. note:: |
|
328 | .. note:: | |
329 |
|
329 | |||
330 | The best place to see how to use the testing tools, are the tests for these |
|
330 | The best place to see how to use the testing tools, are the tests for these | |
331 | tools themselves, which live in :mod:`IPython.testing.tests`. |
|
331 | tools themselves, which live in :mod:`IPython.testing.tests`. | |
332 |
|
332 | |||
333 |
|
333 | |||
334 | Design requirements |
|
334 | Design requirements | |
335 | =================== |
|
335 | =================== | |
336 |
|
336 | |||
337 | This section is a set of notes on the key points of the IPython testing needs, |
|
337 | This section is a set of notes on the key points of the IPython testing needs, | |
338 | that were used when writing the system and should be kept for reference as it |
|
338 | that were used when writing the system and should be kept for reference as it | |
339 | eveolves. |
|
339 | eveolves. | |
340 |
|
340 | |||
341 | Testing IPython in full requires modifications to the default behavior of nose |
|
341 | Testing IPython in full requires modifications to the default behavior of nose | |
342 | and doctest, because the IPython prompt is not recognized to determine Python |
|
342 | and doctest, because the IPython prompt is not recognized to determine Python | |
343 | input, and because IPython admits user input that is not valid Python (things |
|
343 | input, and because IPython admits user input that is not valid Python (things | |
344 | like ``%magics`` and ``!system commands``. |
|
344 | like ``%magics`` and ``!system commands``. | |
345 |
|
345 | |||
346 | We basically need to be able to test the following types of code: |
|
346 | We basically need to be able to test the following types of code: | |
347 |
|
347 | |||
348 | 1. Pure Python files containing normal tests. These are not a problem, since |
|
348 | 1. Pure Python files containing normal tests. These are not a problem, since | |
349 | Nose will pick them up as long as they conform to the (flexible) conventions |
|
349 | Nose will pick them up as long as they conform to the (flexible) conventions | |
350 | used by nose to recognize tests. |
|
350 | used by nose to recognize tests. | |
351 |
|
351 | |||
352 |
2. Python files containing doctests. |
|
352 | 2. Python files containing doctests. Here, we have two possibilities: | |
|
353 | ||||
353 | - The prompts are the usual ``>>>`` and the input is pure Python. |
|
354 | - The prompts are the usual ``>>>`` and the input is pure Python. | |
354 | - The prompts are of the form ``In [1]:`` and the input can contain extended |
|
355 | - The prompts are of the form ``In [1]:`` and the input can contain extended | |
355 | IPython expressions. |
|
356 | IPython expressions. | |
356 |
|
357 | |||
357 | In the first case, Nose will recognize the doctests as long as it is called |
|
358 | In the first case, Nose will recognize the doctests as long as it is called | |
358 | with the ``--with-doctest`` flag. But the second case will likely require |
|
359 | with the ``--with-doctest`` flag. But the second case will likely require | |
359 | modifications or the writing of a new doctest plugin for Nose that is |
|
360 | modifications or the writing of a new doctest plugin for Nose that is | |
360 | IPython-aware. |
|
361 | IPython-aware. | |
361 |
|
362 | |||
362 | 3. ReStructuredText files that contain code blocks. For this type of file, we |
|
363 | 3. ReStructuredText files that contain code blocks. For this type of file, we | |
363 | have three distinct possibilities for the code blocks: |
|
364 | have three distinct possibilities for the code blocks: | |
|
365 | ||||
364 | - They use ``>>>`` prompts. |
|
366 | - They use ``>>>`` prompts. | |
365 | - They use ``In [1]:`` prompts. |
|
367 | - They use ``In [1]:`` prompts. | |
366 | - They are standalone blocks of pure Python code without any prompts. |
|
368 | - They are standalone blocks of pure Python code without any prompts. | |
367 |
|
369 | |||
368 | The first two cases are similar to the situation #2 above, except that in |
|
370 | The first two cases are similar to the situation #2 above, except that in | |
369 | this case the doctests must be extracted from input code blocks using |
|
371 | this case the doctests must be extracted from input code blocks using | |
370 | docutils instead of from the Python docstrings. |
|
372 | docutils instead of from the Python docstrings. | |
371 |
|
373 | |||
372 | In the third case, we must have a convention for distinguishing code blocks |
|
374 | In the third case, we must have a convention for distinguishing code blocks | |
373 | that are meant for execution from others that may be snippets of shell code |
|
375 | that are meant for execution from others that may be snippets of shell code | |
374 | or other examples not meant to be run. One possibility is to assume that |
|
376 | or other examples not meant to be run. One possibility is to assume that | |
375 | all indented code blocks are meant for execution, but to have a special |
|
377 | all indented code blocks are meant for execution, but to have a special | |
376 | docutils directive for input that should not be executed. |
|
378 | docutils directive for input that should not be executed. | |
377 |
|
379 | |||
378 | For those code blocks that we will execute, the convention used will simply |
|
380 | For those code blocks that we will execute, the convention used will simply | |
379 | be that they get called and are considered successful if they run to |
|
381 | be that they get called and are considered successful if they run to | |
380 | completion without raising errors. This is similar to what Nose does for |
|
382 | completion without raising errors. This is similar to what Nose does for | |
381 | standalone test functions, and by putting asserts or other forms of |
|
383 | standalone test functions, and by putting asserts or other forms of | |
382 | exception-raising statements it becomes possible to have literate examples |
|
384 | exception-raising statements it becomes possible to have literate examples | |
383 | that double as lightweight tests. |
|
385 | that double as lightweight tests. | |
384 |
|
386 | |||
385 | 4. Extension modules with doctests in function and method docstrings. |
|
387 | 4. Extension modules with doctests in function and method docstrings. | |
386 | Currently Nose simply can't find these docstrings correctly, because the |
|
388 | Currently Nose simply can't find these docstrings correctly, because the | |
387 | underlying doctest DocTestFinder object fails there. Similarly to #2 above, |
|
389 | underlying doctest DocTestFinder object fails there. Similarly to #2 above, | |
388 | the docstrings could have either pure python or IPython prompts. |
|
390 | the docstrings could have either pure python or IPython prompts. | |
389 |
|
391 | |||
390 | Of these, only 3-c (reST with standalone code blocks) is not implemented at |
|
392 | Of these, only 3-c (reST with standalone code blocks) is not implemented at | |
391 | this point. |
|
393 | this point. |
General Comments 0
You need to be logged in to leave comments.
Login now