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 | 42 | application is the :command:`ipython` command line program. Each |
|
43 | 43 | application reads a *single* configuration file and command line options |
|
44 | 44 | and then produces a master configuration object for the application. This |
|
45 |
configuration object is then passed to the co |
|
|
46 | creates. Components implement the actual logic of the application and know | |
|
47 |
how to configure themselves given the |
|
|
48 | ||
|
49 | Component: :class:`~IPython.core.component.Component` | |
|
50 | A component is a regular Python class that serves as a base class for all | |
|
51 | main classes in an application. The | |
|
52 | :class:`~IPython.core.component.Component` base class is lightweight and | |
|
53 | only does two main things. | |
|
54 | ||
|
55 | First, it keeps track of all instances of itself and provides an | |
|
56 | interfaces for querying those instances. This enables components to get | |
|
57 | references to other components, even though they are not "nearby" in the | |
|
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 | |
|
45 | configuration object is then passed to the configurable objects that the | |
|
46 | application creates. These configurable objects implement the actual logic | |
|
47 | of the application and know how to configure themselves given the | |
|
48 | configuration object. | |
|
49 | ||
|
50 | Component: :class:`~IPython.config.configurable.Configurable` | |
|
51 | A configurable is a regular Python class that serves as a base class for | |
|
52 | all main classes in an application. The | |
|
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. | |
|
61 | ||
|
62 | Developers create :class:`~IPython.config.configurable.Configurable` | |
|
63 | subclasses that implement all of the logic in the application. Each of | |
|
64 | these subclasses has its own configuration information that controls how | |
|
68 | 65 | instances are created. |
|
69 | 66 | |
|
70 | 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 | 70 | a given class are configured in the same way. Furthermore, if two instances |
|
74 | 71 | need to be configured differently, they need to be instances of two different |
|
75 | 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 | 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 | 97 | * The type of each attribute. |
|
99 | 98 | |
|
100 | 99 | The answers to these two questions are provided by the various |
|
101 |
:class:`~IPython.co |
|
|
102 |
uses. |
|
|
100 | :class:`~IPython.config.configurable.Configurable` subclasses that an | |
|
101 | application uses. Let's look at how this would work for a simple component | |
|
102 | subclass:: | |
|
103 | 103 | |
|
104 | 104 | # Sample component that can be configured. |
|
105 |
from IPython.co |
|
|
105 | from IPython.config.configurable import Configurable | |
|
106 | 106 | from IPython.utils.traitlets import Int, Float, Str, Bool |
|
107 | 107 | |
|
108 |
class MyC |
|
|
108 | class MyClass(Configurable): | |
|
109 | 109 | name = Str('defaultname', config=True) |
|
110 | 110 | ranking = Int(0, config=True) |
|
111 | 111 | value = Float(99.0) |
|
112 | 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 | 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 | 117 | but not configured, these default values will be used. But let's see how |
|
118 | 118 | to configure this class in a configuration file:: |
|
119 | 119 | |
|
120 | 120 | # Sample config file |
|
121 | 121 | c = get_config() |
|
122 | 122 | |
|
123 |
c.MyC |
|
|
124 |
c.MyC |
|
|
123 | c.MyClass.name = 'coolname' | |
|
124 | c.MyClass.ranking = 10 | |
|
125 | 125 | |
|
126 | 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 | 128 | these attributes will be type checked and validated anytime they are set. |
|
129 | 129 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, |
|
130 | 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 | 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 | 137 | :class:`IPython.utils.traitlets.HasTraits`. The |
|
138 | 138 | :mod:`IPython.utils.traitlets` module is a lightweight version of |
|
139 | 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 | 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 | 161 | the above example) attribute of the configuration object ``c`` gets |
|
162 | 162 | created. These attributes are created on the fly by the |
|
163 | 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 | 165 | instance whose name begins with an uppercase character is assumed to be a |
|
166 | 166 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` |
|
167 | 167 | instance is dynamically created for that attribute. This allows deeply |
|
168 | hierarchical information created easily (``c.Foo.Bar.value``) on the | |
|
169 | fly. | |
|
168 | hierarchical information created easily (``c.Foo.Bar.value``) on the fly. | |
|
170 | 169 | |
|
171 | 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 | 179 | # base_config.py |
|
181 | 180 | c = get_config() |
|
182 |
c.MyC |
|
|
183 |
c.MyC |
|
|
181 | c.MyClass.name = 'coolname' | |
|
182 | c.MyClass.ranking = 100 | |
|
184 | 183 | |
|
185 | 184 | into the configuration file :file:`main_config.py`:: |
|
186 | 185 | |
@@ -191,7 +190,7 into the configuration file :file:`main_config.py`:: | |||
|
191 | 190 | load_subconfig('base_config.py') |
|
192 | 191 | |
|
193 | 192 | # Now override one of the values |
|
194 |
c.MyC |
|
|
193 | c.MyClass.name = 'bettername' | |
|
195 | 194 | |
|
196 | 195 | In a situation like this the :func:`load_subconfig` makes sure that the |
|
197 | 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 | 204 | Sometimes, your classes will have an inheritance hierarchy that you want |
|
206 | 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 | 208 | from IPython.utils.traitlets import Int, Float, Str, Bool |
|
210 | 209 | |
|
211 |
class Foo(Co |
|
|
210 | class Foo(Configurable): | |
|
212 | 211 | name = Str('fooname', config=True) |
|
213 | 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 | 327 | dynamic language and you don't always know everything that needs to be |
|
329 | 328 | configured when a program starts. |
|
330 | 329 | |
|
331 |
General Comments 0
You need to be logged in to leave comments.
Login now