Skip to content

Commit

Permalink
Add more samples
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgredowski committed Sep 19, 2024
1 parent 2848b0e commit 2e0b14c
Show file tree
Hide file tree
Showing 6 changed files with 796 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cpython@3.11.3
cpython@3.12
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ pip install -r requirements.lock -r requirements-dev.lock

### Option 2

Install [rye](https://github.com/mitsuhiko/rye).
Install [uv](https://docs.astral.sh/uv/getting-started/installation/).

Run

```bash
rye sync
rye shell
uv sync
uv venv
```

## Commands

Test with

```bash
python -m pytest --doctest-modules src --cov src --cov-report=html
uv run pytest --doctest-modules src --cov src --cov-report=html
```

Or without coverage

```bash
python -m pytest --doctest-modules src
uv run pytest --doctest-modules src
```

## Cases
Expand Down
33 changes: 15 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name = "python-samples"
version = "0.1.0"
description = "Monorepo for my Python code samples for talks, presentations, workshops, demos."
dependencies = [
"fastapi[uvicorn]>=0.100.0",
"fastapi>=0.100.0",
"httpx>=0.27.2",
"uvicorn[standard]>=0.23.1",
"numpy",
]
readme = "README.md"
requires-python = ">= 3.8"
Expand All @@ -14,22 +14,6 @@ requires-python = ">= 3.8"
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = [
"black[d]>=23.7.0",
"ruff>=0.0.280",
"pre-commit>=3.3.3",
"bandit>=1.7.5",
"pytest>=7.4.0",
"pytest-cov>=4.1.0",
"mypy>=1.4.1",
"httpx>=0.24.1",
"ipython>=8.12.2",
"pytest-xdist>=3.5.0",
]
excluded-dependencies = ["uvloop"]

[tool.hatch.metadata]
allow-direct-references = true

Expand Down Expand Up @@ -113,3 +97,16 @@ force-single-line = true

[tool.mypy]
exclude = [".venv"]

[tool.uv]
dev-dependencies = [
"ruff>=0.0.280",
"pre-commit>=3.3.3",
"bandit>=1.7.5",
"pytest>=7.4.0",
"mypy>=1.4.1",
"httpx>=0.24.1",
"ipython>=8.12.2",
"pytest-cov>=5.0.0",
"pytest-xdist>=3.6.1",
]
13 changes: 6 additions & 7 deletions src/optimization/sum_of_squares.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import timeit

import numpy as np


def sum_of_squares_v1(numbers):
result = 0
Expand Down Expand Up @@ -64,9 +62,10 @@ def sum_of_squares_v2(numbers):



def sum_of_squares_v3(numbers):
# Using a more efficient built-in function for large datasets
return np.sum(np.square(numbers))
# def sum_of_squares_v3(numbers):
# import numpy as np
# # Using a more efficient built-in function for large datasets
# return np.sum(np.square(numbers))



Expand All @@ -86,8 +85,8 @@ def sum_of_squares_v3(numbers):
numbers = [1, 2, 3, 4, 5]
print(sum_of_squares_v1.__name__, sum_of_squares_v1(numbers)) # Output: 55
print(sum_of_squares_v2.__name__, sum_of_squares_v2(numbers)) # Output: 55
print(sum_of_squares_v3.__name__, sum_of_squares_v3(numbers)) # Output: 55
# print(sum_of_squares_v3.__name__, sum_of_squares_v3(numbers)) # Output: 55

print(sum_of_squares_v1.__name__, timeit.timeit(lambda: sum_of_squares_v1(numbers * 1000), number=1_000))
print(sum_of_squares_v2.__name__, timeit.timeit(lambda: sum_of_squares_v2(numbers * 1000), number=1_000))
print(sum_of_squares_v3.__name__, timeit.timeit(lambda: sum_of_squares_v3(numbers * 1000), number=1_000))
# print(sum_of_squares_v3.__name__, timeit.timeit(lambda: sum_of_squares_v3(numbers * 1000), number=1_000))
8 changes: 3 additions & 5 deletions src/parallel_tests/test_parallel.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import datetime
import logging
import time
import warnings

import pytest

logging.basicConfig(format="%(message)s")


@pytest.mark.parametrize("n", [10, 20, 30, 40])
def test_it(n):
n = n/2
n = n / 2
i = 0
while True:
if i == n:
break

logging.warning(f"{datetime.datetime.now().isoformat()} {n} Slept {i}/{n} seconds")
warnings.warn(f"{datetime.datetime.now().isoformat()} {n} Slept {i}/{n} seconds")
time.sleep(1)
i += 1

Expand Down
Loading

0 comments on commit 2e0b14c

Please sign in to comment.