创建像对象python建议这样的自定义文件? [英] Creating a custom file like object python suggestions?

查看:81
本文介绍了创建像对象python建议这样的自定义文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求为我们在工作中使用的内部二进制格式实现自己的自定义文件(如对象)(我真的不想过多地介绍细节,因为我不知道我能否做到).我正在尝试采用一种更加Python化的处理方式,因为当前我们有两个可以读/写的函数(每行约4k行代码)可以执行所有操作.但是,我们需要更多的控制权/精细度,因此我实际上是在重写这些东西.

Hi i am looking to implement my own custom file like object for an internal binary format we use at work(i don't really want to go into too much detail because i don't know if i can). I am trying to go for a more pythonic way of doing things since currently we have two functions read/write(each ~4k lines of code) which do everything. However we need more control/finesse hence the fact of me rewriting this stuff.

我看了看python文档,他们说了我需要实现什么方法,但是没有提及 iter ()/之类的东西.

I looked at the python documentation and they say what methods i need to implement, but don't mention stuff like iter() / etc.

基本上我想做的是这样的事情:

Basically what i would love to do is stuff like this:

output_file_objs = [
    open("blah.txt", "w")
    open("blah142.txt", "wb")
    my_lib.open("internal_file.something", "wb", ignore_something=True)
]

data_to_write = <data>

for f in output_file_objs:
    f.write(data_to_write)

因此,我可以将其与其他项混合使用,并且基本上具有一定的透明度.我将向其添加自定义方法,但这不是问题.

So i can mix it in with the others, and basically have a level of transparency. I will add custom methods to it, but thats not a problem.

在编写自己的自定义文件(如对象)方面,是否有很好的参考?类似于任何形式的限制或特殊方法( iter ).我应该实施吗?

Is there any sort of good reference regarding writing your own custom file like objects? Like any form of restrictions or special methods (iter). I should implement?

或者我可以在python标准库中找到一个很好的例子吗?

Or is there a good example of one from within the python standard library that i can look at?

推荐答案

组成类似文件"的内容实际上取决于您打算将其用于什么.并非所有方法都必须实现(或明智地实现).

What makes up a "file-like" actually depends on what you intend to use it for; not all methods are required to be implemented (or to have a sane implementation).

话虽如此, file 迭代器文档就是您想要的.

Having said that, the file and iterator docs are what you want.

这篇关于创建像对象python建议这样的自定义文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆