@@ -41,47 +41,47 @@ class BastionClass:
4141 """
4242
4343 def __init__ (self , get , name ):
44- """Constructor.
44+ """Constructor.
4545
46- Arguments:
46+ Arguments:
4747
48- get - a function that gets the attribute value (by name)
49- name - a human-readable name for the original object
50- (suggestion: use repr(object))
48+ get - a function that gets the attribute value (by name)
49+ name - a human-readable name for the original object
50+ (suggestion: use repr(object))
5151
52- """
53- self ._get_ = get
54- self ._name_ = name
52+ """
53+ self ._get_ = get
54+ self ._name_ = name
5555
5656 def __repr__ (self ):
57- """Return a representation string.
57+ """Return a representation string.
5858
59- This includes the name passed in to the constructor, so that
60- if you print the bastion during debugging, at least you have
61- some idea of what it is.
59+ This includes the name passed in to the constructor, so that
60+ if you print the bastion during debugging, at least you have
61+ some idea of what it is.
6262
63- """
64- return "<Bastion for %s>" % self ._name_
63+ """
64+ return "<Bastion for %s>" % self ._name_
6565
6666 def __getattr__ (self , name ):
67- """Get an as-yet undefined attribute value.
67+ """Get an as-yet undefined attribute value.
6868
69- This calls the get() function that was passed to the
70- constructor. The result is stored as an instance variable so
71- that the next time the same attribute is requested,
72- __getattr__() won't be invoked.
69+ This calls the get() function that was passed to the
70+ constructor. The result is stored as an instance variable so
71+ that the next time the same attribute is requested,
72+ __getattr__() won't be invoked.
7373
74- If the get() function raises an exception, this is simply
75- passed on -- exceptions are not cached.
74+ If the get() function raises an exception, this is simply
75+ passed on -- exceptions are not cached.
7676
77- """
78- attribute = self ._get_ (name )
79- self .__dict__ [name ] = attribute
80- return attribute
77+ """
78+ attribute = self ._get_ (name )
79+ self .__dict__ [name ] = attribute
80+ return attribute
8181
8282
8383def Bastion (object , filter = lambda name : name [:1 ] != '_' ,
84- name = None , bastionclass = BastionClass ):
84+ name = None , bastionclass = BastionClass ):
8585 """Create a bastion for an object, using an optional filter.
8686
8787 See the Bastion module's documentation for background.
@@ -109,57 +109,57 @@ def Bastion(object, filter = lambda name: name[:1] != '_',
109109 # the user has full access to all instance variables!
110110
111111 def get1 (name , object = object , filter = filter ):
112- """Internal function for Bastion(). See source comments."""
113- if filter (name ):
114- attribute = getattr (object , name )
115- if type (attribute ) == MethodType :
116- return attribute
117- raise AttributeError , name
112+ """Internal function for Bastion(). See source comments."""
113+ if filter (name ):
114+ attribute = getattr (object , name )
115+ if type (attribute ) == MethodType :
116+ return attribute
117+ raise AttributeError , name
118118
119119 def get2 (name , get1 = get1 ):
120- """Internal function for Bastion(). See source comments."""
121- return get1 (name )
120+ """Internal function for Bastion(). See source comments."""
121+ return get1 (name )
122122
123123 if name is None :
124- name = `object`
124+ name = `object`
125125 return bastionclass (get2 , name )
126126
127127
128128def _test ():
129129 """Test the Bastion() function."""
130130 class Original :
131- def __init__ (self ):
132- self .sum = 0
133- def add (self , n ):
134- self ._add (n )
135- def _add (self , n ):
136- self .sum = self .sum + n
137- def total (self ):
138- return self .sum
131+ def __init__ (self ):
132+ self .sum = 0
133+ def add (self , n ):
134+ self ._add (n )
135+ def _add (self , n ):
136+ self .sum = self .sum + n
137+ def total (self ):
138+ return self .sum
139139 o = Original ()
140140 b = Bastion (o )
141141 testcode = """if 1:
142142 b.add(81)
143143 b.add(18)
144144 print "b.total() =", b.total()
145145 try:
146- print "b.sum =", b.sum,
146+ print "b.sum =", b.sum,
147147 except:
148- print "inaccessible"
148+ print "inaccessible"
149149 else:
150- print "accessible"
150+ print "accessible"
151151 try:
152- print "b._add =", b._add,
152+ print "b._add =", b._add,
153153 except:
154- print "inaccessible"
154+ print "inaccessible"
155155 else:
156- print "accessible"
156+ print "accessible"
157157 try:
158- print "b._get_.func_defaults =", b._get_.func_defaults,
158+ print "b._get_.func_defaults =", b._get_.func_defaults,
159159 except:
160- print "inaccessible"
160+ print "inaccessible"
161161 else:
162- print "accessible"
162+ print "accessible"
163163 \n """
164164 exec testcode
165165 print '=' * 20 , "Using rexec:" , '=' * 20
0 commit comments