class widget:
''' parent class'''
count = 0
instances = {}
def __init__(self, name):
self.name = name.upper()
self.count = self.__class__.count
self.__class__.count += 1
if self.name not in self.__class__.instances:
self.__class__.instances[self.name] = self
def put(self, key, value):
setattr(self, key, value)
def get(self, key):
if hasattr(self, key):
return (self.__getattribute__(key))
def __repr__(self):
''' Print out a representation that evaluates to this widget.'''
return f'{self.__class__.__name__}({self.name!r})'
def __str__(self):
''' Return string version of the widget that includes its name and count. '''
return f"<{self.__class__.__name__} ({self.count}): {self.name}>"
a = widget("a")
b = widget("b")
a
widget('A')
a.put('age',33)
a.get('age')
33
a.age
33
getattr(a,'age')
33
print (a)
<widget (0): A>
print (b)
<widget (1): B>
widget.instances
{'A': widget('A'), 'B': widget('B')}
a.count
0
widget.count
2
a.__class__
__main__.widget
class issue(widget):
''' Class for issues '''
# keep track of each instance of an issue.
count = 0 # how many issues have we created?
instances = {} # store the issues in a dictionary, aka, a hash table.
def __init__(self, name):
''' This is the constructor for an issue. It is invoked with the class name and
the name of the issue, e.g., issue("abortion")
We use the Python string method upper() to convert the name to upper case.
If the issue is already in the dictionary, we ignore this instance.
Otherwise, we add it to the dictionary.
We assign a sequential count to the instance and increment the class count.
We stick the new issue in dictionary.'''
widget.__init__(self, name)
def __eq__(self, other):
''' Overload == operator. Two issues must match issue name. '''
return self.name == other.name
i = issue("abortion")
j = issue('gun control')
k = issue('free speech')
issue.count
3
widget.count
2
issue.instances
{'ABORTION': issue('ABORTION'), 'GUN CONTROL': issue('GUN CONTROL'), 'FREE SPEECH': issue('FREE SPEECH')}
i.__class__
__main__.issue
widget.instances
{'A': widget('A'), 'B': widget('B')}
i.put("color", "red")
i.get("color")
'red'
getattr(i, 'color')
'red'
i
issue('ABORTION')
print (i)
<issue (0): ABORTION>
print (j)
<issue (1): GUN CONTROL>
print (k)
<issue (2): FREE SPEECH>
i.get('taste')
i.__getattribute__('color')
'red'
dir(issue)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'count', 'get', 'instances', 'put']
issue.__class__.__bases__
(object,)
class stance:
''' Class for importance and side on a given issue.'''
count = 0
stances = []
def __init__(self, issuename, side='pro', importance='A'):
''' Constructor for stance(). If the issuename is not already an issue,
create a new issue '''
if not issuename.upper() in issue.issues:
issue(issuename)
self.issue = issue.issues[issuename.upper()]
self.side = side.upper()
self.importance = importance.upper()
self.count = stance.count
stance.count += 1
stance.stances.append(self)
def __repr__(self):
''' Print out code that evaluates to this stance.'''
return f'stance({self.issue.name!r}, {self.side!r}, {self.importance!r})'
def __str__(self):
''' Return string version of self '''
return f"<stance ({self.count}): {self.issue.name} [{self.side}:{self.importance}]>"
def __eq__(self, other):
''' Overload == operator. Two stances must match issue and side,
though not importance. '''
return self.issue == other.issue and self.side == other.side
def copy(self):
''' Clone a stance. New stance has same issue, side, and importance. '''
return stance(self.issue.name, self.side, self.importance)
def __hash__(self):
''' hash() function for stance.
Need this for set() to remove duplicates.
Note: do not need to include importance. Match is on issue and side only. '''
return hash((self.issue.name, self.side))
def __lt__(self, other):
''' Comparison operator < to allow sorting stances. '''
return self.issue.name + self.side < other.issue.name + other.side