Further optimizations & refactoring of PlanetsData class
2 files changed, 37 insertions(+), 19 deletions(-)

M common.py
M fixed.py
M common.py +33 -12
@@ 2,14 2,13 @@ 
 Common classes & functions
 """
 
-# Ignore 'abstract class not referenced' warning
-# pylint: disable=R0921
+import struct
 
 class PlanetsData(object):
     """Base class for all Planets data objects"""
-    COUNT = None
-    PACK_LENGTH = None
-    id = None
+    COUNT = None # Default number of objects in a data file
+    PACK_LENGTH = None # Length of binary data fo single object
+    id = None # Every object needs an id (if not set, generated automatically)
     
     def unpack(self, data):
         """Unpacks binary data into class instance variables"""

          
@@ 24,13 23,35 @@ class PlanetsData(object):
                 count = cls.COUNT
             elif len(data) >= cls.PACK_LENGTH:
                 count = int(len(data) / cls.PACK_LENGTH)
-        for i in range(0, count):
+        for i in range(1, count+1): # For compatibility, count from 1 and not 0
             instance = cls()
-            start = cls.PACK_LENGTH*i
-            end = cls.PACK_LENGTH*(i+1)
+            start = cls.PACK_LENGTH*(i-1)
+            end = cls.PACK_LENGTH*i
             instance.unpack(data[start:end])
-            if instance.id is not None:
-                result[instance.id] = instance
-            else:
-                result[i] = instance
+            if instance.id is None:
+                instance.id = i
+            result[instance.id] = instance
+        return result
+    
+    @classmethod
+    def read(cls, f, count=None, hasCount=False):
+        """Reads from file and loads contents in list of object instances"""
+        # Data may be preceeded by a WORD indicating how much objects follow
+        if hasCount:
+            count = struct.unpack('< h', f.read(2))
+        # Load "count" objects or predetermined "COUNT" objects or whole file
+        if count is not None:
+            data = f.read(cls.PACK_LENGTH * count)
+        elif cls.COUNT is not None:
+            data = f.read(cls.PACK_LENGTH * cls.COUNT)
+        else:
+            data = f.read()
+        return cls.load(data)
+    
+    @classmethod
+    def open(cls, name, count=None, hasCount=False):
+        """Opens file and loads contents in list of object instances"""
+        f = open(name,'rb')
+        result = cls.read(f, count, hasCount)
+        f.close()
         return result
  No newline at end of file

          
M fixed.py +4 -7
@@ 3,15 3,12 @@ Classes for reading files that are const
 such as the "spec" and name lists, which are used by both client and host.
 """
 
-# Ignore 'too many instance attributes' pylint warning triggered by some classes
-# pylint: disable=R0902
-
 import struct
 from common import PlanetsData
 
 class BeamSpec(PlanetsData):
     """List of buildable starship beam weapons"""
-    COUNT = 10 # Default number of records in data file
+    COUNT = 10
     PACK_LENGTH = 36
     name = ''
     cost = 0

          
@@ 19,9 16,9 @@ class BeamSpec(PlanetsData):
     duranium = 0
     molybdenum = 0
     mass = 0
-    tech = 0    # Tech level required
-    kill = 0    # Crew kill
-    damage = 0  # Starship damage
+    tech = 0 
+    kill = 0
+    damage = 0
     
     def unpack(self, data):
         if len(data) is not self.PACK_LENGTH: