Repository: psychopy/psychopy

Issue:
Experiments not loading
Hi - I think I have located the underlying cause for the reports of experiments not working in the latest version.

When a user renames a Routine in a loop and saves it - for some reason the resultant element in the flow XML opens and closes with different names - please see SS below:

<img width="565" alt="screen shot 2016-09-27 at 10 01 33" src="https://cloud.githubusercontent.com/assets/16899600/18872997/2f950dc0-84b5-11e6-8962-a342c49843cd.png">

I can't think why this would be the case.  I am pretty sure that the old name is held somewhere since renaming a routine to the same name results in a Routine_Name_2 - so it may be this variable that informs the closing of the XML element.

I would like to fix this since it was my feature, but I have run out of ideas! Can anyone give me a clue?



Discussion:

Resolving patch:
diff --git a/psychopy/app/builder/components/keyboard/__init__.py b/psychopy/app/builder/components/keyboard/__init__.py
--- a/psychopy/app/builder/components/keyboard/__init__.py
+++ b/psychopy/app/builder/components/keyboard/__init__.py
@@ -155,21 +155,18 @@ def writeFrameCode(self, buff):
         if allowedKeysIsVar:
             # if it looks like a variable, check that the variable is suitable
             # to eval at run-time
-            code = ("# AllowedKeys looks like a variable named `%s`\n"
-                    "if not '%s' in locals():\n"
-                    "    logging.error('AllowedKeys variable `%s` is not defined.')\n"
-                    "    core.quit()\n"
-                    "if not type(%s) in [list, tuple, np.ndarray]:\n"
-                    "    if not isinstance(%s, basestring):\n"
-                    "        logging.error('AllowedKeys variable `%s` is "
+            code = ("# AllowedKeys looks like a variable named `{0}`\n"
+                    "if not type({0}) in [list, tuple, np.ndarray]:\n"
+                    "    if not isinstance({0}, basestring):\n"
+                    "        logging.error('AllowedKeys variable `{0}` is "
                     "not string- or list-like.')\n"
-                    "        core.quit()\n" %
-                    allowedKeys)
+                    "        core.quit()\n"
+                    .format(allowedKeys))
 
-            vals = (allowedKeys, allowedKeys, allowedKeys)
             code += (
-                "    elif not ',' in %s: %s = (%s,)\n" % vals +
-                "    else:  %s = eval(%s)\n" % (allowedKeys, allowedKeys))
+                "    elif not ',' in {0}: {0} = ({0},)\n"
+                "    else:  {0} = eval({0})\n"
+                .format(allowedKeys))
             buff.writeIndentedLines(code)
 
             keyListStr = "keyList=list(%s)" % allowedKeys  # eval at run time
diff --git a/psychopy/app/builder/components/static/__init__.py b/psychopy/app/builder/components/static/__init__.py
--- a/psychopy/app/builder/components/static/__init__.py
+++ b/psychopy/app/builder/components/static/__init__.py
@@ -151,4 +151,4 @@ def writeParamUpdates(self, buff, updateType=None, paramNames=None):
                                       updateType=prms[fieldName].updates,
                                       params=prms)
             code = "# component updates done\n"
-            buff.writeIndented(code % self.params['name'])
+            buff.writeIndented(code)
diff --git a/psychopy/app/builder/experiment.py b/psychopy/app/builder/experiment.py
--- a/psychopy/app/builder/experiment.py
+++ b/psychopy/app/builder/experiment.py
@@ -1541,6 +1541,13 @@ def __repr__(self):
         _rep = "psychopy.experiment.Routine(name='%s', exp=%s, components=%s)"
         return _rep % (self.name, self.exp, str(list(self)))
 
+    @property
+    def name(self):
+        return self.params['name']
+    @name.setter
+    def name(self, name):
+        self.params['name'] = name
+
     def addComponent(self, component):
         """Add a component to the end of the routine"""
         self.append(component)


Test patch: