harmsm / PyCmdMessenger

Python interface for CmdMessenger arduino serial communication library
MIT License
80 stars 32 forks source link

Python 2.x compatibility #23

Open sphh opened 7 years ago

sphh commented 7 years ago

Hi Mike,

Some time ago, I had the same problem as @zlite. My solution does keep the same functionality in Python 3.x, but is compatible with 2.x;

There is only one change in PyCmdMessenger.py and the diff is:

@@ -116,7 +116,7 @@
                               "?":self._recv_bool,
                               "g":self._recv_guess}

-    def send(self,cmd,*args,arg_formats=None):
+    def send(self,cmd,*args,**kwargs):
         """
         Send a command (which may or may not have associated arguments) to an
         arduino using the CmdMessage protocol.  The command and any parameters
@@ -127,6 +127,10 @@
         arg_formats supercedes formats specified on initialization.
         """

+        arg_formats = kwargs.pop('arg_formats', None)
+        if kwargs:
+            raise TypeError("'send()' got unexpected keyword arguments: {}".format(', '.join(kwargs.keys())))
+
         # Turn the command into an integer.
         try:
             command_as_int = self._cmd_name_to_int[cmd]

Maybe the setup.py must also be changed.

I don't know if you want to incorporate it into you code. I can understand it if you don't want to keep backward compatibility. Maybe you just want to add a comment what has to be changed for Python 2.x?

Best wishes, Stephan

harmsm commented 7 years ago

Good idea to use **kwargs. I'm going to incorporate the change and take away the python 3 requirement. Thanks for all of your input on the library.