File: dictlike.html

package info (click to toggle)
sqlalchemy 0.9.8%2Bdfsg-0.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,952 kB
  • ctags: 24,534
  • sloc: python: 152,282; ansic: 1,346; makefile: 257; xml: 17
file content (298 lines) | stat: -rw-r--r-- 23,761 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
        <title>
            
    
                examples.vertical.dictlike
             &mdash;
    SQLAlchemy 0.9 Documentation

        </title>

        
            <!-- begin iterate through SQLA + sphinx environment css_files -->
                <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
                <link rel="stylesheet" href="../../../_static/docs.css" type="text/css" />
                <link rel="stylesheet" href="../../../_static/sphinx_paramlinks.css" type="text/css" />
                <link rel="stylesheet" href="../../../_static/changelog.css" type="text/css" />
            <!-- end iterate through SQLA + sphinx environment css_files -->
        

        

    

    <!-- begin layout.mako headers -->

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '../../../',
          VERSION:     '0.9.8',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>

    <!-- begin iterate through sphinx environment script_files -->
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <!-- end iterate through sphinx environment script_files -->

    <script type="text/javascript" src="../../../_static/detectmobile.js"></script>
    <script type="text/javascript" src="../../../_static/init.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
        <link rel="copyright" title="Copyright" href="../../../copyright.html" />
    <link rel="top" title="SQLAlchemy 0.9 Documentation" href="../../../index.html" />
        <link rel="up" title="Module code" href="../../index.html" />
    <!-- end layout.mako headers -->


    </head>
    <body>
        















<div id="docs-container">





<div id="docs-top-navigation-container" class="body-background">
<div id="docs-header">
    <div id="docs-version-header">
        Release: <span class="version-num">0.9.8</span> | Release Date: October 13, 2014
    </div>

    <h1>SQLAlchemy 0.9 Documentation</h1>

</div>
</div>

<div id="docs-body-container">

    <div id="fixed-sidebar" class="">

        <div id="index-nav">
            <form class="search" action="../../../search.html" method="get">
              <input type="text" name="q" size="12" /> <input type="submit" value="Search" />
              <input type="hidden" name="check_keywords" value="yes" />
              <input type="hidden" name="area" value="default" />
            </form>

            <p>
            <a href="../../../index.html">Contents</a> |
            <a href="../../../genindex.html">Index</a>
            </p>

        </div>


    </div>

    

    <div id="docs-body" class="" >
        
<h1>Source code for examples.vertical.dictlike</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;Mapping a vertical table as a dictionary.</span>

<span class="sd">This example illustrates accessing and modifying a &quot;vertical&quot; (or</span>
<span class="sd">&quot;properties&quot;, or pivoted) table via a dict-like interface.  These are tables</span>
<span class="sd">that store free-form object properties as rows instead of columns.  For</span>
<span class="sd">example, instead of::</span>

<span class="sd">  # A regular (&quot;horizontal&quot;) table has columns for &#39;species&#39; and &#39;size&#39;</span>
<span class="sd">  Table(&#39;animal&#39;, metadata,</span>
<span class="sd">        Column(&#39;id&#39;, Integer, primary_key=True),</span>
<span class="sd">        Column(&#39;species&#39;, Unicode),</span>
<span class="sd">        Column(&#39;size&#39;, Unicode))</span>

<span class="sd">A vertical table models this as two tables: one table for the base or parent</span>
<span class="sd">entity, and another related table holding key/value pairs::</span>

<span class="sd">  Table(&#39;animal&#39;, metadata,</span>
<span class="sd">        Column(&#39;id&#39;, Integer, primary_key=True))</span>

<span class="sd">  # The properties table will have one row for a &#39;species&#39; value, and</span>
<span class="sd">  # another row for the &#39;size&#39; value.</span>
<span class="sd">  Table(&#39;properties&#39;, metadata</span>
<span class="sd">        Column(&#39;animal_id&#39;, Integer, ForeignKey(&#39;animal.id&#39;),</span>
<span class="sd">               primary_key=True),</span>
<span class="sd">        Column(&#39;key&#39;, UnicodeText),</span>
<span class="sd">        Column(&#39;value&#39;, UnicodeText))</span>

<span class="sd">Because the key/value pairs in a vertical scheme are not fixed in advance,</span>
<span class="sd">accessing them like a Python dict can be very convenient.  The example below</span>
<span class="sd">can be used with many common vertical schemas as-is or with minor adaptations.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="k">class</span> <span class="nc">ProxiedDictMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Adds obj[key] access to a mapped class.</span>

<span class="sd">    This class basically proxies dictionary access to an attribute</span>
<span class="sd">    called ``_proxied``.  The class which inherits this class</span>
<span class="sd">    should have an attribute called ``_proxied`` which points to a dictionary.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_proxied</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_proxied</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_proxied</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_proxied</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_proxied</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_proxied</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Column</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">Unicode</span><span class="p">,</span>
        <span class="n">ForeignKey</span><span class="p">,</span> <span class="n">UnicodeText</span><span class="p">,</span> <span class="n">and_</span><span class="p">,</span> <span class="n">create_engine</span><span class="p">)</span>
    <span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">relationship</span><span class="p">,</span> <span class="n">Session</span>
    <span class="kn">from</span> <span class="nn">sqlalchemy.orm.collections</span> <span class="kn">import</span> <span class="n">attribute_mapped_collection</span>
    <span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
    <span class="kn">from</span> <span class="nn">sqlalchemy.ext.associationproxy</span> <span class="kn">import</span> <span class="n">association_proxy</span>

    <span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">AnimalFact</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;A fact about an animal.&quot;&quot;&quot;</span>

        <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;animal_fact&#39;</span>

        <span class="n">animal_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;animal.id&#39;</span><span class="p">),</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="n">key</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Unicode</span><span class="p">(</span><span class="mi">64</span><span class="p">),</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">UnicodeText</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Animal</span><span class="p">(</span><span class="n">ProxiedDictMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;an Animal&quot;&quot;&quot;</span>

        <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;animal&#39;</span>

        <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Unicode</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>

        <span class="n">facts</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;AnimalFact&quot;</span><span class="p">,</span>
                    <span class="n">collection_class</span><span class="o">=</span><span class="n">attribute_mapped_collection</span><span class="p">(</span><span class="s">&#39;key&#39;</span><span class="p">))</span>

        <span class="n">_proxied</span> <span class="o">=</span> <span class="n">association_proxy</span><span class="p">(</span><span class="s">&quot;facts&quot;</span><span class="p">,</span> <span class="s">&quot;value&quot;</span><span class="p">,</span>
                            <span class="n">creator</span><span class="o">=</span>
                            <span class="k">lambda</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="n">AnimalFact</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">value</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>

        <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="s">&quot;Animal(</span><span class="si">%r</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

        <span class="nd">@classmethod</span>
        <span class="k">def</span> <span class="nf">with_characteristic</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">facts</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">value</span><span class="p">)</span>

    <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&quot;sqlite://&quot;</span><span class="p">)</span>
    <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>

    <span class="n">session</span> <span class="o">=</span> <span class="n">Session</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>

    <span class="n">stoat</span> <span class="o">=</span> <span class="n">Animal</span><span class="p">(</span><span class="s">&#39;stoat&#39;</span><span class="p">)</span>
    <span class="n">stoat</span><span class="p">[</span><span class="s">&#39;color&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;reddish&#39;</span>
    <span class="n">stoat</span><span class="p">[</span><span class="s">&#39;cuteness&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;somewhat&#39;</span>

    <span class="c"># dict-like assignment transparently creates entries in the</span>
    <span class="c"># stoat.facts collection:</span>
    <span class="k">print</span><span class="p">(</span><span class="n">stoat</span><span class="o">.</span><span class="n">facts</span><span class="p">[</span><span class="s">&#39;color&#39;</span><span class="p">])</span>

    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">stoat</span><span class="p">)</span>
    <span class="n">session</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>

    <span class="n">critter</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Animal</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">Animal</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">&#39;stoat&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">one</span><span class="p">()</span>
    <span class="k">print</span><span class="p">(</span><span class="n">critter</span><span class="p">[</span><span class="s">&#39;color&#39;</span><span class="p">])</span>
    <span class="k">print</span><span class="p">(</span><span class="n">critter</span><span class="p">[</span><span class="s">&#39;cuteness&#39;</span><span class="p">])</span>

    <span class="n">critter</span><span class="p">[</span><span class="s">&#39;cuteness&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;very&#39;</span>

    <span class="k">print</span><span class="p">(</span><span class="s">&#39;changing cuteness:&#39;</span><span class="p">)</span>

    <span class="n">marten</span> <span class="o">=</span> <span class="n">Animal</span><span class="p">(</span><span class="s">&#39;marten&#39;</span><span class="p">)</span>
    <span class="n">marten</span><span class="p">[</span><span class="s">&#39;color&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;brown&#39;</span>
    <span class="n">marten</span><span class="p">[</span><span class="s">&#39;cuteness&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;somewhat&#39;</span>
    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">marten</span><span class="p">)</span>

    <span class="n">shrew</span> <span class="o">=</span> <span class="n">Animal</span><span class="p">(</span><span class="s">&#39;shrew&#39;</span><span class="p">)</span>
    <span class="n">shrew</span><span class="p">[</span><span class="s">&#39;cuteness&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;somewhat&#39;</span>
    <span class="n">shrew</span><span class="p">[</span><span class="s">&#39;poisonous-part&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;saliva&#39;</span>
    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">shrew</span><span class="p">)</span>

    <span class="n">loris</span> <span class="o">=</span> <span class="n">Animal</span><span class="p">(</span><span class="s">&#39;slow loris&#39;</span><span class="p">)</span>
    <span class="n">loris</span><span class="p">[</span><span class="s">&#39;cuteness&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;fairly&#39;</span>
    <span class="n">loris</span><span class="p">[</span><span class="s">&#39;poisonous-part&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;elbows&#39;</span>
    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">loris</span><span class="p">)</span>

    <span class="n">q</span> <span class="o">=</span> <span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Animal</span><span class="p">)</span><span class="o">.</span>
         <span class="nb">filter</span><span class="p">(</span><span class="n">Animal</span><span class="o">.</span><span class="n">facts</span><span class="o">.</span><span class="n">any</span><span class="p">(</span>
           <span class="n">and_</span><span class="p">(</span><span class="n">AnimalFact</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="s">&#39;color&#39;</span><span class="p">,</span>
                <span class="n">AnimalFact</span><span class="o">.</span><span class="n">value</span> <span class="o">==</span> <span class="s">&#39;reddish&#39;</span><span class="p">))))</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&#39;reddish animals&#39;</span><span class="p">,</span> <span class="n">q</span><span class="o">.</span><span class="n">all</span><span class="p">())</span>

    <span class="n">q</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Animal</span><span class="p">)</span><span class="o">.</span>\
            <span class="nb">filter</span><span class="p">(</span><span class="n">Animal</span><span class="o">.</span><span class="n">with_characteristic</span><span class="p">(</span><span class="s">&quot;color&quot;</span><span class="p">,</span> <span class="s">&#39;brown&#39;</span><span class="p">))</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&#39;brown animals&#39;</span><span class="p">,</span> <span class="n">q</span><span class="o">.</span><span class="n">all</span><span class="p">())</span>

    <span class="n">q</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Animal</span><span class="p">)</span><span class="o">.</span>\
           <span class="nb">filter</span><span class="p">(</span><span class="o">~</span><span class="n">Animal</span><span class="o">.</span><span class="n">with_characteristic</span><span class="p">(</span><span class="s">&quot;poisonous-part&quot;</span><span class="p">,</span> <span class="s">&#39;elbows&#39;</span><span class="p">))</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&#39;animals without poisonous-part == elbows&#39;</span><span class="p">,</span> <span class="n">q</span><span class="o">.</span><span class="n">all</span><span class="p">())</span>

    <span class="n">q</span> <span class="o">=</span> <span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Animal</span><span class="p">)</span><span class="o">.</span>
         <span class="nb">filter</span><span class="p">(</span><span class="n">Animal</span><span class="o">.</span><span class="n">facts</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="s">&#39;somewhat&#39;</span><span class="p">)))</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&#39;any animal with any .value of &quot;somewhat&quot;&#39;</span><span class="p">,</span> <span class="n">q</span><span class="o">.</span><span class="n">all</span><span class="p">())</span></pre></div>
    </div>

</div>

<div id="docs-bottom-navigation" class="docs-navigation-links">

    <div id="docs-copyright">
        &copy; <a href="../../../copyright.html">Copyright</a> 2007-2014, the SQLAlchemy authors and contributors.
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
    </div>
</div>

</div>

        
    </body>
</html>