Advertisement
Joeytje50

Untitled

Dec 20th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. #FILE: temp.py:
  2.  
  3. import re
  4. from bs4 import BeautifulSoup
  5.  
  6. def reBIReplace(txt):
  7.     txt = re.sub(r"</?b>","'''",txt)
  8.     txt = re.sub(r"</?i>","''",txt)
  9.     return txt
  10.  
  11. def soupBIReplace(txt):
  12.     soup = BeautifulSoup(txt)
  13.     btag = soup.b
  14.     itag = soup.i
  15.     txt = txt.replace(str(btag), "'''"+str(btag.string)+"'''")
  16.     txt = txt.replace(str(itag), "''"+str(itag.string)+"''")
  17.     return txt
  18.  
  19. #SHELL:
  20.  
  21. >>> import timeit
  22. >>> timeit.Timer('temp.reBIReplace("foo <b>bar</b> <i>baz</i>")','import temp').timeit(100)
  23. 0.0029594631914433072
  24. >>> timeit.Timer('temp.soupBIReplace("foo <b>bar</b> <i>baz</i>")','import temp').timeit(100)
  25. 0.08923220030303725
  26. >>> print "regex is",0.08923220030303725 / 0.0029594631914433072,"times faster than soup."
  27. regex is 30.151481715 times faster than soup.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement