Last active
January 2, 2016 10:49
-
-
Save ka8725/8292703 to your computer and use it in GitHub Desktop.
Oop model comparison Ruby and Python
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
| from abc import ABCMeta | |
| from random import randint | |
| class SortableGoods(object): | |
| __metaclass__ = ABCMeta | |
| _GOODS_CLASSES_PRIORITY = { | |
| 'Person': 0, | |
| 'Container': 1, | |
| 'Platform': 2, | |
| 'Cylinder': 3 | |
| } | |
| def __eq__(self, other): | |
| return self._GOODS_CLASSES_PRIORITY[self.__class__.__name__] == self._GOODS_CLASSES_PRIORITY[other.__class__.__name__] | |
| def __ne__(self, other): | |
| return self._GOODS_CLASSES_PRIORITY[self.__class__.__name__] != self._GOODS_CLASSES_PRIORITY[other.__class__.__name__] | |
| def __lt__(self, other): | |
| return self._GOODS_CLASSES_PRIORITY[self.__class__.__name__] < self._GOODS_CLASSES_PRIORITY[other.__class__.__name__] | |
| def __le__(self, other): | |
| return self._GOODS_CLASSES_PRIORITY[self.__class__.__name__] <= self._GOODS_CLASSES_PRIORITY[other.__class__.__name__] | |
| def __gt__(self, other): | |
| return self._GOODS_CLASSES_PRIORITY[self.__class__.__name__] > self._GOODS_CLASSES_PRIORITY[other.__class__.__name__] | |
| def __ge__(self, other): | |
| return self._GOODS_CLASSES_PRIORITY[self.__class__.__name__] >= self._GOODS_CLASSES_PRIORITY[other.__class__.__name__] | |
| class WeightCalculation(object): | |
| __metaclass__ = ABCMeta | |
| def __init__(self, square, height, density, container_weight): | |
| self._square = square | |
| self._height = height | |
| self._density = density | |
| self._container_weight = container_weight | |
| def weight(self): | |
| return self._square * self._height * self._density + self._container_weight | |
| def __str__(self): | |
| return '%s: %s + %s' % (self.__class__.__name__, 'x'.join([str(self._square), str(self._height), str(self._density)]), self._container_weight) | |
| class OwnWeight(object): | |
| __metaclass__ = ABCMeta | |
| def __init__(self, weight): | |
| self._weight = weight | |
| def weight(self): | |
| return self._weight | |
| def __str__(self): | |
| return '%s: %s' % (self.__class__.__name__, self._weight) | |
| class Container(WeightCalculation, SortableGoods): | |
| pass | |
| class Cylinder(WeightCalculation, SortableGoods): | |
| pass | |
| class Platform(OwnWeight, SortableGoods): | |
| pass | |
| class Person(OwnWeight, SortableGoods): | |
| pass | |
| class Ferry(object): | |
| def __init__(self, capacity): | |
| self._capacity = capacity | |
| self._goods = [] | |
| @property | |
| def goods(self): | |
| return self._goods | |
| def put(self, product): | |
| self._goods.append(product) | |
| def is_overloaded(self): | |
| return sum([g.weight() for g in self._goods]) > self._capacity | |
| ferry = Ferry(10000) | |
| for i in range(100): | |
| ferry.put(Container(randint(1, 10), randint(1, 10), randint(1, 10), randint(100, 1000))) | |
| ferry.put(Cylinder(randint(1, 10), randint(1, 10), randint(1, 10), randint(100, 1000))) | |
| ferry.put(Platform(randint(1, 100))) | |
| ferry.put(Person(randint(1, 100))) | |
| print 'Overloaded?: %s' % ferry.is_overloaded() | |
| print [str(g) for g in sorted(ferry.goods)] |
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
| module SortableGoods | |
| include Comparable | |
| GOODS_CLASSES_WEIGHTS = { | |
| 'Person' => 0, | |
| 'Container' => 1, | |
| 'Platform' => 2, | |
| 'Cylinder' => 3 | |
| }.freeze | |
| def <=>(other) | |
| GOODS_CLASSES_WEIGHTS[self.class.name] <=> GOODS_CLASSES_WEIGHTS[other.class.name] | |
| end | |
| end | |
| module WeightCalculation | |
| def initialize(square, height, density, container_weight) | |
| @square = square | |
| @height = height | |
| @density = density | |
| @container_weight = container_weight | |
| end | |
| def weight | |
| @square * @height * @density + @container_weight | |
| end | |
| def to_s | |
| "#{self.class.name}: #{[@square, @height, @density].join('x')} + #{@container_weight}" | |
| end | |
| end | |
| module OwnWeight | |
| def initialize(weight) | |
| @weight = weight | |
| end | |
| def weight | |
| @weight | |
| end | |
| def to_s | |
| "#{self.class.name}: #{@weight}" | |
| end | |
| end | |
| class Container | |
| include WeightCalculation | |
| include SortableGoods | |
| end | |
| class Cylinder | |
| include WeightCalculation | |
| include SortableGoods | |
| end | |
| class Platform | |
| include OwnWeight | |
| include SortableGoods | |
| end | |
| class Person | |
| include OwnWeight | |
| include SortableGoods | |
| end | |
| class Ferry | |
| attr_reader :goods | |
| def initialize(capacity) | |
| @capacity = capacity | |
| @goods = [] | |
| end | |
| def <<(product) | |
| @goods << product | |
| end | |
| def overloaded? | |
| @goods.reduce(0) { |sum, product| sum += product.weight } > @capacity | |
| end | |
| end | |
| def random(n, add = 1) | |
| rand(n) + add | |
| end | |
| ferry = Ferry.new(10000) | |
| 100.times do | |
| ferry << Container.new(random(10), random(10), random(10), random(1000)) | |
| ferry << Cylinder.new(random(10), random(10), random(10), random(1000)) | |
| ferry << Platform.new(random(100)) | |
| ferry << Person.new(random(100)) | |
| end | |
| puts "Ready to go?: #{!ferry.overloaded?}" | |
| puts ferry.goods.sort |
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
| $ time ruby model.rb | |
| real 0m0.103s | |
| user 0m0.064s | |
| sys 0m0.038s | |
| $ time python model.py | |
| real 0m0.037s | |
| user 0m0.025s | |
| sys 0m0.010s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment