Skip to content

Commit 0e0ce8c

Browse files
[python] Add pyproject and lint with ruff
1 parent 35c644a commit 0e0ce8c

File tree

5 files changed

+96
-33
lines changed

5 files changed

+96
-33
lines changed

pyproject.toml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[tool.ruff]
2+
line-length = 120
3+
target-version = "py310"
4+
5+
[tool.ruff.lint]
6+
select = [
7+
"F", # Pyflakes
8+
"E", # pycodestyle errors
9+
"W", # pycodestyle warnings
10+
"C90", # mccabe complexity
11+
"I", # isort
12+
"N", # pep8-naming
13+
"UP", # pyupgrade
14+
"ANN", # flake8-annotations
15+
"ASYNC", # flake8-async
16+
"S", # flake8-bandit
17+
"BLE", # flake8-blind-except
18+
"B", # flake8-bugbear
19+
"A", # flake8-builtins
20+
"C4", # flake8-comprehensions
21+
"DTZ", # flake8-datetimez
22+
"T10", # flake8-debugger
23+
"EM", # flake8-errmsg
24+
"ISC", # flake8-implicit-str-concat
25+
"ICN", # flake8-import-conventions
26+
"G", # flake8-logging-format
27+
"PIE", # flake8-pie
28+
"T20", # flake8-print
29+
"PT", # flake8-pytest-style
30+
"Q", # flake8-quotes
31+
"RSE", # flake8-raise
32+
"RET", # flake8-return
33+
"SIM", # flake8-simplify
34+
"TID", # flake8-tidy-imports
35+
"ARG", # flake8-unused-arguments
36+
"PTH", # flake8-use-pathlib
37+
"ERA", # eradicate
38+
"PL", # Pylint
39+
"PERF", # Perflint
40+
"RUF", # Ruff-specific rules
41+
]
42+
ignore = [
43+
"D203", # one-blank-line-before-class (conflicts with D211)
44+
"D213", # multi-line-summary-second-line (conflicts with D212)
45+
"T201", # print statement used
46+
"PLR2004", # constant variable
47+
]
48+
49+
[tool.ruff.lint.per-file-ignores]
50+
"**/__init__.py" = ["D104"] # missing docstring in public package
51+
"**/test_*.py" = ["S101", "D"] # allow assert in tests, skip docstrings
52+
"**/tests/**/*.py" = ["S101", "D"] # allow assert in tests, skip docstrings
53+
54+
[tool.ruff.lint.pydocstyle]
55+
convention = "google"
56+
57+
[tool.ruff.lint.mccabe]
58+
max-complexity = 10
59+
60+
[tool.ruff.lint.isort]
61+
known-first-party = ["scenarios"]

scenarios/python_basic_3.11/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
22
from threading import Thread
33
from time import sleep
4+
45
from ddtrace.profiling import Profiler
56

67

7-
def target(n):
8+
def target(n: int)->None:
89
sleep(n)
910

1011
if __name__ == "__main__":

scenarios/python_basic_gevent/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
monkey.patch_all()
44

55
import os # noqa: E402
6+
import time # noqa: E402
67
from threading import Thread # noqa: E402
78

89

910
def target(n: int) -> None:
1011
# Do actual work instead of just sleeping so profiler can capture it
11-
import time
12-
1312
end_time = time.monotonic() + n
1413
count = 0
1514
while time.monotonic() < end_time:

scenarios/python_cpu/expected_profile.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"profile-type": "cpu-time",
66
"stack-content": [
77
{
8-
"regular_expression": "<module>;main;b",
8+
"regular_expression": "<module>;.*main;.*b",
99
"percent": 66,
1010
"error_margin": 5,
1111
"labels": [
@@ -18,7 +18,7 @@
1818
]
1919
},
2020
{
21-
"regular_expression": "<module>;main;a",
21+
"regular_expression": "<module>;.*main;.*a",
2222
"percent": 33,
2323
"error_margin": 6,
2424
"labels": [

scenarios/python_cpu/main.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
import os
22
from time import time
33

4-
x = 0
5-
i = 0
6-
7-
def main():
8-
global x, i
9-
EXECUTION_TIME_SEC = int(os.getenv("EXECUTION_TIME_SEC", "10")) # defaults to 10 if not set
10-
end = time() + EXECUTION_TIME_SEC
11-
while time() < end:
12-
a()
13-
b()
14-
# We add a print to prevent optimization that could turn this into a no-op program
15-
print(x)
16-
17-
def a():
18-
global x, i
19-
i = 0
20-
while i < 1000000:
21-
x += i
22-
i += 1
23-
24-
def b():
25-
global x, i
26-
i = 0
27-
while i < 2000000:
28-
x += i
29-
i += 1
30-
31-
main()
4+
5+
class CPUBurner:
6+
def __init__(self) -> None:
7+
self.x = 0
8+
self.i = 0
9+
10+
def a(self) -> None:
11+
self.i = 0
12+
while self.i < 1000000:
13+
self.x += self.i
14+
self.i += 1
15+
16+
def b(self) -> None:
17+
self.i = 0
18+
while self.i < 2000000:
19+
self.x += self.i
20+
self.i += 1
21+
22+
def main(self) -> None:
23+
execution_time_sec = int(os.getenv("EXECUTION_TIME_SEC", "10")) # defaults to 10 if not set
24+
end = time() + execution_time_sec
25+
while time() < end:
26+
self.a()
27+
self.b()
28+
29+
# We add a print to prevent optimization that could turn this into a no-op program
30+
print(self.x)
31+
32+
33+
CPUBurner().main()

0 commit comments

Comments
 (0)