Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import timeit
- def deslugify(name):
- """Deslugify function for player names from DDNet
- https://github.com/ddnet/ddnet-scripts/blob/203fcb4241261ae8f006362303723e4546e0e7f7/servers/scripts/ddnet.py#L177
- :type name: str
- :return:
- """
- for special_char in re.findall('(-([\d]+)-)', name):
- name = name.replace(special_char[0], unichr(int(special_char[1])))
- return name.encode('utf-8')
- def deslugify2(string):
- try:
- n = u''
- t = 0
- i = 0
- for c in string:
- if t == 0:
- if c == '-':
- t = 1
- else:
- n += c
- else:
- if c == '-':
- n += chr(i)
- t = 0
- i = 0
- else:
- i = i * 10 + int(c)
- return n.encode('utf-8')
- except:
- return string
- def test1():
- deslugify("https://ddnet.tw/players/meter-39--46-/")
- def test2():
- deslugify2("https://ddnet.tw/players/meter-39--46-/")
- print('slugify replace: ' + str(timeit.timeit("test1()", setup="from __main__ import test1")))
- print('slugify ddnet: ' + str(timeit.timeit("test2()", setup="from __main__ import test2")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement