code-formatting

How to automating Code Formatting in VSCode for Jupyter Notebooks with Black Formatter?

How to automating Code Formatting in VSCode for Jupyter Notebooks with Black Formatter? Question: I’ve been enjoying the convenience of the Black Formatter extension in Visual Studio Code, especially its "Format on Save" feature for Python files. Being able to automatically format my code upon saving with Ctrl+S has significantly streamlined my workflow. However, I’ve …

Total answers: 2

How to prevent VSCode from reordering python imports across statements?

How to prevent VSCode from reordering python imports across statements? Question: This is the correct way to import Gtk3 into python: import gi gi.require_version(‘Gtk’, ‘3.0’) from gi.repository import Gtk, Gdk, GObject When I save such code in VSCode with "editor.formatOnSave": true, it gets reordered to: from gi.repository import Gtk, Gdk import gi gi.require_version(‘Gtk’, ‘3.0’) which …

Total answers: 2

In python, how to tweak Black formatter, if possible?

In python, how to tweak Black formatter, if possible? Question: I know that Black is an opinionated formatter, but I love everything it does except one major thing. When I have a function with multiple arguments, instead of displaying it like this: def example_function(arg_1: str, arg_2: bool, arg_3: int = 0, arg_4: int = 1, …

Total answers: 3

Black formatter – Ignore specific multi-line code

Black formatter – Ignore specific multi-line code Question: I would like to ignore a specific multi-line code by black python formatter. Particularly, this is used for np.array or matrix construction which turned ugly when formatted. Below is the example. np.array( [ [1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, …

Total answers: 3

I'm getting an IndentationError. How do I fix it?

I'm getting an IndentationError. How do I fix it? Question: I have a Python script: if True: if False: print(‘foo’) print(‘bar’) However, when I attempt to run my script, Python raises an IndentationError: File "script.py", line 4 print(‘bar’) ^ IndentationError: unindent does not match any outer indentation level I kept playing around with my program, …

Total answers: 6

How to set Python language specific tab spacing in Visual Studio Code?

How to set Python language specific tab spacing in Visual Studio Code? Question: Using VSCode 1.9.0 with the (donjayamanne) Python 0.5.8 extension, is it possible to provide Python specific editor options? Or more generally speaking, is it possible to provide language specific tab spacing and replacement rules? For example, Python should be tab=4 spaces (replaced …

Total answers: 5

Custom indent width for BeautifulSoup .prettify()

Custom indent width for BeautifulSoup .prettify() Question: Is there any way to define custom indent width for .prettify() function? From what I can get from it’s source – def prettify(self, encoding=None, formatter=”minimal”): if encoding is None: return self.decode(True, formatter=formatter) else: return self.encode(encoding, True, formatter=formatter) There is no way to specify indent width. I think it’s …

Total answers: 4

What is the proper way to format a multi-line dict in Python?

What is the proper way to format a multi-line dict in Python? Question: In Python, I want to write a multi-line dict in my code. There are a couple of ways one could format it. Here are a few that I could think of: mydict = { “key1”: 1, “key2”: 2, “key3”: 3, } mydict …

Total answers: 8

Python "best formatting practice" for lists, dictionary, etc

Python "best formatting practice" for lists, dictionary, etc Question: I have been looking over the Python documentation for code formatting best practice for large lists and dictionaries, for example, something = {‘foo’ : ‘bar’, ‘foo2’ : ‘bar2’, ‘foo3’ : ‘bar3’….. 200 chars wide, etc..} or something = {‘foo’ : ‘bar’, ‘foo2’ : ‘bar2’, ‘foo3’ : …

Total answers: 11

How can I break up this long line in Python?

How can I break up this long line in Python? Question: How would you go about formatting a long line such as this? I’d like to get it to no more than 80 characters wide: logger.info(“Skipping {0} because its thumbnail was already in our system as {1}.”.format(line[indexes[‘url’]], video.title)) Is this my best option? url = …

Total answers: 6