【发布时间】:2016-02-09 21:38:46
【问题描述】:
我在 Python 持久性方面完全没有经验。 我尝试使用泡菜来存储一些代理以进行模拟。 我收到消息“TypeError:无法腌制 SwigPyObject 对象” 我换了货架。我得到了同样的错误。 这是我的代码。 我有 5 个对象列表,每个列表包含给定类的许多实例
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
然后我得到同样的泡菜错误 对不起,如果这听起来很简单。 但我以前从未使用过 python 存储(只是保存到 csv 并从中读取)。 谢谢。
这是我的代理课程:
类代理(对象):
# 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 几何的类? 这是治理的代码。 region 是一个 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
其他类类似。 家族和公司在其 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包含osgeo.ogr对象的类实例?
【问题讨论】:
-
Agent类中没有任何明显的内容可以解释SwigPyObject的来源。此外,如果我用random替换fixed_seed,我可以腌制它的一个实例:pickle.dumps(Agent(1, 'm', 22, 'qualification', 100))有效。那么,fixed_seed是什么?你能举例说明创建一个无法腌制的Agent吗? -
fixed_seed = random.Random (0)只是为了通过不同的模块保持相同的种子。 -
好的,那么创建一个由于
TypeError: can't pickle SwigPyObject objects而无法腌制的Agent的示例怎么样?必须有一些东西被传递给__init__(),它引入了SwigPyObject对象。 -
好的。我在问题中介绍了区域的 __init__() 。它确实包含一个 osgeo.ogr 对象(几何,ESRI 的 shapefile)。自治市的实际边界!感谢您的时间@mhawke
标签: python database save persistence