Last active
May 18, 2021 00:43
-
-
Save likangwei/21ffc2855bcc2221c0cd to your computer and use it in GitHub Desktop.
python utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
__author__ = 'likangwei' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
__author__ = 'likangwei' | |
import socket | |
def get_ip_address(): | |
return socket.gethostbyname(socket.gethostname())#得到本地ip | |
print get_ip_address() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
__author__ = 'likangwei' | |
#!/usr/bin/env python | |
# -*- coding: cp936 -*- | |
import hashlib | |
def get_md5_value(src): | |
myMd5 = hashlib.md5() | |
myMd5.update(src) | |
myMd5_Digest = myMd5.hexdigest() | |
return myMd5_Digest | |
def get_sha1_value(src): | |
mySha1 = hashlib.sha1() | |
mySha1.update(src) | |
mySha1_Digest = mySha1.hexdigest() | |
return mySha1_Digest | |
if __name__== '__main__': | |
src = 'aaa' | |
result_md5_value = get_md5_value(src) | |
result_sha1_value = get_sha1_value(src) | |
print 'source string: ', src | |
print 'MD5: ', result_md5_value | |
print 'SHA1: ', result_sha1_value | |
print __file__.startswith("") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'hanzhao' | |
# -*- coding=utf-8 -*- | |
import re | |
def is_word(text): | |
patstr = r'^[a-zA-Z-]{2,50}$' | |
return re.match(patstr, text) | |
if __name__== '__main__': | |
print is_word("a") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'hanzhao' | |
# -*- coding=utf-8 -*- | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
SPLIT_STR_LIST = [' ', '\n', '.', '?', '!'] | |
UNICODE_SPLIT_STR_LIST = [] | |
for word in SPLIT_STR_LIST: | |
UNICODE_SPLIT_STR_LIST.append(word.decode()) | |
def get_split_words(lines, with_blank_space=True): | |
if isinstance(lines, str): | |
result = [lines] | |
elif isinstance(lines, unicode): | |
result = lines | |
elif lines is None: | |
return [] | |
else: | |
raise Exception(lines) | |
for split_word in SPLIT_STR_LIST: | |
result = get_split_word(result, split_word) | |
return result | |
def change_unicode_2_str(unicode_str): | |
if isinstance(unicode_str, unicode): | |
return unicode_str.encode() | |
elif isinstance(unicode_str, str): | |
return unicode_str | |
else: | |
raise Exception("a") | |
def get_split_word(lines, split_word): | |
""" | |
分词 get_split_words(" a b \n") return [' ', 'a', ' ', ' ', 'b', ' ', '\n'] | |
:param lines: | |
:param split_word: | |
:return: | |
""" | |
if isinstance(lines, str) or isinstance(lines, unicode): | |
lines = [lines] | |
result = [] | |
# print lines | |
for line in lines: | |
from_idx = 0 | |
end_idx = 0 | |
while end_idx < len(line): | |
find_idx = line.find(split_word, end_idx) | |
if find_idx != -1: | |
if from_idx < find_idx: | |
end_idx = find_idx | |
result.append(line[from_idx:end_idx]) | |
from_idx = find_idx | |
end_idx = from_idx + len(split_word) | |
result.append(line[from_idx:end_idx]) | |
from_idx = end_idx | |
else: | |
result.append(line[end_idx:len(line)]) | |
break | |
return result | |
if __name__== '__main__': | |
v = u'Django provides an abstraction layer (the “models”) for structuring and\nmanipulating the data of your Web application. Learn more about it below:' | |
print get_split_words(v) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cp ~/PycharmProjects/baymax/util/*.py ./ | |
git status | |
git add IpUtil.py | |
git commit -m "test" | |
git push | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment