Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python region folding syntax

I know python does not formally support any built in regional code folding syntax. I do know that a few syntaxes exist out in the wild with conventions tied to specific editors and particular comment pairs.

I know of the python source code folding syntax recognized by VS Code which uses the #region keyword

#region MY_CODE_REGION
...
#endregion

I came across another I don't recognize taken from some source over at

https://github.com/gitpython-developers/GitPython/blob/master/git/objects/tree.py#L96-L104

which used the following syntax

#{ MY_CODE_REGION
#} END MY_CODE_REGION

What other editor syntaxes exist for python? Is there a pending PEP open on the subject?

like image 722
jxramos Avatar asked Sep 04 '19 17:09

jxramos


People also ask

How do you code a fold in Python?

It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn't fold any of the nested sub levels. You can still use za to toggle folding for the current block.

How to create region in intellij?

For example, select a block of code for which you want to create a folding region, press Ctrl+Alt+T , select either <editor fold> or region... endregion comments depending on the style you need, and name the created custom region.

What is foldable region in Vscode?

Folding By Region VS Code allows you to specify arbitrary blocks that should be folded. These are called "regions". To create one of these, use a comment with \\#region at the start of the block and \\#endregion at the end.

What is Vscode folding maximum regions?

The number of foldable regions is limited to a maximum of 5000.


1 Answers

I think #%% is another option for the syntax you are looking for. This is from the Spyder editor as the method for separating sections of code (and it is listed there as the "standard cell divider" syntax)

https://docs.spyder-ide.org/3/editor.html#defining-code-cells

there are also ways to convert this to and from a jupyter notebook

https://stackoverflow.com/a/55920098/13665895

like image 128
Richard Boyne Avatar answered Oct 03 '22 07:10

Richard Boyne