-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_attr.py
93 lines (82 loc) · 2.86 KB
/
class_attr.py
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
#s.dobrev 2k4-5
'various descriptors for class-level-attributes'
class Descriptor4AutoCreatedReadonlyAttr( object):
""" switchable attribute, like classmethod but on attributes:
class A:
class X_klas:
...
x = Descriptor4AutoCreatedReadonlyAttr( X_klas)
a = A()
A.x gets X_klas
a.x gets a._props.x, which is (lazy) autosetup as X_klas()
"""
__slots__ = ( 'factory', 'name' )
def __init__( me, factory, name):
me.factory = factory
me.name = name
def __get__( me, obj, klas ):
#print 'getattr', me.factory, klas
#try:
if obj is None: return me.factory
_props = obj._props #=me if same for all instances
name = me.name
try:
me_obj = getattr( _props, name)
except AttributeError:
me_obj = me.factory()
setattr( _props, name, me_obj)
return me_obj
#except:
# traceback.print_exc()
class Descriptor4AutoCreatedAttr( Descriptor4AutoCreatedReadonlyAttr):
def __set__( me, obj, value):
#print 'setattr', me.factory, value
_props = obj._props #=me if same for all instances
return setattr( _props, me.name, value)
#######
class Descriptor4AutoCreatedReadonlySharedAttr( object):
""" switchable attribute, like classmethod but on attributes:
class A:
class X_klas:
...
x = Descriptor4AutoCreatedReadonlySharedAttr( X_klas)
a = A()
A.x gets X_klas
a.x gets A.x.obj, which is (lazy) autosetup as X_klas() -
same/shared for all instances of A
"""
__slots__ = ( 'factory', 'obj' )
def __init__( me, factory, name):
me.factory = factory
def __get__( me, obj, klas ):
#print 'getattr', me.factory.__name__, klas
if obj is None: return me.factory
try:
me_obj = me.obj
except AttributeError:
me_obj = me.obj = me.factory()
return me_obj
class Descriptor4AutoCreatedSharedAttr( Descriptor4AutoCreatedReadonlySharedAttr):
def __set__( me, obj, value):
#print 'setattr', me.factory, value
me.obj = value
#######
class Descriptor4ClassOnlyAttr( object):
""" instance-inaccessible attribute:
class A:
class X:
...
X= Descriptor4ClassOnlyAttr( X)
a = A()
A.X gets X
a.X gets AttributeError
"""
__slots__ = ( 'factory', 'name' )
def __init__( me, factory, name):
me.factory = factory
me.name = name
def __get__( me, obj, klas ):
#print 'getattr', me.factory, klas
if obj is None: return me.factory
raise AttributeError, 'class-only attribute %s is instance-inaccessible' % me.name
# vim:ts=4:sw=4:expandtab