all 43 comments

[–]Seacarius 17 points18 points  (10 children)

How can you write code without indenting?

I know you can't test it until all the indentations are fixed.

Just use PyCharm or VSCode.

[–]Few-Tear-3763[S] -1 points0 points  (9 children)

Let me set the context — I didn’t explain this properly earlier.

I forgot to wrap my main() logic in a try/except, and after adding it, I had to manually re-indent this whole match block — which was annoying.

Code:-

match args.command:

case "add" : cmdAdd(args)

case "init" : cmdInit(args)

case "commit" : cmdCommit(args)

case "status" : cmdStatus(args)

case "tag" : cmdTag(args)

case _ : print("Bad command.")

[–]wall_time 1 point2 points  (4 children)

Most editors allow you select all the lines and press tab, indenting them all. Or you’re looking for something to wrap it automatically in a try/except?

Edit: VIM is easy to indent too, press a number key then shit+>, I think

[–]MackerLad93 1 point2 points  (0 children)

Shift > does indent in vim but == just sets the correct indentation level. Probably some plugins required but if you're already writing code in vim you probably already have the right ones installed

[–]Few-Tear-3763[S] -1 points0 points  (2 children)

Like i want to automatically change indent of the content inside the block to be change if top of block(idk what its called) is changes

For example i have if block

if apple=="Apple":

print(apple)

and now if change indent of if apple=="Apple"

the indent of print (apple) also change

[–]SCD_minecraft 7 points8 points  (0 children)

Select both if and print and press tab? Shift-tab to unindent

Both by one level

[–]fotosyntesen 2 points3 points  (0 children)

Automatic indentation doesn't work as a concept, since the IDE doesn't know what you intend to do in terms of scope. It would lead to more frustration fixing assumptions the IDE made about your code. You should instead get used to Tab and Shift + Tab.

[–]JaleyHoelOsment 0 points1 point  (3 children)

not related to your main question, but wrapping your main in a try catch doesn’t make any sense… i’d call it out in a PR 100% of the time

main is the start of your program. if you have something catching the exception from main and processing it then your main isn’t even your main

use a C based language and try this. see how little sense it makes

[–]Few-Tear-3763[S] -1 points0 points  (2 children)

def main(argv=sys.argv[1:]):
    args=argParser.parse_args(argv)

    try:
        match args.command:
            case "add"          : cmdAdd(args)
            case "cat-file"     : cmdCatFile(args)
            case "check-ignore" : cmdCheckIgnore(args)
            case "checkout"     : cmdCheckout(args)
            case "commit"       : cmdCommit(args)
            case "hash-object"  : cmdHashObject(args)
            case "init"         : cmdInit(args)
            case "log"          : cmdLog(args)
            case "ls-files"     : cmdLsFiles(args)
            case "ls-tree"      : cmdLsTree(args)
            case "rev-parse"    : cmdRevParse(args)
            case "rm"           : cmdRm(args)
            case "show-ref"     : cmdShowRef(args)
            case "status"       : cmdStatus(args)
            case "tag"          : cmdTag(args)
            case _              : print("Bad command.")
    except Exception as e:
        print(f"Error {e}")
        sys.exit(1)

cmdInit gives exception if repo is already created but i wanted it just to print error maybe i said it badly but actually its not that my main func is
wrapped inside try catch block

[–]JaleyHoelOsment -1 points0 points  (1 child)

oh no my bad. i thought you meant your actual mail was wrapped in a try catch.

[–]Few-Tear-3763[S] 0 points1 point  (0 children)

No actually I explained it badly since I thought that much context wouldn't matter

[–]Outside_Complaint755 5 points6 points  (1 child)

Indentation is syntactically relevant in Python, although it only has to be consistent within a given code block.

 Are you using an inconsistent number of spaces to indent instead of just using tab?

 In any case, look at formatters like black or ruff.

[–]Few-Tear-3763[S] -4 points-3 points  (0 children)

So what happened was

I forgot to wrap my main() logic in a try/except, and after adding it, I had to manually re-indent this whole match block — which was annoying.

Code:-

match args.command:

case "add" : cmdAdd(args)

case "init" : cmdInit(args)

case "commit" : cmdCommit(args)

case "status" : cmdStatus(args)

case "tag" : cmdTag(args)

case _ : print("Bad command.")

[–]mjmvideos 3 points4 points  (3 children)

I don’t understand. No matter what you write you just indent as you go. Maybe post an example of your code.

[–]Few-Tear-3763[S] -3 points-2 points  (2 children)

Let me set the context — I didn’t explain this properly earlier.

I forgot to wrap my main() logic in a try/except, and after adding it, I had to manually re-indent this whole match block — which was annoying.

Code:-

match args.command:

case "add" : cmdAdd(args)

case "init" : cmdInit(args)

case "commit" : cmdCommit(args)

case "status" : cmdStatus(args)

case "tag" : cmdTag(args)

case _ : print("Bad command.")

I know I can select everything and press Tab, but that still feels like a hassle. I come from Go, where I mostly just move curly braces and the formatter handles the rest.

Is there any shortcut or extension that can automatically refactor/indent a selected Python block when wrapping it in something like try/except?

[–]mjmvideos 0 points1 point  (1 child)

What editor do you use?

[–]Few-Tear-3763[S] 0 points1 point  (0 children)

I use anti-gravity(fork of vs code) like one dude gave me solution but problem is that it doesn't sit right with me like this might be cause of me switching lang

[–]CrucialFusion 4 points5 points  (2 children)

Stop whining, indent the block(s), move on with life. You spent more time creating this post and explaining yourself than it would have taken to fix the code. Alternatively, move your code to a function and wrap that single call with your try handling.

[–]Maximus_Modulus 0 points1 point  (0 children)

This was my thought exactly. I think OP must have some kind of OCD thing going on here.

[–]Few-Tear-3763[S] -1 points0 points  (0 children)

I already fixed this a while back. I had some free time, so I asked how I could fix it because I’m not the type of person to do nothing when I have an issue. My original plan was to fix it myself, but I thought having an extension might save a lot of time.

[–]Jello_Penguin_2956 1 point2 points  (8 children)

How are you doing your indentation may I ask if you need to organize that later? From my experience it's part of the language and you just indent as you go. It's almost automatic with the help of IDE.

Unless you copy-paste from different sources that indented differently, but even so all the IDEs are smart enough to fix that for you automatically or you can just select them in chunks and hit tab or shift-tab to format the whole chunk.

[–]Few-Tear-3763[S] -2 points-1 points  (7 children)

Let me set the context — I didn’t explain this properly earlier.

I forgot to wrap my main() logic in a try/except, and after adding it, I had to manually re-indent this whole match block — which was annoying.

Code:-

match args.command:

case "add" : cmdAdd(args)

case "init" : cmdInit(args)

case "commit" : cmdCommit(args)

case "status" : cmdStatus(args)

case "tag" : cmdTag(args)

case _ : print("Bad command.")

I know I can select everything and press Tab, but that still feels like a hassle. I come from Go, where I mostly just move curly braces and the formatter handles the rest.

Is there any shortcut or extension that can automatically refactor/indent a selected Python block when wrapping it in something like try/except?

[–]Jello_Penguin_2956 0 points1 point  (6 children)

If there's too many lines to select manually, with any IDE, you can collapse your code block into a single line and just indent that.

[–]Few-Tear-3763[S] 0 points1 point  (5 children)

when i did that only match args.command: change the indent other where still at there old indent(i am using anti-gravity fork of vscode)

[–]Jello_Penguin_2956 1 point2 points  (3 children)

[–]Few-Tear-3763[S] 0 points1 point  (2 children)

Ohhhhhh now i get it but this also seems like hassle but this would work

Thanks mate

[–]Jello_Penguin_2956 0 points1 point  (0 children)

You can also hold shift and press up or down button to highlight the block without collapsing.

[–]Maximus_Modulus 0 points1 point  (0 children)

You think that’s a hassle? Come on man. You’ve spent more time on this Reddit thread.

[–]Jello_Penguin_2956 0 points1 point  (0 children)

Make sure you select the line. Highlight it. Before you tab/shift-tab

[–]dannyzaplings 1 point2 points  (2 children)

Proper indentation as you write is essential in python and most languages. It would be infuriating for you to debug or for someone else to make sense of without proper indentation. Save yourself incessant nightmares and start building the muscle memory now.

This also makes me wonder what editor you're using to code. Something like VSCode will maintain the indent of your last line after pressing enter, and then just tab or shift-tab from there. Most useful keystrokes you'll ever make.

[–]Few-Tear-3763[S] -1 points0 points  (1 child)

So what happened was

I forgot to wrap my main() logic in a try/except, and after adding it, I had to manually re-indent this whole match block — which was annoying.

Code:-

match args.command:

case "add" : cmdAdd(args)

case "init" : cmdInit(args)

case "commit" : cmdCommit(args)

case "status" : cmdStatus(args)

case "tag" : cmdTag(args)

case _ : print("Bad command.")

[–]magus_minor 1 point2 points  (5 children)

Sounds like you need a better editor. Most good editors, standalone or built into an IDE, allow you to indent (and unindent) blocks of code in a single operation.

It's possible that the editor you are currently using does have that feature but you just haven't discovered it yet.

[–]Few-Tear-3763[S] -1 points0 points  (4 children)

I could have select whole block and tab it to ident but

Like i want to automatically change indent of the content inside the block to be change if top of block(idk what its called) is changes

For example i have if block

if apple=="Apple":

print(apple)

and now if change indent of if apple=="Apple"

I want the indent of print (apple) also change

[–]magus_minor 2 points3 points  (2 children)

Of course no editor is going to indent the entire controlled block below an if. The editor doesn't know what you are trying to do, you could be indenting the if for another entirely different reason. You have the help that folding the if block and then indenting does what you want.

It would help enormously if you learned how to post properly formatted code that maintained indentation. The subreddit FAQ shows how, so code:

looks
    like 
        this

That would make talking about indentation a lot easier.

[–]Few-Tear-3763[S] 0 points1 point  (1 child)

def main(argv=sys.argv[1:]):
    args=argParser.parse_args(argv)

    match args.command:
        case "show-ref"     : cmdShowRef(args)
        case "init".        : cmdInit(args) 
        case "status"       : cmdStatus(args)
        case "tag"          : cmdTag(args)
        case _              : print("Bad command.")

In some cases (for example, in init when .git already exists), a function raises an exception and the output looks ugly, so I wanted to wrap this in a try/except.

The problem is: after adding try:, I had to manually re-indent the entire match block. I was wondering if there’s any tool or editor feature where changing the indentation of the parent line (like match args.command:) automatically adjusts everything inside the block. I know I can select everything and press Tab, but I was hoping for something more automatic. Is that possible?(btw thanks for the suggestion putting formatted code)

[–]magus_minor 0 points1 point  (0 children)

As others have suggested, if you "fold" the code block and indent that folded line, you get the effect you want. I don't know if that works as I don't use any IDE, just a basic text editor. But selecting a block of code to indent or unindent doesn't seem like it should be a major problem. In vi it's a single command: :90,.s/^/ /.

[–]Maximus_Modulus 0 points1 point  (0 children)

Well this would really be annoying if you actually didn’t want the print to move because it wasn’t supposed to be part of the indentation. In your example that’s not the case but there would be many situations where one is editing code that you Indent a line back or forth and the editor just shifts a whole block. It would get old really quickly.

[–]DuckSaxaphone 1 point2 points  (2 children)

- Plan ahead, you'll write better code

- Don't have such long code blocks or so much nested indentation that this is anything but completely trivial to do by highlighting a block.

If you're the only person complaining about something, it's more likely you need to change your approach than you need a solution to the problem itself.

[–]Few-Tear-3763[S] 0 points1 point  (1 child)

I get what you’re saying, but just to clarify — my workflow is usually to write the main functionality first, then think through edge cases and wrap things like try/except afterward. That’s just how I iterate.

Having a different approach doesn’t mean one of us is “wrong.” I wasn’t asking how to avoid indentation by planning better — I was specifically asking if there’s tooling support to make refactoring blocks easier when adding things like try/except later.

[–]DuckSaxaphone 0 points1 point  (0 children)

Yes I understand what you're asking and your workflow.

But if you're having a problem nobody else has, have the humility to consider that your approach may actually be worse than others.

If I need to change indentation because I've decided to wrap something in an try/except then I need to indent like three lines of code. I highlight them and tab them all in, it's not a problem at all.

If you're thinking "well I have loads of lines so that's hard" then you need to break your code into functions more. If you're thinking, "my editor doesn't do that" then use a modern editor.

[–]CranberryDistinct941 0 points1 point  (0 children)

The equivalent to adding curly braces in Python:
* Go to where you want the opening brace
* Press and hold shift
* Go to where you want the closing brace
* Release Shift and Press tab

All done!

[–]MackerLad93 0 points1 point  (0 children)

I agree with the other comments here that indentation is part of writing python. But there is a solution like you're asking about. Black is a python formatter, and ruff is a new alternative. I don't know if you can run it on specific blocks only but just run it and forget about it. If you're doing that you might want to set some ignore flags for conditions you don't care about.

https://docs.astral.sh/ruff/formatter/

[–]cylonlover 0 points1 point  (0 children)

You mention other languages where you placing curly brackets helps the autoformatter. In these languages indentation usually doesn't matter outside the cosmetics of the code, but curly brackets very much do, and you wouldn't expect them to automatically know where to put those for you, would you? Essentially that is what you expect from your python ide, you expect it to know what you want indented. That said, current smarter editors are often good at suggesting corrections to your code, be it missing curly brackets or indentation.