Skip to content

Commit

Permalink
Template/first craft idea (#1)
Browse files Browse the repository at this point in the history
* update project structure
  • Loading branch information
haicheviet authored Jul 21, 2023
1 parent 1c10ee0 commit ce95baa
Show file tree
Hide file tree
Showing 56 changed files with 4,441 additions and 15 deletions.
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Created by .ignore support plugin (hsz.mobi)
### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
.ipynb_checkpoints
**__pycache__**
**.DS_Store**
htmlcov
.mypy_cache
.coverage

# Dynamic update version
__version__.py

# Compiled python modules.
*.pyc


# Setuptools distribution folder.
dist

# Python egg metadata, regenerated from source files by setuptools.
*.egg-info
build
*.egg
*.eggs
*.pickle
test.py
.env
*.zip

*.png
*.pth
*.pt
*.tfevents.*
version*
.vscode
**/target

*.json
*.log
*.pth
*log*
*.ipynb
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ This is an example of how python structure in monorepo fashion, with the goal of
- `Tooling`: for handling builds with shared libraries, CI-CD and Coverage Page.


# Note

Most of this project was inspire by [dermidgen](https://github.com/dermidgen/python-monorepo) but I found the current methology is old and can not use poetry in Docker is really pain point. Here is the thing that I planning to enhance from original repo:

- Poetry playwell with docker build.
- Format and lint for python project.
- CI-CD in Docker that can share stage between libs.
- Test and coverage page for whole repo including all subprojects.

## Tooling

* [Python3](https://docs.python.org/3/whatsnew/3.8.html) ^3.8: (3.8.16 preferred)

* [Poetry](https://python-poetry.org/) ^1.5: (1.5.1 preferred)

* Bash script for CI/CD builds, most convention was inherited from [FastAPI author](https://github.com/tiangolo/full-stack-fastapi-postgresql).
* Bash script for CI/CD builds, most conventions was inherited from [author of FastAPI](https://github.com/tiangolo/full-stack-fastapi-postgresql).

* Docker for containers.

Expand All @@ -42,11 +50,7 @@ To install the monorepo system, please read and follow these steps:
cd your/project/folder

pip install poetry
poetry install --with lint,test

# If the .env_template is available [Optional]
cp .env_template .env
vim .env
poetry install --with dev,lint,test # If you want to package the code, poetry install is enough
```

2. Before create MR, format code and check lint with these steps
Expand All @@ -63,14 +67,6 @@ bash scripts/lint.sh # Lint check
```bash
cd your/project/folder

pytest
bash scripts/test.sh
```

# Note

Most of this project was inspire by [dermidgen](https://github.com/dermidgen/python-monorepo) but I found the current methology is old and can not use poetry in Docker is really pain point. Here is the thing that I planning to enhance from original repo:

- Poetry playwell with docker build.
- Format and lint for python project.
- CI-CD in Docker that can share stage between libs.
- Coverage page for whole repo including all subprojects.
Empty file added libs/ml/ml/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions libs/ml/ml/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import torch
from telemetry.core import main


def sum_two_tensor(tensor_a: torch.Tensor, tensor_b: torch.Tensor) -> torch.Tensor:
return tensor_a + tensor_b


def call_telemetry_function() -> None:
main()
752 changes: 752 additions & 0 deletions libs/ml/poetry.lock

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions libs/ml/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
[tool.poetry]
name = "ml"
version = "0" # standard placeholder, will update dynamic when install service
description = ""
authors = ["Hai Che <cheviethai123@gmail.com>"]

[tool.poetry.dependencies]
python = "^3.8.1"
torch = {version = "2.0.1", source="torchcpu"}
telemetry = {path = "../telemetry"}

[tool.poetry.dev-dependencies]
telemetry = {path = "../telemetry", develop = true} # for developing environment

[[tool.poetry.source]]
name = "torchcpu"
url = "https://download.pytorch.org/whl/cpu"
priority = "explicit"


[tool.poetry.group.test]
optional = true

[tool.poetry.group.test.dependencies]
pytest = "6.2.2"
pytest-cov = "4.0.0"

[tool.pytest.ini_options]
log_cli = true
log_cli_level = "DEBUG"
addopts = "--cov --cov-report term"
testpaths = ["tests"]

[tool.coverage.run]
source = ["ml"]
omit = ["./venv/*", "*tests*", "*_Users_*"]

[tool.coverage.paths]
source = ["ml"]

[tool.poetry.group.lint]
optional = true


[tool.poetry.group.lint.dependencies]
mypy = "1.4.1"
black = "23.3.0"
ruff = "0.0.278"

[tool.mypy]
strict = true
ignore_missing_imports = true

[tool.ruff]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
]
ignore = [
"E501", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"C901", # too complex
]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".mypy_cache",
".pytest_cache",
".ruff_cache",
".venv",
"__pypackages__",
"__pycache__",
"build",
"dist",
"venv",
]

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]


### Versioning by git setup ###
[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
style = "pep440"


[build-system]
requires = ["poetry_core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
5 changes: 5 additions & 0 deletions libs/ml/scripts/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -x

ruff ml --fix
black ml
6 changes: 6 additions & 0 deletions libs/ml/scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

mypy ml
ruff ml
black ml --check
5 changes: 5 additions & 0 deletions libs/ml/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Exit in case of error
set -e
set -x

pytest
Empty file added libs/ml/tests/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions libs/ml/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import io
from unittest import mock

import pytest
import torch

from ml.core import call_telemetry_function, sum_two_tensor


@pytest.mark.parametrize(
argnames=["a", "b", "expected_result"], argvalues=[(1, 2, 3), (0.1, 0.2, 0.3)]
)
def test_sum(a, b, expected_result):
a = torch.tensor(a)
b = torch.tensor(b)
expected_result = torch.tensor(expected_result)

result = sum_two_tensor(a, b)
assert torch.equal(result, expected_result)


@mock.patch("sys.stdout", new_callable=io.StringIO)
def test_call_sublib(mock_stdout):
call_telemetry_function()
assert mock_stdout.getvalue().strip() == "Go for opentelemetry"
Empty file added libs/mq/mq/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions libs/mq/mq/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pika

def main():
print(f"Go for mq version: {pika.__version__}")
Loading

0 comments on commit ce95baa

Please sign in to comment.