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_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 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([])