changeset 24:23720d0fd9a0

Make SCIENCE_TYPE explicit.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 06 May 2012 19:17:04 +0200
parents f6a3b213857b
children 27570aca5d17
files gamelib/lab.py gamelib/products.py gamelib/research.py gamelib/tests/test_lab.py
diffstat 4 files changed, 8 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/lab.py	Sun May 06 19:06:28 2012 +0200
+++ b/gamelib/lab.py	Sun May 06 19:17:04 2012 +0200
@@ -23,21 +23,19 @@
 
     def _load_data(self, init_data):
         for name, points in init_data['science'].iteritems():
-            module, cls = name.split('.')
-            if module == 'products':
+            science_type, cls = name.split('.')
+            if science_type == 'product':
                 science = getattr(products, cls)
-            elif module == 'research':
+            elif science_type == 'research':
                 science = getattr(research, cls)
             else:
-                raise ValueError("Unknown science type: %s" % (module,))
+                raise ValueError("Unknown science type: %s" % (science_type,))
             self._gain_science(science(points))
 
     def save_data(self):
         data = {'science': {}}
         for science in self.science:
-            science_class = type(science)
-            name = "%s.%s" % (science_class.__module__.split('.')[-1],
-                              science_class.__name__)
+            name = "%s.%s" % (science.SCIENCE_TYPE, type(science).__name__)
             data['science'][name] = science.points
         return data
 
--- a/gamelib/products.py	Sun May 06 19:06:28 2012 +0200
+++ b/gamelib/products.py	Sun May 06 19:17:04 2012 +0200
@@ -8,6 +8,7 @@
     COST = 0
     UPGRADE_REQUIREMENT = 1
     STARTING_PRODUCT = False
+    SCIENCE_TYPE = 'product'
 
     def __init__(self, points=0):
         self.points = points
--- a/gamelib/research.py	Sun May 06 19:06:28 2012 +0200
+++ b/gamelib/research.py	Sun May 06 19:17:04 2012 +0200
@@ -1,6 +1,7 @@
 class ResearchArea(object):
     NAME = None
     PREREQUISITES = ()
+    SCIENCE_TYPE = 'research'
 
     def __init__(self, points=0):
         self.points = points
--- a/gamelib/tests/test_lab.py	Sun May 06 19:06:28 2012 +0200
+++ b/gamelib/tests/test_lab.py	Sun May 06 19:17:04 2012 +0200
@@ -44,5 +44,5 @@
         # Add a product and check that it gets saved as well.
         lab._gain_science(products.LightningGun())
         new_science = dict(
-            LAB_DATA['science'].items() + [('products.LightningGun', 0)])
+            LAB_DATA['science'].items() + [('product.LightningGun', 0)])
         self.assertEqual({'science': new_science}, lab.save_data())