''' This file demonstrates data aggregation security problems. The Color class represents colors. The Person class represents a person. ''' import random class Color: def __init__(self, color): self.set_color(color) def set_color(self, color): if isinstance(color, Color): self.__color = color.get_color() color = color.lower() if color in ("amber", "auburn", "black", "blond", "blue", "brown", "chestnut", "cyan", "gray", "green", "grey", "hazel", "magenta", "orange", "pink", "purple", "red", "violet", "white", "yellow"): self.__color = color else: raise ValueError('invalid color') def get_color(self): return self.__color class Person: def __init__(self, name, age, hair_color, eye_color): self.__name = name self.__age = age self.set_hair_color(hair_color) ''' if isinstance(hair_color, Color): self.__hair_color = Color(hair_color) else: self.__hair_color = Color('white') #just set to default color self.set_hair_color(hair_color) ''' self.__set_eye_color(eye_color) ''' if isinstance(eye_color, Color): self.__eye_color = Color(eye_color) else: self.__eye_color = Color('white') #just set to default color self.__set_eye_color(eye_color) ''' def get_name(self): return self.__name def get_age(self): return self.__age def increment_age(self): self.__age += 1 def set_hair_color(self, color): if not isinstance(color, (Color, str)): raise TypeError('color must be a valid color') self.__hair_color = Color(color) def get_hair_color(self): return Color(self.__hair_color) def __set_eye_color(self, color): if not isinstance(color, (Color, str)): raise TypeError('color must be a valid color') self.__eye_color = Color(color) def get_eye_color(self): return Color(self.__eye_color) def reproduce(self, mate, child_name): #pick the child's hair color -- 50% chance of being mom's and 50% chance of being dad's #hcolor = Color(random.choice((self.__hair_color, mate.__hair_color))) hcolor = random.choice(self.get_hair_color(), mate.get_hair_color()) #pick the child's eye color -- 50% chance of being mom's and 50% chance of being dad's ecolor = Color(random.choice((self.__eye_color, mate.__eye_color))) return Person(child_name, 0, hcolor, ecolor) def print_person(person): hcolor = person.get_hair_color() ecolor = person.get_eye_color() print(person.get_name()+": "+hcolor.get_color()+" hair and "+ecolor.get_color()+" eyes") alice = Person('Alice', 35, "black", "brown") bob = Person('Bob', 33, "brown", "green") carol = alice.reproduce(bob, 'Carol') print_person(alice) print_person(bob) print_person(carol) print() print("Changing Carol's hair to red.") print() carol.set_hair_color('red') print_person(alice) print_person(bob) print_person(carol)