utils.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #coding: utf-8
  2. #修正输入的文件路径,输入路径的标准格式:abc,不需要前后置的路径符号
  3. def FixFilePath(OutputPath):
  4. if callable(OutputPath):
  5. try:
  6. OutputPath=OutputPath()
  7. except Exception:
  8. OutputPath=""
  9. if len(OutputPath)>0:
  10. if OutputPath[-1]!="/":OutputPath="%s/" % OutputPath
  11. return OutputPath
  12. #在上传的文件名后面追加一个日期时间+随机,如abc.jpg--> abc_20120801202409.jpg
  13. def GenerateRndFilename(filename):
  14. import datetime
  15. import random
  16. from os.path import splitext
  17. f_name,f_ext=splitext(filename)
  18. return "%s_%s%s.%s" % (f_name, datetime.datetime.now().strftime("%Y%m%d_%H%M%S_"),random.randrange(10,99),f_ext)
  19. #文件大小类
  20. class FileSize():
  21. SIZE_UNIT={"Byte":1,"KB":1024,"MB":1048576,"GB":1073741824,"TB":1099511627776L}
  22. def __init__(self,size):
  23. self.size=long(FileSize.Format(size))
  24. @staticmethod
  25. def Format(size):
  26. import re
  27. if isinstance(size,int) or isinstance(size,long):
  28. return size
  29. else:
  30. if not isinstance(size,str):
  31. return 0
  32. else:
  33. oSize=size.lstrip().upper().replace(" ","")
  34. pattern=re.compile(r"(\d*\.?(?=\d)\d*)(byte|kb|mb|gb|tb)",re.I)
  35. match=pattern.match(oSize)
  36. if match:
  37. m_size, m_unit=match.groups()
  38. if m_size.find(".")==-1:
  39. m_size=long(m_size)
  40. else:
  41. m_size=float(m_size)
  42. if m_unit!="BYTE":
  43. return m_size*FileSize.SIZE_UNIT[m_unit]
  44. else:
  45. return m_size
  46. else:
  47. return 0
  48. #返回字节为单位的值
  49. @property
  50. def size(self):
  51. return self.size
  52. @size.setter
  53. def size(self,newsize):
  54. try:
  55. self.size=long(newsize)
  56. except:
  57. self.size=0
  58. #返回带单位的自动值
  59. @property
  60. def FriendValue(self):
  61. if self.size<FileSize.SIZE_UNIT["KB"]:
  62. unit="Byte"
  63. elif self.size<FileSize.SIZE_UNIT["MB"]:
  64. unit="KB"
  65. elif self.size<FileSize.SIZE_UNIT["GB"]:
  66. unit="MB"
  67. elif self.size<FileSize.SIZE_UNIT["TB"]:
  68. unit="GB"
  69. else:
  70. unit="TB"
  71. if (self.size % FileSize.SIZE_UNIT[unit])==0:
  72. return "%s%s" % ((self.size / FileSize.SIZE_UNIT[unit]),unit)
  73. else:
  74. return "%0.2f%s" % (round(float(self.size) /float(FileSize.SIZE_UNIT[unit]) ,2),unit)
  75. def __str__(self):
  76. return self.FriendValue
  77. #相加
  78. def __add__(self, other):
  79. if isinstance(other,FileSize):
  80. return FileSize(other.size+self.size)
  81. else:
  82. return FileSize(FileSize(other).size+self.size)
  83. def __sub__(self, other):
  84. if isinstance(other,FileSize):
  85. return FileSize(self.size-other.size)
  86. else:
  87. return FileSize(self.size-FileSize(other).size)
  88. def __gt__(self, other):
  89. if isinstance(other,FileSize):
  90. if self.size>other.size:
  91. return True
  92. else:
  93. return False
  94. else:
  95. if self.size>FileSize(other).size:
  96. return True
  97. else:
  98. return False
  99. def __lt__(self, other):
  100. if isinstance(other,FileSize):
  101. if other.size>self.size:
  102. return True
  103. else:
  104. return False
  105. else:
  106. if FileSize(other).size > self.size:
  107. return True
  108. else:
  109. return False
  110. def __ge__(self, other):
  111. if isinstance(other,FileSize):
  112. if self.size>=other.size:
  113. return True
  114. else:
  115. return False
  116. else:
  117. if self.size>=FileSize(other).size:
  118. return True
  119. else:
  120. return False
  121. def __le__(self, other):
  122. if isinstance(other,FileSize):
  123. if other.size>=self.size:
  124. return True
  125. else:
  126. return False
  127. else:
  128. if FileSize(other).size >= self.size:
  129. return True
  130. else:
  131. return False