使用 Python 动态存储数据
我有一个程序,我想在其中记录函数名称、传递给函数的参数和结果。我目前正在使用装饰器记录这一点。但是我不确定如何存储它。目前我每次只是附加到一个文件。
我希望能够将我的装饰器添加到任何 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将所有这些包装在一个类中,然后使用 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
使用标准库中的
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.