Created
June 23, 2014 15:10
-
-
Save lidavidm/37a0a27582805872334d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from HTMLParser import HTMLParser | |
class MultiResultParser(HTMLParser): | |
def __init__(self): | |
HTMLParser.__init__(self) | |
self.results = [[]] | |
self.tag_type = None | |
def handle_starttag(self, tag, attrs): | |
attrs = dict(attrs) | |
if tag == 'div' and attrs.get('class') == 'cell_input': | |
self.tag_type = 'cell_input' | |
elif tag == 'script' and attrs.get('type') == 'math/tex; mode=display': | |
self.tag_type = 'latex' | |
def handle_data(self, data): | |
if self.tag_type == 'cell_input': | |
self.results[-1].append(data) | |
elif self.tag_type == 'latex': | |
self.results[-1].append(data) | |
self.results.append([]) | |
self.tag_type = None | |
case1 = """<ul> | |
<li><script type="math/tex; mode=display">4</script></li> | |
<li><script type="math/tex; mode=display" data-numeric="true" data-output-repr="-2 - 2*sqrt(3)*I" data-approximation="-2.0 - 3.46410161513775 i">-2 - 2 \sqrt{3} i</script></li> | |
<li><script type="math/tex; mode=display" data-numeric="true" data-output-repr="-2 + 2*sqrt(3)*I" data-approximation="-2.0 + 3.46410161513775 i">-2 + 2 \sqrt{3} i</script></li> | |
</ul>""" | |
case2 = """<ul> | |
<li> | |
<div class="cell_input"> | |
integrate(exp(x)/(exp(2*x) + 1), x) | |
</div> | |
<script type="math/tex; mode=display">\operatorname{RootSum} {\left(4 z^{2} + 1, \Lambda {\left (i, i \log{\left (2 i + e^{x} \right )} \right )}\right)}</script> | |
</li> | |
<li> | |
<div class="cell_input"> | |
sympy.integrals.manualintegrate(exp(x)/(exp(2*x) + 1), x) | |
</div> | |
<script type="math/tex; mode=display">\operatorname{atan}{\left (e^{x} \right )}</script> | |
</li> | |
</ul>""" | |
parser = MultiResultParser() | |
parser.feed(case1) | |
print "Case 1:" | |
print case1 | |
print "Results:" | |
print parser.results | |
parser = MultiResultParser() | |
parser.feed(case2) | |
print "Case 2:" | |
print case2 | |
print "Results:" | |
print parser.results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Case 1: | |
<ul> | |
<li><script type="math/tex; mode=display">4</script></li> | |
<li><script type="math/tex; mode=display" data-numeric="true" data-output-repr="-2 - 2*sqrt(3)*I" data-approximation="-2.0 - 3.46410161513775 i">-2 - 2 \sqrt{3} i</script></li> | |
<li><script type="math/tex; mode=display" data-numeric="true" data-output-repr="-2 + 2*sqrt(3)*I" data-approximation="-2.0 + 3.46410161513775 i">-2 + 2 \sqrt{3} i</script></li> | |
</ul> | |
Results: | |
[['4'], ['-2 - 2 \\sqrt{3} i'], ['-2 + 2 \\sqrt{3} i'], []] | |
Case 2: | |
<ul> | |
<li> | |
<div class="cell_input"> | |
integrate(exp(x)/(exp(2*x) + 1), x) | |
</div> | |
<script type="math/tex; mode=display">\operatorname{RootSum} {\left(4 z^{2} + 1, \Lambda {\left (i, i \log{\left (2 i ight)}</script> | |
</li> | |
<li> | |
<div class="cell_input"> | |
sympy.integrals.manualintegrate(exp(x)/(exp(2*x) + 1), x) | |
</div> | |
ight )}</script>th/tex; mode=display">\operatorname{atan}{\left (e^{x} | |
</li> | |
</ul> | |
Results: | |
[['\nintegrate(exp(x)/(exp(2*x) + 1), x)\n', '\\operatorname{RootSum} {\\left(4 z^{2} + 1, \\Lambda {\\left (i, i \\log{\\left (2 i + e^{x} \right )} \right )}\right)}'], ['\nsympy.integrals.manualintegrate(exp(x)/(exp(2*x) + 1), x)\n', '\\operatorname{atan}{\\left (e^{x} \right )}'], []] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment