clock-pattern
The Clock Pattern is a Python π package that turns time into an injectable dependency π§©. By replacing ad-hoc datetime.now() calls with a swappable Clock interface π°οΈ you unlock deterministic tests π§ͺ and decouple business logic from the OS clock.
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
βCITATION.cff file
Found CITATION.cff file -
βcodemeta.json file
Found codemeta.json file -
β.zenodo.json file
Found .zenodo.json file -
βDOI references
-
βAcademic publication links
-
βCommitters with academic emails
-
βInstitutional organization owner
-
βJOSS paper metadata
-
βScientific vocabulary similarity
Low similarity (8.6%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
The Clock Pattern is a Python π package that turns time into an injectable dependency π§©. By replacing ad-hoc datetime.now() calls with a swappable Clock interface π°οΈ you unlock deterministic tests π§ͺ and decouple business logic from the OS clock.
Basic Info
- Host: GitHub
- Owner: adriamontoto
- License: mit
- Language: Python
- Default Branch: master
- Homepage: https://pypi.org/project/clock-pattern
- Size: 91.8 KB
Statistics
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
- Releases: 5
Topics
Metadata Files
README.md
π°οΈ Clock Pattern
The Clock Pattern is a Python π package that turns time into an injectable dependency π§©. By replacing ad-hoc datetime.now() calls with a swappable Clock interface π°οΈ you unlock deterministic tests π§ͺ, decouple business logic from the OS clock, and gain the freedom to swap in high-precision or logical clocks without touching domain code.
Table of Contents
## π₯ Installation You can install **Clock Pattern** using `pip`: ```bash pip install clock-pattern ```
## π Documentation This [project's documentation](https://deepwiki.com/adriamontoto/clock-pattern) is powered by DeepWiki, which provides a comprehensive overview of the **Clock Pattern** and its usage.
## π» Utilization The **Clock Pattern** library is designed to be straightforward. Simply import the desired clock and use its `now()` or `today()` methods to get the current datetime/date. This approach allows for easy dependency injection and testing. Here is a basic example of how to use the [`SystemClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/system_clock.py) clock: ```python from datetime import timezone from clock_pattern import SystemClock clock = SystemClock(timezone=timezone.utc) print(clock.now()) # >>> 2025-06-16 13:57:26.210964+00:00 ```
## π Available Clocks The package offers several clock implementations to suit different needs: - [`clock_pattern.SystemClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/system_clock.py): The standard clock implementation that returns the system's current datetime/date with the provided timezone. - [`clock_pattern.UtcClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/utc_clock.py): A clock implementation that returns the system's current datetime/date in UTC. Ideal for production environments. - [`clock_pattern.clocks.testing.FixedClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/testing/fixed_clock.py): A clock that always returns a fixed, preset datetime/date. It is perfect for basic testing as it allows you to control the datetime/date within your test environment, ensuring deterministic results. - [`clock_pattern.clocks.testing.MockClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/testing/mock_clock.py): A clock that allows you to mock the system clock. It is perfect for more complex testing as it allows you to control the datetime/date within your test environment and if or not the methods are called or not.
## π Real-Life Case: Christmas Detector Service Below is an example of a real-life scenario where Clock Pattern can create clean and testable code. We have a `ChristmasDetectorService` that checks if the curren date falls within a specific Christmas holiday range. Using the Clock Pattern, in this case [`UtcClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/utc_clock.py) and [`MockClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/testing/mock_clock.py), we can decouple the service from the python `datetime.now()` and `datetime.today()` functions, making it easy to test for different dates without changing the system's time. ```python from datetime import date from clock_pattern import Clock, UtcClock from clock_pattern.clocks.testing import MockClock class ChristmasDetectorService: def __init__(self, clock: Clock) -> None: self.clock = clock self.christmas_start = date(year=2024, month=12, day=24) self.christmas_end = date(year=2025, month=1, day=6) def is_christmas(self) -> bool: return self.christmas_start <= self.clock.today() <= self.christmas_end clock = UtcClock() christmas_detector_service = ChristmasDetectorService(clock=clock) print(christmas_detector_service.is_christmas()) # >>> False def test_christmas_detector_is_christmas() -> None: clock = MockClock() christmas_detector_service = ChristmasDetectorService(clock=clock) today = date(year=2024, month=12, day=25) clock.prepare_today_method_return_value(today=today) assert christmas_detector_service.is_christmas() is True clock.assert_today_method_was_called_once() def test_christmas_detector_is_not_christmas() -> None: clock = MockClock() christmas_detector_service = ChristmasDetectorService(clock=clock) today = date(year=2025, month=1, day=7) clock.prepare_today_method_return_value(today=today) assert christmas_detector_service.is_christmas() is False clock.assert_today_method_was_called_once() ```
## π€ Contributing We love community help! Before you open an issue or pull request, please read: - [`π€ How to Contribute`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/CONTRIBUTING.md) - [`π§ Code of Conduct`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/CODE_OF_CONDUCT.md) - [`π Security Policy`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/SECURITY.md) _Thank you for helping make **π°οΈ Clock Pattern** package awesome! π_
## π License This project is licensed under the terms of the [`MIT license`](https://github.com/adriamontoto/clock-pattern/blob/master/LICENSE.md).
Owner
- Login: adriamontoto
- Kind: user
- Location: /dev/null
- Company: Amazon
- Repositories: 9
- Profile: https://github.com/adriamontoto
Software Development Engineer @ Amazon
Citation (CITATION.cff)
cff-version: 1.2.0
title: Clock Pattern
message: >
If you use this software, please cite it using the metadata below.
type: software
authors:
- given-names: AdriΓ
family-names: Montoto
repository-code: https://github.com/adriamontoto/clock-pattern
url: https://pypi.org/project/clock-pattern
abstract: >
The Clock Pattern is a Python package that turns time into an
injectable dependency. By replacing ad-hoc datetime.now() calls
with a swappable Clock interface you unlock deterministic tests
and decouple business logic from the OS clock.
keywords:
- python
- clock
- dependency-injection
- domain-driven-design
license: MIT
GitHub Events
Total
- Release event: 3
- Watch event: 3
- Delete event: 19
- Issue comment event: 24
- Push event: 27
- Pull request review event: 11
- Pull request event: 39
- Create event: 22
Last Year
- Release event: 3
- Watch event: 3
- Delete event: 19
- Issue comment event: 24
- Push event: 27
- Pull request review event: 11
- Pull request event: 39
- Create event: 22
Committers
Last synced: 5 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Adria Montoto | 7****o | 22 |
| dependabot[bot] | 4****] | 9 |
| adriamontoto-bot | 1****t | 5 |
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 0
- Total pull requests: 26
- Average time to close issues: N/A
- Average time to close pull requests: 4 days
- Total issue authors: 0
- Total pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 0.85
- Merged pull requests: 13
- Bot issues: 0
- Bot pull requests: 26
Past Year
- Issues: 0
- Pull requests: 26
- Average time to close issues: N/A
- Average time to close pull requests: 4 days
- Issue authors: 0
- Pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 0.85
- Merged pull requests: 13
- Bot issues: 0
- Bot pull requests: 26
Top Authors
Issue Authors
Pull Request Authors
- dependabot[bot] (26)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- pypi 778 last-month
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 5
- Total maintainers: 1
pypi.org: clock-pattern
The Clock Pattern is a Python package that turns time into an injectable dependency.
- Homepage: https://github.com/adriamontoto/clock-pattern
- Documentation: https://clock-pattern.readthedocs.io/
- License: MIT License
-
Latest release: 0.5.0
published 6 months ago
Rankings
Maintainers (1)
Dependencies
- actions/checkout 11bd71901bbe5b1630ceea73d27597364c9af683 composite
- actions/download-artifact d3f86a106a0bac45b974a628896c90dbdf5c8093 composite
- actions/setup-python a26af69be951a213d495a4c3e4e4022e16d87065 composite
- actions/upload-artifact ea165f8d65b6e75b540449e92b4886f43607fa02 composite
- astral-sh/setup-uv 445689ea25e0de0a23313031f5fe577c74ae45a1 composite
- github/codeql-action/analyze ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 composite
- github/codeql-action/init ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 composite
- py-cov-action/python-coverage-comment-action 970a227e0c16ef4589a99a9970ab0ceb8c53059a composite
- pypa/gh-action-pypi-publish 76f52bc884231f62b9a034ebfe128415bbaabdfc composite
- python-semantic-release/python-semantic-release f9e152fb36cd2e590fe8c2bf85bbff08f7fc1c52 composite
- re-actors/alls-green 05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe composite
- step-security/harden-runner 002fdce3c6a235733a90a27c80493a3241e56863 composite
- value-object-pattern >=0.4.0