On Wed, Jan 25, 2012 at 6:12 AM, Edward K. Ream <[email protected]> wrote:

> We compute the result, merged_d, as follows::

Actually, d.update() updates d in place: it does not return a value.
Thus, the code must be::

    inverted_old_d = self.invert(old_d)
    inverted_new_d = self.invert(new_d)
    inverted_old_d.update(inverted_new_d)
    result = self.uninvert(inverted_old_d)

Here are the invert and uninvert methods, devoid of traces, asserts, etc.::

    def invert (self,d):
        '''Invert a shortcut dict whose keys are command names,
        returning a dict whose keys are strokes.'''
        result = {}
        for commandName in d.keys():
            if commandName == '_hash':
                result['_hash'] = 'inverted %s' % d.get('_hash')
            else:
                for si in d.get(commandName,[]):
                    stroke = si.val # Already canonicalized.
                    si.commandName = commandName # Add for uninvert
                    aList = result.get(stroke,[])
                    if si not in aList:
                        aList.append(si)
                        result[stroke] = aList
        return result

    def uninvert (self,d):
        '''Invert a shortcut dict whose keys are strokes,
        returning a dict whose keys are command names.'''
        result = {}
        for stroke in d.keys():
            if stroke == '_hash':
                result['_hash'] = 'uninverted %s' % d.get('_hash')
            else:
                for si in d.get(stroke,[]):
                    commandName = si.commandName
                    aList = result.get(commandName,[])
                    if si not in aList:
                        aList.append(si)
                        result[commandName] = aList
        return result

Before releasing the new code to the trunk a new set of unit tests
must pass.  In the meantime, this post is my backup :-)

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en.

Reply via email to