Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_slug():
hans = '中心'
assert slug(hans) == 'zhong-xin'
assert slug(hans, heteronym=True) == 'zhong-xin'
def test_slug():
hans = '中心'
assert slug(hans) == 'zhong-xin'
assert slug(hans, heteronym=True) == 'zhong-xin'
def test_simple_seg():
data = {
'北京abcc': 'be3i ji1ng abcc',
'你好にほんごРусский язык': 'ni3 ha3o にほんごРусский язык',
}
for h, p in data.items():
assert slug([h], style=TONE2, separator=' ') == p
hans = '你好にほんごРусский язык'
ret = 'ni3 ha3o'
assert slug(hans, style=TONE2, separator=' ', errors=lambda x: None) == ret
"tmp_auth_code": data['code']}
)
try:
response = response.json()
if response["errcode"] == 0:
try:
user = UserProfile.objects.get(unionid=response['user_info']['unionid'])
user = User.objects.get(id=user.user_id)
except Exception as e:
password = make_password('admin')
with transaction.atomic():
try:
user = User.objects.create(username=pypinyin.slug(response['user_info']['nick'], separator=''), password=password,
first_name=response['user_info']['nick'])
except Exception as e:
user = User.objects.create(username=pypinyin.slug(response['user_info']['nick'], separator='') + str(random.randint(0,9999)),
password=password,
first_name=response['user_info']['nick'])
UserProfile.objects.create(user=user, openId=response['user_info']['openid'], unionid=response['user_info']['unionid'])
data = TokenSerializer(Token.objects.get(user=user)).data
data["userphoto"] = '/file/userphoto.jpg'
return JsonResponse(data=data, code="999999", msg="成功")
else:
return JsonResponse(code="999998", msg='登录失败!')
except:
return JsonResponse(code="999998", msg='登录失败!')
response = requests.post(
url='https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature={}×tamp={}&accessKey=dingoapfjxo0dzezwe47sy'.format(
parse.quote(signature(timestamp)), timestamp), json={
"tmp_auth_code": data['code']}
)
try:
response = response.json()
if response["errcode"] == 0:
try:
user = UserProfile.objects.get(unionid=response['user_info']['unionid'])
user = User.objects.get(id=user.user_id)
except Exception as e:
password = make_password('admin')
with transaction.atomic():
try:
user = User.objects.create(username=pypinyin.slug(response['user_info']['nick'], separator=''), password=password,
first_name=response['user_info']['nick'])
except Exception as e:
user = User.objects.create(username=pypinyin.slug(response['user_info']['nick'], separator='') + str(random.randint(0,9999)),
password=password,
first_name=response['user_info']['nick'])
UserProfile.objects.create(user=user, openId=response['user_info']['openid'], unionid=response['user_info']['unionid'])
data = TokenSerializer(Token.objects.get(user=user)).data
data["userphoto"] = '/file/userphoto.jpg'
return JsonResponse(data=data, code="999999", msg="成功")
else:
return JsonResponse(code="999998", msg='登录失败!')
except:
return JsonResponse(code="999998", msg='登录失败!')
def test_simple_seg():
data = {
'北京abcc': 'be3i ji1ng abcc',
'你好にほんごРусский язык': 'ni3 ha3o にほんごРусский язык',
}
for h, p in data.items():
assert slug([h], style=TONE2, separator=' ') == p
hans = '你好にほんごРусский язык'
ret = 'ni3 ha3o'
assert slug(hans, style=TONE2, separator=' ', errors=lambda x: None) == ret
def showlist(self, event):
keywd = self.keywdbox.get().strip()
if keywd:
self.listbox.delete(0, END)
# print(urllist)
for item in self.urllist:
cond_1 = keywd.lower() in item.lower()
cond_2 = keywd.lower() in pypinyin.slug(item.lower(), separator='')
cond_3 = keywd.lower() in pypinyin.slug(item.lower(), style=Style.FIRST_LETTER, separator='')
if any([cond_1, cond_2, cond_3]):
self.listbox.insert(END, item) # 加载搜索结果
else:
self.listbox.delete(0, END)
for item in self.urllist:
self.listbox.insert(END, item) # 空字符时,加载所有列表
def showlist(self, event):
keywd = self.keywdbox.get().strip()
if keywd:
self.listbox.delete(0, END)
# print(urllist)
for item in self.urllist:
cond_1 = keywd.lower() in item.lower()
cond_2 = keywd.lower() in pypinyin.slug(item.lower(), separator='')
cond_3 = keywd.lower() in pypinyin.slug(item.lower(), style=Style.FIRST_LETTER, separator='')
if any([cond_1, cond_2, cond_3]):
self.listbox.insert(END, item) # 加载搜索结果
else:
self.listbox.delete(0, END)
for item in self.urllist:
self.listbox.insert(END, item) # 空字符时,加载所有列表
'FINALS': pypinyin.Style.FINALS,
'ao': pypinyin.Style.FINALS,
'FINALS_TONE': pypinyin.Style.FINALS_TONE,
'4ao': pypinyin.Style.FINALS_TONE,
'FINALS_TONE2': pypinyin.Style.FINALS_TONE2,
'a4o': pypinyin.Style.FINALS_TONE2,
'FINALS_TONE3': pypinyin.Style.FINALS_TONE3,
'ao4': pypinyin.Style.FINALS_TONE3,
'BOPOMOFO': pypinyin.Style.BOPOMOFO,
'BOPOMOFO_FIRST': pypinyin.Style.BOPOMOFO_FIRST,
'CYRILLIC': pypinyin.Style.CYRILLIC,
'CYRILLIC_FIRST': pypinyin.Style.CYRILLIC_FIRST,
}
func_map = {
'pinyin': pypinyin.pinyin,
'slug': pypinyin.slug,
}
default_style = 'zh4ao'
class NullWriter(object):
"""数据流黑洞,类似 linux/unix 下 /dev/null 的效果。"""
def write(self, string):
pass
def get_parser():
parser = ArgumentParser(description='convert chinese to pinyin.')
parser.add_argument('-V', '--version', action='version',
version='{0} {1}'.format(
pypinyin.__title__, pypinyin.__version__
))