Show More
@@ -0,0 +1,5 | |||||
|
1 | .. _extensions_overview: | |||
|
2 | ||||
|
3 | ================== | |||
|
4 | IPython extensions | |||
|
5 | ================== No newline at end of file |
@@ -42,29 +42,26 Application: :class:`~IPython.core.application.Application` | |||||
42 | application is the :command:`ipython` command line program. Each |
|
42 | application is the :command:`ipython` command line program. Each | |
43 | application reads a *single* configuration file and command line options |
|
43 | application reads a *single* configuration file and command line options | |
44 | and then produces a master configuration object for the application. This |
|
44 | and then produces a master configuration object for the application. This | |
45 |
configuration object is then passed to the co |
|
45 | configuration object is then passed to the configurable objects that the | |
46 | creates. Components implement the actual logic of the application and know |
|
46 | application creates. These configurable objects implement the actual logic | |
47 |
how to configure themselves given the |
|
47 | of the application and know how to configure themselves given the | |
48 |
|
48 | configuration object. | ||
49 | Component: :class:`~IPython.core.component.Component` |
|
49 | ||
50 | A component is a regular Python class that serves as a base class for all |
|
50 | Component: :class:`~IPython.config.configurable.Configurable` | |
51 | main classes in an application. The |
|
51 | A configurable is a regular Python class that serves as a base class for | |
52 | :class:`~IPython.core.component.Component` base class is lightweight and |
|
52 | all main classes in an application. The | |
53 | only does two main things. |
|
53 | :class:`~IPython.config.configurable.Configurable` base class is | |
|
54 | lightweight and only does one things. | |||
|
55 | ||||
|
56 | This :class:`~IPython.config.configurable.Configurable` is a subclass | |||
|
57 | of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure | |||
|
58 | itself. Class level traits with the metadata ``config=True`` become | |||
|
59 | values that can be configured from the command line and configuration | |||
|
60 | files. | |||
54 |
|
61 | |||
55 | First, it keeps track of all instances of itself and provides an |
|
62 | Developers create :class:`~IPython.config.configurable.Configurable` | |
56 | interfaces for querying those instances. This enables components to get |
|
63 | subclasses that implement all of the logic in the application. Each of | |
57 | references to other components, even though they are not "nearby" in the |
|
64 | these subclasses has its own configuration information that controls how | |
58 | runtime object graph. |
|
|||
59 |
|
||||
60 | Second, it declares what class attributes are configurable and specifies |
|
|||
61 | the default types and values of those attributes. This information is used |
|
|||
62 | to automatically configure instances given the applications configuration |
|
|||
63 | object. |
|
|||
64 |
|
||||
65 | Developers create :class:`~IPython.core.component.Component` subclasses |
|
|||
66 | that implement all of the logic in the application. Each of these |
|
|||
67 | subclasses has its own configuration information that controls how |
|
|||
68 | instances are created. |
|
65 | instances are created. | |
69 |
|
66 | |||
70 | Having described these main concepts, we can now state the main idea in our |
|
67 | Having described these main concepts, we can now state the main idea in our | |
@@ -73,7 +70,9 attributes to be controlled on a class by class basis*. Thus all instances of | |||||
73 | a given class are configured in the same way. Furthermore, if two instances |
|
70 | a given class are configured in the same way. Furthermore, if two instances | |
74 | need to be configured differently, they need to be instances of two different |
|
71 | need to be configured differently, they need to be instances of two different | |
75 | classes. While this model may seem a bit restrictive, we have found that it |
|
72 | classes. While this model may seem a bit restrictive, we have found that it | |
76 | expresses most things that need to be configured extremely well. |
|
73 | expresses most things that need to be configured extremely well. However, it | |
|
74 | is possible to create two instances of the same class that have different | |||
|
75 | trait values. This is done by overriding the configuration. | |||
77 |
|
76 | |||
78 | Now, we show what our configuration objects and files look like. |
|
77 | Now, we show what our configuration objects and files look like. | |
79 |
|
78 | |||
@@ -98,33 +97,34 attributes on it. All you have to know is: | |||||
98 | * The type of each attribute. |
|
97 | * The type of each attribute. | |
99 |
|
98 | |||
100 | The answers to these two questions are provided by the various |
|
99 | The answers to these two questions are provided by the various | |
101 |
:class:`~IPython.co |
|
100 | :class:`~IPython.config.configurable.Configurable` subclasses that an | |
102 |
uses. |
|
101 | application uses. Let's look at how this would work for a simple component | |
|
102 | subclass:: | |||
103 |
|
103 | |||
104 | # Sample component that can be configured. |
|
104 | # Sample component that can be configured. | |
105 |
from IPython.co |
|
105 | from IPython.config.configurable import Configurable | |
106 | from IPython.utils.traitlets import Int, Float, Str, Bool |
|
106 | from IPython.utils.traitlets import Int, Float, Str, Bool | |
107 |
|
107 | |||
108 |
class MyC |
|
108 | class MyClass(Configurable): | |
109 | name = Str('defaultname', config=True) |
|
109 | name = Str('defaultname', config=True) | |
110 | ranking = Int(0, config=True) |
|
110 | ranking = Int(0, config=True) | |
111 | value = Float(99.0) |
|
111 | value = Float(99.0) | |
112 | # The rest of the class implementation would go here.. |
|
112 | # The rest of the class implementation would go here.. | |
113 |
|
113 | |||
114 |
In this example, we see that :class:`MyC |
|
114 | In this example, we see that :class:`MyClass` has three attributes, two | |
115 | of whom (``name``, ``ranking``) can be configured. All of the attributes |
|
115 | of whom (``name``, ``ranking``) can be configured. All of the attributes | |
116 |
are given types and default values. If a :class:`MyC |
|
116 | are given types and default values. If a :class:`MyClass` is instantiated, | |
117 | but not configured, these default values will be used. But let's see how |
|
117 | but not configured, these default values will be used. But let's see how | |
118 | to configure this class in a configuration file:: |
|
118 | to configure this class in a configuration file:: | |
119 |
|
119 | |||
120 | # Sample config file |
|
120 | # Sample config file | |
121 | c = get_config() |
|
121 | c = get_config() | |
122 |
|
122 | |||
123 |
c.MyC |
|
123 | c.MyClass.name = 'coolname' | |
124 |
c.MyC |
|
124 | c.MyClass.ranking = 10 | |
125 |
|
125 | |||
126 | After this configuration file is loaded, the values set in it will override |
|
126 | After this configuration file is loaded, the values set in it will override | |
127 |
the class defaults anytime a :class:`MyC |
|
127 | the class defaults anytime a :class:`MyClass` is created. Furthermore, | |
128 | these attributes will be type checked and validated anytime they are set. |
|
128 | these attributes will be type checked and validated anytime they are set. | |
129 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, |
|
129 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, | |
130 | which provides the :class:`Str`, :class:`Int` and :class:`Float` types. In |
|
130 | which provides the :class:`Str`, :class:`Int` and :class:`Float` types. In | |
@@ -133,7 +133,7 traitlets for a number of other types. | |||||
133 |
|
133 | |||
134 | .. note:: |
|
134 | .. note:: | |
135 |
|
135 | |||
136 |
Underneath the hood, the :class:`Co |
|
136 | Underneath the hood, the :class:`Configurable` base class is a subclass of | |
137 | :class:`IPython.utils.traitlets.HasTraits`. The |
|
137 | :class:`IPython.utils.traitlets.HasTraits`. The | |
138 | :mod:`IPython.utils.traitlets` module is a lightweight version of |
|
138 | :mod:`IPython.utils.traitlets` module is a lightweight version of | |
139 | :mod:`enthought.traits`. Our implementation is a pure Python subset |
|
139 | :mod:`enthought.traits`. Our implementation is a pure Python subset | |
@@ -157,7 +157,7 attribute of ``c`` is not the actual class, but instead is another | |||||
157 |
|
157 | |||
158 | .. note:: |
|
158 | .. note:: | |
159 |
|
159 | |||
160 |
The careful reader may wonder how the ``ClassName`` (``MyC |
|
160 | The careful reader may wonder how the ``ClassName`` (``MyClass`` in | |
161 | the above example) attribute of the configuration object ``c`` gets |
|
161 | the above example) attribute of the configuration object ``c`` gets | |
162 | created. These attributes are created on the fly by the |
|
162 | created. These attributes are created on the fly by the | |
163 | :class:`~IPython.config.loader.Config` instance, using a simple naming |
|
163 | :class:`~IPython.config.loader.Config` instance, using a simple naming | |
@@ -165,8 +165,7 attribute of ``c`` is not the actual class, but instead is another | |||||
165 | instance whose name begins with an uppercase character is assumed to be a |
|
165 | instance whose name begins with an uppercase character is assumed to be a | |
166 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` |
|
166 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` | |
167 | instance is dynamically created for that attribute. This allows deeply |
|
167 | instance is dynamically created for that attribute. This allows deeply | |
168 | hierarchical information created easily (``c.Foo.Bar.value``) on the |
|
168 | hierarchical information created easily (``c.Foo.Bar.value``) on the fly. | |
169 | fly. |
|
|||
170 |
|
169 | |||
171 | Configuration files inheritance |
|
170 | Configuration files inheritance | |
172 | =============================== |
|
171 | =============================== | |
@@ -179,8 +178,8 example that loads all of the values from the file :file:`base_config.py`:: | |||||
179 |
|
178 | |||
180 | # base_config.py |
|
179 | # base_config.py | |
181 | c = get_config() |
|
180 | c = get_config() | |
182 |
c.MyC |
|
181 | c.MyClass.name = 'coolname' | |
183 |
c.MyC |
|
182 | c.MyClass.ranking = 100 | |
184 |
|
183 | |||
185 | into the configuration file :file:`main_config.py`:: |
|
184 | into the configuration file :file:`main_config.py`:: | |
186 |
|
185 | |||
@@ -191,7 +190,7 into the configuration file :file:`main_config.py`:: | |||||
191 | load_subconfig('base_config.py') |
|
190 | load_subconfig('base_config.py') | |
192 |
|
191 | |||
193 | # Now override one of the values |
|
192 | # Now override one of the values | |
194 |
c.MyC |
|
193 | c.MyClass.name = 'bettername' | |
195 |
|
194 | |||
196 | In a situation like this the :func:`load_subconfig` makes sure that the |
|
195 | In a situation like this the :func:`load_subconfig` makes sure that the | |
197 | search path for sub-configuration files is inherited from that of the parent. |
|
196 | search path for sub-configuration files is inherited from that of the parent. | |
@@ -205,10 +204,10 There is another aspect of configuration where inheritance comes into play. | |||||
205 | Sometimes, your classes will have an inheritance hierarchy that you want |
|
204 | Sometimes, your classes will have an inheritance hierarchy that you want | |
206 | to be reflected in the configuration system. Here is a simple example:: |
|
205 | to be reflected in the configuration system. Here is a simple example:: | |
207 |
|
206 | |||
208 |
from IPython.co |
|
207 | from IPython.config.configurable import Configurable | |
209 | from IPython.utils.traitlets import Int, Float, Str, Bool |
|
208 | from IPython.utils.traitlets import Int, Float, Str, Bool | |
210 |
|
209 | |||
211 |
class Foo(Co |
|
210 | class Foo(Configurable): | |
212 | name = Str('fooname', config=True) |
|
211 | name = Str('fooname', config=True) | |
213 | value = Float(100.0, config=True) |
|
212 | value = Float(100.0, config=True) | |
214 |
|
213 | |||
@@ -328,4 +327,3 Here are the main requirements we wanted our configuration system to have: | |||||
328 | dynamic language and you don't always know everything that needs to be |
|
327 | dynamic language and you don't always know everything that needs to be | |
329 | configured when a program starts. |
|
328 | configured when a program starts. | |
330 |
|
329 | |||
331 |
|
General Comments 0
You need to be logged in to leave comments.
Login now