Last active
August 29, 2015 14:16
-
-
Save Spacecow99/124c907af245fdedfd53 to your computer and use it in GitHub Desktop.
Who needs to count sheep to fall asleep when we have the power of OOP to count for us!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import fractions | |
| import copy | |
| class Apple(object): | |
| """A simple apple class""" | |
| def __init__(self, color, name, volume): | |
| self._color = color | |
| self._name = name | |
| self._volume = int(volume) | |
| def __lt__(self, arg): | |
| return self._volume < arg.volume | |
| def __le__(self, arg): | |
| return self._volume <= arg.volume | |
| def __gt__(self, arg): | |
| return self._volume > arg.volume | |
| def __ge__(self, arg): | |
| return self._volume >= arg.volume | |
| def __eq__(self, arg): | |
| return (self._color == arg.color and | |
| self._name == arg.name and | |
| self._volume == arg.volume | |
| ) | |
| def __ne__(self, arg): | |
| return (self._color != arg.color and | |
| self._name != arg.name and | |
| self._volume != arg.volume | |
| ) | |
| @property | |
| def color(self): | |
| return self._color | |
| @color.setter | |
| def color(self, value): | |
| self._color = value | |
| @property | |
| def name(self): | |
| return self._name | |
| @name.setter | |
| def name(self, n): | |
| if type(n) != type(str): | |
| raise ValueError("Name property must be str type") | |
| self._name = n | |
| @property | |
| def volume(self): | |
| return self._volume | |
| class GrannySmith(Apple): | |
| """Subclass type of Apple""" | |
| def __init__(self, volume, sour, name="Granny-Smith", color="Green"): | |
| super(GrannySmith, self).__init__(color, name, volume) | |
| self._sour = bool(sour) | |
| @property | |
| def sour(self): | |
| return self._sour | |
| @sour.setter | |
| def sour(self, b): | |
| self._sour = bool(b) | |
| @property | |
| def worm(self): | |
| return self._worm | |
| @worm.setter | |
| def worm(self, b): | |
| self._worm = bool(b) | |
| @worm.deleter | |
| def worm(self): | |
| #del self._worm | |
| self._worm = None | |
| class ApplePickingBasket(object): | |
| """A basket for holding apples""" | |
| def __init__(self, max_yield, *apples): | |
| self._max = int(max_yield) | |
| self._apples = list(apples) | |
| if len(self._apples) > self.max_yield: | |
| raise OverflowError("Basket can only hold {0} apples at maximum".format(self._max)) | |
| @property | |
| def count(self): | |
| return fractions.Fraction(len(self._apples), self._max) | |
| @property | |
| def apples(self): | |
| try: | |
| ret_apples = copy.deepcopy(self._apples) | |
| except(copy.error): | |
| ret_apples = self._apples | |
| finally: | |
| return ret_apples | |
| @apples.setter | |
| def apples(self, new_apple): | |
| if not (len(self._apples) > self._max): | |
| raise AttributeError("Basket can not hold anymore apples ({0})".format(self._max)) | |
| self._apples.append(new_apple) | |
| @apples.deleter | |
| def apples(self): | |
| self._apples.pop(-1) | |
| @property | |
| def maximum(self): | |
| return self._max | |
| def dump(self, new_basket): | |
| """Dump apples from this basket in to another basket""" | |
| while new_basket.count < new_basket.maximum and len(self._apples) > 0: | |
| new_basket = self._apples[-1] | |
| self._apples.pop(-1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment