def test_can_not_be_pack_a_package_in_small_bin(self): package = Package("400x300x200") bin = Package("300x300x300") best, rest = binpack([package], bin) best.should.be([]) rest.should.be.length_of(1) package.should.be.within(rest)
def test_can_be_pack_two_packages_in_one_bin(self): package_one = Package("100x100x100") package_two = Package("200x200x200") packages = [package_one, package_two] bin = Package("300x300x300") best, rest = binpack(packages, bin) package_one.should.be.within(best[0]) package_two.should.be.within(best[0]) best[0].should.be.length_of(2) rest.should.be([0])
def estimate_shipping(self, dimensions, country, zipcode=None, state=None, city=None, service=None): """ method that finds optimal solution for packing the products and according to the zone's carrier calc shipping estimation to zipcode :Parameter - dimensions: a list of product's dimension '{height}x{width}x{length}x{weight}' - zipcode: a valid zip code - state: a valid state """ packages = [] for dimension in dimensions: height, width, length, weight = dimension.split('x') size = (float(height), float(width), float(length)) package = Package(size, weight=float(weight)) packages.append(package) best_bin = self.get_best_bin_for_packages(packages) if not best_bin: raise ValueError('This carrier does not have a valid bin') # calc the best packing best_packing, rest = binpack(packages, best_bin.get_package()) if rest: raise ValueError('Shipping could not be estimated! these itens do not have a valid package: %s ' % rest) total_cost, currency = self.interface(service).get_shipping_cost( bin=best_bin, packages=best_packing, country=country, zipcode=zipcode, state=state, city=city) return total_cost, currency
def test_can_be_pack_four_packages_in_two_bins(self): package_one = Package("100x100x100") package_two = Package("200x200x200") package_three = Package("100x100x100") package_four = Package("200x200x200") packages = [package_one, package_two, package_three, package_four] bin = Package("300x300x300") best, rest = binpack(packages, bin) package_one.should.be.within(best[0]) package_two.should.be.within(best[0]) package_three.should.be.within(best[0]) package_four.should.be.within(best[1]) best.should.be.length_of(2) best[0].should.be.length_of(3) best[1].should.be.length_of(1) rest.should.be([])
def test_sort(self): """Test multiplication.""" data = [Package((1600, 490, 480)), Package((1600, 470, 480)), Package((1600, 480, 480))] data.sort() self.assertEqual(data, [Package((1600, 470, 480)), Package((1600, 480, 480)), Package((1600, 490, 480))])
def allpermutations_helper(permuted, todo, maxcounter, callback, bin, bestpack, counter): if not todo: return counter + callback(bin, permuted, bestpack) else: others = todo[1:] thispackage = todo[0] for dimensions in set( permutations( (thispackage[0], thispackage[1], thispackage[2]))): thispackage = Package(dimensions, nosort=True) if thispackage in bin: counter = allpermutations_helper(permuted + [thispackage], others, maxcounter, callback, bin, bestpack, counter) if counter > maxcounter: raise Timeout('more than %d iterations tries' % counter) return counter
def get_package(self): return Package((self.height, self.width, self.length))
def test_init(self): """Tests object initialisation with different constructors.""" self.assertEqual(Package((100, 100, 200)), Package(('100', '200', '100'))) self.assertEqual(Package((100.0, 200.0, 200.0)), Package('200x200x100'))
def test_mul(self): """Test multiplication.""" self.assertEqual(Package((200, 200, 200)), Package((100, 200, 200)) * 2)
def test_gurtmass(self): """Test gurtmass calculation.""" self.assertEqual(800, Package((100, 200, 200)).gurtmass) self.assertEqual(900, Package((100, 200, 300)).gurtmass) self.assertEqual(1000, Package((200, 200, 200)).gurtmass) self.assertEqual(3060, Package((1600, 250, 480)).gurtmass)
def test_repr(self): """Test __repr__ implementation.""" self.assertEqual('<Package 200x200x100 44>', Package((100, 200, 200), 44).__repr__())
def test_str(self): """Test __unicode__ implementation.""" self.assertEqual('200x200x100', Package((100, 200, 200)).__str__()) self.assertEqual('200x200x100', Package('100x200x200').__str__())
def test_volume(self): """Tests volume calculation""" self.assertEqual(4000000, Package((100, 200, 200)).volume) self.assertEqual(8000, Package((20, 20, 20)).volume)
def test_eq(self): """Tests __eq__() implementation.""" self.assertEqual(Package((200, 100, 200)), Package(('200', '100', '200'))) self.assertNotEqual(Package((200, 200, 100)), Package(('100', '100', '200')))