~openerp-community/openobject-server/training

« back to all changes in this revision

Viewing changes to presentation.en/examples/property_01.py

  • Committer: Fabien Pinckaers
  • Date: 2009-08-04 16:14:19 UTC
  • Revision ID: fp@tinyerp.com-20090804161419-vwan9tdtjheeua87
add

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# examples/property_01.py
 
4
 
 
5
def make_spamm():
 
6
    return "Spamm!"
 
7
 
 
8
def is_valid(value):
 
9
    if "spamm" not in value.lower():
 
10
        return False
 
11
    return True
 
12
 
 
13
class Foo(object):
 
14
    def _get_spamm(self):
 
15
        return make_spamm()
 
16
    def _set_spamm(self, value):
 
17
        if is_valid(value):
 
18
            self.__dict__['spamm'] = value
 
19
        else:
 
20
            raise ValueError('I want my spamm!')
 
21
    spamm = property(_get_spamm, _set_spamm, None, "Tasty spamm")
 
22
 
 
23
 
 
24
if __name__ == '__main__':
 
25
    import traceback as tb
 
26
 
 
27
    f = Foo()
 
28
    print f.spamm
 
29
    try:
 
30
        f.spamm = "Canned spamm"
 
31
        f.spamm = "Eggs"
 
32
    except ValueError, exc:
 
33
        tb.print_exc()
 
34
    try:
 
35
        del f.spamm
 
36
    except AttributeError, exc:
 
37
        tb.print_exc()