使用 Python 动态存储数据

发布于 2024-11-18 04:52:17 字数 206 浏览 10 评论 0原文

我有一个程序,我想在其中记录函数名称、传递给函数的参数和结果。我目前正在使用装饰器记录这一点。但是我不确定如何存储它。目前我每次只是附加到一个文件。

我希望能够将我的装饰器添加到任何 python 程序中的任何函数并记录结果。因此,我需要能够每次存储数据。这使得使用 XML 存储数据变得困难,因为每次都必须解析、添加和重写文档。

关于如何保存这些信息有什么建议吗?

I have a program where I want to record the function name, parameters passed to the function and the result. I am currently recording this using a decorator. However I am unsure how to store this. Currently I am just appending to a file each time.

I want to just be able to add my decorators to any function in any python program and record the result. As such I need to be able to store the data with each time. This makes it difficult to use XML to store the data as the document would have to be parsed, added to and rewritten each time.

Any suggestions on how I can save this information?

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

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

发布评论

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

评论(2

岁月无声 2024-11-25 04:52:17

将所有这些包装在一个类中,然后使用 pickle 模块

我有一个项目可以做一些非常接近你想要的:coopy

wrap all those inside a class and then use pickle module

I have a project that do something very close to what you want: coopy

等风来 2024-11-25 04:52:17

使用标准库中的logging模块。它旨在提供一个灵活的框架,用于记录正在运行的应用程序(跨模块和源文件)的信息。

特别是,它支持处理程序的概念,允许您将日志信息发送到各种接收器:文件、套接字、电子邮件、系统日志守护进程(在 Unix 上)等。如果现有的处理程序都不能满足您的需求,那么编写一个自定义处理程序并将其插入并不困难。例如,您可以实现一个写入数据库的记录器。

Use the logging module from the standard library. It is designed to provide a flexible framework for logging information from a running application (across modules and source files).

In particular, it supports the notion of handlers, allowing you to send logging information into various sinks: files, sockets, email, syslog daemon (on Unix) and so on. If neither of the existing handlers answers your needs, it isn't difficult to write a custom one and plug it in. For instance, you can implement a logger that writes into a database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文