Get attributes and filter out methods a private attributes
May 1, 2008 – 3:59 pmby Jake D
Example (a big one) of usage with an object
Here is an object that I put in:
class Widget(object):
"""The Widget represents an object of work. it can be processed by a worker. it is assigned to a worker by an organization."""
def __init__(self, organization_object = False, worker_object=False, id=-1, type=False, steps_until_due=-1, p_success_lie=1):
self.__id=id
self.__worker = worker_object #an object of type Worker
self.__organization = organization_object #the organization this widget belongs to
self.__type = type #integer or string; what type of Widget is this? used by the organization and worker
self.__p_success_lie = p_success_lie #p(success) that A lie will work on this widget
self.__complete = False #a bool: is this Widget complete?
self.__honestly_done = True #a bool; if this Widget is complete, was it completed honestly/to spec? Assumed True at start
self.__inspected = False #has not yet been inspected
self.__time_spoiling = 0 #this widget has not yet begun to spoil
self.__time_consumed = 0 #this widget has not yet begun to be consumed
self.public=35
### MANAGED ATTRIBUTES ###
#id
def id():
"""id is readonly"""""
def fget(self):
return self.__id
return locals()
id = property(**id())
#END id
#worker
def worker():
"""worker is readonly"""""
def fget(self):
return self.__worker
return locals()
worker = property(**worker())
#END worker
#organization
def organization():
"""organization is readonly"""""
def fget(self):
return self.__organization
return locals()
organization = property(**organization())
#END organization
#type
def type():
"""type is readonly"""""
def fget(self):
return self.__type
return locals()
type = property(**type())
#END type
#p_success_lie
def p_success_lie():
"""p_success_lie is readonly"""""
def fget(self):
return self.__p_success_lie
return locals()
p_success_lie = property(**p_success_lie())
#END p_success_lie
#complete
def complete():
"""complete is readonly"""""
def fget(self):
return self.__complete
return locals()
complete = property(**complete())
#END complete
#honestly_done
def honestly_done():
"""honestly_done is readonly"""""
def fget(self):
return self.__honestly_done
return locals()
honestly_done = property(**honestly_done())
#END honestly_done
#inspected
def inspected():
"""inspected is readonly"""""
def fget(self):
return self.__inspected
return locals()
inspected = property(**inspected())
#END inspected
#time_spoiling
def time_spoiling():
"""time_spoiling is readonly"""""
def fget(self):
return self.__time_spoiling
return locals()
time_spoiling = property(**time_spoiling())
#END time_spoiling
#time_consumed
def time_consumed():
"""time_consumed is readonly"""""
def fget(self):
return self.__time_consumed
return locals()
time_consumed = property(**time_consumed())
#END time_consumed
### END MANAGED ATTRIBUTES ###
def set_id(self, new_id):
self.__id=new_id
#check
if self.id == new_id:
return True
else:
return False
def set_id(self, new_id):
self.__id=new_id
#check
if self.id == new_id:
return True
else:
return False
def attempt_to_deceieve(self):
"""This is the only way to say that a Widget was not done honestly"""
pass
def honest_attempt(self):
"""This is the only way to say that a Widget was completed"""
pass
def spoil_step(self):
"""Increases the steps that this widget has been spoiling"""
old = self.__time_spoiling
self.__time_spoiling += 1
#check for sure
if old + 1 == self.__time_spoiling:
return self.__time_spoiling
else:
return False
def consume_step(self):
"""Increases the steps that this widget has been consumed"""
old = self.__time_consumed
self.__time_consumed += 1
#check for sure
if old + 1 == self.__time_consumed:
return self.__time_consumed
else:
return False
####END Widget OBJECT ####
## Widget Object testing ###
#attribute testing
def attribute_test():
import tester
w = Widget()
#print w.__class__
tester.attributes(w)
attribute_test()
#method testing
def method_test():
w = Widget()
import tester
tester.methods(w)
method_test()
Inserting a widget object above into the attribute and method getting functions yielded the following output (usage is at the bottom of the class definition):
++++ Starting attributes checking ++++
object: <__main__ .Widget object at 0x76fd0>
Results from attempt to read a value:
complete: (Ok) False
honestly_done: (Ok) True
id: (Ok) -1
inspected: (Ok) False
organization: (Ok) False
p_success_lie: (Ok) 1
public: (Ok) 35
time_consumed: (Ok) 0
time_spoiling: (Ok) 0
type: (Ok) False
worker: (Ok) False
===== ATTRIBUTE SUMMARY FOR OBJECT OF TYPE ============
Total public attributes: 11
11 are readable ['complete', 'honestly_done', 'id', 'inspected', 'organization', 'p_success_lie', 'public', 'time_consumed', 'time_spoiling', 'type', 'worker']
1 are writable ['public']
1 are deleteable ['public']
Total private attributes: 10 ['_Widget__complete', '_Widget__honestly_done', '_Widget__id', '_Widget__inspected', '_Widget__organization', '_Widget__p_success_lie', '_Widget__time_consumed', '_Widget__time_spoiling', '_Widget__type', '_Widget__worker']
Total system attributes: 14 ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
++++ Starting methods checking ++++
object: <__main__ .Widget object at 0x76fd0>
===== Method SUMMARY FOR OBJECT OF TYPE ============
Total public methods: 5 ['attempt_to_deceieve', 'consume_step', 'honest_attempt', 'set_id', 'spoil_step']
Total private methods: 0 []
Total system methods: 1 ['__init__']
Conclusion
I havn’t done the most thorough testing on this, so if you find a bug, please let me know. Also, as I said I am somewhat new to python, so if there is a better way please let me and other readers know!



