|
Size: 446
Comment:
|
Size: 2667
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| Describe Python/pyproject.toml here. | = pyproject.toml = |
| Line 5: | Line 5: |
{{{ my_project/ ├── pyproject.toml └── my_package/ ├── __init__.py └── main.py }}} {{{ #!highlight python # my_package/main.py def say_hello(): print("Hello from the world of TOML-configured Python!") if __name__ == "__main__": say_hello() }}} |
|
| Line 23: | Line 43: |
== Other example == {{{ hello_project/ ├── pyproject.toml ├── Makefile ├── .gitignore ├── src/ │ └── main.py └── tests/ └── test_main.py }}} {{{ [project] name = "hello_world_example" version = "0.1.0" dependencies = [] [project.optional-dependencies] dev = [ "coverage", "ruff", ] [tool.ruff] target-version = "py310" lint.select = ["E", "F", "I"] line-length = 88 [tool.coverage.run] source = ["src"] branch = true [tool.coverage.report] show_missing = true }}} {{{ VENV = .venv PYTHON = $(VENV)/bin/python3 PIP = $(VENV)/bin/pip RUFF = $(VENV)/bin/ruff COVERAGE = $(VENV)/bin/coverage .PHONY: venv install lint format test coverage clean check $(VENV)/bin/activate: python3 -m venv $(VENV) $(PIP) install --upgrade pip venv: $(VENV)/bin/activate install: venv $(PIP) install -e ".[dev]" lint: venv $(RUFF) check . format: venv $(RUFF) format . test: venv $(PYTHON) -m unittest discover tests coverage: venv $(COVERAGE) run -m unittest discover tests $(COVERAGE) report $(COVERAGE) html check: format lint coverage clean: rm -rf $(VENV) .ruff_cache htmlcov .coverage find . -type d -name "__pycache__" -exec rm -rf {} + }}} {{{ .venv/ __pycache__/ *.py[cod] .coverage htmlcov/ .ruff_cache/ .DS_Store }}} {{{ def get_hello_message(name="World"): return f"Hello, {name}!" if __name__ == "__main__": print(get_hello_message()) }}} {{{ import unittest from src.main import get_hello_message class TestHelloWorld(unittest.TestCase): def test_default_greeting(self): self.assertEqual(get_hello_message(), "Hello, World!") def test_custom_greeting(self): self.assertEqual(get_hello_message("Gemini"), "Hello, Gemini!") if __name__ == "__main__": unittest.main() }}} |
pyproject.toml
Replaces requirements.txt
To install pip install -e .
my_project/
├── pyproject.toml
└── my_package/
├── __init__.py
└── main.py[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "my_hello_app" version = "0.1.0" dependencies = [] [project.scripts] # This creates a command 'hello-world' that runs say_hello() hello-world = "my_package.main:say_hello" [tool.setuptools] packages = ["my_package"]
Other example
hello_project/
├── pyproject.toml
├── Makefile
├── .gitignore
├── src/
│ └── main.py
└── tests/
└── test_main.py[project]
name = "hello_world_example"
version = "0.1.0"
dependencies = []
[project.optional-dependencies]
dev = [
"coverage",
"ruff",
]
[tool.ruff]
target-version = "py310"
lint.select = ["E", "F", "I"]
line-length = 88
[tool.coverage.run]
source = ["src"]
branch = true
[tool.coverage.report]
show_missing = trueVENV = .venv
PYTHON = $(VENV)/bin/python3
PIP = $(VENV)/bin/pip
RUFF = $(VENV)/bin/ruff
COVERAGE = $(VENV)/bin/coverage
.PHONY: venv install lint format test coverage clean check
$(VENV)/bin/activate:
python3 -m venv $(VENV)
$(PIP) install --upgrade pip
venv: $(VENV)/bin/activate
install: venv
$(PIP) install -e ".[dev]"
lint: venv
$(RUFF) check .
format: venv
$(RUFF) format .
test: venv
$(PYTHON) -m unittest discover tests
coverage: venv
$(COVERAGE) run -m unittest discover tests
$(COVERAGE) report
$(COVERAGE) html
check: format lint coverage
clean:
rm -rf $(VENV) .ruff_cache htmlcov .coverage
find . -type d -name "__pycache__" -exec rm -rf {} +.venv/ __pycache__/ *.py[cod] .coverage htmlcov/ .ruff_cache/ .DS_Store
def get_hello_message(name="World"):
return f"Hello, {name}!"
if __name__ == "__main__":
print(get_hello_message())import unittest
from src.main import get_hello_message
class TestHelloWorld(unittest.TestCase):
def test_default_greeting(self):
self.assertEqual(get_hello_message(), "Hello, World!")
def test_custom_greeting(self):
self.assertEqual(get_hello_message("Gemini"), "Hello, Gemini!")
if __name__ == "__main__":
unittest.main()