- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在要放入多处理的对象中使用命名元组时遇到问题。我收到 pickle 错误。我尝试了其他 stackoverflow 帖子中的几件事,但我无法成功。这是我的代码结构:
package_main, test_module
import myprogram.package_of_classes.data_object_module
import ....obj_calculate
class test(object):
if __name__ == '__main__':
my_obj=create_obj('myobject',['f1','f2'])
input = multiprocessing.Queue()
output = multiprocessing.Queue()
input.put(my_obj)
j=Process(target=obj_calculate, args=(input,output))
j.start()
package_of_classes, data_object_module
import collections
import ....load_flat_file
def get_ntuple_format(obj):
nt_fields=''
for fld in obj.fields:
nt_fields=nt_fields+fld+', '
nt_fields=nt_fields[0:-2]
ntuple=collections.namedtuple('ntuple_format',nt_fields)
return ntuple
Class Data_obj:
def __init__(self, name,fields):
self.name=name
self.fields=fields
self.ntuple_form=get_ntuple_format(self)
def calculate(self):
self.file_read('C:/files','division.txt')
def file_read(self,data_directory,filename):
output=load_flat_file(data_directory,filename,self.ntuple_form)
self.data=output
utils_package,utils_module
def create_dataobj(name,fields):
locals()[name]=Data_Obj(name,fields)
return locals()[name]
def obj_calculate(input,output):
obj=input.get()
obj.calculate()
output.put(obj)
加载模块
def load_flat_file(data_directory,filename,ntuple_form):
csv.register_dialect('csvrd', delimiter='\t', quoting=csv.QUOTE_NONE)
ListofTuples=[]
with open(os.path.join(data_directory,filename), 'rb') as f:
reader = csv.reader(f,'csvrd')
for line in reader:
if line:
ListofTuples.append(ntuple_form._make(line))
return ListofTuples
我得到的错误是:
PicklingError: PicklingError: Can't pickle class '__main__ . ntuple_format: it's not the same object as __ main __. ntuple_format
附言由于我是从一个大型项目中提取此示例代码,因此请忽略细微的不一致之处。
最佳答案
您不能 pickle 动态创建的类(在本例中为命名元组)(通过 get_ntuple_format
)。对于一个可 pickle 的类,it has to be defined在可导入模块的顶层。
如果您只需要支持几种元组,请考虑提前在模块的顶层定义所有元组,然后动态选择正确的元组。如果您需要完全动态的容器格式,请考虑只使用 dict
。
关于python - 具有 namedtuple 的多处理对象 - pickle 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22304815/
typing module documentation表示下面的两个代码片段是等价的。 from typing import NamedTuple class Employee(NamedTuple)
是否可以在另一个 namedtuple 中包含一个 namedtuple? 例如: from collections import namedtuple Position = namedtuple('
为什么我不能 pickle typing.NamedTuple 而我可以 pickle collections.namedtuple?我如何设法 pickle NamedTuple? 这段代码展示了我
我正在尝试创建一个 NamedTuple,其中一个字段默认为空字典。这大部分都有效,但是默认值在 NamedTuple 的实例之间共享: from typing import NamedTuple,
我正在寻找一种类似结构的数据结构,我可以从中创建多个实例并具有某种类型提示而不是不可变的。 所以我有这样的东西: class ConnectionConfig(NamedTuple): nam
在下面的代码中: point=namedtuple('point','x y') 我很困惑,一个类被视为一个值。就像我做的那样: class point: ... 它在命名空间中创建一个名为
考虑以下扩展 list 并附加 orderedtuple 的类: from collections import namedtuple Order = namedtuple('Order', ['id
我一直在使用这个基本代码来构建一个简单的库存系统。我需要扩展它以跟踪离开库存的另一个零件列表。我想我可以通过为数据结构创建另一个命名元组来做到这一点。我遇到一些问题,因为两个命名元组需要存在并同时访问
Python namedtuple 工厂函数允许它创建的子类的名称被指定两次——第一次在声明的左侧,然后作为函数的第一个参数(IPython 1.0.0 , Python 3.3.1): In [1]
最近我有一个关于数据类型的问题。 从那时起,我一直在尝试使用 NamedTuples(或多或少取得了成功)。 我目前的问题: - 如何将行从文件导入到新元组, - 如何将以空格/制表符(/无论什么)分
我有一个类,它几乎没有静态字段,并且是从可迭代对象初始化的(比如 csvreader 的输出)。 __init__ 为其中一些执行从字符串到数字的类型转换: class PerformanceTest
我知道 collections.namedtuple 的存在和目的,但我注意到,至少在 IDLE (3.2.2) 中,这个工厂函数也在 functools: >>> import functools
我想存储一个维度 namedtuple (x, y)。在我的整个程序中我只需要一次。 我能做到: Dimension = namedtuple('Dimension', ['x', 'y']) dim
有没有办法为 python 中的 namedtuple 重载相等运算符 __eq__(self, other)? 我知道这在类和重新定义方法中是可能的,但这对于 namedtuple 是否也可能,您将
这个问题在这里已经有了答案: Why should I use CamelCase for namedtuple? (1 个回答) 关闭 3 年前。 我是 Python 的新手,我一直在阅读在线文档
我们像这样使用 namedtuple: >>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y'
我正在处理命名元组列表。我想在创建每个命名元组后为其添加一个字段。似乎我可以通过将它作为属性引用来做到这一点(如 namedtuple.attribute = 'foo'),但它不会添加到字段列表中。
我正在尝试记录 namedtuple。当我构建文档时,我收到警告 WARNING: duplicate object description 和在记录的函数之后相同的空函数。例如: 如何删除那些别名?
在 Python 3.6 之后,我们有 typing.NamedTuple ,它是 collections.namedtuple() 的类型化版本,我们可以像类一样继承它: class Employe
鉴于我想从类型模块中正确使用命名元组的类型注释: from typing import NamedTuple, List class Foo(NamedTuple): my_list: Lis
我是一名优秀的程序员,十分优秀!