小编Jay*_*hai的帖子

如何在Faker中生成感知时间对象?

我有以下(简化的)模型和工厂:

模型.py

class Event():
    duration = FloatField()
    start_time = TimeField()
    finish_time = DateTimeField()

    def save(self, *args, **kwargs):
        self.finish_time = self.start_time + timedelta(hours=self.duration)
Run Code Online (Sandbox Code Playgroud)

事件工厂.py

from factory import Faker

class EventFactory:
    date = Faker(
        "date_time_this_month",
        before_now=False,
        after_now=True,
        tzinfo=timezone.get_current_timezone(),
        )
    start_time = Faker("time_object")
    duration = Faker("random_int")
Run Code Online (Sandbox Code Playgroud)

但是,我的save方法提出了Warning: DateTimeField Event.finish_time received a naive datetime (2022-03-28 12:43:38) while time zone support is active.

date由于参数而知道tzinfo,但start_time很天真(我用 django 检查过timezone.is_aware()),因为 Faker 中的时间提供程序似乎不允许任何时区参数。

关于在 Factory-boy/Faker 中获取假感知时间对象有什么建议吗?

python django class faker factory-boy

5
推荐指数
1
解决办法
2642
查看次数

将 map() 与字典一起使用

我有一本字典。

prices = {'n': 99, 'a': 99, 'c': 147}

Run Code Online (Sandbox Code Playgroud)

使用map()我需要接收新的字典:

def formula(value):
    value = value -value * 0.05
    return value
Run Code Online (Sandbox Code Playgroud)
new_prices = dict(map(formula, prices.values()))
Run Code Online (Sandbox Code Playgroud)

但它不起作用

TypeError: cannot convert dictionary update sequence element #0 to a sequence
Run Code Online (Sandbox Code Playgroud)

使用以下方法解决我的代码map()

new_prices = {'n': 94.05, 'a': 94.05, 'c': 139.65}
Run Code Online (Sandbox Code Playgroud)

python dictionary list

2
推荐指数
1
解决办法
767
查看次数

标签 统计

python ×2

class ×1

dictionary ×1

django ×1

factory-boy ×1

faker ×1

list ×1