diff --git a/MkpWIFI/protocol.py b/MkpWIFI/protocol.py new file mode 100644 index 0000000..ad85272 --- /dev/null +++ b/MkpWIFI/protocol.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Fri Sep 27 14:35:31 2019 + +@author: Guido Longoni - guidolongoni@gmail.com +""" + +import commands as cmd +import re + + +def cmdString(cmdName, cmdArgs=None): + cmdDict = cmd.MKPcommands[cmdName] + return str(cmd.DEFprefix['begin'] if cmdDict['defPrefix'] else cmdDict['begin']) \ + + str(cmdDict['command']) \ + + str(';'.join([cmdArgs[k] for k in cmdDict['args']]) if cmdArgs is not None else '') \ + + str(cmd.DEFprefix['end'] + if cmdDict['defPrefix'] else cmdDict['end']) + + +def checkResponse(cmdName, response): + sucKey = 'success' + cmdDict = cmd.MKPcommands[cmdName] + if sucKey in cmdDict and cmdDict[sucKey] is not None: + if cmdDict[sucKey] == True: + error = checkError(cmdName, response) + if error is None: + return None + else: + return error + if re.match(cmdDict[sucKey], response, re.I): + return True + error = checkError(cmdName, response) + if error is None: + raise NotImplementedError( + 'Ricevuta risposta sconosciuta: {}'.format(response)) + else: + return error + else: + error = checkError(cmdName, response) + return error + + +def checkError(cmdName, response): + errKey = 'error' + cmdDict = cmd.MKPcommands[cmdName] + if errKey in cmdDict and cmdDict[errKey] is not None: + return None + error_codes = re.match(cmdDict[errKey], response, re.I) + if error_codes: + return error_codes.groups() + return None diff --git a/MkpWIFI/test.py b/MkpWIFI/test.py deleted file mode 100644 index dfcf517..0000000 --- a/MkpWIFI/test.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -Created on Fri Sep 27 14:35:31 2019 - -@author: Guido Longoni - guidolongoni@gmail.com -""" - -import commands as cmd -import re - -def cmdString(cmdName, cmdArgs=None): - cmdDict = cmd.MKPcommands[cmdName] - return str(cmd.DEFprefix['begin'] if cmdDict['defPrefix'] else cmdDict['begin']) \ - + str(cmdDict['command']) \ - + str(';'.join([cmdArgs[k] for k in cmdDict['args']]) if cmdArgs is not None else '') \ - + str(cmd.DEFprefix['end'] if cmdDict['defPrefix'] else cmdDict['end']) - -def checkResponse(cmdName,response): - sucKey = 'success' - cmdDict = cmd.MKPcommands[cmdName] - if sucKey in cmdDict and cmdDict[sucKey] is not None: - if cmdDict[sucKey] == True: - error = checkError(cmdName,response) - if error is None: - return None - else: - return error - if re.match(cmdDict[sucKey],response,re.I): - return True - error = checkError(cmdName,response) - if error is None: - raise NotImplementedError('Ricevuta risposta sconosciuta: {}'.format(response)) - else: - return error - else: - error = checkError(cmdName,response) - return error - -def checkError(cmdName,response): - errKey = 'error' - cmdDict = cmd.MKPcommands[cmdName] - if errKey in cmdDict and cmdDict[errKey] is not None: - return None - error_codes = re.match(cmdDict[errKey],response,re.I) - if error_codes: - return error_codes.groups() - return None \ No newline at end of file diff --git a/MkpWIFI/test_filtro.py b/MkpWIFI/test_filtro.py index 75ae2f7..ef603c8 100644 --- a/MkpWIFI/test_filtro.py +++ b/MkpWIFI/test_filtro.py @@ -7,14 +7,15 @@ Created on Fri Sep 27 11:21:37 2019 """ regole = [ - (lambda x: isinstance(x, int) and x > 0, 'è un intero positivo'), - (lambda x: isinstance(x, int) and x < 0, 'è un intero negativo'), - (lambda x: isinstance(x, float) and x > 0, 'è un reale positivo'), - (lambda x: isinstance(x, float) and x < 0, 'è un reale negativo'), - (lambda x: True, 'non è un numero'), + (lambda x: isinstance(x, int) and x > 0, 'è un intero positivo'), + (lambda x: isinstance(x, int) and x < 0, 'è un intero negativo'), + (lambda x: isinstance(x, float) and x > 0, 'è un reale positivo'), + (lambda x: isinstance(x, float) and x < 0, 'è un reale negativo'), + (lambda x: True, 'non è un numero'), ] -test = [1,-1,'ciao',3.14,-0.5] +test = [1, -1, 'ciao', 3.14, -0.5] -risultato_filtro = [str(dato)+' '+next(regola[1] for regola in regole if regola[0](dato)) for dato in test] +risultato_filtro = [str(dato)+' '+next(regola[1] + for regola in regole if regola[0](dato)) for dato in test]