Advertisement
DaRealFreak

x

Nov 27th, 2018
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import re
  2. import timeit
  3.  
  4.  
  5. def deslugify(name):
  6.     """Deslugify function for player names from DDNet
  7.    https://github.com/ddnet/ddnet-scripts/blob/203fcb4241261ae8f006362303723e4546e0e7f7/servers/scripts/ddnet.py#L177
  8.    :type name: str
  9.    :return:
  10.    """
  11.     for special_char in re.findall('(-([\d]+)-)', name):
  12.         name = name.replace(special_char[0], unichr(int(special_char[1])))
  13.     return name.encode('utf-8')
  14.  
  15. def deslugify2(string):
  16.     try:
  17.         n = u''
  18.         t = 0
  19.         i = 0
  20.  
  21.         for c in string:
  22.             if t == 0:
  23.                 if c == '-':
  24.                     t = 1
  25.                 else:
  26.                     n += c
  27.             else:
  28.                 if c == '-':
  29.                     n += chr(i)
  30.                     t = 0
  31.                     i = 0
  32.                 else:
  33.                     i = i * 10 + int(c)
  34.         return n.encode('utf-8')
  35.     except:
  36.         return string
  37.  
  38.  
  39. def test1():
  40.     deslugify("https://ddnet.tw/players/meter-39--46-/")
  41.  
  42.  
  43. def test2():
  44.     deslugify2("https://ddnet.tw/players/meter-39--46-/")
  45.  
  46.  
  47. print('slugify replace: ' + str(timeit.timeit("test1()", setup="from __main__ import test1")))
  48. print('slugify ddnet: ' + str(timeit.timeit("test2()", setup="from __main__ import test2")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement