DataConvert.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: UTF-8 -*-
  2. import decimal,math,time,datetime,random
  3. def str2int(strs):
  4. strr=filter(lambda x:(x.isdigit or x=='-'),str(strs))
  5. if strr==None:
  6. return 0
  7. elif strr=='':
  8. return 0
  9. else:
  10. return int(strr)
  11. def parseDecimal(strs,totalLen,apart):
  12. apt=strs.split(".",1)
  13. if len(apt) == 1:
  14. return decimal.Decimal(str(str2int(strs)))
  15. else:
  16. ipart=str(str2int(apt[0]))
  17. fpart=str(str2int(apt[1].replace('.','')))
  18. if(len(fpart)>apart):
  19. fptx=fpart[:apart]
  20. fpi=int(fptx)
  21. if(int(fpart[apart])>4):
  22. fpi=fpi+1
  23. else:
  24. fpi=int(fpart)
  25. if(len(ipart)>totalLen-apart):
  26. iptx='9'*(totalLen-apart)
  27. ipi=int(iptx)
  28. else:
  29. ipi=int(ipart)
  30. return decimal.Decimal(str(ipi)+'.'+str(fpi))
  31. def CheckPOST(needlist,postkeys):
  32. for i in needlist:
  33. if i not in postkeys:
  34. return i
  35. return ""
  36. def MakeSummary(text,length):
  37. # if len(text.decode('utf8','ignore'))>length:
  38. # text=text.decode('utf8','ignore')[:length].encode("utf8")+u"..."
  39. # return text
  40. if len(text)>length:
  41. text=text[:length]+u"..."
  42. return text
  43. def convertBytes(bytes,lst=['字节', 'KB', 'MB', 'GB', 'TB', 'PB']):
  44. i = int(math.floor(
  45. math.log(bytes, 1024)
  46. ))
  47. if i >= len(lst):
  48. i = len(lst) - 1
  49. return ('%.2f' + " " + lst[i]) % (bytes/math.pow(1024, i))
  50. def Str2Date(strdate):
  51. t = time.strptime(strdate,"%Y-%m-%d")
  52. return datetime.date(t.tm_year,t.tm_mon,t.tm_mday)
  53. def UniqueID():
  54. return hash(str(time.time())+str(random.random())) & 0x0fffffff