Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if element.isdigit():
- newelement=int(element)
- partition=element.partition('.')
- 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]==''):
- newelement=float(element)
- try:
- float(element)
- except ValueError:
- print "Not a float"
- import re
- if re.match("^d+?.d+?$", element) is None:
- print "Not float"
- def is_float_try(str):
- try:
- float(str)
- return True
- except ValueError:
- return False
- import re
- _float_regexp = re.compile(r"^[-+]?(?:b[0-9]+(?:.[0-9]*)?|.[0-9]+b)(?:[eE][-+]?[0-9]+b)?$")
- def is_float_re(str):
- return re.match(_float_regexp, str)
- def is_float_partition(element):
- partition=element.partition('.')
- if (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and pa
- rtition[2].isdigit()) or (partition[0].isdigit() and partition[1]=='.' and partition[2]==''):
- return True
- if __name__ == '__main__':
- import unittest
- import timeit
- class ConvertTests(unittest.TestCase):
- def test_re(self):
- self.failUnless(is_float_re("20e2"))
- def test_try(self):
- self.failUnless(is_float_try("20e2"))
- def test_re_perf(self):
- print
- print 're sad:', timeit.Timer('floatstr.is_float_re("12.2x")', "import floatstr").timeit()
- print 're happy:', timeit.Timer('floatstr.is_float_re("12.2")', "import floatstr").timeit()
- def test_try_perf(self):
- print
- print 'try sad:', timeit.Timer('floatstr.is_float_try("12.2x")', "import floatstr").timeit()
- print 'try happy:', timeit.Timer('floatstr.is_float_try("12.2")', "import floatstr").timeit()
- def test_partition_perf(self):
- print
- print 'partition sad:', timeit.Timer('floatstr.is_float_partition("12.2x")', "import floatstr").timeit()
- print 'partition happy:', timeit.Timer('floatstr.is_float_partition("12.2")', "import floatstr").timeit()
- def test_partition(self):
- self.failUnless(is_float_partition("20e2"))
- def test_partition2(self):
- self.failUnless(is_float_partition(".2"))
- def test_partition3(self):
- self.failIf(is_float_partition("1234x.2"))
- unittest.main()
- def is_float_try(str):
- try:
- float(str)
- return True
- except ValueError:
- return False
- import re
- _float_regexp = re.compile(r"^[-+]?(?:b[0-9]+(?:.[0-9]*)?|.[0-9]+b)(?:[eE][-+]?[0-9]+b)?$")
- def is_float_re(str):
- return re.match(_float_regexp, str)
- def is_float_partition(element):
- partition=element.partition('.')
- if (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and pa
- rtition[2].isdigit()) or (partition[0].isdigit() and partition[1]=='.' and partition[2]==''):
- return True
- if __name__ == '__main__':
- import unittest
- import timeit
- class ConvertTests(unittest.TestCase):
- def test_re(self):
- self.failUnless(is_float_re("20e2"))
- def test_try(self):
- self.failUnless(is_float_try("20e2"))
- def test_re_perf(self):
- print
- print 're sad:', timeit.Timer('floatstr.is_float_re("12.2x")', "import floatstr").timeit()
- print 're happy:', timeit.Timer('floatstr.is_float_re("12.2")', "import floatstr").timeit()
- def test_try_perf(self):
- print
- print 'try sad:', timeit.Timer('floatstr.is_float_try("12.2x")', "import floatstr").timeit()
- print 'try happy:', timeit.Timer('floatstr.is_float_try("12.2")', "import floatstr").timeit()
- def test_partition_perf(self):
- print
- print 'partition sad:', timeit.Timer('floatstr.is_float_partition("12.2x")', "import floatstr").timeit()
- print 'partition happy:', timeit.Timer('floatstr.is_float_partition("12.2")', "import floatstr").timeit()
- def test_partition(self):
- self.failUnless(is_float_partition("20e2"))
- def test_partition2(self):
- self.failUnless(is_float_partition(".2"))
- def test_partition3(self):
- self.failIf(is_float_partition("1234x.2"))
- unittest.main()
- ^[-+]?(?:b[0-9]+(?:.[0-9]*)?|.[0-9]+b)(?:[eE][-+]?[0-9]+b)?$
Add Comment
Please, Sign In to add comment