Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize Python code into collapsable / expandable chunks?

Tags:

python

pycharm

In Pycharm, there's "code structurure" side bar which provides a tree to navigate through the code, but, it is only useful when the code has classes and methods and objects. If nothing of that is in code then it is useless. My question is: is there any way in which I dictate that this is a block, and I want to be able to collapse it and expand it? Something similar to Jupyter where the code is inherently divided to cells.

Currently, I'm doing this:

# ---------------------------------- chunck x blah blah -----------------------

EDIT: Most of comments say that I'm dumb and I don't know how to code efficiently and that I should use functions and classes. Guys, I know how to use those, that's not my question. Thanks.

like image 937
Alex Deft Avatar asked Jul 11 '19 20:07

Alex Deft


People also ask

How do I organize my Python code?

Modules are files with “. py” extension containing Python code. They help to organise related functions, classes or any code block in the same file. It is considered as a best practice to split the large Python code blocks into modules containing up to 300–400 lines of code.

How do I collapse and expand code in Visual Studio?

CTRL + M + O will collapse all. CTRL + M + P will expand all and disable outlining. CTRL + M + M will collapse/expand the current section.

How do you fold codes in PyCharm?

To fold or unfold a code fragment, press Ctrl+NumPad - / Ctrl+NumPad + . PyCharm folds or unfolds the current code fragment, for example, a single method. To collapse or expand all code fragments, press Ctrl+Shift+NumPad - / Ctrl+Shift+NumPad + .

How do I collapse code in Visual Studio?

(Ctrl+M, Ctrl+H) - Collapses a selected block of code that would not normally be available for outlining, for example an if block. To remove the custom region, use Stop Hiding Current (or Ctrl+M, Ctrl+U). Not available in Visual Basic.


1 Answers

Turns out that the answer is very simple: Select the code, right click, do custom folding

enter image description here

like image 117
Alex Deft Avatar answered Sep 18 '22 09:09

Alex Deft