uid.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. import time
  2. from random import randint
  3. def uid(prefixe = ""):
  4. """construit un identifiant unique de 10 caracteres"""
  5. #representation de la date (annee/mois/jour/heure/min/seconde)
  6. base = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n", \
  7. "o","p","q","r","s","t","u","v","w","x","y","z", \
  8. "A","B","C","D","E","F","G","H","I","J","K","L", \
  9. "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", \
  10. "2","3","4","5","6","7","8","9"]
  11. dat = time.gmtime()
  12. a = dat[0] ; m = dat[1] ; j = dat[2] ; h = dat[3] ; mn = dat[4] ; s = dat[5]
  13. a_, m_, j_, h_, mn_, s_ = base[(int(a)-2000)], base[int(m)], base[int(j)], base[int(h)], base[int(mn)], base[int(s)]
  14. #ajout de 2 caracteres aleatoires
  15. c1 = base[randint(0,59)] ; c2 = base[randint(0,59)]
  16. #concatenation
  17. if len(prefixe) >= 2:
  18. p_ = prefixe[0:2]
  19. else:
  20. while len(prefixe) < 2:
  21. prefixe += "-"
  22. p_ = prefixe
  23. retour = "{}{}{}{}{}{}{}{}{}".format(p_, a_, m_, j_, h_, mn_, s_, c1, c2)
  24. return retour