File: python_scripts_code_style.md

package info (click to toggle)
vulkan-validationlayers 1.4.328.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 49,412 kB
  • sloc: cpp: 615,223; python: 12,115; sh: 24; makefile: 20; xml: 14
file content (45 lines) | stat: -rw-r--r-- 1,253 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Python Scripts Coding Style

In effort to keep the python scripts consistent (as most developers are C++ devs, not python experts), please follow the following:

# Functions

All helper functions should have the types strongly declared

```python
def someFunction(varA: bool, varB: str = "default") -> str:
```

# Strings

## Format strings with f'string (unless have reason not to)

There are many ways to format a string in python, by default use  [f'string](https://note.nkmk.me/en/python-f-strings/)

```python
function = f'void {name}(uint32_t {param}) {{ return true; }}'
```

### Exceptions

f'string has some known limitations, in those cases it is ok to use `.format` or template strings

## Use join() then write to file

```python
out = []
out.append(getSomeString())
out.append(f'something {variable}')

self.write("".join(out))
```

These are the [fastet](https://www.tutorialspoint.com/What-is-the-most-efficient-string-concatenation-method-in-python#)

## Each line is in charge of adding the new line at the end

It is easy to start putting random `\n` everywhere.

Each line to the string array is in charge of adding the `\n` at the end, so each line can assume it will.

This is important when calling into utils that return strings