私信  •  关注

shad0w_wa1k3r

shad0w_wa1k3r 最近创建的主题
shad0w_wa1k3r 最近回复了
7 年前
回复了 shad0w_wa1k3r 创建的主题 » For循环范围步骤更改为float python[duplicate]

为了解决浮点精度问题,可以使用 Decimal module .

这需要付出额外的努力 十进制的 int float 在编写代码时,但是您可以通过 str 如果确实需要这种方便,可以修改函数。

from decimal import Decimal
from decimal import Decimal as D


def decimal_range(*args):

    zero, one = Decimal('0'), Decimal('1')

    if len(args) == 1:
        start, stop, step = zero, args[0], one
    elif len(args) == 2:
        start, stop, step = args + (one,)
    elif len(args) == 3:
        start, stop, step = args
    else:
        raise ValueError('Expected 1 or 2 arguments, got %s' % len(args))

    if not all([type(arg) == Decimal for arg in (start, stop, step)]):
        raise ValueError('Arguments must be passed as <type: Decimal>')

    # neglect bad cases
    if (start == stop) or (start > stop and step >= zero) or \
                          (start < stop and step <= zero):
        return []

    current = start
    while abs(current) < abs(stop):
        yield current
        current += step

样本输出-

list(decimal_range(D('2')))
# [Decimal('0'), Decimal('1')]
list(decimal_range(D('2'), D('4.5')))
# [Decimal('2'), Decimal('3'), Decimal('4')]
list(decimal_range(D('2'), D('4.5'), D('0.5')))
# [Decimal('2'), Decimal('2.5'), Decimal('3.0'), Decimal('3.5'), Decimal('4.0')]
list(decimal_range(D('2'), D('4.5'), D('-0.5')))
# []
list(decimal_range(D('2'), D('-4.5'), D('-0.5')))
# [Decimal('2'),
#  Decimal('1.5'),
#  Decimal('1.0'),
#  Decimal('0.5'),
#  Decimal('0.0'),
#  Decimal('-0.5'),
#  Decimal('-1.0'),
#  Decimal('-1.5'),
#  Decimal('-2.0'),
#  Decimal('-2.5'),
#  Decimal('-3.0'),
#  Decimal('-3.5'),
#  Decimal('-4.0')]
6 年前
回复了 shad0w_wa1k3r 创建的主题 » 如何在django oscar仪表板中添加推荐产品

这是一个流式搜索字段,无论您键入什么,都应该在现有产品数据库中搜索相关术语。

例如,如果您键入 <search_term> 它最终将查询(在多次中间查询子字符串之后)&hit http://localhost:8000/dashboard/catalogue/product-lookup/?q=<search_term> 其观点可以是 found here . 正如你所看到的,它只搜索产品标题,如果你需要其他东西,你可以永远 modify it .

从外观上看,您还没有填充产品数据库,或者您的安装或设置有其他问题。