import sys
###########################################################################
###########################################################################
class Protector(object):
"""Object capable de proteger des variables"""
###################################################################
def __init__(self):
###################################################################
# On initialise la liste des variables protegees
object.__setattr__(self, 'protected', ['protected'])
# On cree un attribut protege
self.toto = "Hello guys"
self.protect("toto")
###################################################################
def test(self):
"""Test d'acces a un attribut protege"""
###################################################################
self.toto = "Goodbye guys"
print self.toto
###################################################################
def protect(self, name):
"""Proteger un attribut"""
###################################################################
if not (name in object.__getattribute__(self, 'protected')):
object.__getattribute__(self, 'protected').append(name)
###################################################################
def unprotect(self, name):
"""De-proteger un attribut"""
###################################################################
if name != 'protected' and name in object.__getattribute__(self, 'protected'):
object.__getattribute__(self, 'protected').remove(name)
###################################################################
def read(self, name):
"""Lire un attribut protege"""
###################################################################
return object.__getattribute__(self, name)
###################################################################
def write(self, name, value):
"""Definir la valeur d'un attribut protege"""
###################################################################
object.__setattr__(self, name, value)
###################################################################
def __getattribute__(self, name):
# On surdefinit l'accesseur standard a un attribut (lecture)
###################################################################
if (sys._getframe(1).f_locals.has_key('self') and isinstance(sys._getframe(1).f_locals['self'], object.__getattribute__(self, '__class__'))) or not (name in object.__getattribute__(self, 'protected')):
return object.__getattribute__(self, name)
else:
return None
###################################################################
def __setattr__(self, name, value):
# On surdefinit l'accesseur standard a un attribut (ecriture)
###################################################################
if (sys._getframe(1).f_locals.has_key('self') and isinstance(sys._getframe(1).f_locals['self'], object.__getattribute__(self, '__class__'))) or not (name in object.__getattribute__(self, 'protected')):
object.__setattr__(self, name, value)
###################################################################
def __delattr__(self, name):
# On surdefinit l'accesseur standard a un attribut (suppression)
###################################################################
if (sys._getframe(1).f_locals.has_key('self') and isinstance(sys._getframe(1).f_locals['self'], object.__getattribute__(self, '__class__'))) or not (name in object.__getattribute__(self, 'protected')):
object.__delattr__(self, name)
###########################################################################
###########################################################################
class A(object):
"""Cette classe permet de verifier que l'on ne peut pas atteindre
un attribut protege a partir d'une autre classe"""
def __init__(self):
self.p = Protector()
self.p.a = "Hello world"
self.p.protect("a")
def test(self):
print self.p.a
print self.p.read("a")
###########################################################################
###########################################################################
class B(Protector):
"""Cette classe permet de verifier que l'on peut bien acceder a un
attribut protege a partir de la classe elle-meme"""
def __init__(self):
Protector.__init__(self)
self.a = "Hello world"
self.protect("a")
def test(self):
print self.a
print self.toto
###########################################################################
###########################################################################
class C(B):
"""Cette classe permet de verifier que l'on peut bien acceder a un
attribut protege a partir d'une classe fille"""
def __init__(self):
B.__init__(self)
def test(self):
print self.a
print self.toto
###########################################################################
###########################################################################
class D(Protector):
"""Cette classe permet de verifier que l'on peut bien acceder a un
attribut protege a partir d'une classe de meme type"""
def __init__(self):
Protector.__init__(self)
def test(self, other):
print "Son toto : " + other.toto
###########################################################################
###########################################################################
if __name__ == "__main__":
# On test si un attribut est bien protege de l'exterieur
print ">> p = Protector()"
p = Protector()
print ">> p.pi = 3.14159"
p.pi = 3.14159
print ">> print p.pi"
print p.pi
print ">> p.protect('pi')"
p.protect("pi")
print ">> p.pi = 3.15"
p.pi = 3.15
print ">> print p.read('pi')"
print p.read('pi')
print ">> print p.pi"
print p.pi
print ">> p.write('pi', 3.15)"
p.write('pi', 3.15)
print ">> print p.read('pi')"
print p.read('pi')
print ">> p.test()"
p.test()
# On test si un attribut est bien accessible de l'interieur
print ">> a = A()"
a = A()
print ">> a.test()"
a.test()
# On test si un attribut est bien protege d'une autre classe de type different
print ">> b = B()"
b = B()
print ">> b.test()"
b.test()
print ">> print b.toto"
print b.toto
# On test si un attribut est bien accessible d'un objet derive
print ">> c = C()"
c = C()
print ">> c.test()"
c.test()
print ">> print c.toto"
print c.toto
# On test si est attribut est bien accessible depuis une autre classe de meme type
# On test si un attribut est bien accessible d'un objet derive
print ">> d1 = D()"
d1 = D()
print ">> d2 = D()"
d2 = D()
print ">> d2.write('toto', 'd2 say hello')"
d2.write('toto', 'd2 say hello')
print ">> d1.test(d1)"
d1.test(d1)
print ">> d1.test(d2)"
d1.test(d2)
syntax highlighted by Code2HTML, v. 0.9.1