I didn't perfectly like Tobias' answer because when I org-edit-special
(C-c '
) a code block, my python-mode buffer would yell at me (I have several python minor modes with syntax checkers) because of the unexpected indent for blocks with stand-alone methods (because with org-src-preserve-indentation
set, I would have indents in front of all my methods in their individual blocks). So instead, I like this solution using org-mode's :noweb header argument:
#+BEGIN_SRC python :tangle "sample.py" :noweb yes
class Test:
<<init_method>> # these are indented
<<more_methods>> # these are indented
#+END_SRC
#+BEGIN_SRC python :noweb-ref init_method
def __init__(self):
self.a = 'a test class'
#+END_SRC
#+BEGIN_SRC python :noweb-ref more_methods
def say_hi(self):
print 'Hi from this Test object!'
print 'ID: {}'.format(repr(self))
print 'Data: {}'.format(str(self.__dict__))
#+END_SRC
As long as you just indent the Noweb syntax references (the double << >>
) in your class definition, the other blocks ("init_method" and "more_methods") will be tangled with respect to your indentation. So, the final output "sample.py" file looks like this:
class Test:
def __init__(self):
self.a = 'a test class'
def say_hi(self):
print 'Hi from this Test object!'
print 'ID: {}'.format(repr(self))
print 'Data: {}'.format(str(self.__dict__))
Nice!
C-'
you can change the indentation withC-c >
frompython-mode
. But, I guess this is not what you want. What is the reason for the wanted option:indentation-level
? – Tobiasorg-babel-tangle
the file, indentation is not respected given a whitespace prefix with the#+begin_src
environment. Everything is alright if I keep it all in one source block, but good sense says to split it up and explain each part. – Sean Allred