如何最好地在 Python 中保存多个 class 实例?更新:如何 pickle class 个包含 osgeo.ogr 个对象的实例?
How to best save a number of class instances in Python? Updated: how to pickle class instances that contains osgeo.ogr objects?
我完全没有 Python 坚持的经验。
我尝试使用泡菜来存储一些用于模拟的代理。
我收到消息 "TypeError: can't pickle SwigPyObject objects"
我切换到货架。我得到了同样的错误。
这是我的代码。
我有 5 个对象列表,每个列表包含给定 class
的许多实例
to_store = [my_regions, my_agents, my_houses, my_families, my_firms]
我打开一个架子。
boxes = shelve.open((parameters.OUTPUT_PATH + "my_box"))
并尝试使用通用密钥从我的 5 个列表中保存每个实例
n = 0
for item in to_store:
for instance in item:
boxes[n] = instance
n += 1
然后我得到了同样的 pickle 错误
对不起,如果这听起来很简单。
但我以前从未使用过 python 存储(只是保存到 csv 并从中读取)。
谢谢。
这是我的 Class 代理人:
class 代理(对象):
# Class for agents. Citizens of the model
# Agents live in families, work in firms, consume
def __init__(self, id, gender, age, qualification, money, firm_id=None, family_id=None, utility=1,
address=None, distance=None, region_id=None):
self.id = id
self.gender = gender
self.age = age
self.month = fixed_seed.randrange(1, 13, 1)
self.qualification = qualification
self.money = money
self.firm_id = firm_id
self.family_id = family_id
self.utility = utility
self.address = address
self.distance = distance
self.region_id = region_id
也许问题出在包含 GDAL/OSGEO 几何体的 class 上?
这是 Govern 的代码。区域是一个 shapefile ogr.object
class Govern(object):
def __init__(self, region, index=1, old_index=1, treasure=0, region_gdp=0, pop=0, total_commute=0):
# region is an OSGEO object that contains Fields and Geometry
self.region = region
# Make sure FIELD 0 is Name
self.name = self.region.GetField(0)
# Make sure FIELD 1 is IBGE CODE
self.id = self.region.GetField(1)
self.index = index
self.old_index = old_index
self.treasure = treasure
self.region_gdp = region_gdp
self.pop = pop
self.total_commute = total_commute
其他class也是类似的。
家庭和公司在他们的 init()
中有字典
class Family(object):
def __init__(self, family_id, balance=0, household_id=None, region_id=None, house_price=0, address=None, house=None):
# Family is a set of agents store in a dictionary keyed by id
self.family_id = family_id
self.balance = balance
self.members = {}
下面是我创建一些公司的方法,例如
def create_firm(num_firms, region, firm_id):
dummy_sector = []
for dummy_firm in range(num_firms):
address = get_random_point_in_polygon(region)
total_balance = fixed_seed.randrange(20, 400)
dummy_sector.append(firms.Firm(firm_id, address, total_balance, region.get_region_id()))
firm_id += 1
return dummy_sector
好的。经过一番争论,我猜问题出在OSGEO/GDAL生成的对象上。我使用: from osgeo import ogr
所以,问题应该改为:如何 pickle class 个包含 osgeo.ogr 个对象的实例?
在后台 shelve
使用 pickle
,因此如果您的对象不能被 pickle,shelve
就不能使用它们。
可能有一种方法可以存储每个对象的信息,以便以后可以通过对象实例化来重建该对象,但我们需要知道对象的详细信息,以及如何创建一个对象.
或者,对象可能提供了一种转储它们的方法。
对象属于 class SwigPyObject
表明对象存在于 Python 外部,在 C/C++ 库中。
如果可能,请提供 classes 的详细信息。
我完全没有 Python 坚持的经验。 我尝试使用泡菜来存储一些用于模拟的代理。 我收到消息 "TypeError: can't pickle SwigPyObject objects" 我切换到货架。我得到了同样的错误。 这是我的代码。 我有 5 个对象列表,每个列表包含给定 class
的许多实例to_store = [my_regions, my_agents, my_houses, my_families, my_firms]
我打开一个架子。
boxes = shelve.open((parameters.OUTPUT_PATH + "my_box"))
并尝试使用通用密钥从我的 5 个列表中保存每个实例
n = 0
for item in to_store:
for instance in item:
boxes[n] = instance
n += 1
然后我得到了同样的 pickle 错误 对不起,如果这听起来很简单。 但我以前从未使用过 python 存储(只是保存到 csv 并从中读取)。 谢谢。
这是我的 Class 代理人:
class 代理(对象):
# Class for agents. Citizens of the model
# Agents live in families, work in firms, consume
def __init__(self, id, gender, age, qualification, money, firm_id=None, family_id=None, utility=1,
address=None, distance=None, region_id=None):
self.id = id
self.gender = gender
self.age = age
self.month = fixed_seed.randrange(1, 13, 1)
self.qualification = qualification
self.money = money
self.firm_id = firm_id
self.family_id = family_id
self.utility = utility
self.address = address
self.distance = distance
self.region_id = region_id
也许问题出在包含 GDAL/OSGEO 几何体的 class 上? 这是 Govern 的代码。区域是一个 shapefile ogr.object
class Govern(object):
def __init__(self, region, index=1, old_index=1, treasure=0, region_gdp=0, pop=0, total_commute=0):
# region is an OSGEO object that contains Fields and Geometry
self.region = region
# Make sure FIELD 0 is Name
self.name = self.region.GetField(0)
# Make sure FIELD 1 is IBGE CODE
self.id = self.region.GetField(1)
self.index = index
self.old_index = old_index
self.treasure = treasure
self.region_gdp = region_gdp
self.pop = pop
self.total_commute = total_commute
其他class也是类似的。 家庭和公司在他们的 init()
中有字典class Family(object):
def __init__(self, family_id, balance=0, household_id=None, region_id=None, house_price=0, address=None, house=None):
# Family is a set of agents store in a dictionary keyed by id
self.family_id = family_id
self.balance = balance
self.members = {}
下面是我创建一些公司的方法,例如
def create_firm(num_firms, region, firm_id):
dummy_sector = []
for dummy_firm in range(num_firms):
address = get_random_point_in_polygon(region)
total_balance = fixed_seed.randrange(20, 400)
dummy_sector.append(firms.Firm(firm_id, address, total_balance, region.get_region_id()))
firm_id += 1
return dummy_sector
好的。经过一番争论,我猜问题出在OSGEO/GDAL生成的对象上。我使用: from osgeo import ogr
所以,问题应该改为:如何 pickle class 个包含 osgeo.ogr 个对象的实例?
在后台 shelve
使用 pickle
,因此如果您的对象不能被 pickle,shelve
就不能使用它们。
可能有一种方法可以存储每个对象的信息,以便以后可以通过对象实例化来重建该对象,但我们需要知道对象的详细信息,以及如何创建一个对象.
或者,对象可能提供了一种转储它们的方法。
对象属于 class SwigPyObject
表明对象存在于 Python 外部,在 C/C++ 库中。
如果可能,请提供 classes 的详细信息。