当前位置: 首页>>代码示例>>Python>>正文


Python Order.to_json方法代码示例

本文整理汇总了Python中models.Order.to_json方法的典型用法代码示例。如果您正苦于以下问题:Python Order.to_json方法的具体用法?Python Order.to_json怎么用?Python Order.to_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Order的用法示例。


在下文中一共展示了Order.to_json方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: optimize_shipping

# 需要导入模块: from models import Order [as 别名]
# 或者: from models.Order import to_json [as 别名]
def optimize_shipping():
    """
    """

    def make_combinations(items):
        """
        makes all possible combinations to the length of the origin input
        """

        def inner(items, r):
            """
            recursively yields partitioned remainders of original partition lists
            """
            items = set(items)
            if not len(items):
                yield ()
                return
            first = next(iter(items))
            remainder = items.difference((first, ))
            for combination in combinations(remainder, r-1):
                first_subset = (first, ) + combination
                for partition in inner(remainder.difference(combination), r):
                    yield (first_subset, ) + partition

        def outter(items, r):
            """
            combines partition lists
            """
            items = set(items)
            for i in range(len(items), -1, -r):
                if i == 0:
                    for partition in inner(items, r):
                        yield partition
                elif i != r:
                    for combination in combinations(items, i):
                        for partition in inner(items.difference(combination), r):
                            yield partition + (combination, )

        # step through length of origin combination partitions to ensure full list
        for i in range(1, len(items)):
            gen = outter(items, i)
            for row in gen:
                yield row

    # get posted json
    data = json.loads(request.data)

    # pull zipcode
    zipcode = data['zip']

    # create items
    regular, irregular = [], []
    for item in data['items']:
        qty = item['qty']
        del item['qty']
        for __ in xrange(qty):
            if 'is_irregular' in item:
                if item['is_irregular']:
                    irregular.append(Item(**item))
                else:
                    regular.append(Item(**item))
            else:
                regular.append(Item(**item))

    # process irregular items
    irregular_boxes = []
    for item in irregular:
        box = Box()
        box.items.append(item)
        cost = get_irregular_price(zipcode, box.weight)
        box.shipping_method = 'UPS Ground'
        box.shipping_cost = cost
        irregular_boxes.append(box)

    # process regular items
    if len(regular):
        if len(regular) == 1:
            box = Box()
            box.items.extend(regular)
            method, cost = get_cheapest_option(zipcode, box.weight)
            box.shipping_method = method
            box.shipping_cost = cost
            order = Order()
            order.boxes.append(box)
            # add irregular boxes to order
            order.boxes.extend(irregular_boxes)
            return str(order.to_json())

        else:  # create orders/bundles from items combinations
            orders = []
            for combination in make_combinations(regular):  # full tuple of tuples of items. ex: ( (one,two), (three,) )
                order = Order()
                for grouping in combination:  # tuple of items. ex (one,two) from above
                    box = Box()
                    # add items to the box
                    box.items.extend(grouping)
                    # get cheapest shipping option for this box
                    method, cost = get_cheapest_option(zipcode, box.weight)
                    box.shipping_method = method
                    box.shipping_cost = cost
#.........这里部分代码省略.........
开发者ID:muniu,项目名称:sirius,代码行数:103,代码来源:app.py


注:本文中的models.Order.to_json方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。