def unescape_doctest(text):
 if (not contains_doctest(text)):
  return text
 code = u''
 for line in text.split(u'\n'):
  m = re.match(u'^\\s*(>>>|\\.\\.\\.) (.*)$', line)
  if m:
   code += (m.group(2) + u'\n')
  elif line.strip():
   code += ((u'# ' + line.strip()) + u'\n')
  else:
   code += u'\n'
 return code

def unescape_doctest ( text ) : if ( not contains_doctest ( text ) ) : return text code = u'' for line in text . split ( u'\n' ) : m = re . match ( u'^\\s*(>>>|\\.\\.\\.) (.*)$' , line ) if m : code += ( m . group ( 2 ) + u'\n' ) elif line . strip ( ) : code += ( ( u'# ' + line . strip ( ) ) + u'\n' ) else : code += u'\n' return code
extract code from a piece of text .