python 中的辅助函数为正常格式

发布于 2025-01-10 09:39:02 字数 644 浏览 2 评论 0原文

我正在 arq 工作人员中工作,在测试一些代码时,我得到了这种格式,

JobDef(function=<function helper at 0x7f79b3582f70>, args=(4, 5, 6, 6), kwargs={}, job_try=None, enqueue_time=datetime.datetime(2022, 2, 28, 6, 18, 7, 247000, tzinfo=datetime.timezone.utc), score=1646029087247)

但我需要采用规范化格式,例如

{'args': (4, 5, 6, 6) or [4, 5, 6, 6],
 'kwargs': {},
 'job_try': None,
 'enqueue_time': datetime.datetime(2022, 2, 28, 6, 18, 7, 247000, 
                 tzinfo=datetime.timezone.utc) or 2022, 2, 28, 6, 18, 7, 247000-> correct 
                 date and time,
 'score': 1646029087247
}

任何预定义的方法来解决此问题。

i am working in arq worker, while testing some code, i was getting like this format

JobDef(function=<function helper at 0x7f79b3582f70>, args=(4, 5, 6, 6), kwargs={}, job_try=None, enqueue_time=datetime.datetime(2022, 2, 28, 6, 18, 7, 247000, tzinfo=datetime.timezone.utc), score=1646029087247)

But I need in normalize format like

{'args': (4, 5, 6, 6) or [4, 5, 6, 6],
 'kwargs': {},
 'job_try': None,
 'enqueue_time': datetime.datetime(2022, 2, 28, 6, 18, 7, 247000, 
                 tzinfo=datetime.timezone.utc) or 2022, 2, 28, 6, 18, 7, 247000-> correct 
                 date and time,
 'score': 1646029087247
}

Any pre-defined way to fix this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2025-01-17 09:39:02

我研究了 arq 源代码JobDef< /code> 定义如下,

@dataclass
class JobDef:
    function: str
    args: Tuple[Any, ...]
    kwargs: Dict[str, Any]
    job_try: int
    enqueue_time: datetime
    score: Optional[int]

因此您应该能够将 JobDef 的实例提供给 dataclasses.asdict 函数 应该返回字典。考虑以下简化示例

from dataclasses import dataclass, asdict
@dataclass
class SimpleJob:
    args: list
    kwargs: dict
    score: int
somejob = SimpleJob([1,2,3],{},123)
print(somejob)
print(asdict(somejob))

输出

SimpleJob(args=[1, 2, 3], kwargs={}, score=123)
{'args': [1, 2, 3], 'kwargs': {}, 'score': 123}

I looked into arq source code and JobDef is defined as follows

@dataclass
class JobDef:
    function: str
    args: Tuple[Any, ...]
    kwargs: Dict[str, Any]
    job_try: int
    enqueue_time: datetime
    score: Optional[int]

therefore you should be able to feed instance of JobDef into dataclasses.asdict function which should return dict. Consider following simplified example

from dataclasses import dataclass, asdict
@dataclass
class SimpleJob:
    args: list
    kwargs: dict
    score: int
somejob = SimpleJob([1,2,3],{},123)
print(somejob)
print(asdict(somejob))

output

SimpleJob(args=[1, 2, 3], kwargs={}, score=123)
{'args': [1, 2, 3], 'kwargs': {}, 'score': 123}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文