所使用的数据集:
/home/yrd/eric_workspace/chap14/ships_conv/ships.py
#!/usr/bin/env python3
import platform
from PyQt5.QtCore import QDataStream, QFile,QIODevice,Qt
from PyQt5.QtWidgets import QApplication
NAME, OWNER, COUNTRY, DESCRIPTION, TEU = range(5)
MAGIC_NUMBER = 0x570C4
FILE_VERSION = 1
class Ship(object):
def __init__(self, name, owner, country, teu=0, description=""):
self.name = name
self.owner = owner
self.country = country
self.teu = teu
self.description = description
def __hash__(self):
return super(Ship, self).__hash__()
def __lt__(self, other):
return bool(self.name.lower()<other.name.lower())
def __eq__(self, other):
return bool(self.name.lower()==other.name.lower())
class ShipContainer(object):
def __init__(self, filename=""):
self.filename = filename
self.dirty = False
self.ships = {}
self.owners = set()
self.countries = set()
def ship(self, identity):
return self.ships.get(identity)
def addShip(self, ship):
self.ships[id(ship)] = ship
self.owners.add(str(ship.owner))
self.countries.add(str(ship.country))
self.dirty = True
def removeShip(self, ship):
del self.ships[id(ship)]
del ship
self.dirty = True
def __len__(self):
return len(self.ships)
def __iter__(self):
for ship in self.ships.values():
yield ship
def inOrder(self):