Module base
[hide private]
[frames] | no frames]

Source Code for Module base

  1  #!/usr/bin/python3 
  2  """ This module implements the base class for almost everything""" 
  3   
  4   
5 -class Base(object):
6 """ 7 This is the base class for all other classes 8 9 @ivar name: name of this object 10 @ivar description: one line this description 11 @ivar attributes: dict of <name,value> pairs" 12 """ 13
14 - def __init__(self, name, descr=None):
15 """ 16 create a new object 17 @param name: display name of this object 18 @param descr: human description of this object 19 """ 20 self.name = name 21 self.description = descr 22 self.attributes = {}
23
24 - def __str__(self):
25 """ 26 return a descriptive string for this object 27 """ 28 if self.description is None: 29 return self.name 30 return "{}({})".format(self.name, self.description)
31
32 - def get(self, attribute):
33 """ 34 return: value of an attribute 35 36 @param attribute: name of attribute to be fetched 37 @return: (string) value (or none) 38 """ 39 if attribute in self.attributes: 40 return self.attributes[attribute] 41 return None
42
43 - def set(self, attribute, value):
44 """ 45 set the value of an attribute 46 47 @param attribute: name of attribute to be fetched 48 @param value: value to be stored for that attribute 49 """ 50 self.attributes[attribute] = value
51 52 53 # UNIT TESTING
54 -def main():
55 """ 56 basic test cases for name, description, and attributes 57 """ 58 59 describe = "simple get/set test object" 60 go1 = Base("Base Object 1", describe) 61 go2 = Base("Base Object 2") 62 63 tried = 0 64 passed = 0 65 66 # new object has name, description, and nothing else 67 print("Created 'Base 1', descr={}\n got '{}'" 68 .format(describe, str(go1))) 69 tried += 2 70 assert (go1.name == "Base Object 1"), \ 71 "New object does not have assigned name" 72 assert (go1.description == describe), \ 73 "New object does not have assigned description" 74 passed += 2 75 76 # a new set correctly adds a value 77 print(" before set(): get('attribute#1') -> {}" 78 .format(go1.get("attribute#1"))) 79 tried += 2 80 assert (go1.get("attribute#1") is None), \ 81 "New object has attribute values before set" 82 go1.set("attribute#1", "value1") 83 print(" after set('attribute#1', 'value1'): get('attribute#1') -> '{}'" 84 .format(go1.get("attribute#1"))) 85 assert (go1.get("attribute#1") == "value1"), \ 86 "set does not correctly set new value" 87 passed += 2 88 89 # a second set correctly changes a value 90 go1.set("attribute#1", "value2") 91 print(" after set('attribute#1', 'value2'): get('attribute#1') -> '{}'" 92 .format(go1.get("attribute#1"))) 93 tried += 1 94 assert (go1.get("attribute#1") == "value2"), \ 95 "set does not correctly change value" 96 passed += 1 97 98 # description defaults to None 99 print("Created 'Base Object 2, descr=None\n got '{}'" 100 .format(str(go2))) 101 tried += 2 102 assert (go2.name == "Base Object 2"), \ 103 "New object does not have assigned name" 104 assert (go2.description is None), \ 105 "New description does not default to None" 106 passed += 2 107 108 print() 109 if tried == passed: 110 print("Passed all {} Base tests".format(passed)) 111 else: 112 print("FAILED {}/{} Base tests".format(tried-passed, tried))
113 114 115 if __name__ == "__main__": 116 main() 117