Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: bengy en 3 Septiembre 2016, 22:41 pm



Título: ayuda a comentar codigo python
Publicado por: bengy en 3 Septiembre 2016, 22:41 pm
este codigo es de github https://github.com/overxfl0w/Typing-Recognition se trata de identificacion por dinamica de tecleo y me gustaria comentarlo para poder entender mejor, en especial la funcion _nErrors
Código
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import win32api
  5. import win32con
  6. import sys
  7. import time
  8. import pickle
  9.  
  10. def _clear():
  11.    for i in xrange(30): print "\n"
  12.  
  13. def _nErrors(a,b,B,I,S):
  14. V = {}
  15. V[0,0] = 0
  16. for i in xrange(1,len(b)+1): V[0,i] = i
  17. for i in xrange(1,len(a)+1): V[i,0] = i
  18. for i in xrange(1,len(a)+1):
  19. for j in xrange(1,len(b)+1):
  20. V[i,j] = min(V[i-1,j]+B(a,i-1),V[i,j-1]+I(b,j-1),V[i-1,j-1]+S(a,i-1,b,j-1))
  21. return V[len(a),len(b)]
  22.  
  23. def _generateSample():
  24.    _clear()
  25.    print u"Teclea: El veloz 1 murcielago hindu 2 comia feliz cardillo 3 y kiwi 5. La cigueña tocaba el 6 saxofon 1 detras del palenque 9 de paja. - Escrutinio 1337"
  26.    m,sentence,times,l,replace,it,et,p       = 0,[],[],0,{190:46,188:44,189:45,192:241},0,0,0
  27.    gs = u"El veloz 1 murcielago hindu 2 comia feliz cardillo 3 y kiwi 5. La cigueña tocaba el 6 saxofon 1 detras del palenque 9 de paja. - Escrutinio 1337"
  28.    es = ""
  29.    ls = len(gs)
  30.    while True and l!=ls:
  31. for i in xrange(256):
  32.    k = win32api.GetAsyncKeyState(i)
  33.    if k==-32767:
  34. if i==16 or i==160: m=1
  35. elif i>=65 and i<=90:
  36.    if m==1: sentence.append(unichr(i))
  37.    else:    sentence.append(unichr(i+32))
  38.    et = time.clock()
  39.    times.append(et-it)
  40.    m,l,it = 0,l+1,et
  41. else:
  42.    if i==8 and len(sentence)>0 and len(times)>0: sentence = sentence[:-1] ; l = max(l-1,0) ; times = times[:-1]
  43.    else:
  44. if i!=1 and i!=13:
  45.    sentence.append(unichr(i) if i not in replace else unichr(replace[i]))
  46.    et = time.clock()
  47.    times.append(et-it)
  48.    l,it = l+1,et
  49. _clear()
  50. print gs
  51. sys.stdout.write("".join(sentence))
  52. sys.stdout.flush()
  53.    es = "".join(sentence)
  54.    nErrors = _nErrors(gs,es,lambda x,i:1,lambda x,i:1,lambda x,i,y,j: 0 if x[i]==y[j] else 1)
  55.    v = times+[nErrors]
  56.    return v
  57.  
  58. if __name__ == "__main__":
  59.    if len(sys.argv)>1:
  60. v = _generateSample()
  61. with open(sys.argv[1],"wb") as fd: pickle.dump(v,fd)
  62.    else: print "Especifica el nombre del fichero para almacenar el patron"
  63.  
  64.