Skip to content

Commit 0bc7fec

Browse files
committed
DOC: handle pure python code blocks more correctly in ipython_directive, close #560
1 parent 9df5d75 commit 0bc7fec

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

doc/source/io.rst

+11-10
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ data into a DataFrame object. They can take a number of arguments:
9797
non-ascii
9898
- ``verbose`` : show number of NA values inserted in non-numeric columns
9999

100+
100101
.. ipython:: python
101102
:suppress:
102103
103-
f = open('foo.csv', 'w')
104+
f = open('foo.csv','w')
104105
f.write('date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5')
105106
f.close()
106107
@@ -198,7 +199,7 @@ so it's ok to have extra separation between the columns in the file.
198199
.. ipython:: python
199200
:suppress:
200201
201-
os.remove('bar.csv')
202+
# os.remove('bar.csv')
202203
203204
Files with an "implicit" index column
204205
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -270,8 +271,9 @@ module.
270271
.. ipython:: python
271272
:suppress:
272273
273-
df[:7].to_csv('tmp.sv', sep='|')
274-
df[:7].to_csv('tmp2.sv', sep=':')
274+
df = DataFrame(np.random.randn(10, 4))
275+
df.to_csv('tmp.sv', sep='|')
276+
df.to_csv('tmp2.sv', sep=':')
275277
276278
.. ipython:: python
277279
@@ -299,15 +301,14 @@ rather than reading the entire file into memory, such as the following:
299301
By specifiying a ``chunksize`` to ``read_csv`` or ``read_table``, the return
300302
value will be an iterable object of type ``TextParser``:
301303

302-
.. ipython::
304+
.. ipython:: python
303305
304-
In [1]: reader = read_table('tmp.sv', sep='|', chunksize=4)
306+
reader = read_table('tmp.sv', sep='|', chunksize=4)
307+
reader
305308
306-
In [1]: reader
309+
for chunk in reader:
310+
print chunk
307311
308-
In [2]: for chunk in reader:
309-
...: print chunk
310-
...:
311312
312313
Specifying ``iterator=True`` will also return the ``TextParser`` object:
313314

doc/source/merging.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ them together on their indexes. The same is true for ``Panel.join``.
553553
df1
554554
df1.join([df2, df3])
555555
556-
.. _merging.multiple_join:
556+
.. _merging.combine_first:
557557

558558
Merging together values within Series or DataFrame columns
559559
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/sphinxext/ipython_directive.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ def process_pure_python2(self, content):
548548

549549
ct = 0
550550

551+
# nuke empty lines
552+
content = [line for line in content if len(line.strip()) > 0]
553+
551554
for lineno, line in enumerate(content):
552555

553556
line_stripped = line.strip()
@@ -581,9 +584,17 @@ def process_pure_python2(self, content):
581584
else:
582585
modified = u'%s %s' % (continuation, line)
583586
output.append(modified)
587+
584588
try:
585589
ast.parse('\n'.join(content[multiline_start:lineno+1]))
586-
output.append(u'')
590+
591+
if (lineno < len(content) - 1 and
592+
_count_indent(content[multiline_start]) <
593+
_count_indent(content[lineno + 1])):
594+
595+
continue
596+
597+
output.extend([continuation, u''])
587598
multiline = False
588599
except Exception:
589600
pass
@@ -592,6 +603,13 @@ def process_pure_python2(self, content):
592603

593604
return output
594605

606+
def _count_indent(x):
607+
import re
608+
m = re.match('(\s+)(.*)', x)
609+
if not m:
610+
return 0
611+
return len(m.group(1))
612+
595613
class IpythonDirective(Directive):
596614

597615
has_content = True

0 commit comments

Comments
 (0)