Guest User

Untitled

a guest
Jun 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. if element.isdigit():
  2. newelement=int(element)
  3.  
  4. partition=element.partition('.')
  5. if (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and partition[2].isdigit()) or (partition[0].isdigit() and partition[1]=='.' and partition[2]==''):
  6. newelement=float(element)
  7.  
  8. try:
  9. float(element)
  10. except ValueError:
  11. print "Not a float"
  12.  
  13. import re
  14. if re.match("^d+?.d+?$", element) is None:
  15. print "Not float"
  16.  
  17. def is_float_try(str):
  18. try:
  19. float(str)
  20. return True
  21. except ValueError:
  22. return False
  23.  
  24. import re
  25. _float_regexp = re.compile(r"^[-+]?(?:b[0-9]+(?:.[0-9]*)?|.[0-9]+b)(?:[eE][-+]?[0-9]+b)?$")
  26. def is_float_re(str):
  27. return re.match(_float_regexp, str)
  28.  
  29.  
  30. def is_float_partition(element):
  31. partition=element.partition('.')
  32. if (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and pa
  33. rtition[2].isdigit()) or (partition[0].isdigit() and partition[1]=='.' and partition[2]==''):
  34. return True
  35.  
  36. if __name__ == '__main__':
  37. import unittest
  38. import timeit
  39.  
  40. class ConvertTests(unittest.TestCase):
  41. def test_re(self):
  42. self.failUnless(is_float_re("20e2"))
  43.  
  44. def test_try(self):
  45. self.failUnless(is_float_try("20e2"))
  46.  
  47. def test_re_perf(self):
  48. print
  49. print 're sad:', timeit.Timer('floatstr.is_float_re("12.2x")', "import floatstr").timeit()
  50. print 're happy:', timeit.Timer('floatstr.is_float_re("12.2")', "import floatstr").timeit()
  51.  
  52. def test_try_perf(self):
  53. print
  54. print 'try sad:', timeit.Timer('floatstr.is_float_try("12.2x")', "import floatstr").timeit()
  55. print 'try happy:', timeit.Timer('floatstr.is_float_try("12.2")', "import floatstr").timeit()
  56.  
  57. def test_partition_perf(self):
  58. print
  59. print 'partition sad:', timeit.Timer('floatstr.is_float_partition("12.2x")', "import floatstr").timeit()
  60. print 'partition happy:', timeit.Timer('floatstr.is_float_partition("12.2")', "import floatstr").timeit()
  61.  
  62. def test_partition(self):
  63. self.failUnless(is_float_partition("20e2"))
  64.  
  65. def test_partition2(self):
  66. self.failUnless(is_float_partition(".2"))
  67.  
  68. def test_partition3(self):
  69. self.failIf(is_float_partition("1234x.2"))
  70.  
  71. unittest.main()
  72.  
  73. def is_float_try(str):
  74. try:
  75. float(str)
  76. return True
  77. except ValueError:
  78. return False
  79.  
  80. import re
  81. _float_regexp = re.compile(r"^[-+]?(?:b[0-9]+(?:.[0-9]*)?|.[0-9]+b)(?:[eE][-+]?[0-9]+b)?$")
  82. def is_float_re(str):
  83. return re.match(_float_regexp, str)
  84.  
  85.  
  86. def is_float_partition(element):
  87. partition=element.partition('.')
  88. if (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and pa
  89. rtition[2].isdigit()) or (partition[0].isdigit() and partition[1]=='.' and partition[2]==''):
  90. return True
  91.  
  92. if __name__ == '__main__':
  93. import unittest
  94. import timeit
  95.  
  96. class ConvertTests(unittest.TestCase):
  97. def test_re(self):
  98. self.failUnless(is_float_re("20e2"))
  99.  
  100. def test_try(self):
  101. self.failUnless(is_float_try("20e2"))
  102.  
  103. def test_re_perf(self):
  104. print
  105. print 're sad:', timeit.Timer('floatstr.is_float_re("12.2x")', "import floatstr").timeit()
  106. print 're happy:', timeit.Timer('floatstr.is_float_re("12.2")', "import floatstr").timeit()
  107.  
  108. def test_try_perf(self):
  109. print
  110. print 'try sad:', timeit.Timer('floatstr.is_float_try("12.2x")', "import floatstr").timeit()
  111. print 'try happy:', timeit.Timer('floatstr.is_float_try("12.2")', "import floatstr").timeit()
  112.  
  113. def test_partition_perf(self):
  114. print
  115. print 'partition sad:', timeit.Timer('floatstr.is_float_partition("12.2x")', "import floatstr").timeit()
  116. print 'partition happy:', timeit.Timer('floatstr.is_float_partition("12.2")', "import floatstr").timeit()
  117.  
  118. def test_partition(self):
  119. self.failUnless(is_float_partition("20e2"))
  120.  
  121. def test_partition2(self):
  122. self.failUnless(is_float_partition(".2"))
  123.  
  124. def test_partition3(self):
  125. self.failIf(is_float_partition("1234x.2"))
  126.  
  127. unittest.main()
  128.  
  129. ^[-+]?(?:b[0-9]+(?:.[0-9]*)?|.[0-9]+b)(?:[eE][-+]?[0-9]+b)?$
Add Comment
Please, Sign In to add comment