code: def _build_command(self, cmd, **kwargs):
          
          try:
              cmd_spec = self.api_commands[cmd]
          except AttributeError:
              raise NotImplementedError("API command specifications could not be "
                                        "found; use a derived class which defines"
                                        " 'api_commands'.")
  
          packet = b''
  
          for field in cmd_spec:
              try:
                  # Read this field's name from the function arguments dict
                  data = kwargs[field['name']]
                  if isinstance(data, str):
                      data = stringToBytes(data)
  
              except KeyError:
                  # Data wasn't given
                  # Only a problem if the field has a specific length
                  if field['len'] is not None:
                      # Was a default value specified?
                      default_value = field['default']
                      if default_value:
                          # If so, use it
                          data = default_value
                      else:
                          # Otherwise, fail
                          raise KeyError(
                              "The expected field {} of length {} "
                              "was not provided".format(
                                  field['name'], field['len']
                              )
                          )
                  else:
                      # No specific length, ignore it
                      data = None
  
              # Ensure that the proper number of elements will be written
              if field['len'] and len(data) != field['len']:
                  raise ValueError(
                      "The data provided for '{}' was not {} "
                      "bytes long".format(field['name'], field['len'])
                  )
  
              # Add the data to the packet, if it has been specified.
              # Otherwise, the parameter was of variable length, and not given.
              if data:
                  packet += data
  
          return packet
docs: _build_command: string (binary data) ... -> binary data
  
          _build_command will construct a command packet according to the
          specified command's specification in api_commands. It will expect
          named arguments for all fields other than those with a default
          value or a length of 'None'.
  
          Each field will be written out in the order they are defined
          in the command definition.