Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import sublime, sublime_plugin
  2. import random
  3. import time
  4.  
  5. class PushidCommand(sublime_plugin.TextCommand):
  6. def run(self, edit):
  7. self.view.insert(edit, self.view.sel()[0].begin(), PushID().next_id())
  8.  
  9. class PushID(object):
  10. PUSH_CHARS = ('-0123456789'
  11. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  12. '_abcdefghijklmnopqrstuvwxyz')
  13.  
  14. def __init__(self):
  15. self.lastPushTime = 0
  16. self.lastRandChars = [0,0,0,0,0,0,0,0,0,0,0,0];
  17.  
  18. def next_id(self):
  19. now = int(time.time() * 1000)
  20. duplicateTime = (now == self.lastPushTime)
  21. self.lastPushTime = now
  22. timeStampChars = ["", "", "", "", "", "", "", ""]
  23.  
  24. for i in range(7, -1, -1):
  25. timeStampChars[i] = self.PUSH_CHARS[now % 64]
  26. now = int(now / 64)
  27.  
  28. if (now != 0):
  29. raise ValueError('We should have converted the entire timestamp.')
  30.  
  31. uid = ''.join(timeStampChars)
  32.  
  33. if not duplicateTime:
  34. for i in range(12):
  35. self.lastRandChars[i] = int(random.random() * 64)
  36. else:
  37. for i in range(11, -1, -1):
  38. if self.lastRandChars[i] == 63:
  39. self.lastRandChars[i] = 0
  40. else:
  41. break
  42. self.lastRandChars[i] += 1
  43.  
  44. for i in range(12):
  45. uid += self.PUSH_CHARS[self.lastRandChars[i]]
  46.  
  47. if len(uid) != 20:
  48. raise ValueError('Length should be 20.')
  49. return uid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement