Python code-folding in emacs? - python

I have many classes and defs ...
I want to have + and - keys before class and def to collapse the class or open it ( toggle it ).
How i can do this?

Hideshow works out of the box and folds python code. It is built-in my version of emacs (24.3.1)
I have never needed more than these commands:
M-x hs-minor-mode
M-x hs-hide-all
M-x hs-show-all
To toggle use C-c # C-c which probably needs rebinding. You might also want to setup a hook in your .emacs file for hs-minor-mode to automatically be enabled when opening .py files.
I use it in combination the following to jump around.
M-x imenu <my_func_name>

You can get code folding (and more) with CEDET. With CEDET, you should consider putting the following setting in your emacs configuration file:
(global-semantic-folding-mode t)
CEDET handles Python and other languages.
Other ideas about how you can make emacs even more convenient when programming can be found on StackOverflow.

Related

emacs flycheck-mode python3 print statement with file=sys.stderr

I am using emacs on Ubuntu 16.04 and added the configuration for flycheck-mode to include the python3 setup below:
Emacs: How do I set flycheck to Python 3?
Answer: https://stackoverflow.com/a/55000284/719016
(custom-set-variables
'(flycheck-python-flake8-executable "python3")
'(flycheck-python-pycompile-executable "python3")
'(flycheck-python-pylint-executable "python3"))
But my python3 buffer still gives me an invalid syntax [E0001] error in a line like below:
print("# My message for the stderr", file=stderr)
The syntax checkers loaded are python-pylint and python-pycompile (for some reason python-flake8 does not seem to be found.
Any ideas why?
Well, if you took the answer that you quoted literally, your configuration isn't getting loaded. The answer suggests putting the config in ~/.emacs.c/custom.el. That's a typo. The correct path is ~/.emacs.d/custom.el. The more correct answer is to put the config in the file pointed to by custom-file. The most correct answer is to never edit the custom file by hand. Instead use the customize facility.
Run M-x customize-group flycheck.
Scroll to the bottom of the buffer and click on "Flycheck executables".
Find the python executables you want to change. (Always use python3 for Python3 stuff, even if you only have Python3 on your system. It'll save you headaches later.)
Scroll to the top of the buffer.
Click "Apply and Save".
Boom. Your settings are saved in the correct "custom.el" file.
Now, load up a Python3 file you want to use flycheck with. If it's not doing what you expect, check things with C-c ! v (aka flycheck-verify-setup.) Confirm individual checkers with C-c ! ? (aka flycheck-describe-checker.) Check the variables you think you're setting with C-h v. Cut-n-paste them from flycheck's website if you have to.
Don't worry about flake8's config file. It will properly cascade as you expect.
And, lastly, as #jenesaisquois suggests:
#!/usr/bin/env python3
import sys
print("# My message for the stderr", file=sys.stderr)

Can Omnicomplete in Vim for python/3 complete function names or just methods?

To illustrate my question, here is a simple example of how Omnicomplete is acting on my machine:
name = "Bob"
na<C-x><C-o> gives-----> name
name.<C-x><C-o> gives-----> (correct dropdown menu of methods for string)
prin<C-x><C-o> gives-----> (-- Omni completion (^O^N^P) Pattern not found)
I was expecting the last line to expand to print or print( or something like that. Is this the correct behavior (Omnicomplete only does method completion for python/3)? If so, would function completion in the last case above be an example of what snippets are for?
I am using MacVim in the terminal with tmux. My :version says VIM 8.0. I have +python/dyn and +python3/dyn features. My ~/.vimrc has this line (among others):
autocmd FileType python set omnifunc=python3complete#Complete
Right now, I'm only editing python3 files, so I think omnifunc=python3... is okay.
Semi-related SO post: Problem with Vim omnicomplete and system Python
EDIT: I may have a workaround, though I don't know how to implement it.
Observe that:
import builtins
builtins.pr<C-x><C-o> gives-----> (dropdown menu with print() and property() )
builtins.pri<C-x><C-o> gives-----> (autocompletes to builtins.print( )
Can this action with <C-x><C-o> be replicated without writing import builtins and using a builtins object? Perhaps a <C-x><C-o> search assumes builtins. if the string preceding <C-x><C-o> has no . in it?
I'm all ears for any suggestions.
have you tried to use the following for the omnifunc:
autocmd FileType python set omnifunc=syntaxcomplete#Complete
Just set Omni Completion like it is recommended in the Vim Wiki and it will work for all languages:
filetype plugin on
set omnifunc=syntaxcomplete#Complete

Ignore the rest of the python file

My python scripts often contain "executable code" (functions, classes, &c) in the first part of the file and "test code" (interactive experiments) at the end.
I want python, py_compile, pylint &c to completely ignore the experimental stuff at the end.
I am looking for something like #if 0 for cpp.
How can this be done?
Here are some ideas and the reasons they are bad:
sys.exit(0): works for python but not py_compile and pylint
put all experimental code under def test():: I can no longer copy/paste the code into a python REPL because it has non-trivial indent
put all experimental code between lines with """: emacs no longer indents and fontifies the code properly
comment and uncomment the code all the time: I am too lazy (yes, this is a single key press, but I have to remember to do that!)
put the test code into a separate file: I want to keep the related stuff together
PS. My IDE is Emacs and my python interpreter is pyspark.
Use ipython rather than python for your REPL It has better code completion and introspection and when you paste indented code it can automatically "de-indent" the pasted code.
Thus you can put your experimental code in a test function and then paste in parts without worrying and having to de-indent your code.
If you are pasting large blocks that can be considered individual blocks then you will need to use the %paste or %cpaste magics.
eg.
for i in range(3):
i *= 2
# with the following the blank line this is a complete block
print(i)
With a normal paste:
In [1]: for i in range(3):
...: i *= 2
...:
In [2]: print(i)
4
Using %paste
In [3]: %paste
for i in range(10):
i *= 2
print(i)
## -- End pasted text --
0
2
4
In [4]:
PySpark and IPython
It is also possible to launch PySpark in IPython, the enhanced Python interpreter. PySpark works with IPython 1.0.0 and later. To use IPython, set the IPYTHON variable to 1 when running bin/pyspark:1
$ IPYTHON=1 ./bin/pyspark
Unfortunately, there is no widely (or any) standard describing what you are talking about, so getting a bunch of python specific things to work like this will be difficult.
However, you could wrap these commands in such a way that they only read until a signifier. For example (assuming you are on a unix system):
cat $file | sed '/exit(0)/q' |sed '/exit(0)/d'
The command will read until 'exit(0)' is found. You could pipe this into your checkers, or create a temp file that your checkers read. You could create wrapper executable files on your path that may work with your editors.
Windows may be able to use a similar technique.
I might advise a different approach. Separate files might be best. You might explore iPython notebooks as a possible solution, but I'm not sure exactly what your use case is.
Follow something like option 2.
I usually put experimental code in a main method.
def main ():
*experimental code goes here *
Then if you want to execute the experimental code just call the main.
main()
With python-mode.el mark arbitrary chunks as section - for example via py-sectionize-region.
Than call py-execute-section.
Updated after comment:
python-mode.el is delivered by melpa.
M-x list-packages RET
Look for python-mode - the built-in python.el provides 'python, while python-mode.el provides 'python-mode.
Developement just moved hereto: https://gitlab.com/python-mode-devs/python-mode
I think the standard ('Pythonic') way to deal with this is to do it like so:
class MyClass(object):
...
def my_function():
...
if __name__ == '__main__':
# testing code here
Edit after your comment
I don't think what you want is possible using a plain Python interpreter. You could have a look at the IEP Python editor (website, bitbucket): it supports something like Matlab's cell mode, where a cell can be defined with a double comment character (##):
## main code
class MyClass(object):
...
def my_function():
...
## testing code
do_some_testing_please()
All code from a ##-beginning line until either the next such line or end-of-file constitutes a single cell.
Whenever the cursor is within a particular cell and you strike some hotkey (default Ctrl+Enter), the code within that cell is executed in the currently running interpreter. An additional feature of IEP is that selected code can be executed with F9; a pretty standard feature but the nice thing here is that IEP will smartly deal with whitespace, so just selecting and pasting stuff from inside a method will automatically work.
I suggest you use a proper version control system to keep the "real" and the "experimental" parts separated.
For example, using Git, you could only include the real code without the experimental parts in your commits (using add -p), and then temporarily stash the experimental parts for running your various tools.
You could also keep the experimental parts in their own branch which you then rebase on top of the non-experimental parts when you need them.
Another possibility is to put tests as doctests into the docstrings of your code, which admittedly is only practical for simpler cases.
This way, they are only treated as executable code by the doctest module, but as comments otherwise.

Emacs - Running current file in Python

I am using ELPY in emacs in Windows. How do I eval current file inside emacs using a Shortcut.
When I use eval-buffer.
I get below messages.
Flymake unable to run without a buffer file name
Removed if main == 'main' construct, use a prefix argument to evaluate.
There is a built in feature in emacs when working with the python (inferior) shell, to prevent people from running the whole script unintentionally, which is does by redefining (or removing) __name__ before it runs.
Your script does send __name__ = "__main__" but it is overwritten, due to this 'safety feature'.
To execute as you wish, running it as main, use the key binding:
`C-u C-c C-c`
If you would like to remap this to be something quicker or more familiar, try something like this:
(global-set-key (kbd "<f7>") (kbd "C-u C-c C-c"))
this was discussed in this thread here, where there is also some related extra information.

emacs and python updating modules

atm i'm using emacs to write some python code, so far it works quite fine except one problem that is really a bit annoying.
Always when I update something inside a self written module i reevaluate the buffer and the module in the python shell inside emacs doesn't get updated. i always have to end the python process and start it again to get the change. I figured out that emacs copies some things to a tmp dir to execute them, so i guess it has something to do with this.
Maybe someone out there had the same problem and solved it already so help would be appreciated
You have to reload the module manually in the shell in order for it to take effect.
See the documentation here on the Python reload function
I asked a similar question which you can see here
I found a better solution that needs no emacs config:
simply do
$ ipython profile create
that should create ipython profile in
$HOME/.ipython/profile_default/ipython_config.py
then put the following inside
c = get_config()
c.TerminalInteractiveShell.editor = 'emacsclient'
c.InteractiveShellApp.extensions = [
'autoreload'
]
c.InteractiveShellApp.exec_lines = []
c.InteractiveShellApp.exec_lines.append('%load_ext autoreload')
c.InteractiveShellApp.exec_lines.append('%autoreload 2')
then restart emacs. Now each time you save changes to file inside emacs - ipython would reload it automatically
and the following I have in my emacs config
;; ------------------
;; misc python config
;; ------------------
(company-mode -1)
(elpy-enable)
(elpy-use-ipython "ipython")
(setq python-shell-interpreter "ipython" python-shell-interpreter-args "--simple-prompt --pprint")
(setq python-check-command "flake8")
(setq elpy-rpc-backend "jedi")
(setq elpy-rpc-python-command "python")
; https://github.com/gregsexton/ob-ipython/issues/28
(setq python-shell-completion-native-enable nil)
if you want to see my full python config, it's here
You can advise a python-mode.el function to get the desired effect (at least, if I am understanding your request properly). Put the following in your Emacs init file:
(defun py-reload-file (buf)
"Reload buffer's file in Python interpreter."
(let ((file (buffer-file-name buf)))
(if file
(progn
;; Maybe save some buffers
(save-some-buffers (not py-ask-about-save) nil)
(let ((reload-cmd
(if (string-match "\\.py$" file)
(let ((f (file-name-sans-extension
(file-name-nondirectory file))))
(format "if globals().has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
f f f))
(format "execfile(r'%s')\n" file))))
(save-excursion
(set-buffer (get-buffer-create
(generate-new-buffer-name " *Python Command*")))
(insert reload-cmd)
(py-execute-base (point-min) (point-max))))))))
(defadvice py-execute-region
(around reload-in-shell activate)
"After execution, reload in Python interpreter."
(save-window-excursion
(let ((buf (current-buffer)))
ad-do-it
(py-reload-file buf))))
Now, when you're in a python program, you can select a region of code, press C-| to evaluate the region and the python program will be reloaded (or imported if it wasn't previously loaded) in the Python interpreter buffer. The ENTIRE module will be reloaded, not just the region that was selected and you will be prompted to save the python file if it hasn't already been saved. Note that the caveats that were mentioned in the replies to your previous question still apply (e.g. - If you have class instances already created from the imported module, have instantiated other objects, etc, they won't be reloaded). General breakage may occur, so caveat emptor!).
I'd recommend using Elpy as discussed here.
Add the following to your Emacs configuration file:
(defun my-restart-python-console ()
"Restart python console before evaluate buffer or region to avoid various uncanny conflicts, like not reloding modules even when they are changed"
(interactive)
(if (get-buffer "*Python*")
(let ((kill-buffer-query-functions nil)) (kill-buffer "*Python*")))
(elpy-shell-send-region-or-buffer))
(global-set-key (kbd "C-c C-x C-c") 'my-restart-python-console)
restart your Emacs run your code using C-c C-x C-c
In short, this code has the "if clause" for checking if Python buffer is open. This will help to be able to run C-c C-x C-c at any time of development even when there is no Python process already open. Another part is kill-buffer-query-functions which neglects the prompt for killing the Python buffer.

Categories

Resources