This file is indexed.

/usr/share/doc/python-gtkmvc-doc/userman/op.html is in python-gtkmvc-doc 1.99.1-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<!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>Observable Properties &mdash; Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.99.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <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>
    <link rel="top" title="Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation" href="index.html" />
    <link rel="next" title="Observers" href="observers.html" />
    <link rel="prev" title="Details of implementation" href="impl.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="observers.html" title="Observers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="impl.html" title="Details of implementation"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="observable-properties">
<span id="op-top"></span><h1>Observable Properties<a class="headerlink" href="#observable-properties" title="Permalink to this headline"></a></h1>
<p>Observable Properties (<em>OP</em>) is a powerful mechanism which
implement the <em>Observer Pattern</em>.</p>
<p>The mechanism which allows a part of the data contained in models
to be observed by entities called <em>Observers</em>. It is fully
automatic, as its management is carried out transparently by the
base class <tt class="docutils literal"><span class="pre">Model</span></tt>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Note:</th><td class="field-body">This section mainly focuses on OP in <em>Models</em>. The use of
<cite>Observer</cite> here is anticipated only for examples. The section
<a class="reference internal" href="observers.html#observers"><em>Observers</em></a> presents full details about them.</td>
</tr>
</tbody>
</table>
<p>Here a quick example is shown here, later all details are
presented.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span><span class="p">,</span> <span class="n">Observer</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>
   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,)</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyObserver</span> <span class="p">(</span><span class="n">Observer</span><span class="p">):</span>
   <span class="nd">@Observer.observe</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="n">assign</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
   <span class="k">def</span> <span class="nf">notification</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
       <span class="k">print</span> <span class="s">&quot;&#39;name&#39; changed from&quot;</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">old</span><span class="p">,</span> <span class="s">&quot;to&quot;</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">new</span>
       <span class="k">return</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
<span class="n">o</span> <span class="o">=</span> <span class="n">MyObserver</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="n">m</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Tobias&quot;</span>
</pre></div>
</div>
<p>In models, OPs are declared explicitly, and in observers we define
methods which will be called when OPs are changed. In the example,
when <tt class="docutils literal"><span class="pre">m.name</span></tt> is changed in the last line, method
<tt class="docutils literal"><span class="pre">MyObserver.notification</span></tt> is called automatically to notify the
observer. All details about observers will be presented in section
<a class="reference internal" href="observers.html#observers"><em>Observers</em></a>, in particular about what <cite>assign=True</cite> means.</p>
<p>Controllers are also observers, so the OP pattern clicks well with
the MVC pattern.</p>
<p>If the example looks smooth and relatively easy, the topic is much
more complex. OPs can be <em>concrete</em> or <em>logical</em>, and can be values
(like in the previous example), or complex objects like containers or
user&#8217;s classes. All these differences add complexity which will be
described in details in the following sections.</p>
<div class="section" id="concrete-observable-properties">
<span id="opconc"></span><h2>Concrete Observable Properties<a class="headerlink" href="#concrete-observable-properties" title="Permalink to this headline"></a></h2>
<p><em>Concrete</em> OPs have values stored inside the model. They are
different from <em>Logical</em> OP whose values are calculated, or come
from outside (e.g. from a database).</p>
<p><em>Values</em> of OP are declared as class attributes in models, and OP
<em>names</em> are declared in a special class member
<tt class="docutils literal"><span class="pre">__observables__</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>
   <span class="n">secondname</span> <span class="o">=</span> <span class="s">&#39;Mario&#39;</span>
   <span class="n">surname</span> <span class="o">=</span> <span class="s">&quot;Cavada&quot;</span>
   <span class="n">energy</span> <span class="o">=</span> <span class="mf">0.2</span> <span class="c"># needs holidays!</span>
   <span class="n">status</span> <span class="o">=</span> <span class="s">&quot;sleepy&quot;</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="s">&quot;name surname energy&quot;</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>In the example, <tt class="docutils literal"><span class="pre">name</span></tt>, <tt class="docutils literal"><span class="pre">surname</span></tt> and <tt class="docutils literal"><span class="pre">energy</span></tt> are all
observable, whereas <tt class="docutils literal"><span class="pre">status</span></tt> is not observable.</p>
<p>Special member <tt class="docutils literal"><span class="pre">__observables__</span></tt> is a tuple (or a list) of names
of class attributes that has to be observable. Names can contain
wildcards like <tt class="docutils literal"><span class="pre">*</span></tt> to match any sequence of characters, <tt class="docutils literal"><span class="pre">?</span></tt> to
match one single character, etc. See module <a class="reference external" href="http://docs.python.org/library/fnmatch.html">fnmatch</a> in <em>Python</em> library
for other information about possible use of wildcards in
names. Important to say that if wildcards are used, attribute names
starting with a double underscore <tt class="docutils literal"><span class="pre">__</span></tt> will be not matched.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>It is also possible (<em>but deprecated!</em>) for the user to add a
class variable called <tt class="docutils literal"><span class="pre">__properties__</span></tt>. This variable must be
a map, whose elements&#8217; keys are names of properties, and the
associated values are the initial values. Using
<tt class="docutils literal"><span class="pre">__properties__</span></tt> is complementary to the use of
<tt class="docutils literal"><span class="pre">__observables__</span></tt>, but it is provided for backward
compatibility and should be not used in new code.
This is an example of usage of deprecated <tt class="docutils literal"><span class="pre">__properties__</span></tt>,
but you will not find another in this manual:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="k">class</span> <span class="nc">MyModelDeprecated</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
      <span class="n">__properties__</span> <span class="o">=</span> <span class="p">{</span>
      <span class="s">&#39;name&#39;</span> <span class="p">:</span> <span class="s">&#39;Rob&#39;</span><span class="p">,</span>
      <span class="p">}</span>
      <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
</div>
<p>This is another example showing the usage of wildcards in names:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>
   <span class="n">secondname</span> <span class="o">=</span> <span class="s">&#39;Mario&#39;</span>
   <span class="n">surname</span> <span class="o">=</span> <span class="s">&quot;Cavada&quot;</span>
   <span class="n">energy</span> <span class="o">=</span> <span class="mf">0.2</span> <span class="c"># needs holidays!</span>
   <span class="n">entropy</span> <span class="o">=</span> <span class="mf">1.0</span>
   <span class="n">enology</span> <span class="o">=</span> <span class="s">&quot;good science&quot;</span>
   <span class="n">status</span> <span class="o">=</span> <span class="s">&quot;sleepy&quot;</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;*name&quot;</span><span class="p">,</span> <span class="s">&quot;en????y&quot;</span><span class="p">)</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>In the example, all attributes but <tt class="docutils literal"><span class="pre">energy</span></tt> and <tt class="docutils literal"><span class="pre">status</span></tt> are
declared to be observable.</p>
<div class="section" id="concrete-op-and-inheritance">
<h3>Concrete OP and inheritance<a class="headerlink" href="#concrete-op-and-inheritance" title="Permalink to this headline"></a></h3>
<p>Things so far are easy enough, but they get a bit complicated when
you derive custom models from other custom models. For example,
what happens to <em>OP</em> if you derive a new model class from the class
<tt class="docutils literal"><span class="pre">MyModel</span></tt>?</p>
<p>In this case the behavior of the <em>OP</em> trusty follows the typical Object
Oriented rules:</p>
<ul class="simple">
<li>Any concrete <em>OP</em> in base class are inherited by derived classes.</li>
<li>Derived class can override any concrete <em>OP</em> in base classes.</li>
<li>If multiple base classes defines the same <em>OP</em>, only the
first <em>OP</em> will be accessible from the derived class.</li>
</ul>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>

<span class="k">class</span> <span class="nc">Base</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">prop1</span> <span class="o">=</span> <span class="mi">1</span>
   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;prop1&quot;</span><span class="p">,</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">Model</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="c"># this class is an observer of its own properties:</span>
       <span class="bp">self</span><span class="o">.</span><span class="n">register_observer</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
       <span class="k">return</span>

   <span class="nd">@Model.observe</span><span class="p">(</span><span class="s">&quot;prop1&quot;</span><span class="p">,</span> <span class="n">assign</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
   <span class="k">def</span> <span class="nf">prop1_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
       <span class="k">print</span> <span class="n">model</span><span class="p">,</span> <span class="s">&quot;prop1 changed from &#39;</span><span class="si">%s</span><span class="s">&#39; to &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">old</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">new</span><span class="p">)</span>
       <span class="k">return</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># --------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">Der</span> <span class="p">(</span><span class="n">Base</span><span class="p">):</span>
   <span class="n">prop2</span> <span class="o">=</span> <span class="mi">2</span>
   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;prop2&quot;</span><span class="p">,)</span>

   <span class="nd">@Model.observe</span><span class="p">(</span><span class="s">&quot;prop2&quot;</span><span class="p">,</span> <span class="n">assign</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
   <span class="k">def</span> <span class="nf">prop2_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
       <span class="k">print</span> <span class="bp">self</span><span class="p">,</span> <span class="s">&quot;prop2 changed from &#39;</span><span class="si">%s</span><span class="s">&#39; to &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">old</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">new</span><span class="p">)</span>
       <span class="k">return</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># --------------------------------------------------------</span>

<span class="c"># test code:</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">Base</span><span class="p">()</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">Der</span><span class="p">()</span>

<span class="n">d</span><span class="o">.</span><span class="n">prop2</span> <span class="o">*=</span> <span class="mi">10</span>
<span class="n">d</span><span class="o">.</span><span class="n">prop1</span> <span class="o">*=</span> <span class="mi">10</span>
<span class="n">b</span><span class="o">.</span><span class="n">prop1</span> <span class="o">*=</span> <span class="mi">10</span>
</pre></div>
</div>
<p>When executed, this script generates this output:</p>
<div class="highlight-python"><pre>&lt;__main__.Der object  ...&gt; prop2 changed from '2' to '20'
&lt;__main__.Der object  ...&gt; prop1 changed from '1' to '10'
&lt;__main__.Base object ...&gt; prop1 changed from '1' to '10'</pre>
</div>
<p>Let&#8217;s analyse the example.</p>
<p>First, in the <tt class="docutils literal"><span class="pre">Base.__init__</span></tt> constructor you can see that the
instance registers itself as an observer... of itself! As we will see
in section <a class="reference internal" href="observers.html#observers"><em>Observers</em></a>, class <cite>Model</cite> derives from <cite>Observer</cite>, so
all models are also observer.</p>
<p>In the example this is exploited only to write a compact example (it
is not needed to define an additional class for the
observer). However, in complex designs it is quite common to see
models observing them self, or sub-models contained inside them.</p>
<p>Second, method <tt class="docutils literal"><span class="pre">Base.prop1_changed</span></tt> is explicitly marked to
observe property <tt class="docutils literal"><span class="pre">prop1</span></tt>.</p>
<p>Third, in class <tt class="docutils literal"><span class="pre">Der</span></tt> only the OP <tt class="docutils literal"><span class="pre">prop2</span></tt> is declared, as
<tt class="docutils literal"><span class="pre">prop1</span></tt> is inherited from class <tt class="docutils literal"><span class="pre">Base</span></tt>.
This is clearly visible in the output:</p>
<div class="highlight-python"><pre>&lt;__main__.Der object  ...&gt; prop1 changed from '1' to '10'</pre>
</div>
<p>It is possible to change type and default values of OPs in derived
class, by re-declaring the OSs. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Der</span> <span class="p">(</span><span class="n">Base</span><span class="p">):</span>
   <span class="n">prop1</span> <span class="o">=</span> <span class="mi">3</span>
   <span class="n">prop2</span> <span class="o">=</span> <span class="mi">2</span>
   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;prop?&quot;</span><span class="p">,)</span>

   <span class="nd">@Observer.observe</span><span class="p">(</span><span class="s">&quot;prop2&quot;</span><span class="p">,</span> <span class="n">assign</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
   <span class="k">def</span> <span class="nf">prop2_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
       <span class="k">print</span> <span class="bp">self</span><span class="p">,</span> <span class="s">&quot;prop2 changed from &#39;</span><span class="si">%s</span><span class="s">&#39; to &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">old</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">new</span><span class="p">)</span>
       <span class="k">return</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># --------------------------------------------------------</span>
</pre></div>
</div>
<p>This would produce the output:</p>
<div class="highlight-python"><pre>&lt;__main__.Der object  ...&gt; prop2 changed from '2' to '20'
&lt;__main__.Der object  ...&gt; prop1 changed from '3' to '30'
&lt;__main__.Base object ...&gt; prop1 changed from '1' to '10'</pre>
</div>
<p>As you can see, <tt class="docutils literal"><span class="pre">d.prop1</span></tt> overrides the <em>OP</em> <tt class="docutils literal"><span class="pre">prop1</span></tt> defined
in <tt class="docutils literal"><span class="pre">Base</span></tt> (they have different initial values now).</p>
</div>
</div>
<div class="section" id="logical-observable-properties">
<span id="oplog"></span><h2>Logical Observable Properties<a class="headerlink" href="#logical-observable-properties" title="Permalink to this headline"></a></h2>
<p><em>Logical</em> OPs are properties whose values are not necessarily
stored in the model, but which are read and written by a pair of
getter/setter methods.</p>
<p>This make logical OPs ideal for:</p>
<ul class="simple">
<li>Values calculated out of other OPs (which can be either logical
or concrete).</li>
<li>Values living outside the model, like e.g. in a database, or in a
remote server.</li>
</ul>
<p>Logical OPs are declared like concrete OPs, but no correspoding
attributes have to appear in the class. Their name have to appear
only within the special member <tt class="docutils literal"><span class="pre">__observables__</span></tt>.
For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="s">&quot;happiness&quot;</span><span class="p">)</span>
   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
</pre></div>
</div>
<p>In the example, <tt class="docutils literal"><span class="pre">name</span></tt> is a concrete property, whereas
<tt class="docutils literal"><span class="pre">happiness</span></tt> is a logical property, as no corresponding attribute
exists in class <tt class="docutils literal"><span class="pre">MyModel</span></tt>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Notice that names of logical OPs occurring within the
special member <tt class="docutils literal"><span class="pre">__observables__</span></tt> cannot contain wildcards like
concrete properties.</p>
<p class="last">The reasons for this limitation is obvious, as wildcards
can be used to match only class attributes.)</p>
</div>
<p>However, a logical OP&#8217;s value is taken from a getter method, and
for a read/write OP the values are stored through a setter
method. Defining a getter is mandatory, while defining a setter is
required only for writable logical OPs.</p>
<p>getter/setter methods can be defined by exploiting decorators, or
by exploiting a naming convention.</p>
<div class="section" id="use-of-decorators-for-defining-getters-and-or-setters">
<h3>Use of decorators for defining getters and/or setters<a class="headerlink" href="#use-of-decorators-for-defining-getters-and-or-setters" title="Permalink to this headline"></a></h3>
<p>Decorators <tt class="docutils literal"><span class="pre">&#64;Model.getter</span></tt> and <tt class="docutils literal"><span class="pre">&#64;Model.setter</span></tt> can be used for
defining logical OPs getter and setter respectively. The syntax and
semantics are very similar to the python <tt class="docutils literal"><span class="pre">&#64;property</span></tt>
decorator.</p>
<p>E.g. for logical OP <tt class="docutils literal"><span class="pre">happiness</span></tt> in the previous example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="s">&quot;happiness&quot;</span><span class="p">)</span>

   <span class="n">_a_value</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="c"># support for happiness</span>
   <span class="nd">@Model.getter</span>
   <span class="k">def</span> <span class="nf">happiness</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_a_value</span>

   <span class="nd">@Model.setter</span>
   <span class="k">def</span> <span class="nf">happiness</span><span class="p">(</span><span class="bp">self</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">_a_value</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
</pre></div>
</div>
<p>It is possible to define getter/setter methods which serve multiple
logical OPs.
For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="s">&quot;happiness&quot;</span><span class="p">,</span> <span class="s">&quot;energy&quot;</span><span class="p">)</span>

   <span class="n">_a_value</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="c"># support for happiness</span>
   <span class="nd">@Model.getter</span><span class="p">(</span><span class="s">&quot;happiness&quot;</span><span class="p">,</span> <span class="s">&quot;energy&quot;</span><span class="p">)</span>
   <span class="k">def</span> <span class="nf">a_getter_for_several_ops</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="k">if</span> <span class="s">&quot;energy&quot;</span> <span class="o">==</span> <span class="n">name</span><span class="p">:</span> <span class="k">return</span> <span class="mf">0.1</span> <span class="c"># constantly need holidays!</span>
     <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_a_value</span>

   <span class="nd">@Model.setter</span>
   <span class="k">def</span> <span class="nf">happiness</span><span class="p">(</span><span class="bp">self</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">_a_value</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
</pre></div>
</div>
<p>In the example, the decorator <tt class="docutils literal"><span class="pre">&#64;Model.getter</span></tt> is used with
arguments, which have to be the string names of all properties
which have to be handled by the decorated method. The method (the
getter in this case) will receive the name of the property along
with its other arguments.</p>
<p>Use of wildcards is allowed in decorators names, and will match all
logical OPs not exactly matched by other decorators. It is an error
condition if multiple matches are found when matching logical OPs
specified with wildcards. For example this is perfectly legal:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="s">&quot;energy&quot;</span><span class="p">,</span> <span class="s">&quot;entropy&quot;</span><span class="p">,</span> <span class="s">&quot;enology&quot;</span><span class="p">)</span>

   <span class="nd">@Model.getter</span>
   <span class="k">def</span> <span class="nf">energy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="mf">0.1</span>  <span class="c"># constantly need holidays!</span>

   <span class="nd">@Model.getter</span><span class="p">(</span><span class="s">&quot;enology&quot;</span><span class="p">)</span>
   <span class="k">def</span> <span class="nf">getter1</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="k">return</span> <span class="s">&quot;good science!&quot;</span>

   <span class="nd">@Model.getter</span><span class="p">(</span><span class="s">&quot;en*&quot;</span><span class="p">)</span> <span class="c"># matches only remaining &#39;entropy&#39;</span>
   <span class="k">def</span> <span class="nf">getter2</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="k">assert</span> <span class="s">&quot;entropy&quot;</span> <span class="o">==</span> <span class="n">name</span>
     <span class="k">return</span> <span class="mi">0</span>

   <span class="nd">@Model.setter</span><span class="p">(</span><span class="s">&quot;*&quot;</span><span class="p">)</span> <span class="c"># matches &quot;energy&quot;, &quot;entropy&quot;, &quot;enology&quot;</span>
   <span class="k">def</span> <span class="nf">happiness</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="n">value</span><span class="p">):</span>
       <span class="k">print</span> <span class="s">&quot;setter for&quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span>
       <span class="o">...</span>
       <span class="k">return</span>


   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
</pre></div>
</div>
<p>However, this example is not legal:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;energy&quot;</span><span class="p">,</span> <span class="s">&quot;entropy&quot;</span><span class="p">,</span> <span class="s">&quot;enology&quot;</span><span class="p">)</span>

   <span class="nd">@Model.getter</span><span class="p">(</span><span class="s">&quot;en*&quot;</span><span class="p">)</span> <span class="c"># matches energy, entropy, and enology</span>
   <span class="k">def</span> <span class="nf">getter1</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="o">...</span>

   <span class="nd">@Model.getter</span><span class="p">(</span><span class="s">&quot;*&quot;</span><span class="p">)</span> <span class="c"># matches energy, entropy, and enology</span>
   <span class="k">def</span> <span class="nf">getter2</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="o">...</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
</pre></div>
</div>
<p>The example does not work as ambiguity is found when resolving
wilcards.</p>
</div>
<div class="section" id="use-of-naming-convention-for-defining-getters-and-or-setters">
<h3>Use of naming convention for defining getters and/or setters<a class="headerlink" href="#use-of-naming-convention-for-defining-getters-and-or-setters" title="Permalink to this headline"></a></h3>
<p>In some cases, the use of decorators for defining getters/setters
can be a limitation. For example, when the model is built
dynamically, like when generating proxy classes.</p>
<p>In these and other cases, the framework supports a <em>naming
convention</em> which can be used to define implicitly getters and/or
setters for logical OPs.</p>
<p>The naming convention applies to Model&#8217;s method names which are
implicitly declared as getters or setters.</p>
<ul class="simple">
<li><cite>get_&lt;prop_name&gt;_value(self)</cite>: A specific getter for OP <cite>&lt;prop_name&gt;</cite>.</li>
<li><cite>set_&lt;prop_name&gt;_value(self, value)</cite>: A specific setter for OP <cite>&lt;prop_name&gt;</cite>.</li>
<li><cite>get__value(self, name)</cite>: A generic getter receiving the name of
the property to be get.</li>
<li><cite>set__value(self, name, value)</cite>: A generic setter receiving the name of
the property to be set.</li>
</ul>
<p>As you see getters/setters can be either <em>specific</em> or
<em>generic</em>. In the former case, the getter/setter is specific for
one OP. In the latter case, getter/setter is general and will
receive the name of the property.</p>
<p>Generic getter/setter will not be called for OPs which have
specific getter/setter defined. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;energy&quot;</span><span class="p">,</span> <span class="s">&quot;entropy&quot;</span><span class="p">,</span> <span class="s">&quot;enology&quot;</span><span class="p">)</span>

   <span class="k">def</span> <span class="nf">get_energy_value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="mf">0.1</span>  <span class="c"># constantly need holidays!</span>

   <span class="c"># getter for entropy and enology only, as energy has a specific getter</span>
   <span class="k">def</span> <span class="nf">get__value</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="o">...</span>

   <span class="c"># setter for all properties</span>
   <span class="k">def</span> <span class="nf">set_value</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="n">value</span><span class="p">):</span> <span class="o">...</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
</pre></div>
</div>
<p>The first example we presented for decorators could be rewritten
as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c"># ----------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;Roberto&quot;</span>

   <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="s">&quot;energy&quot;</span><span class="p">,</span> <span class="s">&quot;entropy&quot;</span><span class="p">,</span> <span class="s">&quot;enology&quot;</span><span class="p">)</span>

   <span class="k">def</span> <span class="nf">get_energy_value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="mf">0.1</span>  <span class="c"># constantly need holidays!</span>

   <span class="k">def</span> <span class="nf">get_enology_value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="s">&quot;good science!&quot;</span>

   <span class="k">def</span> <span class="nf">get__value</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="k">assert</span> <span class="s">&quot;entropy&quot;</span> <span class="o">==</span> <span class="n">name</span>
     <span class="k">return</span> <span class="mi">0</span>

   <span class="k">def</span> <span class="nf">set__value</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="n">value</span><span class="p">):</span>
       <span class="k">print</span> <span class="s">&quot;setter for&quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span>
       <span class="o">...</span>
       <span class="k">return</span>

   <span class="k">pass</span> <span class="c"># end of class</span>
<span class="c"># ----------------------------</span>
</pre></div>
</div>
<p>Of course, since in naming conventions names <em>matters</em>, some names
in the example had to be adapted.</p>
</div>
</div>
<div class="section" id="types-of-observable-properties">
<span id="optypes"></span><h2>Types of Observable Properties<a class="headerlink" href="#types-of-observable-properties" title="Permalink to this headline"></a></h2>
<p>In section <a class="reference internal" href="obs.html#kobs"><em>Supported types of Observable Properties</em></a> we anticipated that there exist several types
of <em>OP*s. In the examples so far we have seen only *value</em> <em>OPs</em>,
meaning that observers will be notified of any change of <em>value</em>,
i.e. when the OPs are assigned to different values <a class="footnote-reference" href="#f-spurious" id="id1">[1]</a>.</p>
<p>What would happen if the value of
the property would be a complex object like a list, or a user-defined
class, and the object would change internally?</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>

<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">prop1</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
    <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;prop1&quot;</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">Model</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="o">...</span>
        <span class="k">return</span>
    <span class="k">pass</span> <span class="c"># end of class</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
<span class="n">m</span><span class="o">.</span><span class="n">prop1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="n">m</span><span class="o">.</span><span class="n">prop1</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span>
</pre></div>
</div>
<p>Last two lines of the previous example actually change the <em>OP</em>
internally, that is different from <em>assigning</em> a new value to the
property like in <tt class="docutils literal"><span class="pre">m.prop1</span> <span class="pre">=</span> <span class="pre">[5,4,3,2]</span></tt> that would trigger an
<em>assign</em> notifications like those seen in previous examples. Similar
problem is found when the property is assigned to a class instance,
and then a method that change the instance is called.</p>
<div class="section" id="supported-objects">
<h3>Supported objects<a class="headerlink" href="#supported-objects" title="Permalink to this headline"></a></h3>
<p>In this section only the model-side will be presented. In the section
dedicated to observers, we will see how changes occurring to this
objects are notified.</p>
<div class="section" id="mutable-containers">
<h4>Mutable containers<a class="headerlink" href="#mutable-containers" title="Permalink to this headline"></a></h4>
<p>The framework <em>MVC-O</em> provides a full support for python mutable
containers like <em>lists</em> and <em>maps</em>. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span><span class="p">,</span> <span class="n">Observer</span>

<span class="c"># ----------------------------------------------------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">myint</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">mylist</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">mymap</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;my*&quot;</span><span class="p">,</span> <span class="p">)</span>

    <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>This covers those cases where you have your <em>OPs</em> holding mutable
sequence values.</p>
</div>
<div class="section" id="class-instances">
<h4>Class Instances<a class="headerlink" href="#class-instances" title="Permalink to this headline"></a></h4>
<p>What if the value is a user-defined class instance?  Suppose a class
has a method changing the content instance. The idea is that observers
are notified when the <em>method</em> is called, with the possibility to
choose if being notified <em>before</em> or <em>after</em> the call.</p>
<p>However, how can the user declare that method <tt class="docutils literal"><span class="pre">M</span></tt> <em>does changes</em> the
instance? Two mechanism are provided by the framework:</p>
<ul class="simple">
<li>For already existing classes. In this cases the declaration occurs
when the instance is assigned to the <em>OP</em> in the model.</li>
<li>For ad-hoc and new classes. In this case the method will be
<em>declared</em> as <em>Observable</em> at the class level, through a
special <em>decorator</em> provided by the framework. This is the
preferable manner.</li>
</ul>
<p>Examples for new classes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span><span class="p">,</span> <span class="n">Observable</span>

<span class="c"># ----------------------------------------------------------------------</span>
<span class="k">class</span> <span class="nc">AdHocClass</span> <span class="p">(</span><span class="n">Observable</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">Observable</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">return</span>

    <span class="c"># this way the method is declared as &#39;observed&#39;:</span>
    <span class="nd">@Observable.observed</span>
    <span class="k">def</span> <span class="nf">change</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="c"># this is NOT observed (and it does not change the instance):</span>
    <span class="k">def</span> <span class="nf">is_val</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">==</span> <span class="n">val</span>
    <span class="k">pass</span> <span class="c">#end of class</span>

<span class="c"># ----------------------------------------------------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">obj</span> <span class="o">=</span> <span class="n">AdHocClass</span><span class="p">()</span>
    <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;obj&quot;</span><span class="p">,)</span>

    <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>As you can see, declaring a class as <em>observable</em> is as simple as
deriving from <tt class="docutils literal"><span class="pre">gtkmvc.Observable</span></tt> and decorating
those class methods that must be observed with the decorator
<tt class="docutils literal"><span class="pre">gtkmvc.Observable.observed</span></tt> (decorators are supported by
Python version 2.4 and later only).</p>
<p>However, sometimes we want to reuse existing classes and it is not
possible to derive them from <tt class="docutils literal"><span class="pre">gtkmvc.Observable</span></tt>. In this case
declaration of the methods to be observed can be done at time of
declaration of the corresponding <em>OP</em>. In this case the <em>value</em> to be
assigned to the <em>OP</em> must be a triple <tt class="docutils literal"><span class="pre">(class,</span> <span class="pre">instance,</span>
<span class="pre">method_names&gt;</span></tt>, where:</p>
<dl class="docutils">
<dt>class</dt>
<dd>Is the <tt class="docutils literal"><span class="pre">class</span></tt> of the object to be observed.</dd>
<dt>instance</dt>
<dd>Is the object to be observed.</dd>
<dt>method_names</dt>
<dd>Is a tuple of strings, representing the method
names of the instance to be observed.</dd>
</dl>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="c">#----------------------------------------------------------------------</span>
<span class="c"># This is a class the used cannot/don&#39;t want to change</span>
<span class="k">class</span> <span class="nc">HolyClass</span> <span class="p">(</span><span class="nb">object</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="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">def</span> <span class="nf">change</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">pass</span> <span class="c">#end of class</span>


<span class="c"># ----------------------------------------------------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">obj</span> <span class="o">=</span> <span class="p">(</span><span class="n">HolyClass</span><span class="p">,</span> <span class="n">HolyClass</span><span class="p">(),</span> <span class="p">(</span><span class="s">&#39;change&#39;</span><span class="p">,))</span>
    <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;obj&quot;</span><span class="p">,)</span>

    <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
</div>
<div class="section" id="signals">
<h4>Signals<a class="headerlink" href="#signals" title="Permalink to this headline"></a></h4>
<p>Finally, <em>OP</em> can hold special values that are <em>signals</em> that can be
used to notify observers that certain <em>events</em> occurred.</p>
<p>To declare an <em>OP</em> as a signal, the value of the <em>OP</em> must be an
instance of class <tt class="docutils literal"><span class="pre">gtkmvc.Signal</span></tt>. To notify an event,
the model can then invoke method <tt class="docutils literal"><span class="pre">emit</span></tt> of the <em>OP</em>. Emitting a
signal can carry an optional argument.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span><span class="p">,</span> <span class="n">Signal</span>

<span class="c"># ----------------------------------------------------------------------</span>
<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">sgn</span> <span class="o">=</span> <span class="n">Signal</span><span class="p">()</span>
    <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;sgn&quot;</span><span class="p">,)</span>

    <span class="k">pass</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
    <span class="n">m</span><span class="o">.</span><span class="n">sgn</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span> <span class="c"># we emit a signal</span>
    <span class="n">m</span><span class="o">.</span><span class="n">sgn</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="s">&quot;hello!&quot;</span><span class="p">)</span> <span class="c"># with argument</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>In the <tt class="docutils literal"><span class="pre">examples</span></tt>, there are several examples that show how
different types of <em>OPs</em> can be used. Of course all available types
can be used in all available kind of model classes, with or without
multi-threading support.</p>
</div>
</div>
<div class="section" id="class-vs-instance-members-as-ops">
<h3>Class vs Instance members as OPs<a class="headerlink" href="#class-vs-instance-members-as-ops" title="Permalink to this headline"></a></h3>
<p>So far in our examples, all OPs were class members:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">gtkmvc</span> <span class="kn">import</span> <span class="n">Model</span>

<span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">prop1</span> <span class="o">=</span> <span class="mi">10</span>
    <span class="n">prop2</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;prop?&quot;</span><span class="p">,)</span>
    <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>Using class vs instance attributes is not an issue when they are
assigned:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">m1</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
<span class="n">m2</span> <span class="o">=</span> <span class="n">MyModel</span><span class="p">()</span>
<span class="n">m1</span><span class="o">.</span><span class="n">prop1</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">m2</span><span class="o">.</span><span class="n">prop1</span> <span class="o">=</span> <span class="mi">15</span>
</pre></div>
</div>
<p>In this case after the assignment <cite>m1</cite> and <cite>m2</cite> will have their own
value for attribute <cite>prop1</cite>.</p>
<p>However, when dealing with attributes whose type is a class instances,
like for example a list, you must keep in mind the attribute sharing.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">m1</span><span class="o">.</span><span class="n">prop2</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">print</span> <span class="n">m2</span><span class="o">.</span><span class="n">prop2</span> <span class="c"># prints [1] !</span>
</pre></div>
</div>
<p>If attribute sharing is not what you want, simply assign OPs in the
model&#8217;s constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyModel</span> <span class="p">(</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">prop1</span> <span class="o">=</span> <span class="mi">10</span>
    <span class="n">prop2</span> <span class="o">=</span> <span class="p">[]</span> <span class="c"># may be any value actually</span>
    <span class="n">__observables__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;prop?&quot;</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">MyModel</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">prop2</span> <span class="o">=</span> <span class="p">[]</span>
      <span class="k">return</span>
    <span class="k">pass</span> <span class="c"># end of class</span>
</pre></div>
</div>
<p>Now <cite>m1.prop2</cite> and <cite>m2.prop2</cite> are different objects, and sharing no
longer occurs.</p>
<p class="rubric">Section Notes</p>
<table class="docutils footnote" frame="void" id="f-spurious" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Actually there exist <em>spurious</em> assign notifications,
which are issued also when there is no change in the
value of an OP, e.g. when an OP is assigned to
itself.</td></tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="error-and-warning-conditions-with-logical-ops">
<span id="opconstr"></span><h2>Error and Warning conditions with logical OPs<a class="headerlink" href="#error-and-warning-conditions-with-logical-ops" title="Permalink to this headline"></a></h2>
<p>When dealing with logical OPs and in particular with getter/setter
pairs, there are complex conditions and behaviours of the framework
which may be confusing. Here those conditions are listed and
explained.</p>
<div class="section" id="error-conditions">
<h3>Error conditions<a class="headerlink" href="#error-conditions" title="Permalink to this headline"></a></h3>
<ul>
<li><p class="first"><strong>A logical OP has no associated getter.</strong></p>
<p>Getters are mandatory for logical OPs.</p>
</li>
<li><p class="first"><strong>OP matched by multiple getters/setters.</strong></p>
<p>Each OP must be matched exactly with one getter and optionally with
one setter. It is an error condition any multiple matching.</p>
</li>
<li><p class="first"><strong>Exactly the same pattern was used for one or more getters (setters).</strong></p>
<p>This is a specialization of previous case (multiple matching).</p>
</li>
</ul>
</div>
<div class="section" id="warning-conditions">
<h3>Warning conditions<a class="headerlink" href="#warning-conditions" title="Permalink to this headline"></a></h3>
<p>Warning are issued only if the devlopers enables them. In released
applications, warnings should be not visible by default, to avoid
worrying the application user.</p>
<p>When developing, it is important to enable warnings, though.</p>
<ul>
<li><p class="first"><strong>A setter has no corresponing getter.</strong></p>
<p>When a setter has no getter, the setter is simply ignored.</p>
</li>
<li><p class="first"><strong>Getter/setter defined for concrete OPs.</strong></p>
<p>When a getter and/or a setter is defined for a concrete OP (not a
<em>logical</em> OP), the getter/setter are ignored.</p>
</li>
<li><p class="first"><strong>Getter/setter defined for non-existing logical OP.</strong></p>
<p>Getters/setters whose pattern do not match any existing logical OP,
are ignored.</p>
</li>
</ul>
</div>
</div>
<div class="section" id="special-members-for-observable-properties">
<h2>Special members for Observable Properties<a class="headerlink" href="#special-members-for-observable-properties" title="Permalink to this headline"></a></h2>
<p>Classes derived from Model, that exports <em>OPs</em>, have several special
members. Advanced users might be interested in overriding some of them,
but in general they should be considered as private members. They are
explained here for the sake of completeness.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">__observables__</span></tt></dt>
<dd>A class (static) member that lists property
names. This must be provided as either a tuple or a list by the
user. Wilcards in names can be used to match property names, but
properties with names starting with a double underscore
<tt class="docutils literal"><span class="pre">__</span></tt> will be not matched.</dd>
<dt><tt class="docutils literal"><span class="pre">__properties__</span></tt></dt>
<dd>(Deprecated, do not use anymore) A dictionary mapping
observable properties names and their initial value. This
variable has been substituted by __observables__.</dd>
<dt><tt class="docutils literal"><span class="pre">__derived_properties__</span></tt></dt>
<dd>(Deprecated) Automatically generated static member
that maps the <em>OPs</em> exported by all base classes. This does not
contain <em>OPs</em> that the class overrides.</dd>
<dt><tt class="docutils literal"><span class="pre">_prop_&lt;property_name&gt;</span></tt></dt>
<dd>This is an
auto-generated variable holding the property value. For example,
a property called <tt class="docutils literal"><span class="pre">x</span></tt> will generate a variable called
<tt class="docutils literal"><span class="pre">_prop_x</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">get_prop_&lt;property_name&gt;</span></tt></dt>
<dd>This public method
is the getter for the property. It is automatically generated only
if the user does not define one. This means that the user can change
the behavior of it by defining their own method.  For example, for
property <tt class="docutils literal"><span class="pre">x</span></tt> the method is <tt class="docutils literal"><span class="pre">get_prop_x</span></tt>.  This
method gets only self and returns the corresponding property value.</dd>
<dt><tt class="docutils literal"><span class="pre">set_prop_&lt;property_name&gt;</span></tt></dt>
<dd>This public method
is customizable like
<tt class="docutils literal"><span class="pre">get_prop_&lt;property_name&gt;</span></tt>.  This does not return
anything, and gets self and the value to be assigned to the
property. The default auto-generated code also calls method
<tt class="docutils literal"><span class="pre">gtkmvc.Model.notify_property_change</span></tt> to notify the
change to all registered observers.</dd>
</dl>
<p>For further details about this topic see meta-classes <tt class="docutils literal"><span class="pre">PropertyMeta</span></tt>
and <tt class="docutils literal"><span class="pre">ObservablePropertyMeta</span></tt> from package <tt class="docutils literal"><span class="pre">support</span></tt>.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Observable Properties</a><ul>
<li><a class="reference internal" href="#concrete-observable-properties">Concrete Observable Properties</a><ul>
<li><a class="reference internal" href="#concrete-op-and-inheritance">Concrete OP and inheritance</a></li>
</ul>
</li>
<li><a class="reference internal" href="#logical-observable-properties">Logical Observable Properties</a><ul>
<li><a class="reference internal" href="#use-of-decorators-for-defining-getters-and-or-setters">Use of decorators for defining getters and/or setters</a></li>
<li><a class="reference internal" href="#use-of-naming-convention-for-defining-getters-and-or-setters">Use of naming convention for defining getters and/or setters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#types-of-observable-properties">Types of Observable Properties</a><ul>
<li><a class="reference internal" href="#supported-objects">Supported objects</a><ul>
<li><a class="reference internal" href="#mutable-containers">Mutable containers</a></li>
<li><a class="reference internal" href="#class-instances">Class Instances</a></li>
<li><a class="reference internal" href="#signals">Signals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#class-vs-instance-members-as-ops">Class vs Instance members as OPs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#error-and-warning-conditions-with-logical-ops">Error and Warning conditions with logical OPs</a><ul>
<li><a class="reference internal" href="#error-conditions">Error conditions</a></li>
<li><a class="reference internal" href="#warning-conditions">Warning conditions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#special-members-for-observable-properties">Special members for Observable Properties</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="impl.html"
                        title="previous chapter">Details of implementation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="observers.html"
                        title="next chapter">Observers</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/op.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="observers.html" title="Observers"
             >next</a> |</li>
        <li class="right" >
          <a href="impl.html" title="Details of implementation"
             >previous</a> |</li>
        <li><a href="index.html">Model-View-Controller and Observer patterns for PyGTK 1.99.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, Roberto Cavada.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>