1

Here is a my problem with a minimal reproducible example:

    def additional():
        df_additional_data = pd.read_excel("something.xlsx")
    
        for _, row in df_saga.iterrows():
            req = """
                  UPDATE table_name
                SET column1 = value1, column2 = value2, ...
                WHERE condition;
                  """

I want to add two spaces to both those lines (the one starting with SET and the one starting with WHERE). I cannot use TAB because it adds 4 spaces. I cannot use Ctrl Alt I because it's inside """ """ and it doesn't work inside those. I know you can do it by hand, but in my case I have over 200 lines in the """ """ string. And I don't want to hire an intern just to add spaces.

I'm using PyCharm 2021.3.3 (Community Edition)

Build #PC-213.7172.26, built on March 16, 2022
Runtime version: 11.0.14.1+1-b1751.46 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2030M
Cores: 16
Non-Bundled Plugins:
    net.seesharpsoft.intellij.plugins.csv (2.18.2)
    com.chesterccw.excelreader (2021.3.2)
    ru.meanmail.plugin.requirements (2022.3-2021.3)
nanotek
  • 2,959
  • 15
  • 25
  • So some of the lines (e.g. 'UPDATE' ) is properly formatted, but not others? I'm guessing they aren't always 3 lines where UPDATE is correct and the next 2 are wrong, correct? – nanotek Apr 04 '22 at 15:03
  • 1
    You say you can't use TAB because it does 4 spaces. You can change it to 2 spaces temporarily. If you're OK with that, that's probably the easiest thing to do. 200 lines is not a crazy number of lines IMO. `Settings ->Editor -> Code Style -> Python -> tab size` – nanotek Apr 04 '22 at 15:05
  • Thats a decent solution! – FluidMechanics Potential Flows Apr 04 '22 at 15:21

1 Answers1

0

Since formatters generally try not modifying the contents inside strings too much, this would be tricky in an automated way without seeing the actual code, would be complicated, and would probably have hiccups (IMO).

If the only reason using TAB doesn't solve your answer is because it uses 4 spaces, you can temporarily change it to using 2 spaces To do this you will need to do the following:

  1. Settings ->Editor -> Code Style -> Python -> indent -> 2.
  2. Settings ->Editor -> Code Style -> Uncheck Detect and use existing file indents for editing
nanotek
  • 2,959
  • 15
  • 25
  • I think that's the way to go but it doesn't work, pressing `TAB` still adds 4 spaces with those settings: https://i.imgur.com/8ykMjQF.png – FluidMechanics Potential Flows Apr 04 '22 at 20:20
  • I've put `1`s everywhere and ticked 'Use Tab Character', but still doesn't work. – FluidMechanics Potential Flows Apr 04 '22 at 20:21
  • When you say without seeing the actual code, I don't see why the minimal reproducible example can be used as an example – FluidMechanics Potential Flows Apr 04 '22 at 20:23
  • in practice it looks like this https://i.imgur.com/HAxZ3WI.png – FluidMechanics Potential Flows Apr 04 '22 at 20:42
  • Are you able to remove your award of a bounty? If this doesn't solve your problem it would be good to keep it open to see if someone else can answer it differently. The reason I said 'without seeing the code' was because if I offered a solution involving vim macros it would need to be very specific – nanotek Apr 04 '22 at 22:19
  • @FluidMechanicsPotentialFlows Try it now. Looks like it needs to be `indent` instead of `tab size` (But you could try setting both to 2 if that doesn't work for you). Also looks like PyCharm will ignore this setting by default and base it off of your file. You can disable this behavior. I've tested this locally and it worked for me – nanotek Apr 04 '22 at 22:31
  • I couldn't remove it, a bit stupid on my part to award it before testing the solution. But it ended well because it works now :). Next time I'll be more cautious haha – FluidMechanics Potential Flows Apr 04 '22 at 22:34
  • 1
    @FluidMechanicsPotentialFlows No problem! Sorry about that initially! Ya, I'd definitely hold off on awarding it until an answer actually solves your problem. I'd feel really bad if I didn't actually end up helping! – nanotek Apr 04 '22 at 22:35