程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python utility functions

編輯:Python

Splicing URL function

url = 'http://localhost.com'
# Splicing URL function 
def makeurl(*res):
lst_res = list(res)
for i in range(0,len(lst_res)):
res = lst_res[i]
if res.endswith('/') or res.startswith('/'):
lst_res[i] == res.strip('/')
lst_res.insert(0,url)
return '/'.join(lst_res)
url = makeurl('ceshi','logins')

Request to retry decorator

# Connect retry decorator 
def retry(exception=Timeout,max_times=3):
def inner(func):
def warp(*args,**kwargs):
for try_time in range(max_times):
try:
return func(*args,**kwargs)
except exception as e:
if try_time == max_times -1:
raise e
time.sleep(3)
print(' Retry the last request ')
continue
return warp
return inner

Copy file

# Copy file 
def copy_file(source_file, des_file):
""" Copy file """
from shutil import copyfile
import sys
try:
copyfile(source_file, des_file)
except IOError as e:
print("Unable to copy file. %s" % e)
raise e
except Exception as e:
print("Unexpected error:", sys.exc_info())
raise e
copy_file(source_file, des_file)

Create folder

# Create folder 
def makedir(path):
# Remove the leading and trailing spaces 
path = path.strip()
# Remove the tail \ Symbol 
path.rstrip('\\')
if not os.path.exists(path):
os.makedirs(path)
return path

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved