Определение класса:
class Switch(): """Object switch """ def __init__(self, mac="", ip="" ports = []): self.mac = mac self.ip = ip self.ports = ports
Вот так создаю экземпляры:
for mac in data_from_url: sw = Switch(mac) # Create instance switch_list.append(sw) # Adding object to list # Adding values to attributes of objects: if isinstance(data_from_url[mac], dict): for mac in data_from_url: if switch_key == '1': sw.ip = data_from_url[mac][switch_key] if switch_key == '21': sw.mac = data_from_url[mac][switch_key]
print object1.__dict__ {'hostname': u'name', 'ip': u'192.168.0.1' ... } print object2.__dict__ {'hostname': u'name2', 'ip': u'192.168.0.2' ... } list_objects = [object1, object2, ... ] save_to_file(list_objects, file.db) def save_to_file(lst, file_pickle): with open(file_pickle, 'wb') as output: pickle.dump(lst, output) output.close()
File "/home/thund3r/work/git/zabbix_topology/list_of_objects.py", line 89, in save_to_file pickle.dump(lst, output) File "/usr/lib/python2.7/pickle.py", line 1376, in dump Pickler(file, protocol).dump(obj) File "/usr/lib/python2.7/pickle.py", line 224, in dump self.save(obj) File "/usr/lib/python2.7/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.7/pickle.py", line 606, in save_list self._batch_appends(iter(obj)) File "/usr/lib/python2.7/pickle.py", line 621, in _batch_appends save(x) File "/usr/lib/python2.7/pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.7/pickle.py", line 704, in save_inst args = obj.__getinitargs__() TypeError: 'str' object is not callable
Откуда str?
type(lst) is {list}
type(output) is {file}
Хотя список можно распечать и проблем в нем не видно:
for x in zabbix_switches: if x.mac == '1024CF94FD97': print x.__name__ print x.__class__ print x.__dict__
__name__ __main__.Switch {'ip': u'192.168.0.1', 'mac': u'1024CF94FD97', ...}
Подскажите, что делаю не так. Заранее благодарен!