Skip to content

Commit

Permalink
Merge pull request #50 from a5chin/feature/timer
Browse files Browse the repository at this point in the history
Add trace Timer
  • Loading branch information
a5chin authored Dec 30, 2024
2 parents ef0073d + 4919503 commit 7000d8c
Show file tree
Hide file tree
Showing 28 changed files with 257 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG UV_VERSION=0.5.11
ARG UV_VERSION=0.5.13
ARG DEBIAN_VERSION=bookworm


Expand Down
5 changes: 3 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"context": "..",
"dockerfile": "Dockerfile",
"args": {
"UV_VERSION": "0.5.11",
"UV_VERSION": "0.5.13",
"DEBIAN_VERSION": "bookworm"
}
},
Expand All @@ -23,7 +23,8 @@
"njpwerner.autodocstring",
"redhat.vscode-yaml",
"shardulm94.trailing-spaces",
"tamasfe.even-better-toml"
"tamasfe.even-better-toml",
"yzhang.markdown-all-in-one"
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12.6
3.13.1
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"shardulm94.trailing-spaces",
"tamasfe.even-better-toml",
"usernamehw.errorlens",
"yzhang.markdown-all-in-one",
"yzhang.markdown-all-in-one"
]
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG DEBIAN_VERSION=bookworm
ARG UV_VERSION=0.5.11
ARG VARIANT=3.12
ARG UV_VERSION=0.5.13
ARG VARIANT=3.13


FROM ghcr.io/astral-sh/uv:$UV_VERSION AS uv
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Specifically, you can solve this problem by following the steps below.
- [GitHub Actions](#github-actions)
- [Ruff](#ruff)
- [pre-commit](#pre-commit)
- [pytest](#pytest)
- [Appendix](#appendix)
- [Install libraries](#install-libraries)
- [The structure of this repository](#the-structure-of-this-repository)
Expand Down Expand Up @@ -170,7 +171,9 @@ uv add {libraries}
│ └── settings.json
├── tests/
│ └── tools/
│ └── test__logger.py
│ ├── test__config.py
│ ├── test__logger.py
│ └── test__trace.py
├── tools/
│ ├── config/
│ │ ├── __init__.py
Expand All @@ -184,6 +187,9 @@ uv add {libraries}
│ │ ├── logger.py
│ │ ├── style.py
│ │ └── type.py
│ ├── trace/
│ │ ├── __init__.py
│ │ └── time.py
│ └── __init__.py
├── .dockerignore
├── .env.local
Expand Down
2 changes: 1 addition & 1 deletion docs/configurations/pyright.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

```{.json title="pyrightconfig.json"}
{
"pythonVersion": "3.12",
"pythonVersion": "3.13",
"pythonPlatform": "All",
"venv": ".venv",
"typeCheckingMode": "standard",
Expand Down
4 changes: 2 additions & 2 deletions docs/configurations/ruff.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ The Ruff formatter is an extremely fast Python code formatter designed as a drop
line-length = 88
indent-width = 4

# Assume Python 3.12
target-version = "py312"
# Assume Python 3.13
target-version = "py313"

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
Expand Down
1 change: 1 addition & 0 deletions docs/guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ How to use this repository.
- [How to use Tools](tools/index.md)
- [How to use config in this repository](tools/config.md)
- [How to use logger in this repository](tools/logger.md)
- [How to use trace in this repository](tools/trace.md)
16 changes: 14 additions & 2 deletions docs/guides/tools/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
Addendum environment variables to `tools/config/settings.py`:
```{.py title="tools/config/settings.py" hl_lines="9"}
class Settings(BaseSettings):
"""Environment variables settings."""
"""Environment variables settings.
Examples:
>>> from tools.config import Settings
>>> from tools.logger import Logger, LogType
>>>
>>> settings = Settings()
>>> logger = Logger(
>>> name=__name__,
>>> log_type=LogType.LOCAL if settings.IS_LOCAL else LogType.GOOGLE_CLOUD
>>> )
"""

model_config = SettingsConfigDict(
env_file=(".env", ".env.local"),
Expand All @@ -22,7 +34,7 @@ class Settings(BaseSettings):
```python
from fastapi import FastAPI

from tools import Settings
from tools.config import Settings


settings = Settings()
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/tools/logger.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```python
from tools import Logger
from tools.logger import Logger


def main() -> None:
Expand Down
36 changes: 36 additions & 0 deletions docs/guides/tools/trace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Timer

### Decorator
```{.py hl_lines="6"}
import time

from tools.trace import Timer


@Timer("sleep")
def sleep(t: int = 1) -> None:
time.sleep(t)


sleep(1)
```

!!! NOTE
```sh
2038-01-19 03:14:07,000 | DEBUG | sleep:__exit__:50 - executed in 1000.000000 ms
```

### ContextManager
```{.py hl_lines="5"}
import time

from tools.trace import Timer

with Timer("examples"):
time.sleep(1)
```

!!! NOTE
```sh
2038-01-19 03:14:07,000 | DEBUG | examples:__exit__:50 - executed in 1000.000000 ms
```
2 changes: 1 addition & 1 deletion docs/guides/uv.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ uv remove ruff
## Pin Python Version
If you want to pin the Python version, run the following command:
```sh
uv pin python 3.12
uv pin python 3.13
```

## Configuration for uv
Expand Down
7 changes: 6 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ This repository contains configurations to set up a Python development environme
│ └── settings.json
├── tests/
│ └── tools/
│ └── test__logger.py
│ ├── test__config.py
│ ├── test__logger.py
│ └── test__trace.py
├── tools/
│ ├── config/
│ │ ├── __init__.py
Expand All @@ -41,6 +43,9 @@ This repository contains configurations to set up a Python development environme
│ │ ├── logger.py
│ │ ├── style.py
│ │ └── type.py
│ ├── trace/
│ │ ├── __init__.py
│ │ └── time.py
│ └── __init__.py
├── .dockerignore
├── .env.local
Expand Down
9 changes: 5 additions & 4 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
repo_name: a5chin/python-uv
repo_url: https://github.com/a5chin/python-uv
site_name: python-uv
site_url: https://a5chin.github.io/a5chin

theme:
name: material
Expand Down Expand Up @@ -40,9 +43,6 @@ markdown_extensions:
- pymdownx.tabbed:
alternate_style: true

repo_url: https://github.com/a5chin/python-uv
repo_name: a5chin/python-uv

nav:
- Home: index.md
- Getting Started:
Expand All @@ -57,10 +57,11 @@ nav:
- Pyright: guides/pyright.md
- pre-commit: guides/pre-commit.md
- Test: guides/test.md
- Using tools:
- tools:
- guides/tools/index.md
- config: guides/tools/config.md
- logger: guides/tools/logger.md
- trace: guides/tools/trace.md
- Configurations:
- configurations/index.md
- uv: configurations/uv.md
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dev-dependencies = [
"mkdocs-material>=9.5.49",
"pre-commit>=4.0.1",
"pyright>=1.1.391",
"pytest>=8.3.4",
"pytest-cov>=6.0.0",
"pytest>=8.3.4",
"ruff>=0.8.4",
]
2 changes: 1 addition & 1 deletion pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"pythonVersion": "3.12",
"pythonVersion": "3.13",
"pythonPlatform": "All",
"venv": ".venv",
"typeCheckingMode": "standard",
Expand Down
4 changes: 2 additions & 2 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ exclude = [
line-length = 88
indent-width = 4

# Assume Python 3.12
target-version = "py312"
# Assume Python 3.13
target-version = "py313"

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from tools import Settings
from tools.config import Settings


@pytest.fixture
Expand Down
3 changes: 1 addition & 2 deletions tests/tools/test__config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from tools import Settings
from tools.config import FastAPIKwArgs
from tools.config import FastAPIKwArgs, Settings


class TestSettings:
Expand Down
2 changes: 1 addition & 1 deletion tests/tools/test__logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tools import Logger, LogType
from tools.logger import Logger, LogType


class TestLocalLogger:
Expand Down
14 changes: 14 additions & 0 deletions tests/tools/test__trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from tools.trace import Timer


class TestTimer:
"""Test class for Timer."""

def test_contextmanager(self) -> None:
"""Test for ContextManager."""
with Timer("ContextManager"):
pass

@Timer("Decorator")
def test_decorator(self) -> None:
"""Test for Decorator."""
9 changes: 0 additions & 9 deletions tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
"""Tools."""

from tools.config import Settings
from tools.logger import Logger, LogType

__all__ = [
"LogType",
"Logger",
"Settings",
]
14 changes: 13 additions & 1 deletion tools/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@


class Settings(BaseSettings):
"""Environment variables settings."""
"""Environment variables settings.
Examples:
>>> from tools.config import Settings
>>> from tools.logger import Logger, LogType
>>>
>>> settings = Settings()
>>> logger = Logger(
>>> name=__name__,
>>> log_type=LogType.LOCAL if settings.IS_LOCAL else LogType.GOOGLE_CLOUD
>>> )
"""

model_config = SettingsConfigDict(
env_file=(".env", ".env.local"),
Expand Down
16 changes: 10 additions & 6 deletions tools/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@


class Logger(logging.Logger):
"""Logger."""
"""Logger.
Examples:
>>> from tools.logger import Logger
>>>
>>>
>>> logger = Logger(__name__)
>>> logger.info("Logger")
"""

def __init__(
self,
Expand All @@ -26,11 +35,6 @@ def __init__(
log_type (LogType, optional): Local or something.
Defaults to LogType.LOCAL.
Examples:
>>> from tools import Logger, LogType
>>> local_logger = Logger(name=__name__, log_type=LogType.LOCAL)
>>> gc_logger = Logger(name=__name__, log_type=LogType.GOOGLE_CLOUD)
"""
super().__init__(name=name)

Expand Down
7 changes: 7 additions & 0 deletions tools/trace/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Timer."""

from tools.trace.time import Timer

__all__ = [
"Timer",
]
Loading

1 comment on commit 7000d8c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCover
tests
   conftest.py50100%
tests/tools
   test__config.py90100%
   test__logger.py240100%
   test__trace.py70100%
tools
   __init__.py00100%
tools/config
   __init__.py30100%
   fastapi.py110100%
   settings.py200100%
tools/logger
   __init__.py50100%
   color.py120100%
   googlecloud.py100100%
   local.py120100%
   logger.py230100%
   style.py70100%
   type.py50100%
tools/trace
   __init__.py20100%
   time.py160100%
TOTAL1710100%

Tests Skipped Failures Errors Time
8 0 💤 0 ❌ 0 🔥 1.423s ⏱️

Please sign in to comment.